Skip to content

Commit cf0d755

Browse files
authored
fix: Support URL objects (#139)
1 parent 626a84c commit cf0d755

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

examples/jest-node-fetch/__mocks__/node-fetch/index.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,7 @@ global.Headers = Headers;
1111

1212
// Use global fetch as a mock. It will be
1313
// the one overwritten by polly
14-
const mock = (url, options) => {
15-
return global.fetch(
16-
// Polly doesn't support URL objects with fetch
17-
// adapter, we will work around it by stringifying
18-
// the passed URL
19-
url.toString(),
20-
options
21-
);
22-
};
14+
const mock = (url, options) => global.fetch(url, options);
2315

2416
mock.Request = Request;
2517
mock.Response = Response;

packages/@pollyjs/adapter-fetch/tests/browser/integration/adapter-test.js

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import adapterBrowserTests from '@pollyjs-tests/integration/adapter-browser-test
55
import RESTPersister from '@pollyjs/persister-rest';
66

77
import FetchAdapter from '../../../src';
8+
import commonTests from '../../common-tests';
89

910
describe('Integration | Fetch Adapter | Browser', function() {
1011
setupPolly.beforeEach({
@@ -21,4 +22,5 @@ describe('Integration | Fetch Adapter | Browser', function() {
2122

2223
adapterTests();
2324
adapterBrowserTests();
25+
commonTests();
2426
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { URL } from '@pollyjs/utils';
2+
3+
export default function commonTests() {
4+
it('should support URL instances', async function() {
5+
const { server } = this.polly;
6+
7+
server.any(this.recordUrl()).intercept((_, res) => res.sendStatus(200));
8+
9+
const res = await this.fetch(new URL(this.recordUrl()));
10+
11+
expect(res.status).to.equal(200);
12+
});
13+
}

packages/@pollyjs/adapter-fetch/tests/node/integration/adapter-test.js

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import FSPersister from '@pollyjs/persister-fs';
66
import { setupMocha as setupPolly } from '@pollyjs/core';
77

88
import FetchAdapter from '../../../src';
9+
import commonTests from '../../common-tests';
910

1011
describe('Integration | Fetch Adapter | Node', function() {
1112
setupPolly.beforeEach({
@@ -21,4 +22,5 @@ describe('Integration | Fetch Adapter | Node', function() {
2122
setupPolly.afterEach();
2223

2324
adapterTests();
25+
commonTests();
2426
});

packages/@pollyjs/core/src/-private/request.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ export default class PollyRequest extends HTTPBase {
2727
constructor(polly, request) {
2828
super();
2929

30-
assert('Url is required.', typeof request.url === 'string');
31-
assert('Method is required.', typeof request.method === 'string');
30+
assert('Url is required.', request.url);
31+
assert(
32+
'Method is required.',
33+
request.method && typeof request.method === 'string'
34+
);
3235

3336
this.didRespond = false;
3437
this.url = request.url;
@@ -68,7 +71,9 @@ export default class PollyRequest extends HTTPBase {
6871
}
6972

7073
set url(value) {
71-
this[PARSED_URL] = parseUrl(value, true);
74+
// Make sure to coerce the value into a string as the passed value could be
75+
// a WHATWG's URL object.
76+
this[PARSED_URL] = parseUrl(`${value}`, true);
7277
}
7378

7479
get absoluteUrl() {

0 commit comments

Comments
 (0)