@zimic/interceptor 0.15.0-canary.0 → 0.15.0-canary.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -7
- package/dist/{chunk-YGJD3JT6.mjs → chunk-3O3YPVEG.mjs} +69 -104
- package/dist/chunk-3O3YPVEG.mjs.map +1 -0
- package/dist/{chunk-L6Y4EV6T.js → chunk-CWBDZYUG.js} +69 -104
- package/dist/chunk-CWBDZYUG.js.map +1 -0
- package/dist/cli.js +7 -7
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +3 -3
- package/dist/cli.mjs.map +1 -1
- package/dist/http.d.ts +38 -35
- package/dist/http.js +173 -235
- package/dist/http.js.map +1 -1
- package/dist/http.mjs +173 -235
- package/dist/http.mjs.map +1 -1
- package/dist/server.d.ts +15 -10
- package/dist/server.js +5 -5
- package/dist/server.mjs +1 -1
- package/package.json +2 -2
- package/src/cli/server/start.ts +1 -1
- package/src/http/interceptor/HttpInterceptorClient.ts +23 -34
- package/src/http/interceptor/HttpInterceptorStore.ts +2 -2
- package/src/http/interceptor/LocalHttpInterceptor.ts +22 -25
- package/src/http/interceptor/RemoteHttpInterceptor.ts +22 -25
- package/src/http/interceptor/errors/NotStartedHttpInterceptorError.ts +1 -1
- package/src/http/interceptor/errors/UnknownHttpInterceptorPlatformError.ts +1 -1
- package/src/http/interceptor/types/options.ts +3 -3
- package/src/http/interceptor/types/public.ts +12 -9
- package/src/http/interceptorWorker/HttpInterceptorWorker.ts +10 -28
- package/src/http/interceptorWorker/LocalHttpInterceptorWorker.ts +21 -25
- package/src/http/interceptorWorker/RemoteHttpInterceptorWorker.ts +22 -26
- package/src/http/requestHandler/HttpRequestHandlerClient.ts +11 -22
- package/src/http/requestHandler/LocalHttpRequestHandler.ts +16 -20
- package/src/http/requestHandler/RemoteHttpRequestHandler.ts +18 -28
- package/src/http/requestHandler/types/public.ts +22 -24
- package/src/server/InterceptorServer.ts +45 -68
- package/src/server/types/public.ts +15 -10
- package/src/webSocket/WebSocketClient.ts +1 -1
- package/src/webSocket/WebSocketHandler.ts +11 -19
- package/src/webSocket/WebSocketServer.ts +4 -4
- package/dist/chunk-L6Y4EV6T.js.map +0 -1
- package/dist/chunk-YGJD3JT6.mjs.map +0 -1
|
@@ -19,7 +19,7 @@ import { BrowserHttpWorker, HttpResponseFactory, HttpWorker, NodeHttpWorker } fr
|
|
|
19
19
|
class LocalHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
20
20
|
readonly type: 'local';
|
|
21
21
|
|
|
22
|
-
private
|
|
22
|
+
private internalWorker?: HttpWorker;
|
|
23
23
|
|
|
24
24
|
private defaultHttpHandler: MSWHttpHandler;
|
|
25
25
|
|
|
@@ -38,18 +38,18 @@ class LocalHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
internalWorkerOrThrow() {
|
|
42
|
-
if (!this.
|
|
41
|
+
get internalWorkerOrThrow() {
|
|
42
|
+
if (!this.internalWorker) {
|
|
43
43
|
throw new NotStartedHttpInterceptorError();
|
|
44
44
|
}
|
|
45
|
-
return this.
|
|
45
|
+
return this.internalWorker;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
internalWorkerOrCreate() {
|
|
49
|
-
if (!this.
|
|
50
|
-
this.
|
|
48
|
+
get internalWorkerOrCreate() {
|
|
49
|
+
if (!this.internalWorker) {
|
|
50
|
+
this.internalWorker = this.createInternalWorker();
|
|
51
51
|
}
|
|
52
|
-
return this.
|
|
52
|
+
return this.internalWorker;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
private createInternalWorker() {
|
|
@@ -67,21 +67,21 @@ class LocalHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
67
67
|
|
|
68
68
|
async start() {
|
|
69
69
|
await super.sharedStart(async () => {
|
|
70
|
-
const internalWorker = this.internalWorkerOrCreate
|
|
70
|
+
const internalWorker = this.internalWorkerOrCreate;
|
|
71
71
|
|
|
72
72
|
const sharedOptions: MSWWorkerSharedOptions = {
|
|
73
73
|
onUnhandledRequest: 'bypass',
|
|
74
74
|
};
|
|
75
75
|
|
|
76
76
|
if (this.isInternalBrowserWorker(internalWorker)) {
|
|
77
|
-
|
|
77
|
+
this.platform = 'browser';
|
|
78
78
|
await this.startInBrowser(internalWorker, sharedOptions);
|
|
79
79
|
} else {
|
|
80
|
-
|
|
80
|
+
this.platform = 'node';
|
|
81
81
|
this.startInNode(internalWorker, sharedOptions);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
|
|
84
|
+
this.isRunning = true;
|
|
85
85
|
});
|
|
86
86
|
}
|
|
87
87
|
|
|
@@ -106,7 +106,7 @@ class LocalHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
106
106
|
|
|
107
107
|
async stop() {
|
|
108
108
|
await super.sharedStop(() => {
|
|
109
|
-
const internalWorker = this.internalWorkerOrCreate
|
|
109
|
+
const internalWorker = this.internalWorkerOrCreate;
|
|
110
110
|
|
|
111
111
|
if (this.isInternalBrowserWorker(internalWorker)) {
|
|
112
112
|
this.stopInBrowser(internalWorker);
|
|
@@ -115,8 +115,8 @@ class LocalHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
115
115
|
}
|
|
116
116
|
this.clearHandlers();
|
|
117
117
|
|
|
118
|
-
this.
|
|
119
|
-
|
|
118
|
+
this.internalWorker = undefined;
|
|
119
|
+
this.isRunning = false;
|
|
120
120
|
});
|
|
121
121
|
}
|
|
122
122
|
|
|
@@ -133,7 +133,7 @@ class LocalHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
hasInternalBrowserWorker() {
|
|
136
|
-
return this.isInternalBrowserWorker(this.internalWorkerOrThrow
|
|
136
|
+
return this.isInternalBrowserWorker(this.internalWorkerOrThrow);
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
hasInternalNodeWorker() {
|
|
@@ -146,7 +146,6 @@ class LocalHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
146
146
|
rawURL: string | URL,
|
|
147
147
|
createResponse: HttpResponseFactory,
|
|
148
148
|
) {
|
|
149
|
-
const internalWorker = this.internalWorkerOrThrow();
|
|
150
149
|
const lowercaseMethod = method.toLowerCase<typeof method>();
|
|
151
150
|
|
|
152
151
|
const url = new URL(rawURL);
|
|
@@ -180,7 +179,7 @@ class LocalHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
180
179
|
return response;
|
|
181
180
|
});
|
|
182
181
|
|
|
183
|
-
|
|
182
|
+
this.internalWorkerOrThrow.use(httpHandler);
|
|
184
183
|
|
|
185
184
|
this.httpHandlerGroups.push({ interceptor, httpHandler });
|
|
186
185
|
}
|
|
@@ -199,25 +198,22 @@ class LocalHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
199
198
|
}
|
|
200
199
|
|
|
201
200
|
clearHandlers() {
|
|
202
|
-
|
|
203
|
-
internalWorker.resetHandlers();
|
|
201
|
+
this.internalWorkerOrThrow.resetHandlers();
|
|
204
202
|
this.httpHandlerGroups = [];
|
|
205
203
|
}
|
|
206
204
|
|
|
207
205
|
clearInterceptorHandlers<Schema extends HttpSchema>(interceptor: HttpInterceptorClient<Schema>) {
|
|
208
|
-
const internalWorker = this.internalWorkerOrThrow();
|
|
209
|
-
|
|
210
206
|
const groupToRemoveIndex = this.httpHandlerGroups.findIndex((group) => group.interceptor === interceptor);
|
|
211
207
|
removeArrayIndex(this.httpHandlerGroups, groupToRemoveIndex);
|
|
212
208
|
|
|
213
|
-
|
|
209
|
+
this.internalWorkerOrThrow.resetHandlers();
|
|
214
210
|
|
|
215
211
|
for (const { httpHandler } of this.httpHandlerGroups) {
|
|
216
|
-
|
|
212
|
+
this.internalWorkerOrThrow.use(httpHandler);
|
|
217
213
|
}
|
|
218
214
|
}
|
|
219
215
|
|
|
220
|
-
interceptorsWithHandlers() {
|
|
216
|
+
get interceptorsWithHandlers() {
|
|
221
217
|
return this.httpHandlerGroups.map((group) => group.interceptor);
|
|
222
218
|
}
|
|
223
219
|
}
|
|
@@ -28,7 +28,8 @@ interface HttpHandler {
|
|
|
28
28
|
class RemoteHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
29
29
|
readonly type: 'remote';
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
webSocketClient: WebSocketClient<InterceptorServerWebSocketSchema>;
|
|
32
|
+
|
|
32
33
|
private httpHandlers = new Map<HttpHandler['id'], HttpHandler>();
|
|
33
34
|
|
|
34
35
|
constructor(options: RemoteHttpInterceptorWorkerOptions) {
|
|
@@ -36,7 +37,7 @@ class RemoteHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
36
37
|
this.type = options.type;
|
|
37
38
|
|
|
38
39
|
const webSocketServerURL = this.deriveWebSocketServerURL(options.serverURL);
|
|
39
|
-
this.
|
|
40
|
+
this.webSocketClient = new WebSocketClient({
|
|
40
41
|
url: webSocketServerURL.toString(),
|
|
41
42
|
});
|
|
42
43
|
}
|
|
@@ -47,20 +48,15 @@ class RemoteHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
47
48
|
return webSocketServerURL;
|
|
48
49
|
}
|
|
49
50
|
|
|
50
|
-
webSocketClient() {
|
|
51
|
-
return this._webSocketClient;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
51
|
async start() {
|
|
55
52
|
await super.sharedStart(async () => {
|
|
56
|
-
await this.
|
|
53
|
+
await this.webSocketClient.start();
|
|
57
54
|
|
|
58
|
-
this.
|
|
59
|
-
this.
|
|
55
|
+
this.webSocketClient.onEvent('interceptors/responses/create', this.createResponse);
|
|
56
|
+
this.webSocketClient.onEvent('interceptors/responses/unhandled', this.handleUnhandledServerRequest);
|
|
60
57
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
super.setIsRunning(true);
|
|
58
|
+
this.platform = this.readPlatform();
|
|
59
|
+
this.isRunning = true;
|
|
64
60
|
});
|
|
65
61
|
}
|
|
66
62
|
|
|
@@ -118,12 +114,12 @@ class RemoteHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
118
114
|
await super.sharedStop(async () => {
|
|
119
115
|
await this.clearHandlers();
|
|
120
116
|
|
|
121
|
-
this.
|
|
122
|
-
this.
|
|
117
|
+
this.webSocketClient.offEvent('interceptors/responses/create', this.createResponse);
|
|
118
|
+
this.webSocketClient.offEvent('interceptors/responses/unhandled', this.handleUnhandledServerRequest);
|
|
123
119
|
|
|
124
|
-
await this.
|
|
120
|
+
await this.webSocketClient.stop();
|
|
125
121
|
|
|
126
|
-
|
|
122
|
+
this.isRunning = false;
|
|
127
123
|
});
|
|
128
124
|
}
|
|
129
125
|
|
|
@@ -133,7 +129,7 @@ class RemoteHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
133
129
|
rawURL: string | URL,
|
|
134
130
|
createResponse: HttpResponseFactory,
|
|
135
131
|
) {
|
|
136
|
-
if (!
|
|
132
|
+
if (!this.isRunning) {
|
|
137
133
|
throw new NotStartedHttpInterceptorError();
|
|
138
134
|
}
|
|
139
135
|
|
|
@@ -146,7 +142,7 @@ class RemoteHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
146
142
|
const handler: HttpHandler = {
|
|
147
143
|
id: crypto.randomUUID(),
|
|
148
144
|
url: {
|
|
149
|
-
base: interceptor.
|
|
145
|
+
base: interceptor.baseURLAsString,
|
|
150
146
|
full: url.toString(),
|
|
151
147
|
},
|
|
152
148
|
method,
|
|
@@ -159,7 +155,7 @@ class RemoteHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
159
155
|
|
|
160
156
|
this.httpHandlers.set(handler.id, handler);
|
|
161
157
|
|
|
162
|
-
await this.
|
|
158
|
+
await this.webSocketClient.request('interceptors/workers/use/commit', {
|
|
163
159
|
id: handler.id,
|
|
164
160
|
url: handler.url,
|
|
165
161
|
method,
|
|
@@ -167,19 +163,19 @@ class RemoteHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
167
163
|
}
|
|
168
164
|
|
|
169
165
|
async clearHandlers() {
|
|
170
|
-
if (!
|
|
166
|
+
if (!this.isRunning) {
|
|
171
167
|
throw new NotStartedHttpInterceptorError();
|
|
172
168
|
}
|
|
173
169
|
|
|
174
170
|
this.httpHandlers.clear();
|
|
175
171
|
|
|
176
|
-
if (this.
|
|
177
|
-
await this.
|
|
172
|
+
if (this.webSocketClient.isRunning) {
|
|
173
|
+
await this.webSocketClient.request('interceptors/workers/use/reset', undefined);
|
|
178
174
|
}
|
|
179
175
|
}
|
|
180
176
|
|
|
181
177
|
async clearInterceptorHandlers<Schema extends HttpSchema>(interceptor: HttpInterceptorClient<Schema>) {
|
|
182
|
-
if (!
|
|
178
|
+
if (!this.isRunning) {
|
|
183
179
|
throw new NotStartedHttpInterceptorError();
|
|
184
180
|
}
|
|
185
181
|
|
|
@@ -189,18 +185,18 @@ class RemoteHttpInterceptorWorker extends HttpInterceptorWorker {
|
|
|
189
185
|
}
|
|
190
186
|
}
|
|
191
187
|
|
|
192
|
-
if (this.
|
|
188
|
+
if (this.webSocketClient.isRunning) {
|
|
193
189
|
const groupsToRecommit = Array.from<HttpHandler, HttpHandlerCommit>(this.httpHandlers.values(), (handler) => ({
|
|
194
190
|
id: handler.id,
|
|
195
191
|
url: handler.url,
|
|
196
192
|
method: handler.method,
|
|
197
193
|
}));
|
|
198
194
|
|
|
199
|
-
await this.
|
|
195
|
+
await this.webSocketClient.request('interceptors/workers/use/reset', groupsToRecommit);
|
|
200
196
|
}
|
|
201
197
|
}
|
|
202
198
|
|
|
203
|
-
interceptorsWithHandlers() {
|
|
199
|
+
get interceptorsWithHandlers() {
|
|
204
200
|
const interceptors = Array.from(this.httpHandlers.values(), (handler) => handler.interceptor);
|
|
205
201
|
return interceptors;
|
|
206
202
|
}
|
|
@@ -58,8 +58,7 @@ class HttpRequestHandlerClient<
|
|
|
58
58
|
|
|
59
59
|
private numberOfMatchedRequests = 0;
|
|
60
60
|
private unmatchedRequestGroups: UnmatchedHttpInterceptorRequestGroup[] = [];
|
|
61
|
-
private
|
|
62
|
-
[];
|
|
61
|
+
private _requests: InterceptedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[] = [];
|
|
63
62
|
|
|
64
63
|
private createResponseDeclaration?: HttpRequestHandlerResponseDeclarationFactory<
|
|
65
64
|
Path,
|
|
@@ -69,19 +68,11 @@ class HttpRequestHandlerClient<
|
|
|
69
68
|
|
|
70
69
|
constructor(
|
|
71
70
|
private interceptor: HttpInterceptorClient<Schema>,
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
public method: Method,
|
|
72
|
+
public path: Path,
|
|
74
73
|
private handler: InternalHttpRequestHandler<Schema, Method, Path, StatusCode>,
|
|
75
74
|
) {}
|
|
76
75
|
|
|
77
|
-
method() {
|
|
78
|
-
return this._method;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
path() {
|
|
82
|
-
return this._path;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
76
|
with(restriction: HttpRequestHandlerRestriction<Schema, Method, Path>): this {
|
|
86
77
|
this.restrictions.push(restriction);
|
|
87
78
|
return this;
|
|
@@ -100,7 +91,7 @@ class HttpRequestHandlerClient<
|
|
|
100
91
|
|
|
101
92
|
newThis.numberOfMatchedRequests = 0;
|
|
102
93
|
newThis.unmatchedRequestGroups.length = 0;
|
|
103
|
-
newThis.
|
|
94
|
+
newThis._requests.length = 0;
|
|
104
95
|
|
|
105
96
|
this.interceptor.registerRequestHandler(this.handler);
|
|
106
97
|
|
|
@@ -141,7 +132,7 @@ class HttpRequestHandlerClient<
|
|
|
141
132
|
declarationPointer: this.timesDeclarationPointer,
|
|
142
133
|
unmatchedRequestGroups: this.unmatchedRequestGroups,
|
|
143
134
|
hasRestrictions: this.restrictions.length > 0,
|
|
144
|
-
hasSavedRequests: this.interceptor.shouldSaveRequests
|
|
135
|
+
hasSavedRequests: this.interceptor.shouldSaveRequests,
|
|
145
136
|
});
|
|
146
137
|
}
|
|
147
138
|
}
|
|
@@ -157,7 +148,7 @@ class HttpRequestHandlerClient<
|
|
|
157
148
|
|
|
158
149
|
this.numberOfMatchedRequests = 0;
|
|
159
150
|
this.unmatchedRequestGroups.length = 0;
|
|
160
|
-
this.
|
|
151
|
+
this._requests.length = 0;
|
|
161
152
|
|
|
162
153
|
this.createResponseDeclaration = undefined;
|
|
163
154
|
|
|
@@ -177,7 +168,7 @@ class HttpRequestHandlerClient<
|
|
|
177
168
|
this.numberOfMatchedRequests++;
|
|
178
169
|
} else {
|
|
179
170
|
const shouldSaveUnmatchedGroup =
|
|
180
|
-
this.interceptor.shouldSaveRequests
|
|
171
|
+
this.interceptor.shouldSaveRequests &&
|
|
181
172
|
this.restrictions.length > 0 &&
|
|
182
173
|
this.timesDeclarationPointer !== undefined;
|
|
183
174
|
|
|
@@ -388,7 +379,7 @@ class HttpRequestHandlerClient<
|
|
|
388
379
|
response: HttpInterceptorResponse<Default<Schema[Path][Method]>, StatusCode>,
|
|
389
380
|
) {
|
|
390
381
|
const interceptedRequest = this.createInterceptedRequest(request, response);
|
|
391
|
-
this.
|
|
382
|
+
this._requests.push(interceptedRequest);
|
|
392
383
|
}
|
|
393
384
|
|
|
394
385
|
private createInterceptedRequest(
|
|
@@ -411,14 +402,12 @@ class HttpRequestHandlerClient<
|
|
|
411
402
|
return interceptedRequest;
|
|
412
403
|
}
|
|
413
404
|
|
|
414
|
-
requests(): readonly InterceptedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[] {
|
|
415
|
-
if (!this.interceptor.shouldSaveRequests
|
|
405
|
+
get requests(): readonly InterceptedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[] {
|
|
406
|
+
if (!this.interceptor.shouldSaveRequests) {
|
|
416
407
|
throw new DisabledRequestSavingError();
|
|
417
408
|
}
|
|
418
409
|
|
|
419
|
-
|
|
420
|
-
Object.freeze(interceptedRequestsCopy);
|
|
421
|
-
return interceptedRequestsCopy;
|
|
410
|
+
return this._requests;
|
|
422
411
|
}
|
|
423
412
|
}
|
|
424
413
|
|
|
@@ -22,26 +22,22 @@ class LocalHttpRequestHandler<
|
|
|
22
22
|
{
|
|
23
23
|
readonly type = 'local';
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
client: HttpRequestHandlerClient<Schema, Method, Path, StatusCode>;
|
|
26
26
|
|
|
27
27
|
constructor(interceptor: HttpInterceptorClient<Schema>, method: Method, path: Path) {
|
|
28
|
-
this.
|
|
28
|
+
this.client = new HttpRequestHandlerClient(interceptor, method, path, this);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
return this.
|
|
31
|
+
get method() {
|
|
32
|
+
return this.client.method;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
return this.
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
path() {
|
|
40
|
-
return this._client.path();
|
|
35
|
+
get path() {
|
|
36
|
+
return this.client.path;
|
|
41
37
|
}
|
|
42
38
|
|
|
43
39
|
with(restriction: HttpRequestHandlerRestriction<Schema, Method, Path>): this {
|
|
44
|
-
this.
|
|
40
|
+
this.client.with(restriction);
|
|
45
41
|
return this;
|
|
46
42
|
}
|
|
47
43
|
|
|
@@ -50,45 +46,45 @@ class LocalHttpRequestHandler<
|
|
|
50
46
|
| HttpRequestHandlerResponseDeclaration<Default<Schema[Path][Method]>, NewStatusCode>
|
|
51
47
|
| HttpRequestHandlerResponseDeclarationFactory<Path, Default<Schema[Path][Method]>, NewStatusCode>,
|
|
52
48
|
): LocalHttpRequestHandler<Schema, Method, Path, NewStatusCode> {
|
|
53
|
-
this.
|
|
49
|
+
this.client.respond(declaration);
|
|
54
50
|
|
|
55
51
|
const newThis = this as unknown as LocalHttpRequestHandler<Schema, Method, Path, NewStatusCode>;
|
|
56
52
|
return newThis;
|
|
57
53
|
}
|
|
58
54
|
|
|
59
55
|
times(minNumberOfRequests: number, maxNumberOfRequests?: number): this {
|
|
60
|
-
this.
|
|
56
|
+
this.client.times(minNumberOfRequests, maxNumberOfRequests);
|
|
61
57
|
return this;
|
|
62
58
|
}
|
|
63
59
|
|
|
64
60
|
checkTimes() {
|
|
65
|
-
this.
|
|
61
|
+
this.client.checkTimes();
|
|
66
62
|
}
|
|
67
63
|
|
|
68
64
|
clear(): this {
|
|
69
|
-
this.
|
|
65
|
+
this.client.clear();
|
|
70
66
|
return this;
|
|
71
67
|
}
|
|
72
68
|
|
|
73
|
-
requests(): readonly InterceptedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[] {
|
|
74
|
-
return this.
|
|
69
|
+
get requests(): readonly InterceptedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[] {
|
|
70
|
+
return this.client.requests;
|
|
75
71
|
}
|
|
76
72
|
|
|
77
73
|
matchesRequest(request: HttpInterceptorRequest<Path, Default<Schema[Path][Method]>>): Promise<boolean> {
|
|
78
|
-
return this.
|
|
74
|
+
return this.client.matchesRequest(request);
|
|
79
75
|
}
|
|
80
76
|
|
|
81
77
|
async applyResponseDeclaration(
|
|
82
78
|
request: HttpInterceptorRequest<Path, Default<Schema[Path][Method]>>,
|
|
83
79
|
): Promise<HttpRequestHandlerResponseDeclaration<Default<Schema[Path][Method]>, StatusCode>> {
|
|
84
|
-
return this.
|
|
80
|
+
return this.client.applyResponseDeclaration(request);
|
|
85
81
|
}
|
|
86
82
|
|
|
87
83
|
saveInterceptedRequest(
|
|
88
84
|
request: HttpInterceptorRequest<Path, Default<Schema[Path][Method]>>,
|
|
89
85
|
response: HttpInterceptorResponse<Default<Schema[Path][Method]>, StatusCode>,
|
|
90
86
|
) {
|
|
91
|
-
this.
|
|
87
|
+
this.client.saveInterceptedRequest(request, response);
|
|
92
88
|
}
|
|
93
89
|
}
|
|
94
90
|
|
|
@@ -27,7 +27,7 @@ class RemoteHttpRequestHandler<
|
|
|
27
27
|
{
|
|
28
28
|
readonly type = 'remote';
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
client: HttpRequestHandlerClient<Schema, Method, Path, StatusCode>;
|
|
31
31
|
|
|
32
32
|
private syncPromises: Promise<unknown>[] = [];
|
|
33
33
|
|
|
@@ -35,7 +35,7 @@ class RemoteHttpRequestHandler<
|
|
|
35
35
|
private synced: this;
|
|
36
36
|
|
|
37
37
|
constructor(interceptor: HttpInterceptorClient<Schema, typeof RemoteHttpRequestHandler>, method: Method, path: Path) {
|
|
38
|
-
this.
|
|
38
|
+
this.client = new HttpRequestHandlerClient(interceptor, method, path, this);
|
|
39
39
|
this.unsynced = this;
|
|
40
40
|
this.synced = this.createSyncedProxy();
|
|
41
41
|
}
|
|
@@ -62,20 +62,16 @@ class RemoteHttpRequestHandler<
|
|
|
62
62
|
return PENDING_PROPERTIES.has(property);
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
return this.
|
|
65
|
+
get method() {
|
|
66
|
+
return this.client.method;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
return this.
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
path() {
|
|
74
|
-
return this._client.path();
|
|
69
|
+
get path() {
|
|
70
|
+
return this.client.path;
|
|
75
71
|
}
|
|
76
72
|
|
|
77
73
|
with(restriction: HttpRequestHandlerRestriction<Schema, Method, Path>): this {
|
|
78
|
-
this.
|
|
74
|
+
this.client.with(restriction);
|
|
79
75
|
return this.unsynced;
|
|
80
76
|
}
|
|
81
77
|
|
|
@@ -85,19 +81,19 @@ class RemoteHttpRequestHandler<
|
|
|
85
81
|
| HttpRequestHandlerResponseDeclarationFactory<Path, Default<Schema[Path][Method]>, NewStatusCode>,
|
|
86
82
|
): RemoteHttpRequestHandler<Schema, Method, Path, NewStatusCode> {
|
|
87
83
|
const newUnsyncedThis = this.unsynced as unknown as RemoteHttpRequestHandler<Schema, Method, Path, NewStatusCode>;
|
|
88
|
-
newUnsyncedThis.
|
|
84
|
+
newUnsyncedThis.client.respond(declaration);
|
|
89
85
|
return newUnsyncedThis;
|
|
90
86
|
}
|
|
91
87
|
|
|
92
88
|
times(minNumberOfRequests: number, maxNumberOfRequests?: number): this {
|
|
93
|
-
this.
|
|
89
|
+
this.client.times(minNumberOfRequests, maxNumberOfRequests);
|
|
94
90
|
return this;
|
|
95
91
|
}
|
|
96
92
|
|
|
97
93
|
async checkTimes() {
|
|
98
94
|
return new Promise<void>((resolve, reject) => {
|
|
99
95
|
try {
|
|
100
|
-
this.
|
|
96
|
+
this.client.checkTimes();
|
|
101
97
|
resolve();
|
|
102
98
|
} catch (error) {
|
|
103
99
|
reject(error);
|
|
@@ -106,42 +102,36 @@ class RemoteHttpRequestHandler<
|
|
|
106
102
|
}
|
|
107
103
|
|
|
108
104
|
clear(): this {
|
|
109
|
-
this.
|
|
105
|
+
this.client.clear();
|
|
110
106
|
return this.unsynced;
|
|
111
107
|
}
|
|
112
108
|
|
|
113
|
-
requests():
|
|
114
|
-
return
|
|
115
|
-
try {
|
|
116
|
-
resolve(this._client.requests());
|
|
117
|
-
} catch (error) {
|
|
118
|
-
reject(error);
|
|
119
|
-
}
|
|
120
|
-
});
|
|
109
|
+
get requests(): readonly InterceptedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[] {
|
|
110
|
+
return this.client.requests;
|
|
121
111
|
}
|
|
122
112
|
|
|
123
113
|
matchesRequest(request: HttpInterceptorRequest<Path, Default<Schema[Path][Method]>>): Promise<boolean> {
|
|
124
|
-
return this.
|
|
114
|
+
return this.client.matchesRequest(request);
|
|
125
115
|
}
|
|
126
116
|
|
|
127
117
|
async applyResponseDeclaration(
|
|
128
118
|
request: HttpInterceptorRequest<Path, Default<Schema[Path][Method]>>,
|
|
129
119
|
): Promise<HttpRequestHandlerResponseDeclaration<Default<Schema[Path][Method]>, StatusCode>> {
|
|
130
|
-
return this.
|
|
120
|
+
return this.client.applyResponseDeclaration(request);
|
|
131
121
|
}
|
|
132
122
|
|
|
133
123
|
saveInterceptedRequest(
|
|
134
124
|
request: HttpInterceptorRequest<Path, Default<Schema[Path][Method]>>,
|
|
135
125
|
response: HttpInterceptorResponse<Default<Schema[Path][Method]>, StatusCode>,
|
|
136
126
|
) {
|
|
137
|
-
this.
|
|
127
|
+
this.client.saveInterceptedRequest(request, response);
|
|
138
128
|
}
|
|
139
129
|
|
|
140
130
|
registerSyncPromise(promise: Promise<unknown>) {
|
|
141
131
|
this.syncPromises.push(promise);
|
|
142
132
|
}
|
|
143
133
|
|
|
144
|
-
isSynced()
|
|
134
|
+
get isSynced() {
|
|
145
135
|
return this.syncPromises.length === 0;
|
|
146
136
|
}
|
|
147
137
|
|
|
@@ -162,7 +152,7 @@ class RemoteHttpRequestHandler<
|
|
|
162
152
|
.then(() => {
|
|
163
153
|
this.syncPromises = this.syncPromises.filter((promise) => !promisesToWait.has(promise));
|
|
164
154
|
|
|
165
|
-
return this.isSynced
|
|
155
|
+
return this.isSynced ? this.synced : this.unsynced;
|
|
166
156
|
})
|
|
167
157
|
.then(onFulfilled, onRejected);
|
|
168
158
|
}
|
|
@@ -30,17 +30,19 @@ export interface HttpRequestHandler<
|
|
|
30
30
|
Path extends HttpSchemaPath<Schema, Method>,
|
|
31
31
|
> {
|
|
32
32
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
33
|
+
* The method that matches this handler.
|
|
34
|
+
*
|
|
35
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlermethod `handler.method` API reference}
|
|
35
36
|
*/
|
|
36
|
-
method:
|
|
37
|
+
method: Method;
|
|
37
38
|
|
|
38
39
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
40
|
+
* The path that matches this handler. The base URL of the interceptor is not included, but it is used when matching
|
|
41
|
+
* requests.
|
|
42
|
+
*
|
|
43
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerpath `handler.path` API reference}
|
|
42
44
|
*/
|
|
43
|
-
path:
|
|
45
|
+
path: Path;
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
export interface InternalHttpRequestHandler<
|
|
@@ -49,7 +51,7 @@ export interface InternalHttpRequestHandler<
|
|
|
49
51
|
Path extends HttpSchemaPath<Schema, Method>,
|
|
50
52
|
StatusCode extends HttpStatusCode = never,
|
|
51
53
|
> extends HttpRequestHandler<Schema, Method, Path> {
|
|
52
|
-
client:
|
|
54
|
+
client: HttpRequestHandlerClient<Schema, Method, Path, StatusCode>;
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
/**
|
|
@@ -118,7 +120,7 @@ export interface LocalHttpRequestHandler<
|
|
|
118
120
|
* intercepted request in the
|
|
119
121
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptormethodpath `interceptor.<method>(path)` API reference}.
|
|
120
122
|
*
|
|
121
|
-
* **
|
|
123
|
+
* **Important**: To make sure that all expected requests were made, use
|
|
122
124
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorchecktimes `interceptor.checkTimes()`}
|
|
123
125
|
* or {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlertimes `handler.times()`}.
|
|
124
126
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorchecktimes `interceptor.checkTimes()`}
|
|
@@ -170,18 +172,17 @@ export interface LocalHttpRequestHandler<
|
|
|
170
172
|
clear: () => this;
|
|
171
173
|
|
|
172
174
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
+
* The intercepted requests that matched this handler, along with the responses returned to each of them. This is
|
|
176
|
+
* useful for testing that the correct requests were made by your application.
|
|
175
177
|
*
|
|
176
|
-
* **
|
|
178
|
+
* **Important**: This method can only be used if `saveRequests` was set to `true` when creating the interceptor. See
|
|
177
179
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
|
|
178
180
|
* for more information.
|
|
179
181
|
*
|
|
180
|
-
* @returns The intercepted requests.
|
|
181
182
|
* @throws {DisabledRequestSavingError} If the interceptor was not created with `saveRequests: true`.
|
|
182
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests
|
|
183
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests` API reference}
|
|
183
184
|
*/
|
|
184
|
-
requests:
|
|
185
|
+
requests: readonly InterceptedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[];
|
|
185
186
|
}
|
|
186
187
|
|
|
187
188
|
/**
|
|
@@ -249,7 +250,7 @@ export interface SyncedRemoteHttpRequestHandler<
|
|
|
249
250
|
* intercepted request in the
|
|
250
251
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptormethodpath `interceptor.<method>(path)` API reference}.
|
|
251
252
|
*
|
|
252
|
-
* **
|
|
253
|
+
* **Important**: To make sure that all expected requests were made, use
|
|
253
254
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-interceptorchecktimes `interceptor.checkTimes()`}
|
|
254
255
|
* or
|
|
255
256
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerchecktimes `handler.checkTimes()`}.
|
|
@@ -306,20 +307,17 @@ export interface SyncedRemoteHttpRequestHandler<
|
|
|
306
307
|
clear: () => PendingRemoteHttpRequestHandler<Schema, Method, Path, StatusCode>;
|
|
307
308
|
|
|
308
309
|
/**
|
|
309
|
-
*
|
|
310
|
-
*
|
|
310
|
+
* The intercepted requests that matched this handler, along with the responses returned to each of them. This is
|
|
311
|
+
* useful for testing that the correct requests were made by your application.
|
|
311
312
|
*
|
|
312
|
-
* **
|
|
313
|
+
* **Important**: This method can only be used if `saveRequests` was set to `true` when creating the interceptor. See
|
|
313
314
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#saving-requests Saving intercepted requests}
|
|
314
315
|
* for more information.
|
|
315
316
|
*
|
|
316
|
-
* @returns The intercepted requests.
|
|
317
317
|
* @throws {DisabledRequestSavingError} If the interceptor was not created with `saveRequests: true`.
|
|
318
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests
|
|
318
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#http-handlerrequests `handler.requests` API reference}
|
|
319
319
|
*/
|
|
320
|
-
requests:
|
|
321
|
-
readonly InterceptedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[]
|
|
322
|
-
>;
|
|
320
|
+
requests: readonly InterceptedHttpInterceptorRequest<Path, Default<Schema[Path][Method]>, StatusCode>[];
|
|
323
321
|
}
|
|
324
322
|
|
|
325
323
|
/**
|