-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmock-interceptor.ts
187 lines (175 loc) · 4.71 KB
/
mock-interceptor.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
import { InvalidArgumentError } from "./mock-fetch.error.ts";
import {
MockMatcher,
MockRequest,
MockRequestInit,
} from "./mock-fetch.type.ts";
import { MockScope } from "./mock-scope.ts";
import { isMockMatcher, safeURL } from "./mock-utils.ts";
/**
* Defines an interceptor for a Mock.
* Instantiated when `MockFetch.intercept(...)` is called.
*/
export class MockInterceptor {
readonly #mockRequests: MockRequest[];
readonly #input: URL | Request | MockMatcher;
readonly #init?: MockRequestInit;
readonly #defaultRequest = new Request("https://example.com");
#setResponseContentLength = false;
#defaultHeadersInit?: HeadersInit;
constructor(
mockRequests: MockRequest[],
input: URL | Request | MockMatcher,
init?: MockRequestInit,
) {
this.#mockRequests = mockRequests;
this.#input = input;
this.#init = init;
if (this.#init?.body instanceof ReadableStream) {
throw new InvalidArgumentError(
"Matching a request body with a ReadableStream is not supported at this time",
);
}
}
/**
* Mock a request with a defined response.
*/
response(
/**
* Response body
*/
body?: BodyInit | null,
/**
* Response response init definition.
*/
init?: ResponseInit,
): MockScope {
const getHeadersInit = this.#getDefaultHeadersInit.bind(this);
const getAutoGeneratedHeaders = this.#getAutoGeneratedHeaders.bind(this);
const mockRequest: MockRequest = {
request: {
input: this.#input,
init: this.#init,
method: this.#methodMatcher(),
url: this.#urlMatcher(),
body: this.#bodyMatcher(),
},
get response() {
const response = new Response(body, init);
const headers = new Headers(getHeadersInit());
for (
const [key, value] of [
...headers.entries(),
...getAutoGeneratedHeaders(body).entries(),
]
) {
response.headers.append(key, value);
}
return response;
},
calls: 0,
times: 0,
delay: 0,
consumed: false,
pending: true,
persist: false,
};
this.#mockRequests.push(mockRequest);
return new MockScope(mockRequest);
}
/**
* Mock a request with a defined error that will be thrown
* when global `fetch` is called.
*/
throwError(
/** Error to be thrown. */
error: Error,
): MockScope {
const mockRequest: MockRequest = {
request: {
input: this.#input,
init: this.#init,
method: this.#methodMatcher(),
url: this.#urlMatcher(),
body: this.#bodyMatcher(),
},
response: new Response(),
error,
calls: 0,
times: 0,
delay: 0,
consumed: false,
pending: true,
persist: false,
};
this.#mockRequests.push(mockRequest);
return new MockScope(mockRequest);
}
/**
* Set default response headers on the interceptor for subsequent responses
*/
defaultResponseHeaders(
/**
* Headers init definition for default response headers.
*/
headersInit: HeadersInit,
): MockInterceptor {
this.#defaultHeadersInit = headersInit;
return this;
}
/**
* Set response content length header for responses. Default: off.
*
* At this time, this only calculates for response body init inputs of type string.
*/
responseContentLength(): MockInterceptor {
this.#setResponseContentLength = true;
return this;
}
#getAutoGeneratedHeaders(bodyInit: BodyInit | null | undefined): Headers {
const headers = new Headers();
if (this.#setResponseContentLength) {
if (typeof bodyInit === "string") {
headers.append("content-length", bodyInit.length.toString());
}
}
return headers;
}
#getDefaultHeadersInit(): HeadersInit | undefined {
return this.#defaultHeadersInit;
}
/**
* Get interceptor method matcher
*/
#methodMatcher(): MockMatcher {
const method = this.#init?.method;
return isMockMatcher(method) ? method : this.#defaultRequest.method;
}
/**
* Get interceptor URL matcher
*/
#urlMatcher(): MockMatcher {
let url: MockMatcher;
if (isMockMatcher(this.#input)) {
url = this.#input;
} else if (this.#input instanceof URL) {
url = this.#input.href;
} else {
url = this.#input.url;
}
return typeof url === "string" ? safeURL(url) : url;
}
/**
* Get interceptor body matcher
*
* Note, if input is of type Request, this will be handled in MockFetch initialisation.
*/
#bodyMatcher(): MockMatcher | undefined {
let body: MockMatcher | undefined;
const initBody = this.#init?.body;
if (isMockMatcher(initBody)) {
body = initBody;
}
return body;
}
}