Skip to content

Commit 9514170

Browse files
committed
chore(tests): Add random response fields to ensure forward compatibility
1 parent 1c8e92b commit 9514170

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/utils/setup.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import crypto from "crypto";
22
import fs from "fs";
33
import path from "path";
44
import { Gr4vy, withToken } from "../../src";
5+
import { HTTPClient } from "../../src/lib/http";
56

67
const loadPrivateKey = (): string => {
78
let privateKey = process.env.PRIVATE_KEY;
@@ -14,11 +15,47 @@ const loadPrivateKey = (): string => {
1415
return privateKey;
1516
};
1617

18+
const httpClient = new HTTPClient({
19+
/**
20+
* Adds a custom HTTP client that inserts random fields in the response,
21+
* ensuring we test for forward compatibility.
22+
*/
23+
fetcher: async (request) => {
24+
const originalResponse = await fetch(request);
25+
const contentType = originalResponse.headers.get("content-type");
26+
if (!contentType || !contentType.includes("application/json")) {
27+
return originalResponse;
28+
}
29+
30+
try {
31+
const data = await originalResponse.clone().json();
32+
const randomKey = `unexpected_field_${Math.floor(Math.random() * 1000)}`;
33+
data[randomKey] = "this is an injected test value";
34+
35+
const modifiedBody = new Blob([JSON.stringify(data, null, 2)], {
36+
type: 'application/json',
37+
});
38+
39+
const modifiedResponse = new Response(modifiedBody, {
40+
status: originalResponse.status,
41+
statusText: originalResponse.statusText,
42+
headers: originalResponse.headers,
43+
});
44+
45+
return modifiedResponse;
46+
} catch (error) {
47+
console.error("Failed to parse JSON, returning original response:", error);
48+
return originalResponse;
49+
}
50+
},
51+
});
52+
1753
const createGr4vyClient = (
1854
privateKey: string,
1955
merchantAccountId?: string
2056
): Gr4vy => {
2157
return new Gr4vy({
58+
httpClient: httpClient,
2259
server: "sandbox",
2360
id: "e2e",
2461
merchantAccountId,

0 commit comments

Comments
 (0)