Skip to content

Commit 39f2f76

Browse files
authored
Merge pull request #1496 from Adyen/fix-null-in-useragent
Fix value User-Agent HTTP header
2 parents f6f056a + c51bff2 commit 39f2f76

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/__tests__/httpClient.spec.ts

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import nock, { Interceptor } from "nock";
22
import Client from "../client";
33
import { createClient } from "../__mocks__/base";
4-
import { BinLookupAPI } from "../services";
4+
import { BinLookupAPI, CheckoutAPI } from "../services";
55
import ApiException from "../services/exception/apiException";
66
import HttpClientException from "../httpClient/httpClientException";
77
import { binlookup } from "../typings";
88
import { ApiConstants } from "../constants/apiConstants";
9+
import {paymentMethodsSuccess} from "../__mocks__/checkout/paymentMethodsSuccess";
10+
911

1012
beforeEach((): void => {
1113
nock.cleanAll();
@@ -98,6 +100,81 @@ describe("HTTP Client", function (): void {
98100
const requestOptions = { headers: { foo : "bar" }};
99101
const response = await binLookupService.get3dsAvailability(threeDSAvailabilityRequest, requestOptions);
100102
expect(response).toEqual< binlookup.ThreeDSAvailabilityResponse>(threeDSAvailabilitySuccessResponse);
103+
104+
console.log("requestOptions", requestOptions);
101105
});
102106

107+
test("should add default applicationInfo to the headers", async (): Promise<void> => {
108+
const client = createClient();
109+
const checkout = new CheckoutAPI(client);
110+
111+
const expectedUserAgent = `adyen-node-api-library/26.1.0`;
112+
const expectedLibraryName = `adyen-node-api-library`;
113+
const expectedLibraryVersion = `26.1.0`;
114+
115+
const scope = nock("https://checkout-test.adyen.com/v71", {
116+
reqheaders: {
117+
'adyen-library-name': (headerValue) => {
118+
expect(headerValue).toBeTruthy();
119+
expect(headerValue).toEqual(expectedLibraryName);
120+
return true;
121+
},
122+
'adyen-library-version': (headerValue) => {
123+
expect(headerValue).toBeTruthy();
124+
expect(headerValue).toEqual(expectedLibraryVersion);
125+
expect
126+
return true;
127+
},
128+
'user-agent': (headerValue) => {
129+
expect(headerValue).toBeTruthy();
130+
expect(headerValue).toEqual(expectedUserAgent);
131+
expect
132+
return true;
133+
}
134+
}
135+
});
136+
137+
scope.post("/paymentMethods").reply(200, paymentMethodsSuccess);
138+
const response = await checkout.PaymentsApi.paymentMethods({"merchantAccount": "testMerchantAccount"});
139+
140+
expect(response.paymentMethods).toBeTruthy();
141+
});
142+
143+
test("should add custom applicationInfo to the headers", async (): Promise<void> => {
144+
const client = createClient();
145+
client.config.applicationName = "testApp";
146+
const checkout = new CheckoutAPI(client);
147+
148+
const expectedUserAgent = `testApp adyen-node-api-library/26.1.0`; // include applicationName too
149+
const expectedLibraryName = `adyen-node-api-library`;
150+
const expectedLibraryVersion = `26.1.0`;
151+
152+
const scope = nock("https://checkout-test.adyen.com/v71", {
153+
reqheaders: {
154+
'adyen-library-name': (headerValue) => {
155+
expect(headerValue).toBeTruthy();
156+
expect(headerValue).toEqual(expectedLibraryName);
157+
return true;
158+
},
159+
'adyen-library-version': (headerValue) => {
160+
expect(headerValue).toBeTruthy();
161+
expect(headerValue).toEqual(expectedLibraryVersion);
162+
expect
163+
return true;
164+
},
165+
'user-agent': (headerValue) => {
166+
expect(headerValue).toBeTruthy();
167+
expect(headerValue).toEqual(expectedUserAgent);
168+
expect
169+
return true;
170+
}
171+
}
172+
});
173+
174+
scope.post("/paymentMethods").reply(200, paymentMethodsSuccess);
175+
const response = await checkout.PaymentsApi.paymentMethods({"merchantAccount": "testMerchantAccount"});
176+
177+
expect(response.paymentMethods).toBeTruthy();
178+
});
179+
103180
});

src/httpClient/httpURLConnectionClient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ class HttpURLConnectionClient implements ClientInterface {
111111
}
112112

113113
requestOptions.headers[ApiConstants.ACCEPT_CHARSET] = HttpURLConnectionClient.CHARSET;
114-
requestOptions.headers[ApiConstants.USER_AGENT] = `${applicationName} ${LibraryConstants.LIB_NAME}/${LibraryConstants.LIB_VERSION}`;
114+
// user-agent header
115+
const libInfo = `${LibraryConstants.LIB_NAME}/${LibraryConstants.LIB_VERSION}`;
116+
requestOptions.headers[ApiConstants.USER_AGENT] = applicationName? `${applicationName} ${libInfo}`: libInfo;
117+
// custom headers
115118
requestOptions.headers[ApiConstants.ADYEN_LIBRARY_NAME] = LibraryConstants.LIB_NAME;
116119
requestOptions.headers[ApiConstants.ADYEN_LIBRARY_VERSION] = LibraryConstants.LIB_VERSION;
117120

0 commit comments

Comments
 (0)