@@ -6,6 +6,7 @@ import Client from "../client";
66import TerminalCloudAPI from "../services/terminalCloudAPI" ;
77import { terminal } from "../typings" ;
88import { EnvironmentEnum } from "../config" ;
9+ import HttpClientException from "../httpClient/httpClientException" ;
910
1011let client : Client ;
1112let terminalCloudAPI : TerminalCloudAPI ;
@@ -31,29 +32,29 @@ describe("Terminal Cloud API", (): void => {
3132
3233 const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest ( ) ;
3334
34- const requestResponse = await terminalCloudAPI . async ( terminalAPIPaymentRequest ) ;
35+ const requestResponse = await terminalCloudAPI . async ( terminalAPIPaymentRequest ) ;
3536
36- expect ( typeof requestResponse ) . toBe ( "string" ) ;
37- expect ( requestResponse ) . toEqual ( "ok" ) ;
38- } ) ;
37+ expect ( typeof requestResponse ) . toBe ( "string" ) ;
38+ expect ( requestResponse ) . toEqual ( "ok" ) ;
39+ } ) ;
3940
40- test ( "should get an error after async payment request" , async ( ) : Promise < void > => {
41- scope . post ( "/async" ) . reply ( 200 , asyncErrorRes ) ;
41+ test ( "should get an error after async payment request" , async ( ) : Promise < void > => {
42+ scope . post ( "/async" ) . reply ( 200 , asyncErrorRes ) ;
4243
43- const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest ( ) ;
44+ const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest ( ) ;
4445
45- const requestResponse = await terminalCloudAPI . async ( terminalAPIPaymentRequest ) ;
46+ const requestResponse = await terminalCloudAPI . async ( terminalAPIPaymentRequest ) ;
4647
47- if ( typeof requestResponse === "object" ) {
48- expect ( requestResponse . SaleToPOIRequest ?. EventNotification ) . toBeDefined ( ) ;
49- expect ( requestResponse . SaleToPOIRequest ?. EventNotification ?. EventToNotify ) . toBe ( "Reject" ) ;
50- } else {
51- throw new Error ( "Expected structured response, but got raw string" ) ;
52- }
53- } ) ;
48+ if ( typeof requestResponse === "object" ) {
49+ expect ( requestResponse . SaleToPOIRequest ?. EventNotification ) . toBeDefined ( ) ;
50+ expect ( requestResponse . SaleToPOIRequest ?. EventNotification ?. EventToNotify ) . toBe ( "Reject" ) ;
51+ } else {
52+ throw new Error ( "Expected structured response, but got raw string" ) ;
53+ }
54+ } ) ;
5455
55- test ( "should make a sync payment request" , async ( ) : Promise < void > => {
56- scope . post ( "/sync" ) . reply ( 200 , syncRes ) ;
56+ test ( "should make a sync payment request" , async ( ) : Promise < void > => {
57+ scope . post ( "/sync" ) . reply ( 200 , syncRes ) ;
5758
5859 const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest ( ) ;
5960 const terminalAPIResponse : terminal . TerminalApiResponse = await terminalCloudAPI . sync ( terminalAPIPaymentRequest ) ;
@@ -168,6 +169,7 @@ describe("Terminal Cloud API", (): void => {
168169 const terminalApiHost = "https://terminal-api-test.adyen.com" ;
169170
170171 const client = new Client ( { apiKey : "YOUR_API_KEY" , environment : EnvironmentEnum . TEST } ) ;
172+
171173 const terminalCloudAPI = new TerminalCloudAPI ( client ) ;
172174
173175 const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest ( ) ;
@@ -191,14 +193,45 @@ describe("Terminal Cloud API", (): void => {
191193 } ,
192194 } ) ;
193195
194- try {
195- await terminalCloudAPI . sync ( terminalAPIPaymentRequest ) ;
196- fail ( "No exception was thrown" ) ;
196+ try {
197+ await terminalCloudAPI . sync ( terminalAPIPaymentRequest ) ;
198+ fail ( "No exception was thrown" ) ;
197199 } catch ( e ) {
198- expect ( e ) . toBeInstanceOf ( Error ) ;
200+ expect ( e ) . toBeInstanceOf ( Error ) ;
199201 }
200202
201- } ) ;
203+ } ) ;
204+
205+ test ( "async should skip 308 redirect" , async ( ) : Promise < void > => {
206+
207+ const terminalApiHost = "https://terminal-api-test.adyen.com" ;
208+
209+ const client = new Client ( { apiKey : "YOUR_API_KEY" , environment : EnvironmentEnum . TEST , enable308Redirect : false } ) ;
210+ const terminalCloudAPI = new TerminalCloudAPI ( client ) ;
211+
212+ const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest ( ) ;
213+ // custom value to trigger mock 308 response
214+ terminalAPIPaymentRequest . SaleToPOIRequest . MessageHeader . SaleID = "response-with-redirect" ;
215+
216+ // Mock first request: returns a 308 redirect with Location header
217+ nock ( terminalApiHost )
218+ . post ( "/async" , ( body ) => {
219+ return body ?. SaleToPOIRequest ?. MessageHeader ?. SaleID === "response-with-redirect" ;
220+ } )
221+ . reply ( 308 , "" , { Location : `${ terminalApiHost } /async?redirect=false` } ) ;
222+
223+
224+ // Must throw an error
225+ try {
226+ await terminalCloudAPI . async ( terminalAPIPaymentRequest ) ;
227+ fail ( "No exception was thrown" ) ;
228+ } catch ( e : unknown ) {
229+ expect ( e ) . toBeInstanceOf ( HttpClientException ) ;
230+ if ( e instanceof HttpClientException ) {
231+ expect ( e . statusCode ) . toBe ( 308 ) ;
232+ }
233+ }
234+ } ) ;
202235
203236} ) ;
204237
0 commit comments