Skip to content

Commit 0e50696

Browse files
committed
fix: Support URL objects
1 parent 6ecbcd1 commit 0e50696

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

src/dummy/fetch.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ import { isArrayBuffer, str2arrayBuffer, tryToParseJson } from '../common/utils'
44
import { HTTPStatusCodes } from '../config';
55
import { AnyObject, FetchRequest } from '../types';
66

7-
export default function dummyFetch(input: string | FetchRequest, init: AnyObject) {
7+
export default function dummyFetch(
8+
input: string | FetchRequest | URL,
9+
init: AnyObject
10+
) {
811
let url: string | FetchRequest;
912
let params: FetchRequest | AnyObject;
10-
// https://developer.mozilla.org/en-US/docs/Web/API/Request
11-
// Note: the first argument of fetch maybe a Request object.
12-
if (typeof input === 'object') {
13+
// https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch
14+
// Note: the first argument of fetch maybe a Request or URL object.
15+
if (input instanceof URL) {
16+
url = input.toString();
17+
params = init || {};
18+
} else if (typeof input === 'object') {
1319
url = input.url;
1420
params = input as FetchRequest;
1521
} else {
@@ -18,11 +24,11 @@ export default function dummyFetch(input: string | FetchRequest, init: AnyObject
1824
}
1925

2026
return simpleRequest({
21-
url, method:
22-
params.method as string,
27+
url,
28+
method: params.method as string,
2329
headers: params.headers as Record<string, string>,
24-
body: params.body
25-
}).then((res: {body: string, response: IncomingMessage}) => {
30+
body: params.body,
31+
}).then((res: { body: string; response: IncomingMessage }) => {
2632
return getResponse(url as string, res.body, res.response);
2733
});
2834
}

src/interceptor/fetch.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,34 @@ export default class FetchInterceptor extends Base{
3333
private intercept() {
3434
// eslint-disable-next-line @typescript-eslint/no-this-alias
3535
const me = this;
36-
this.global.fetch = function(input: string | FetchRequest, init: AnyObject) {
36+
this.global.fetch = function (
37+
input: string | FetchRequest | URL,
38+
init: AnyObject
39+
) {
3740
let url: string;
3841
let params: FetchRequest | AnyObject;
39-
// https://developer.mozilla.org/en-US/docs/Web/API/Request
40-
// Note: the first argument of fetch maybe a Request object.
41-
if (typeof input === 'object') {
42+
// https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch
43+
// Note: the first argument of fetch maybe a Request or URL object.
44+
if (input instanceof URL) {
45+
url = input.toString();
46+
params = init || {};
47+
} else if (typeof input === 'object') {
4248
url = input.url;
4349
params = input;
4450
} else {
4551
url = input;
4652
params = init || {};
4753
}
48-
const method = (params && params.method ? params.method : 'GET') as unknown as HttpVerb;
54+
const method = (params && params.method
55+
? params.method
56+
: 'GET') as unknown as HttpVerb;
4957
const requestUrl = me.getFullRequestUrl(url, method);
5058

5159
return new Promise((resolve, reject) => {
52-
const mockItem:MockItem | null = me.matchMockRequest(requestUrl, method);
60+
const mockItem: MockItem | null = me.matchMockRequest(
61+
requestUrl,
62+
method
63+
);
5364

5465
if (!mockItem) {
5566
me.fetch(input, init).then(resolve).catch(reject);
@@ -58,7 +69,11 @@ export default class FetchInterceptor extends Base{
5869

5970
me.setTimeoutForSingal(params as FetchRequest, reject);
6071

61-
const requestInfo = me.getRequestInfo({ ...params, url: requestUrl, method: method as HttpVerb });
72+
const requestInfo = me.getRequestInfo({
73+
...params,
74+
url: requestUrl,
75+
method: method as HttpVerb,
76+
});
6277
requestInfo.doOriginalCall = async (): Promise<OriginalResponse> => {
6378
const res = await me.getOriginalResponse(requestUrl, params);
6479
requestInfo.doOriginalCall = undefined;
@@ -69,13 +84,20 @@ export default class FetchInterceptor extends Base{
6984
if (remoteInfo) {
7085
params.method = remoteInfo.method || method;
7186
me.setRequestHeadersForRemoteRequest(mockItem, params as AnyObject);
72-
me.fetch(remoteInfo.url, params).then((fetchResponse: FetchResponse) => {
73-
me.sendRemoteResult(fetchResponse, mockItem, requestInfo, resolve);
74-
}).catch(reject);
87+
me.fetch(remoteInfo.url, params)
88+
.then((fetchResponse: FetchResponse) => {
89+
me.sendRemoteResult(
90+
fetchResponse,
91+
mockItem,
92+
requestInfo,
93+
resolve
94+
);
95+
})
96+
.catch(reject);
7597
return;
7698
}
7799

78-
me.doMockRequest(mockItem, requestInfo, resolve).then(isBypassed => {
100+
me.doMockRequest(mockItem, requestInfo, resolve).then((isBypassed) => {
79101
if (isBypassed) {
80102
me.fetch(requestUrl, params).then(resolve).catch(reject);
81103
}

test/browser-fetch.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,16 @@ describe('mock fetch requests for browser envrioment', () => {
245245
console.log('res:', res);
246246
expect(res).toBe('an error should be caught');
247247
});
248+
249+
it('should support URL objects being passed into fetch', async () => {
250+
mocker.mock({
251+
url: 'http://example.com/some/api',
252+
method: 'post',
253+
body: () => 'some mock response',
254+
});
255+
const res = await fetch(new URL('http://example.com/some/other/api'), {
256+
method: 'post',
257+
});
258+
expect(res.status).toBe(404);
259+
});
248260
});

0 commit comments

Comments
 (0)