Skip to content

Commit 7ef41bf

Browse files
authored
Merge pull request #1595 from Adyen/custom-request-headers
Add test to verify custom request headers
2 parents db057ff + 2aa789f commit 7ef41bf

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,22 @@ checkoutApi.PaymentsApi.payments(paymentRequest)
125125
.then(paymentResponse => console.log(paymentResponse.pspReference))
126126
.catch(error => console.log(error));
127127
```
128-
If you want to pass query string parameters, you can use the `params` field from [IRequest](/src/typings/requestOptions.ts) (also used for idempotency-key and other header fields).
128+
If you want to pass query string parameters, you can use the `params` field from [IRequest](/src/typings/requestOptions.ts) (also used for idempotency-key and other header fields).
129129
The method descriptions contain an example of the possible values you can send to the API for the query parameters, just as stated in the API explorer.
130+
131+
You can also include custom HTTP headers in your request by using the `headers` property within `IRequest.Options`. This can be useful for sending additional metadata or authentication details that are not part of the standard API request body or query parameters.
132+
133+
```typescript
134+
const requestOptions: IRequest.Options = {
135+
headers: {
136+
"X-Custom-Header": "MyValue",
137+
"Another-Custom-Header": "AnotherValue"
138+
}
139+
};
140+
checkoutApi.PaymentsApi.payments(paymentRequest, requestOptions)
141+
.then(response => console.log("Payment successful:", response))
142+
.catch(error => console.error("Payment failed:", error));
143+
```
130144
```javascript
131145
const requestOptions: IRequest.Options = {
132146
params: {

src/__tests__/httpClient.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,32 @@ describe("HTTP Client", function (): void {
159159
expect(response).toEqual<binlookup.ThreeDSAvailabilityResponse>(threeDSAvailabilitySuccessResponse);
160160
});
161161

162+
test("should make a request with custom headers", async (): Promise<void> => {
163+
const client = createClient();
164+
const checkout = new CheckoutAPI(client);
165+
166+
const customHeaderKey = "X-Custom-Header";
167+
const customHeaderValue = "my-custom-value";
168+
169+
const scope = nock("https://checkout-test.adyen.com/v71", {
170+
reqheaders: {
171+
[customHeaderKey]: (headerValue) => {
172+
expect(headerValue).toBeTruthy();
173+
expect(headerValue).toEqual(customHeaderValue);
174+
return true;
175+
},
176+
},
177+
});
178+
179+
scope.post("/paymentMethods").reply(200, paymentMethodsSuccess);
180+
const requestOptions = { headers: { [customHeaderKey]: customHeaderValue } };
181+
182+
const response = await checkout.PaymentsApi.paymentMethods({ "merchantAccount": "testMerchantAccount" }, requestOptions);
183+
184+
expect(response.paymentMethods).toBeTruthy();
185+
expect(scope.isDone()).toBe(true);
186+
});
187+
162188
test("should add default applicationInfo to the headers", async (): Promise<void> => {
163189
const client = createClient();
164190
const checkout = new CheckoutAPI(client);

0 commit comments

Comments
 (0)