@@ -2,6 +2,7 @@ import crypto from "crypto";
2
2
import fs from "fs" ;
3
3
import path from "path" ;
4
4
import { Gr4vy , withToken } from "../../src" ;
5
+ import { HTTPClient } from "../../src/lib/http" ;
5
6
6
7
const loadPrivateKey = ( ) : string => {
7
8
let privateKey = process . env . PRIVATE_KEY ;
@@ -14,11 +15,47 @@ const loadPrivateKey = (): string => {
14
15
return privateKey ;
15
16
} ;
16
17
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
+
17
53
const createGr4vyClient = (
18
54
privateKey : string ,
19
55
merchantAccountId ?: string
20
56
) : Gr4vy => {
21
57
return new Gr4vy ( {
58
+ httpClient : httpClient ,
22
59
server : "sandbox" ,
23
60
id : "e2e" ,
24
61
merchantAccountId,
0 commit comments