-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathfetchHelper.test.ts
More file actions
208 lines (182 loc) · 6.38 KB
/
fetchHelper.test.ts
File metadata and controls
208 lines (182 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright (c) 2023, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import nock from 'nock';
import {Response} from 'node-fetch';
import * as environment from './environment';
import ClientConfig from '../clientConfig';
import {doFetch} from './fetchHelper';
describe('doFetch', () => {
const basePath = 'https://short_code.api.commercecloud.salesforce.com';
const endpointPath =
'/checkout/shopper-baskets/v1/organizations/organization_id/baskets';
const url = `${basePath + endpointPath}?siteId=site_id`;
const clientConfig = new ClientConfig({
parameters: {
shortCode: 'short_code',
organizationId: 'organization_id',
clientId: 'client_id',
siteId: 'site_id',
},
fetchOptions: {
cache: 'no-cache',
},
});
const options = {
method: 'POST',
headers: {
authorization: 'Bearer token',
},
body: {
data: 'data',
},
};
const responseBody = {message: 'request has matched'};
beforeEach(() => {
jest.restoreAllMocks();
nock.cleanAll();
});
test('uses headers from both clientConfig and headers object', async () => {
const copyOptions = {
...options,
headers: {
...options.headers,
optionsOnlyHeader: 'optionsOnlyHeader',
repeatHeader: 'options.headers',
},
};
const copyClientConfig = {
...clientConfig,
headers: {
...clientConfig.headers,
clientConfigOnlyHeader: 'clientConfigOnlyHeader',
repeatHeader: 'clientConfig.headers', // this should get overwritten
},
};
const expectedHeaders = {
authorization: 'Bearer token',
optionsOnlyHeader: 'optionsOnlyHeader',
clientConfigOnlyHeader: 'clientConfigOnlyHeader',
repeatHeader: 'options.headers',
// we should not see this header as repeatHeader in options should override this one
// repeatHeader: 'clientConfig.headers',
};
nock(basePath, {
reqheaders: expectedHeaders,
})
.post(endpointPath)
.query({siteId: 'site_id'})
.reply(200, responseBody);
const spy = jest.spyOn(environment, 'fetch');
const response = await doFetch(url, copyOptions, copyClientConfig);
expect(response).toEqual(responseBody);
expect(spy).toBeCalledTimes(1);
expect(spy).toBeCalledWith(
expect.any(String),
expect.objectContaining({headers: expectedHeaders})
);
});
test('returns raw response when rawResponse flag is passed as true', async () => {
nock(basePath)
.post(endpointPath)
.query({siteId: 'site_id'})
.reply(200, responseBody);
const response = (await doFetch(
url,
options,
clientConfig,
true
)) as Response;
expect(response instanceof Response).toBe(true);
const data = (await response.json()) as Record<string, string>;
expect(data).toEqual(responseBody);
});
test('throws error when clientConfig.throwOnBadResponse is true and fetch call fails', () => {
nock(basePath).post(endpointPath).query({siteId: 'site_id'}).reply(400);
const copyClientConfig = {...clientConfig, throwOnBadResponse: true};
expect(async () => {
await doFetch(url, options, copyClientConfig);
})
.rejects.toThrow('400 Bad Request')
.finally(() => 'resolve promise');
});
test('returns data from response when rawResponse flag is passed as false or not passed', async () => {
nock(basePath).post(endpointPath).query(true).reply(200, responseBody);
const data = await doFetch(url, options, clientConfig, false);
expect(data).toEqual(responseBody);
});
test('passes on fetchOptions from clientConfig to fetch call', async () => {
nock(basePath).post(endpointPath).query(true).reply(200, responseBody);
const spy = jest.spyOn(environment, 'fetch');
await doFetch(url, options, clientConfig, false);
expect(spy).toBeCalledTimes(1);
expect(spy).toBeCalledWith(
expect.any(String),
expect.objectContaining(clientConfig.fetchOptions)
);
});
test('throws error when fetchOptions.signal is passed and aborted during fetch call', async () => {
nock(basePath)
.post(endpointPath)
.query(true)
.delayConnection(200)
.reply(200, responseBody);
const controller = new AbortController();
const copyClientConfig = {
...clientConfig,
fetchOptions: {...clientConfig.fetchOptions, signal: controller.signal},
};
setTimeout(() => controller.abort(), 100);
const spy = jest.spyOn(environment, 'fetch');
await expect(
doFetch(url, options, copyClientConfig, false)
).rejects.toThrow('The user aborted a request.');
expect(spy).toBeCalledTimes(1);
expect(spy).toBeCalledWith(
expect.any(String),
expect.objectContaining({signal: controller.signal})
);
});
test('throws error when options.signal is passed and aborted during fetch call', async () => {
nock(basePath)
.post(endpointPath)
.query(true)
.delayConnection(200)
.reply(200, responseBody);
const controller = new AbortController();
const copyOptions = {...options, signal: controller.signal};
setTimeout(() => controller.abort(), 100);
const spy = jest.spyOn(environment, 'fetch');
await expect(
doFetch(url, copyOptions, clientConfig, false)
).rejects.toThrow('The user aborted a request.');
expect(spy).toBeCalledTimes(1);
expect(spy).toBeCalledWith(
expect.any(String),
expect.objectContaining({signal: controller.signal})
);
});
test('options.signal overrides fetchOptions.signal', async () => {
nock(basePath).post(endpointPath).query(true).reply(200, responseBody);
const clientConfigController = new AbortController();
const optionsController = new AbortController();
const copyClientConfig = {
...clientConfig,
fetchOptions: {
...clientConfig.fetchOptions,
signal: clientConfigController.signal,
},
};
const copyOptions = {...options, signal: optionsController.signal};
const spy = jest.spyOn(environment, 'fetch');
await doFetch(url, copyOptions, copyClientConfig, false);
expect(spy).toBeCalledTimes(1);
expect(spy).toBeCalledWith(
expect.any(String),
expect.objectContaining({signal: optionsController.signal})
);
});
});