-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmock-fetch.ts
247 lines (224 loc) · 6.8 KB
/
mock-fetch.ts
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { MockNotMatchedError } from "./mock-fetch.error.ts";
import {
Fetch,
MockMatcher,
MockRequest,
MockRequestInit,
} from "./mock-fetch.type.ts";
import { MockInterceptor } from "./mock-interceptor.ts";
import { getMockRequest, matchValue } from "./mock-utils.ts";
import { buildKey } from "./mock-utils.ts";
const originalFetch = globalThis.fetch.bind(globalThis);
/**
* Deno Mock Fetch class.
*
* Instantiate this to set up global `fetch` API interception.
*/
export class MockFetch {
readonly #originalFetch: Fetch;
#mockRequests: MockRequest[] = [];
#calls = 0;
#isMockActive = true;
#netConnect: boolean | MockMatcher[];
constructor() {
this.#originalFetch = originalFetch;
this.#netConnect = false;
globalThis.fetch = this.#fetch.bind(this);
}
/**
* Intercept a global `fetch` API call for the defined inputs.
*
* ```typescript
* import { MockFetch } from "https://deno.land/x/[email protected]/mod.ts";
*
* const mockFetch = new MockFetch();
* mockFetch
* // Intercept `GET https://example.com/hello`
* .intercept("https://example.com/hello", { method: "GET" })
* // Response with status `200` and text `hello`
* .response("hello", { status: 200 });
* ```
*/
public intercept(
/**
* The request input of the `fetch` API call.
*/
input: URL | Request | MockMatcher,
/**
* The request init input of the `fetch` API call.
*/
init?: MockRequestInit,
): MockInterceptor {
const interceptor = new MockInterceptor(this.#mockRequests, input, init);
return interceptor;
}
/**
* The total number of times Mock Fetch has been called.
*/
public get calls(): number {
return this.#calls;
}
/**
* Upon close, cleanup
*/
public close(): void {
globalThis.fetch = this.#originalFetch;
this.#calls = 0;
this.#isMockActive = false;
this.#mockRequests = [];
}
/**
* Deactivate Mock Fetch, preserving current state.
*/
public deactivate() {
this.#isMockActive = false;
}
/**
* Deactivate Mock Fetch, restoring current state.
*/
public activate() {
this.#isMockActive = true;
}
/**
* Indicator for whether MockFetch is active or not.
*/
public get isMockActive() {
return this.#isMockActive;
}
/**
* Activate Net Connect support.
*/
activateNetConnect(
/**
* The Net Connect Hostname Matcher.
*/
matcher?: MockMatcher,
) {
if (matcher) {
if (Array.isArray(this.#netConnect)) {
this.#netConnect.push(matcher);
} else {
this.#netConnect = [matcher];
}
} else {
this.#netConnect = true;
}
}
/**
* Deactivate Net Connect support.
*/
deactivateNetConnect() {
this.#netConnect = false;
}
async #fetch(
input: URL | Request | string,
init?: RequestInit,
): Promise<Response> {
if (!this.#isMockActive) {
return this.#originalFetch(input, init);
}
// Run any required initialisations
await this.#init();
// Get Mock Request
const requestKey = await buildKey(input, init);
try {
const mockRequest = getMockRequest(this.#mockRequests, requestKey);
return await this.#mockFetch(mockRequest);
} catch (error: unknown) {
// Handle Net Connect
if (error instanceof MockNotMatchedError) {
const hostname = requestKey.url.hostname;
if (this.#netConnect === false) {
throw new MockNotMatchedError(
`${error.message}: subsequent request to hostname ${hostname} was not allowed (Net Connect deactivated)`,
);
}
if (this.#checkNetConnect(this.#netConnect, requestKey.url)) {
return this.#originalFetch(input, init);
} else {
throw new MockNotMatchedError(
`${error.message}: subsequent request to hostname ${hostname} was not allowed (Net Connect is not activated for this hostname)`,
);
}
} else {
throw error;
}
}
}
#updateMockRequest(mockRequest: MockRequest): MockRequest {
this.#calls++;
// If it's used up and not persistent, mark as consumed
const calls = ++mockRequest.calls;
mockRequest.consumed = !mockRequest.persist && calls >= mockRequest.times;
mockRequest.pending = calls < mockRequest.times;
return mockRequest;
}
/**
* Mock fetch function used to simulate fetch calls.
*/
async #mockFetch(mockRequest: MockRequest): Promise<Response> {
// If specified, simulate a delay
if (mockRequest.delay > 0) {
await new Promise((resolve) => setTimeout(resolve, mockRequest.delay));
}
// Update mock request metadata
const updatedMockRequest = this.#updateMockRequest(mockRequest);
// If specified, throw the defined error
if (mockRequest.error) {
throw mockRequest.error;
}
// Otherwise, return response
return Promise.resolve(updatedMockRequest.response);
}
#checkNetConnect(netConnect: boolean | MockMatcher[], url: URL): boolean {
if (netConnect === true) {
return true;
} else if (
Array.isArray(netConnect) &&
netConnect.some((matcher) => matchValue(matcher, url.hostname))
) {
return true;
}
return false;
}
/**
* Initialise the mock requests for interception.
* This happens before every fetch call.
*/
async #init(): Promise<void> {
await Promise.all(this.#mockRequests.map(async (mockRequest) => {
if (!mockRequest.request.body) {
await this.#setupMockRequestBody(mockRequest);
}
}));
}
/**
* Setup the mock request body for subsequent interception. It sets up the following:
* - If input is a Request and the body is not consumed, render the body text
* - If init.body is a Blob, render the Blob text
* - If init.body is an ArrayBufferView, render the decoded view
* - If init.body is a FormData instance, render the FormData as text
* - If init.body is a URLSearchParams instance, render the params as a string
*/
async #setupMockRequestBody(mockRequest: MockRequest): Promise<void> {
if (
mockRequest.request.input instanceof Request &&
!mockRequest.request.input.bodyUsed
) {
mockRequest.request.body = await mockRequest.request.input.text();
} else if (mockRequest.request.init?.body) {
const body = mockRequest.request.init?.body;
if (body instanceof Blob) {
mockRequest.request.body = await body.text();
} else if ((body as ArrayBufferView).buffer instanceof ArrayBuffer) {
mockRequest.request.body = new TextDecoder().decode(
body as ArrayBufferView,
);
} else if (body instanceof FormData) {
mockRequest.request.body = JSON.stringify([...body.entries()]);
} else if (body instanceof URLSearchParams) {
mockRequest.request.body = body.toString();
}
}
}
}