Skip to content

Commit 727ba92

Browse files
committed
Create individual tests, update assert to verify start of the error message
1 parent a87b6c4 commit 727ba92

File tree

1 file changed

+92
-37
lines changed

1 file changed

+92
-37
lines changed

src/__tests__/httpClient.spec.ts

Lines changed: 92 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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";
9+
import { paymentMethodsSuccess } from "../__mocks__/checkout/paymentMethodsSuccess";
1010
import Config from "../config";
1111
import LibraryConstants from "../constants/libraryConstants";
1212

@@ -28,7 +28,7 @@ const threeDSAvailabilitySuccess = {
2828
type errorType = "HttpClientException" | "ApiException";
2929
type testOptions = { errorType: errorType; errorMessageContains?: string; errorMessageEquals?: string };
3030

31-
const getResponse = async ({apiKey , environment }: { apiKey: string; environment: Environment}, cb: (scope: Interceptor) => testOptions): Promise<void> => {
31+
const getResponse = async ({ apiKey, environment }: { apiKey: string; environment: Environment }, cb: (scope: Interceptor) => testOptions): Promise<void> => {
3232
const client = new Client({ apiKey, environment });
3333
const binLookup = new BinLookupAPI(client);
3434

@@ -41,31 +41,86 @@ const getResponse = async ({apiKey , environment }: { apiKey: string; environmen
4141
await binLookup.BinLookupApi.get3dsAvailability(threeDSAvailabilitySuccess);
4242
fail("request should fail");
4343
} catch (e) {
44-
if(e instanceof ErrorException){
44+
if (e instanceof ErrorException) {
4545
if (errorMessageEquals) expect(e.message).toEqual(errorMessageEquals);
4646
if (errorMessageContains) expect(e.message.toLowerCase()).toContain(errorMessageContains);
4747
} else {
4848
fail();
4949
}
50-
50+
5151
}
5252
};
5353

5454
describe("HTTP Client", function (): void {
55-
it.each`
56-
apiKey | environment | withError | args | errorType | contains | equals
57-
${""} | ${"TEST"} | ${true} | ${["mocked_error_response"]} | ${"ApiException"} | ${"mocked_error_response"} | ${""}
58-
${"MOCKED_API_KEY"} | ${"TEST"} | ${true} | ${["some_error"]} | ${"ApiException"} | ${""} | ${"some_error"}
59-
${"API_KEY"} | ${"TEST"} | ${false} | ${[401, { status: 401, message: "Invalid Request", errorCode: "171", errorType: "validationError"}]} | ${"HttpClientException"} | ${""} | ${"HTTP Exception: 401. null: Invalid Request"}
60-
${"API_KEY"} | ${"TEST"} | ${false} | ${[401, {}]} | ${"HttpClientException"} | ${""} | ${"HTTP Exception: 401. null"}
61-
${"API_KEY"} | ${"TEST"} | ${false} | ${[401, "fail"]} | ${"HttpClientException"} | ${""} | ${"HTTP Exception: 401. Error parsing response: Unexpected token 'i', \"fail\" is not valid JSON"}
62-
`("Should return $errorType, $contains, $equals", async ({ apiKey, environment, withError, args, errorType, contains, equals }) => {
63-
await getResponse({ apiKey, environment }, (scope) => {
64-
if (withError) scope.replyWithError(args[0]);
65-
else scope.reply(args[0], args[1]);
66-
67-
return { errorType, errorMessageContains: contains, errorMessageEquals: equals };
68-
});
55+
56+
test("Should return ApiException with message containing 'mocked_error_response'", async () => {
57+
await getResponse(
58+
{ apiKey: "", environment: "TEST" },
59+
(scope) => {
60+
scope.replyWithError("mocked_error_response");
61+
return {
62+
errorType: "ApiException",
63+
errorMessageContains: "",
64+
errorMessageEquals: "mocked_error_response"
65+
};
66+
}
67+
);
68+
});
69+
70+
test("Should return ApiException with message equal to 'some_error'", async () => {
71+
await getResponse(
72+
{ apiKey: "MOCKED_API_KEY", environment: "TEST" },
73+
(scope) => {
74+
scope.replyWithError("some_error");
75+
return {
76+
errorType: "ApiException",
77+
errorMessageContains: "",
78+
errorMessageEquals: "some_error"
79+
};
80+
}
81+
);
82+
});
83+
84+
test("Should return HttpClientException with message equal to 'HTTP Exception: 401. null: Invalid Request'", async () => {
85+
await getResponse(
86+
{ apiKey: "API_KEY", environment: "TEST" },
87+
(scope) => {
88+
scope.reply(401, { status: 401, message: "Invalid Request", errorCode: "171", errorType: "validationError" });
89+
return {
90+
errorType: "HttpClientException",
91+
errorMessageContains: "",
92+
errorMessageEquals: "HTTP Exception: 401. null: Invalid Request"
93+
};
94+
}
95+
);
96+
});
97+
98+
test("Should return HttpClientException with message equal to 'HTTP Exception: 401. null'", async () => {
99+
await getResponse(
100+
{ apiKey: "API_KEY", environment: "TEST" },
101+
(scope) => {
102+
scope.reply(401, {});
103+
return {
104+
errorType: "HttpClientException",
105+
errorMessageContains: "",
106+
errorMessageEquals: "HTTP Exception: 401. null"
107+
};
108+
}
109+
);
110+
});
111+
112+
test("Should return HttpClientException with message starting with 'HTTP Exception: 401'", async () => {
113+
await getResponse(
114+
{ apiKey: "API_KEY", environment: "TEST" },
115+
(scope) => {
116+
scope.reply(401, "fail");
117+
return {
118+
errorType: "HttpClientException",
119+
errorMessageContains: "http exception: 401", // must be case insensitive assertion
120+
errorMessageEquals: ""
121+
};
122+
}
123+
);
69124
});
70125

71126
test("should succeed on get 3ds availability", async function (): Promise<void> {
@@ -83,9 +138,9 @@ describe("HTTP Client", function (): void {
83138
const binLookupService = new BinLookupAPI(client);
84139
const scope = nock("https://pal-test.adyen.com/pal/servlet/BinLookup/v54", {
85140
reqheaders: {
86-
"Content-Type" : ApiConstants.APPLICATION_JSON_TYPE,
87-
"foo" : "bar"
88-
},
141+
"Content-Type": ApiConstants.APPLICATION_JSON_TYPE,
142+
"foo": "bar"
143+
},
89144
})
90145
.get("/")
91146
.reply(200);
@@ -99,79 +154,79 @@ describe("HTTP Client", function (): void {
99154
scope.post("/get3dsAvailability")
100155
.reply(200, threeDSAvailabilitySuccessResponse);
101156

102-
const requestOptions = { headers: { foo : "bar" }};
157+
const requestOptions = { headers: { foo: "bar" } };
103158
const response = await binLookupService.BinLookupApi.get3dsAvailability(threeDSAvailabilityRequest, requestOptions);
104-
expect(response).toEqual< binlookup.ThreeDSAvailabilityResponse>(threeDSAvailabilitySuccessResponse);
159+
expect(response).toEqual<binlookup.ThreeDSAvailabilityResponse>(threeDSAvailabilitySuccessResponse);
105160
});
106161

107-
test("should add default applicationInfo to the headers", async (): Promise<void> => {
162+
test("should add default applicationInfo to the headers", async (): Promise<void> => {
108163
const client = createClient();
109164
const checkout = new CheckoutAPI(client);
110165

111166
const expectedUserAgent = `${LibraryConstants.LIB_NAME}/${LibraryConstants.LIB_VERSION}`;
112167
const expectedLibraryName = LibraryConstants.LIB_NAME;
113168
const expectedLibraryVersion = LibraryConstants.LIB_VERSION;
114-
169+
115170
const scope = nock("https://checkout-test.adyen.com/v71", {
116171
reqheaders: {
117172
"adyen-library-name": (headerValue) => {
118-
expect(headerValue).toBeTruthy();
173+
expect(headerValue).toBeTruthy();
119174
expect(headerValue).toEqual(expectedLibraryName);
120175
return true;
121176
},
122177
"adyen-library-version": (headerValue) => {
123-
expect(headerValue).toBeTruthy();
178+
expect(headerValue).toBeTruthy();
124179
expect(headerValue).toEqual(expectedLibraryVersion);
125180
return true;
126181
},
127182
"user-agent": (headerValue) => {
128-
expect(headerValue).toBeTruthy();
183+
expect(headerValue).toBeTruthy();
129184
expect(headerValue).toEqual(expectedUserAgent);
130185
return true;
131186
}
132187
}
133188
});
134189

135190
scope.post("/paymentMethods").reply(200, paymentMethodsSuccess);
136-
const response = await checkout.PaymentsApi.paymentMethods({"merchantAccount": "testMerchantAccount"});
191+
const response = await checkout.PaymentsApi.paymentMethods({ "merchantAccount": "testMerchantAccount" });
137192

138193
expect(response.paymentMethods).toBeTruthy();
139-
});
194+
});
140195

141-
test("should add custom applicationInfo to the headers", async (): Promise<void> => {
196+
test("should add custom applicationInfo to the headers", async (): Promise<void> => {
142197
const client = createClient();
143198
client.config.applicationName = "testApp";
144199
const checkout = new CheckoutAPI(client);
145200

146201
const expectedUserAgent = `testApp ${LibraryConstants.LIB_NAME}/${LibraryConstants.LIB_VERSION}`; // include applicationName too
147202
const expectedLibraryName = LibraryConstants.LIB_NAME;
148203
const expectedLibraryVersion = LibraryConstants.LIB_VERSION;
149-
204+
150205
const scope = nock("https://checkout-test.adyen.com/v71", {
151206
reqheaders: {
152207
"adyen-library-name": (headerValue) => {
153-
expect(headerValue).toBeTruthy();
208+
expect(headerValue).toBeTruthy();
154209
expect(headerValue).toEqual(expectedLibraryName);
155210
return true;
156211
},
157212
"adyen-library-version": (headerValue) => {
158-
expect(headerValue).toBeTruthy();
213+
expect(headerValue).toBeTruthy();
159214
expect(headerValue).toEqual(expectedLibraryVersion);
160215
return true;
161216
},
162217
"user-agent": (headerValue) => {
163-
expect(headerValue).toBeTruthy();
218+
expect(headerValue).toBeTruthy();
164219
expect(headerValue).toEqual(expectedUserAgent);
165220
return true;
166221
}
167222
}
168223
});
169224

170225
scope.post("/paymentMethods").reply(200, paymentMethodsSuccess);
171-
const response = await checkout.PaymentsApi.paymentMethods({"merchantAccount": "testMerchantAccount"});
226+
const response = await checkout.PaymentsApi.paymentMethods({ "merchantAccount": "testMerchantAccount" });
172227

173228
expect(response.paymentMethods).toBeTruthy();
174-
});
229+
});
175230

176231
});
177232

0 commit comments

Comments
 (0)