msw 0.20.1 → 0.20.5
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/lib/esm/fetch-deps.js +6 -1
- package/lib/esm/graphql.js +19 -8
- package/lib/esm/index-deps.js +1 -1
- package/lib/esm/index.js +76 -42
- package/lib/esm/index2.js +1 -1
- package/lib/esm/matchRequest-deps.js +168 -0
- package/lib/esm/rest-deps.js +14 -101
- package/lib/esm/rest.js +1 -1
- package/lib/esm/xml-deps.js +6 -9
- package/lib/types/context/cookie.d.ts +1 -1
- package/lib/types/graphql.d.ts +9 -2
- package/lib/types/handlers/requestHandler.d.ts +4 -3
- package/lib/types/index.d.ts +3 -2
- package/lib/types/rest.d.ts +10 -6
- package/lib/types/setupWorker/glossary.d.ts +1 -0
- package/lib/types/setupWorker/start/utils/getWorkerByRegistration.d.ts +3 -3
- package/lib/types/utils/matching/getCleanMask.d.ts +2 -0
- package/lib/types/utils/matching/matchRequest.d.ts +7 -0
- package/lib/umd/index.js +177 -114
- package/native/index.js +57 -149
- package/node/index.js +57 -149
- package/package.json +2 -2
- package/lib/esm/getStatusCodeColor-deps.js +0 -65
package/native/index.js
CHANGED
|
@@ -42,7 +42,6 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
42
42
|
*/
|
|
43
43
|
|
|
44
44
|
var parse_1 = parse;
|
|
45
|
-
var serialize_1 = serialize;
|
|
46
45
|
|
|
47
46
|
/**
|
|
48
47
|
* Module variables.
|
|
@@ -50,19 +49,8 @@ var serialize_1 = serialize;
|
|
|
50
49
|
*/
|
|
51
50
|
|
|
52
51
|
var decode = decodeURIComponent;
|
|
53
|
-
var encode = encodeURIComponent;
|
|
54
52
|
var pairSplitRegExp = /; */;
|
|
55
53
|
|
|
56
|
-
/**
|
|
57
|
-
* RegExp to match field-content in RFC 7230 sec 3.2
|
|
58
|
-
*
|
|
59
|
-
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
|
|
60
|
-
* field-vchar = VCHAR / obs-text
|
|
61
|
-
* obs-text = %x80-FF
|
|
62
|
-
*/
|
|
63
|
-
|
|
64
|
-
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
|
65
|
-
|
|
66
54
|
/**
|
|
67
55
|
* Parse a cookie header.
|
|
68
56
|
*
|
|
@@ -111,109 +99,6 @@ function parse(str, options) {
|
|
|
111
99
|
return obj;
|
|
112
100
|
}
|
|
113
101
|
|
|
114
|
-
/**
|
|
115
|
-
* Serialize data into a cookie header.
|
|
116
|
-
*
|
|
117
|
-
* Serialize the a name value pair into a cookie string suitable for
|
|
118
|
-
* http headers. An optional options object specified cookie parameters.
|
|
119
|
-
*
|
|
120
|
-
* serialize('foo', 'bar', { httpOnly: true })
|
|
121
|
-
* => "foo=bar; httpOnly"
|
|
122
|
-
*
|
|
123
|
-
* @param {string} name
|
|
124
|
-
* @param {string} val
|
|
125
|
-
* @param {object} [options]
|
|
126
|
-
* @return {string}
|
|
127
|
-
* @public
|
|
128
|
-
*/
|
|
129
|
-
|
|
130
|
-
function serialize(name, val, options) {
|
|
131
|
-
var opt = options || {};
|
|
132
|
-
var enc = opt.encode || encode;
|
|
133
|
-
|
|
134
|
-
if (typeof enc !== 'function') {
|
|
135
|
-
throw new TypeError('option encode is invalid');
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (!fieldContentRegExp.test(name)) {
|
|
139
|
-
throw new TypeError('argument name is invalid');
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
var value = enc(val);
|
|
143
|
-
|
|
144
|
-
if (value && !fieldContentRegExp.test(value)) {
|
|
145
|
-
throw new TypeError('argument val is invalid');
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
var str = name + '=' + value;
|
|
149
|
-
|
|
150
|
-
if (null != opt.maxAge) {
|
|
151
|
-
var maxAge = opt.maxAge - 0;
|
|
152
|
-
|
|
153
|
-
if (isNaN(maxAge) || !isFinite(maxAge)) {
|
|
154
|
-
throw new TypeError('option maxAge is invalid')
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
str += '; Max-Age=' + Math.floor(maxAge);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (opt.domain) {
|
|
161
|
-
if (!fieldContentRegExp.test(opt.domain)) {
|
|
162
|
-
throw new TypeError('option domain is invalid');
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
str += '; Domain=' + opt.domain;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (opt.path) {
|
|
169
|
-
if (!fieldContentRegExp.test(opt.path)) {
|
|
170
|
-
throw new TypeError('option path is invalid');
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
str += '; Path=' + opt.path;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (opt.expires) {
|
|
177
|
-
if (typeof opt.expires.toUTCString !== 'function') {
|
|
178
|
-
throw new TypeError('option expires is invalid');
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
str += '; Expires=' + opt.expires.toUTCString();
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (opt.httpOnly) {
|
|
185
|
-
str += '; HttpOnly';
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
if (opt.secure) {
|
|
189
|
-
str += '; Secure';
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (opt.sameSite) {
|
|
193
|
-
var sameSite = typeof opt.sameSite === 'string'
|
|
194
|
-
? opt.sameSite.toLowerCase() : opt.sameSite;
|
|
195
|
-
|
|
196
|
-
switch (sameSite) {
|
|
197
|
-
case true:
|
|
198
|
-
str += '; SameSite=Strict';
|
|
199
|
-
break;
|
|
200
|
-
case 'lax':
|
|
201
|
-
str += '; SameSite=Lax';
|
|
202
|
-
break;
|
|
203
|
-
case 'strict':
|
|
204
|
-
str += '; SameSite=Strict';
|
|
205
|
-
break;
|
|
206
|
-
case 'none':
|
|
207
|
-
str += '; SameSite=None';
|
|
208
|
-
break;
|
|
209
|
-
default:
|
|
210
|
-
throw new TypeError('option sameSite is invalid');
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return str;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
102
|
/**
|
|
218
103
|
* Try decoding a string using a decoding function.
|
|
219
104
|
*
|
|
@@ -230,11 +115,6 @@ function tryDecode(str, decode) {
|
|
|
230
115
|
}
|
|
231
116
|
}
|
|
232
117
|
|
|
233
|
-
var cookie = {
|
|
234
|
-
parse: parse_1,
|
|
235
|
-
serialize: serialize_1
|
|
236
|
-
};
|
|
237
|
-
|
|
238
118
|
function createCommonjsModule(fn, basedir, module) {
|
|
239
119
|
return module = {
|
|
240
120
|
path: basedir,
|
|
@@ -487,7 +367,7 @@ exports.flattenHeadersList = flattenHeadersList_1.flattenHeadersList;
|
|
|
487
367
|
exports.flattenHeadersObject = flattenHeadersObject_1.flattenHeadersObject;
|
|
488
368
|
});
|
|
489
369
|
|
|
490
|
-
var
|
|
370
|
+
var codes = {
|
|
491
371
|
"100": "Continue",
|
|
492
372
|
"101": "Switching Protocols",
|
|
493
373
|
"102": "Processing",
|
|
@@ -553,6 +433,11 @@ var statuses = {
|
|
|
553
433
|
"511": "Network Authentication Required"
|
|
554
434
|
};
|
|
555
435
|
|
|
436
|
+
var statuses = /*#__PURE__*/Object.freeze({
|
|
437
|
+
__proto__: null,
|
|
438
|
+
'default': codes
|
|
439
|
+
});
|
|
440
|
+
|
|
556
441
|
const status = (statusCode, statusText) => {
|
|
557
442
|
return (res) => {
|
|
558
443
|
res.status = statusCode;
|
|
@@ -700,49 +585,72 @@ const response = Object.assign(createResponseComposition(), {
|
|
|
700
585
|
* Returns a mocked response for a given request using following request handlers.
|
|
701
586
|
*/
|
|
702
587
|
const getResponse = (req, handlers) => __awaiter(void 0, void 0, void 0, function* () {
|
|
703
|
-
const
|
|
704
|
-
|
|
705
|
-
//
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
588
|
+
const relevantHandlers = handlers
|
|
589
|
+
.filter((requestHandler) => {
|
|
590
|
+
// Skip a handler if it has been already used for a one-time response.
|
|
591
|
+
return !requestHandler.shouldSkip;
|
|
592
|
+
})
|
|
593
|
+
.map((requestHandler) => {
|
|
709
594
|
// Parse the captured request to get additional information.
|
|
710
595
|
// Make the predicate function accept all the necessary information
|
|
711
596
|
// to decide on the interception.
|
|
712
597
|
const parsedRequest = requestHandler.parse
|
|
713
598
|
? requestHandler.parse(req)
|
|
714
599
|
: null;
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
600
|
+
return [requestHandler, parsedRequest];
|
|
601
|
+
})
|
|
602
|
+
.filter(([requestHandler, parsedRequest]) => {
|
|
603
|
+
return requestHandler.predicate(req, parsedRequest);
|
|
604
|
+
});
|
|
605
|
+
if (relevantHandlers.length == 0) {
|
|
606
|
+
// Handle a scenario when a request has no relevant request handlers.
|
|
607
|
+
// In that case it would be bypassed (performed as-is).
|
|
720
608
|
return {
|
|
721
609
|
handler: null,
|
|
722
610
|
response: null,
|
|
723
611
|
};
|
|
724
612
|
}
|
|
725
|
-
const {
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
613
|
+
const { requestHandler, parsedRequest, mockedResponse, publicRequest, } = yield relevantHandlers.reduce((asyncAcc, [requestHandler, parsedRequest]) => __awaiter(void 0, void 0, void 0, function* () {
|
|
614
|
+
// Now the reduce function is async so we need to await if response was found
|
|
615
|
+
const acc = yield asyncAcc;
|
|
616
|
+
// If a first not empty response was found we'll stop evaluating other requests
|
|
617
|
+
if (acc.requestHandler) {
|
|
618
|
+
return acc;
|
|
619
|
+
}
|
|
620
|
+
const { getPublicRequest, defineContext, resolver } = requestHandler;
|
|
621
|
+
const publicRequest = getPublicRequest
|
|
622
|
+
? getPublicRequest(req, parsedRequest)
|
|
623
|
+
: req;
|
|
624
|
+
const context = defineContext
|
|
625
|
+
? defineContext(publicRequest)
|
|
626
|
+
: defaultContext;
|
|
627
|
+
const mockedResponse = yield resolver(publicRequest, response, context);
|
|
628
|
+
if (!mockedResponse) {
|
|
629
|
+
return acc;
|
|
630
|
+
}
|
|
631
|
+
if (mockedResponse && mockedResponse.once) {
|
|
632
|
+
// When responded with a one-time response, match the relevant request handler
|
|
633
|
+
// as skipped, so it cannot affect the captured requests anymore.
|
|
634
|
+
requestHandler.shouldSkip = true;
|
|
635
|
+
}
|
|
734
636
|
return {
|
|
735
|
-
|
|
637
|
+
requestHandler,
|
|
638
|
+
parsedRequest,
|
|
639
|
+
mockedResponse,
|
|
640
|
+
publicRequest,
|
|
641
|
+
};
|
|
642
|
+
}), Promise.resolve({ mockedResponse: null }));
|
|
643
|
+
// Although reducing a list of relevant request handlers, it's possible
|
|
644
|
+
// that in the end there will be no handler associted with the request
|
|
645
|
+
// (i.e. if relevant handlers are fall-through).
|
|
646
|
+
if (!requestHandler) {
|
|
647
|
+
return {
|
|
648
|
+
handler: null,
|
|
736
649
|
response: null,
|
|
737
650
|
};
|
|
738
651
|
}
|
|
739
|
-
if (mockedResponse.once) {
|
|
740
|
-
// When responded with a one-time response, match the relevant request handler
|
|
741
|
-
// as skipped, so it cannot affect the captured requests anymore.
|
|
742
|
-
relevantHandler.shouldSkip = true;
|
|
743
|
-
}
|
|
744
652
|
return {
|
|
745
|
-
handler:
|
|
653
|
+
handler: requestHandler,
|
|
746
654
|
response: mockedResponse,
|
|
747
655
|
publicRequest,
|
|
748
656
|
parsedRequest,
|
|
@@ -865,7 +773,7 @@ const setupServer = (...requestHandlers) => {
|
|
|
865
773
|
// No need to take `credentials` into account, because in NodeJS requests are intercepted
|
|
866
774
|
// _after_ they happen. Request issuer should have already taken care of sending relevant cookies.
|
|
867
775
|
// Unlike browser, where interception is on the worker level, _before_ the request happens.
|
|
868
|
-
mockedRequest.cookies =
|
|
776
|
+
mockedRequest.cookies = parse_1(requestCookieString);
|
|
869
777
|
}
|
|
870
778
|
if (mockedRequest.headers.get('x-msw-bypass')) {
|
|
871
779
|
return;
|
package/node/index.js
CHANGED
|
@@ -43,7 +43,6 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
43
43
|
*/
|
|
44
44
|
|
|
45
45
|
var parse_1 = parse;
|
|
46
|
-
var serialize_1 = serialize;
|
|
47
46
|
|
|
48
47
|
/**
|
|
49
48
|
* Module variables.
|
|
@@ -51,19 +50,8 @@ var serialize_1 = serialize;
|
|
|
51
50
|
*/
|
|
52
51
|
|
|
53
52
|
var decode = decodeURIComponent;
|
|
54
|
-
var encode = encodeURIComponent;
|
|
55
53
|
var pairSplitRegExp = /; */;
|
|
56
54
|
|
|
57
|
-
/**
|
|
58
|
-
* RegExp to match field-content in RFC 7230 sec 3.2
|
|
59
|
-
*
|
|
60
|
-
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
|
|
61
|
-
* field-vchar = VCHAR / obs-text
|
|
62
|
-
* obs-text = %x80-FF
|
|
63
|
-
*/
|
|
64
|
-
|
|
65
|
-
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
|
66
|
-
|
|
67
55
|
/**
|
|
68
56
|
* Parse a cookie header.
|
|
69
57
|
*
|
|
@@ -112,109 +100,6 @@ function parse(str, options) {
|
|
|
112
100
|
return obj;
|
|
113
101
|
}
|
|
114
102
|
|
|
115
|
-
/**
|
|
116
|
-
* Serialize data into a cookie header.
|
|
117
|
-
*
|
|
118
|
-
* Serialize the a name value pair into a cookie string suitable for
|
|
119
|
-
* http headers. An optional options object specified cookie parameters.
|
|
120
|
-
*
|
|
121
|
-
* serialize('foo', 'bar', { httpOnly: true })
|
|
122
|
-
* => "foo=bar; httpOnly"
|
|
123
|
-
*
|
|
124
|
-
* @param {string} name
|
|
125
|
-
* @param {string} val
|
|
126
|
-
* @param {object} [options]
|
|
127
|
-
* @return {string}
|
|
128
|
-
* @public
|
|
129
|
-
*/
|
|
130
|
-
|
|
131
|
-
function serialize(name, val, options) {
|
|
132
|
-
var opt = options || {};
|
|
133
|
-
var enc = opt.encode || encode;
|
|
134
|
-
|
|
135
|
-
if (typeof enc !== 'function') {
|
|
136
|
-
throw new TypeError('option encode is invalid');
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (!fieldContentRegExp.test(name)) {
|
|
140
|
-
throw new TypeError('argument name is invalid');
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
var value = enc(val);
|
|
144
|
-
|
|
145
|
-
if (value && !fieldContentRegExp.test(value)) {
|
|
146
|
-
throw new TypeError('argument val is invalid');
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
var str = name + '=' + value;
|
|
150
|
-
|
|
151
|
-
if (null != opt.maxAge) {
|
|
152
|
-
var maxAge = opt.maxAge - 0;
|
|
153
|
-
|
|
154
|
-
if (isNaN(maxAge) || !isFinite(maxAge)) {
|
|
155
|
-
throw new TypeError('option maxAge is invalid')
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
str += '; Max-Age=' + Math.floor(maxAge);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if (opt.domain) {
|
|
162
|
-
if (!fieldContentRegExp.test(opt.domain)) {
|
|
163
|
-
throw new TypeError('option domain is invalid');
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
str += '; Domain=' + opt.domain;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
if (opt.path) {
|
|
170
|
-
if (!fieldContentRegExp.test(opt.path)) {
|
|
171
|
-
throw new TypeError('option path is invalid');
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
str += '; Path=' + opt.path;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
if (opt.expires) {
|
|
178
|
-
if (typeof opt.expires.toUTCString !== 'function') {
|
|
179
|
-
throw new TypeError('option expires is invalid');
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
str += '; Expires=' + opt.expires.toUTCString();
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (opt.httpOnly) {
|
|
186
|
-
str += '; HttpOnly';
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (opt.secure) {
|
|
190
|
-
str += '; Secure';
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (opt.sameSite) {
|
|
194
|
-
var sameSite = typeof opt.sameSite === 'string'
|
|
195
|
-
? opt.sameSite.toLowerCase() : opt.sameSite;
|
|
196
|
-
|
|
197
|
-
switch (sameSite) {
|
|
198
|
-
case true:
|
|
199
|
-
str += '; SameSite=Strict';
|
|
200
|
-
break;
|
|
201
|
-
case 'lax':
|
|
202
|
-
str += '; SameSite=Lax';
|
|
203
|
-
break;
|
|
204
|
-
case 'strict':
|
|
205
|
-
str += '; SameSite=Strict';
|
|
206
|
-
break;
|
|
207
|
-
case 'none':
|
|
208
|
-
str += '; SameSite=None';
|
|
209
|
-
break;
|
|
210
|
-
default:
|
|
211
|
-
throw new TypeError('option sameSite is invalid');
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
return str;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
103
|
/**
|
|
219
104
|
* Try decoding a string using a decoding function.
|
|
220
105
|
*
|
|
@@ -231,11 +116,6 @@ function tryDecode(str, decode) {
|
|
|
231
116
|
}
|
|
232
117
|
}
|
|
233
118
|
|
|
234
|
-
var cookie = {
|
|
235
|
-
parse: parse_1,
|
|
236
|
-
serialize: serialize_1
|
|
237
|
-
};
|
|
238
|
-
|
|
239
119
|
function createCommonjsModule(fn, basedir, module) {
|
|
240
120
|
return module = {
|
|
241
121
|
path: basedir,
|
|
@@ -488,7 +368,7 @@ exports.flattenHeadersList = flattenHeadersList_1.flattenHeadersList;
|
|
|
488
368
|
exports.flattenHeadersObject = flattenHeadersObject_1.flattenHeadersObject;
|
|
489
369
|
});
|
|
490
370
|
|
|
491
|
-
var
|
|
371
|
+
var codes = {
|
|
492
372
|
"100": "Continue",
|
|
493
373
|
"101": "Switching Protocols",
|
|
494
374
|
"102": "Processing",
|
|
@@ -554,6 +434,11 @@ var statuses = {
|
|
|
554
434
|
"511": "Network Authentication Required"
|
|
555
435
|
};
|
|
556
436
|
|
|
437
|
+
var statuses = /*#__PURE__*/Object.freeze({
|
|
438
|
+
__proto__: null,
|
|
439
|
+
'default': codes
|
|
440
|
+
});
|
|
441
|
+
|
|
557
442
|
const status = (statusCode, statusText) => {
|
|
558
443
|
return (res) => {
|
|
559
444
|
res.status = statusCode;
|
|
@@ -701,49 +586,72 @@ const response = Object.assign(createResponseComposition(), {
|
|
|
701
586
|
* Returns a mocked response for a given request using following request handlers.
|
|
702
587
|
*/
|
|
703
588
|
const getResponse = (req, handlers) => __awaiter(void 0, void 0, void 0, function* () {
|
|
704
|
-
const
|
|
705
|
-
|
|
706
|
-
//
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
589
|
+
const relevantHandlers = handlers
|
|
590
|
+
.filter((requestHandler) => {
|
|
591
|
+
// Skip a handler if it has been already used for a one-time response.
|
|
592
|
+
return !requestHandler.shouldSkip;
|
|
593
|
+
})
|
|
594
|
+
.map((requestHandler) => {
|
|
710
595
|
// Parse the captured request to get additional information.
|
|
711
596
|
// Make the predicate function accept all the necessary information
|
|
712
597
|
// to decide on the interception.
|
|
713
598
|
const parsedRequest = requestHandler.parse
|
|
714
599
|
? requestHandler.parse(req)
|
|
715
600
|
: null;
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
601
|
+
return [requestHandler, parsedRequest];
|
|
602
|
+
})
|
|
603
|
+
.filter(([requestHandler, parsedRequest]) => {
|
|
604
|
+
return requestHandler.predicate(req, parsedRequest);
|
|
605
|
+
});
|
|
606
|
+
if (relevantHandlers.length == 0) {
|
|
607
|
+
// Handle a scenario when a request has no relevant request handlers.
|
|
608
|
+
// In that case it would be bypassed (performed as-is).
|
|
721
609
|
return {
|
|
722
610
|
handler: null,
|
|
723
611
|
response: null,
|
|
724
612
|
};
|
|
725
613
|
}
|
|
726
|
-
const {
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
614
|
+
const { requestHandler, parsedRequest, mockedResponse, publicRequest, } = yield relevantHandlers.reduce((asyncAcc, [requestHandler, parsedRequest]) => __awaiter(void 0, void 0, void 0, function* () {
|
|
615
|
+
// Now the reduce function is async so we need to await if response was found
|
|
616
|
+
const acc = yield asyncAcc;
|
|
617
|
+
// If a first not empty response was found we'll stop evaluating other requests
|
|
618
|
+
if (acc.requestHandler) {
|
|
619
|
+
return acc;
|
|
620
|
+
}
|
|
621
|
+
const { getPublicRequest, defineContext, resolver } = requestHandler;
|
|
622
|
+
const publicRequest = getPublicRequest
|
|
623
|
+
? getPublicRequest(req, parsedRequest)
|
|
624
|
+
: req;
|
|
625
|
+
const context = defineContext
|
|
626
|
+
? defineContext(publicRequest)
|
|
627
|
+
: defaultContext;
|
|
628
|
+
const mockedResponse = yield resolver(publicRequest, response, context);
|
|
629
|
+
if (!mockedResponse) {
|
|
630
|
+
return acc;
|
|
631
|
+
}
|
|
632
|
+
if (mockedResponse && mockedResponse.once) {
|
|
633
|
+
// When responded with a one-time response, match the relevant request handler
|
|
634
|
+
// as skipped, so it cannot affect the captured requests anymore.
|
|
635
|
+
requestHandler.shouldSkip = true;
|
|
636
|
+
}
|
|
735
637
|
return {
|
|
736
|
-
|
|
638
|
+
requestHandler,
|
|
639
|
+
parsedRequest,
|
|
640
|
+
mockedResponse,
|
|
641
|
+
publicRequest,
|
|
642
|
+
};
|
|
643
|
+
}), Promise.resolve({ mockedResponse: null }));
|
|
644
|
+
// Although reducing a list of relevant request handlers, it's possible
|
|
645
|
+
// that in the end there will be no handler associted with the request
|
|
646
|
+
// (i.e. if relevant handlers are fall-through).
|
|
647
|
+
if (!requestHandler) {
|
|
648
|
+
return {
|
|
649
|
+
handler: null,
|
|
737
650
|
response: null,
|
|
738
651
|
};
|
|
739
652
|
}
|
|
740
|
-
if (mockedResponse.once) {
|
|
741
|
-
// When responded with a one-time response, match the relevant request handler
|
|
742
|
-
// as skipped, so it cannot affect the captured requests anymore.
|
|
743
|
-
relevantHandler.shouldSkip = true;
|
|
744
|
-
}
|
|
745
653
|
return {
|
|
746
|
-
handler:
|
|
654
|
+
handler: requestHandler,
|
|
747
655
|
response: mockedResponse,
|
|
748
656
|
publicRequest,
|
|
749
657
|
parsedRequest,
|
|
@@ -866,7 +774,7 @@ const setupServer = (...requestHandlers) => {
|
|
|
866
774
|
// No need to take `credentials` into account, because in NodeJS requests are intercepted
|
|
867
775
|
// _after_ they happen. Request issuer should have already taken care of sending relevant cookies.
|
|
868
776
|
// Unlike browser, where interception is on the worker level, _before_ the request happens.
|
|
869
|
-
mockedRequest.cookies =
|
|
777
|
+
mockedRequest.cookies = parse_1(requestCookieString);
|
|
870
778
|
}
|
|
871
779
|
if (mockedRequest.headers.get('x-msw-bypass')) {
|
|
872
780
|
return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "msw",
|
|
3
|
-
"version": "0.20.
|
|
3
|
+
"version": "0.20.5",
|
|
4
4
|
"description": "Client-side API mocking using Service Workers.",
|
|
5
5
|
"main": "lib/umd/index.js",
|
|
6
6
|
"module": "lib/esm/index.js",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"headers-utils": "^1.2.0",
|
|
70
70
|
"node-fetch": "^2.6.0",
|
|
71
71
|
"node-match-path": "^0.4.4",
|
|
72
|
-
"node-request-interceptor": "^0.3.
|
|
72
|
+
"node-request-interceptor": "^0.3.5",
|
|
73
73
|
"statuses": "^2.0.0",
|
|
74
74
|
"yargs": "^15.4.1"
|
|
75
75
|
},
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { l as lib } from './fetch-deps.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Parses a given string into a JSON.
|
|
5
|
-
* Does not throw an exception on an invalid JSON string.
|
|
6
|
-
*/
|
|
7
|
-
function jsonParse(str) {
|
|
8
|
-
try {
|
|
9
|
-
return JSON.parse(str);
|
|
10
|
-
}
|
|
11
|
-
catch (error) {
|
|
12
|
-
return undefined;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Returns a parsed JSON from a given valid body string,
|
|
18
|
-
* otherwise returns a given body string as-is.
|
|
19
|
-
*/
|
|
20
|
-
function getJsonBody(body) {
|
|
21
|
-
return jsonParse(body) || body;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Formats a mocked request for introspection in browser's console.
|
|
26
|
-
*/
|
|
27
|
-
function prepareRequest(req) {
|
|
28
|
-
return Object.assign(Object.assign({}, req), { headers: req.headers.getAllHeaders() });
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Formats a mocked response for introspection in browser's console.
|
|
33
|
-
*/
|
|
34
|
-
function prepareResponse(res) {
|
|
35
|
-
var _a;
|
|
36
|
-
const resHeaders = lib.listToHeaders(res.headers);
|
|
37
|
-
return Object.assign(Object.assign({}, res), {
|
|
38
|
-
// Parse a response JSON body for preview in the logs
|
|
39
|
-
body: ((_a = resHeaders.get('content-type')) === null || _a === void 0 ? void 0 : _a.includes('json')) ? getJsonBody(res.body)
|
|
40
|
-
: res.body });
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function getTimestamp() {
|
|
44
|
-
const now = new Date();
|
|
45
|
-
return [now.getHours(), now.getMinutes(), now.getSeconds()]
|
|
46
|
-
.map(String)
|
|
47
|
-
.map((chunk) => chunk.slice(0, 2))
|
|
48
|
-
.map((chunk) => chunk.padStart(2, '0'))
|
|
49
|
-
.join(':');
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Returns a HEX color for a given response status code number.
|
|
54
|
-
*/
|
|
55
|
-
function getStatusCodeColor(status) {
|
|
56
|
-
if (status < 300) {
|
|
57
|
-
return '#69AB32';
|
|
58
|
-
}
|
|
59
|
-
if (status < 400) {
|
|
60
|
-
return '#F0BB4B';
|
|
61
|
-
}
|
|
62
|
-
return '#E95F5D';
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export { prepareResponse as a, getTimestamp as b, getStatusCodeColor as c, getJsonBody as g, jsonParse as j, prepareRequest as p };
|