@@ -6,7 +6,7 @@ import ApiException from "../services/exception/apiException";
6
6
import HttpClientException from "../httpClient/httpClientException" ;
7
7
import { binlookup } from "../typings" ;
8
8
import { ApiConstants } from "../constants/apiConstants" ;
9
- import { paymentMethodsSuccess } from "../__mocks__/checkout/paymentMethodsSuccess" ;
9
+ import { paymentMethodsSuccess } from "../__mocks__/checkout/paymentMethodsSuccess" ;
10
10
import Config from "../config" ;
11
11
import LibraryConstants from "../constants/libraryConstants" ;
12
12
@@ -28,7 +28,7 @@ const threeDSAvailabilitySuccess = {
28
28
type errorType = "HttpClientException" | "ApiException" ;
29
29
type testOptions = { errorType : errorType ; errorMessageContains ?: string ; errorMessageEquals ?: string } ;
30
30
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 > => {
32
32
const client = new Client ( { apiKey, environment } ) ;
33
33
const binLookup = new BinLookupAPI ( client ) ;
34
34
@@ -41,31 +41,86 @@ const getResponse = async ({apiKey , environment }: { apiKey: string; environmen
41
41
await binLookup . BinLookupApi . get3dsAvailability ( threeDSAvailabilitySuccess ) ;
42
42
fail ( "request should fail" ) ;
43
43
} catch ( e ) {
44
- if ( e instanceof ErrorException ) {
44
+ if ( e instanceof ErrorException ) {
45
45
if ( errorMessageEquals ) expect ( e . message ) . toEqual ( errorMessageEquals ) ;
46
46
if ( errorMessageContains ) expect ( e . message . toLowerCase ( ) ) . toContain ( errorMessageContains ) ;
47
47
} else {
48
48
fail ( ) ;
49
49
}
50
-
50
+
51
51
}
52
52
} ;
53
53
54
54
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
+ ) ;
69
124
} ) ;
70
125
71
126
test ( "should succeed on get 3ds availability" , async function ( ) : Promise < void > {
@@ -83,9 +138,9 @@ describe("HTTP Client", function (): void {
83
138
const binLookupService = new BinLookupAPI ( client ) ;
84
139
const scope = nock ( "https://pal-test.adyen.com/pal/servlet/BinLookup/v54" , {
85
140
reqheaders : {
86
- "Content-Type" : ApiConstants . APPLICATION_JSON_TYPE ,
87
- "foo" : "bar"
88
- } ,
141
+ "Content-Type" : ApiConstants . APPLICATION_JSON_TYPE ,
142
+ "foo" : "bar"
143
+ } ,
89
144
} )
90
145
. get ( "/" )
91
146
. reply ( 200 ) ;
@@ -99,79 +154,79 @@ describe("HTTP Client", function (): void {
99
154
scope . post ( "/get3dsAvailability" )
100
155
. reply ( 200 , threeDSAvailabilitySuccessResponse ) ;
101
156
102
- const requestOptions = { headers : { foo : "bar" } } ;
157
+ const requestOptions = { headers : { foo : "bar" } } ;
103
158
const response = await binLookupService . BinLookupApi . get3dsAvailability ( threeDSAvailabilityRequest , requestOptions ) ;
104
- expect ( response ) . toEqual < binlookup . ThreeDSAvailabilityResponse > ( threeDSAvailabilitySuccessResponse ) ;
159
+ expect ( response ) . toEqual < binlookup . ThreeDSAvailabilityResponse > ( threeDSAvailabilitySuccessResponse ) ;
105
160
} ) ;
106
161
107
- test ( "should add default applicationInfo to the headers" , async ( ) : Promise < void > => {
162
+ test ( "should add default applicationInfo to the headers" , async ( ) : Promise < void > => {
108
163
const client = createClient ( ) ;
109
164
const checkout = new CheckoutAPI ( client ) ;
110
165
111
166
const expectedUserAgent = `${ LibraryConstants . LIB_NAME } /${ LibraryConstants . LIB_VERSION } ` ;
112
167
const expectedLibraryName = LibraryConstants . LIB_NAME ;
113
168
const expectedLibraryVersion = LibraryConstants . LIB_VERSION ;
114
-
169
+
115
170
const scope = nock ( "https://checkout-test.adyen.com/v71" , {
116
171
reqheaders : {
117
172
"adyen-library-name" : ( headerValue ) => {
118
- expect ( headerValue ) . toBeTruthy ( ) ;
173
+ expect ( headerValue ) . toBeTruthy ( ) ;
119
174
expect ( headerValue ) . toEqual ( expectedLibraryName ) ;
120
175
return true ;
121
176
} ,
122
177
"adyen-library-version" : ( headerValue ) => {
123
- expect ( headerValue ) . toBeTruthy ( ) ;
178
+ expect ( headerValue ) . toBeTruthy ( ) ;
124
179
expect ( headerValue ) . toEqual ( expectedLibraryVersion ) ;
125
180
return true ;
126
181
} ,
127
182
"user-agent" : ( headerValue ) => {
128
- expect ( headerValue ) . toBeTruthy ( ) ;
183
+ expect ( headerValue ) . toBeTruthy ( ) ;
129
184
expect ( headerValue ) . toEqual ( expectedUserAgent ) ;
130
185
return true ;
131
186
}
132
187
}
133
188
} ) ;
134
189
135
190
scope . post ( "/paymentMethods" ) . reply ( 200 , paymentMethodsSuccess ) ;
136
- const response = await checkout . PaymentsApi . paymentMethods ( { "merchantAccount" : "testMerchantAccount" } ) ;
191
+ const response = await checkout . PaymentsApi . paymentMethods ( { "merchantAccount" : "testMerchantAccount" } ) ;
137
192
138
193
expect ( response . paymentMethods ) . toBeTruthy ( ) ;
139
- } ) ;
194
+ } ) ;
140
195
141
- test ( "should add custom applicationInfo to the headers" , async ( ) : Promise < void > => {
196
+ test ( "should add custom applicationInfo to the headers" , async ( ) : Promise < void > => {
142
197
const client = createClient ( ) ;
143
198
client . config . applicationName = "testApp" ;
144
199
const checkout = new CheckoutAPI ( client ) ;
145
200
146
201
const expectedUserAgent = `testApp ${ LibraryConstants . LIB_NAME } /${ LibraryConstants . LIB_VERSION } ` ; // include applicationName too
147
202
const expectedLibraryName = LibraryConstants . LIB_NAME ;
148
203
const expectedLibraryVersion = LibraryConstants . LIB_VERSION ;
149
-
204
+
150
205
const scope = nock ( "https://checkout-test.adyen.com/v71" , {
151
206
reqheaders : {
152
207
"adyen-library-name" : ( headerValue ) => {
153
- expect ( headerValue ) . toBeTruthy ( ) ;
208
+ expect ( headerValue ) . toBeTruthy ( ) ;
154
209
expect ( headerValue ) . toEqual ( expectedLibraryName ) ;
155
210
return true ;
156
211
} ,
157
212
"adyen-library-version" : ( headerValue ) => {
158
- expect ( headerValue ) . toBeTruthy ( ) ;
213
+ expect ( headerValue ) . toBeTruthy ( ) ;
159
214
expect ( headerValue ) . toEqual ( expectedLibraryVersion ) ;
160
215
return true ;
161
216
} ,
162
217
"user-agent" : ( headerValue ) => {
163
- expect ( headerValue ) . toBeTruthy ( ) ;
218
+ expect ( headerValue ) . toBeTruthy ( ) ;
164
219
expect ( headerValue ) . toEqual ( expectedUserAgent ) ;
165
220
return true ;
166
221
}
167
222
}
168
223
} ) ;
169
224
170
225
scope . post ( "/paymentMethods" ) . reply ( 200 , paymentMethodsSuccess ) ;
171
- const response = await checkout . PaymentsApi . paymentMethods ( { "merchantAccount" : "testMerchantAccount" } ) ;
226
+ const response = await checkout . PaymentsApi . paymentMethods ( { "merchantAccount" : "testMerchantAccount" } ) ;
172
227
173
228
expect ( response . paymentMethods ) . toBeTruthy ( ) ;
174
- } ) ;
229
+ } ) ;
175
230
176
231
} ) ;
177
232
0 commit comments