|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { x402HTTPClient } from "@x402/core/client"; |
| 3 | +import { Keypair } from "@stellar/stellar-sdk"; |
| 4 | +import { describe, test } from "node:test"; |
| 5 | + |
| 6 | +import { createPaidFetch, X402PaymentError, AppraisalResponseParseError } from "./client.js"; |
| 7 | + |
| 8 | +const TEST_SECRET = Keypair.random().secret(); |
| 9 | +const VALID_402_BODY = { |
| 10 | + x402Version: 2, |
| 11 | + resource: "https://example.com/appraise", |
| 12 | + accepts: [ |
| 13 | + { |
| 14 | + scheme: "exact", |
| 15 | + network: "stellar:testnet", |
| 16 | + payTo: "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", |
| 17 | + price: "0.10", |
| 18 | + asset: "USDC", |
| 19 | + maxTimeoutSeconds: 60, |
| 20 | + extra: {}, |
| 21 | + }, |
| 22 | + ], |
| 23 | + error: "Payment required", |
| 24 | + metadata: {}, |
| 25 | +}; |
| 26 | + |
| 27 | +function buildResponse(status: number, body: string | undefined, headers?: Record<string, string>) { |
| 28 | + const headersMap = new Headers(headers ?? {}); |
| 29 | + return new Response(body ?? "", { |
| 30 | + status, |
| 31 | + headers: headersMap, |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +describe("createPaidFetch response parsing", () => { |
| 36 | + test("throws a typed parse error for an unpaid empty response body", async () => { |
| 37 | + const paidFetch = createPaidFetch({ secret: TEST_SECRET }); |
| 38 | + const originalFetch = globalThis.fetch; |
| 39 | + globalThis.fetch = async () => buildResponse(200, ""); |
| 40 | + |
| 41 | + try { |
| 42 | + await assert.rejects( |
| 43 | + () => paidFetch("https://example.com/appraise"), |
| 44 | + (err: unknown) => { |
| 45 | + assert.ok(err instanceof AppraisalResponseParseError); |
| 46 | + assert.equal(err.status, 200); |
| 47 | + assert.equal(err.name, "AppraisalResponseParseError"); |
| 48 | + assert.match(err.message, /invalid JSON body/i); |
| 49 | + return true; |
| 50 | + }, |
| 51 | + ); |
| 52 | + } finally { |
| 53 | + globalThis.fetch = originalFetch; |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + test("throws a typed parse error for a paid non-JSON response body without exposing raw content", async () => { |
| 58 | + const paidFetch = createPaidFetch({ secret: TEST_SECRET }); |
| 59 | + const originalFetch = globalThis.fetch; |
| 60 | + const originalGetPaymentRequiredResponse = x402HTTPClient.prototype.getPaymentRequiredResponse; |
| 61 | + const originalCreatePaymentPayload = x402HTTPClient.prototype.createPaymentPayload; |
| 62 | + const originalEncodePaymentSignatureHeader = |
| 63 | + x402HTTPClient.prototype.encodePaymentSignatureHeader; |
| 64 | + const originalGetPaymentSettleResponse = x402HTTPClient.prototype.getPaymentSettleResponse; |
| 65 | + |
| 66 | + x402HTTPClient.prototype.getPaymentRequiredResponse = () => VALID_402_BODY as never; |
| 67 | + x402HTTPClient.prototype.createPaymentPayload = async () => ({ |
| 68 | + x402Version: 2, |
| 69 | + payload: "stub-payload", |
| 70 | + resource: "https://example.com/appraise", |
| 71 | + accepted: VALID_402_BODY.accepts[0], |
| 72 | + extensions: {}, |
| 73 | + }) as never; |
| 74 | + x402HTTPClient.prototype.encodePaymentSignatureHeader = () => ({ |
| 75 | + "X-PAYMENT": "stub-signature", |
| 76 | + }); |
| 77 | + x402HTTPClient.prototype.getPaymentSettleResponse = () => ({ |
| 78 | + transaction: "stub-tx", |
| 79 | + network: "stellar:testnet", |
| 80 | + payer: "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", |
| 81 | + }) as never; |
| 82 | + |
| 83 | + let calls = 0; |
| 84 | + globalThis.fetch = async (_url, _init) => { |
| 85 | + calls += 1; |
| 86 | + if (calls === 1) { |
| 87 | + return buildResponse(402, JSON.stringify(VALID_402_BODY), { |
| 88 | + "x402-version": "2", |
| 89 | + "x402-payment-required": "1", |
| 90 | + }); |
| 91 | + } |
| 92 | + if (calls === 2) { |
| 93 | + return buildResponse(200, "not-json"); |
| 94 | + } |
| 95 | + throw new Error("unexpected extra fetch call"); |
| 96 | + }; |
| 97 | + |
| 98 | + try { |
| 99 | + await assert.rejects( |
| 100 | + () => paidFetch("https://example.com/appraise"), |
| 101 | + (err: unknown) => { |
| 102 | + assert.ok(err instanceof AppraisalResponseParseError); |
| 103 | + assert.equal(err.status, 200); |
| 104 | + assert.equal(err.name, "AppraisalResponseParseError"); |
| 105 | + assert.doesNotMatch(err.message, /not-json|raw|payload/i); |
| 106 | + return true; |
| 107 | + }, |
| 108 | + ); |
| 109 | + } finally { |
| 110 | + globalThis.fetch = originalFetch; |
| 111 | + x402HTTPClient.prototype.getPaymentRequiredResponse = originalGetPaymentRequiredResponse; |
| 112 | + x402HTTPClient.prototype.createPaymentPayload = originalCreatePaymentPayload; |
| 113 | + x402HTTPClient.prototype.encodePaymentSignatureHeader = originalEncodePaymentSignatureHeader; |
| 114 | + x402HTTPClient.prototype.getPaymentSettleResponse = originalGetPaymentSettleResponse; |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + test("preserves x402 payment errors for non-JSON 402 responses", async () => { |
| 119 | + const paidFetch = createPaidFetch({ secret: TEST_SECRET }); |
| 120 | + const originalFetch = globalThis.fetch; |
| 121 | + const originalGetPaymentRequiredResponse = x402HTTPClient.prototype.getPaymentRequiredResponse; |
| 122 | + x402HTTPClient.prototype.getPaymentRequiredResponse = () => { |
| 123 | + throw new Error("invalid x402 payment required response"); |
| 124 | + }; |
| 125 | + |
| 126 | + globalThis.fetch = async () => buildResponse(402, "not-json"); |
| 127 | + |
| 128 | + try { |
| 129 | + await assert.rejects( |
| 130 | + () => paidFetch("https://example.com/appraise"), |
| 131 | + (err: unknown) => { |
| 132 | + assert.ok(err instanceof X402PaymentError); |
| 133 | + assert.match(err.message, /payment|402/i); |
| 134 | + return true; |
| 135 | + }, |
| 136 | + ); |
| 137 | + } finally { |
| 138 | + globalThis.fetch = originalFetch; |
| 139 | + x402HTTPClient.prototype.getPaymentRequiredResponse = originalGetPaymentRequiredResponse; |
| 140 | + } |
| 141 | + }); |
| 142 | +}); |
0 commit comments