From c1df10299c865b648aea430d8e76b3a29e5bb9ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 16 Jan 2017 16:32:37 +0100 Subject: [PATCH 1/2] Treat any non-Request arg to `new Request()` as string url This aims to match the behavior that the IDL of the spec defines: https://github.com/whatwg/fetch/issues/452#issuecomment-272439900 --- fetch.js | 6 +++--- test/test.js | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/fetch.js b/fetch.js index 44f05408..dfb74be4 100644 --- a/fetch.js +++ b/fetch.js @@ -303,9 +303,7 @@ options = options || {} var body = options.body - if (typeof input === 'string') { - this.url = input - } else { + if (input instanceof Request) { if (input.bodyUsed) { throw new TypeError('Already read') } @@ -320,6 +318,8 @@ body = input._bodyInit input.bodyUsed = true } + } else { + this.url = String(input) } this.credentials = options.credentials || this.credentials || 'omit' diff --git a/test/test.js b/test/test.js index 5b6869d5..ea4caba3 100644 --- a/test/test.js +++ b/test/test.js @@ -1,5 +1,6 @@ var support = { searchParams: 'URLSearchParams' in self, + url: 'URL' in self, blob: 'FileReader' in self && 'Blob' in self && (function() { try { new Blob() @@ -283,11 +284,24 @@ suite('Headers', function() { // https://fetch.spec.whatwg.org/#request-class suite('Request', function() { - test('construct with url', function() { + test('construct with string url', function() { var request = new Request('https://fetch.spec.whatwg.org/') assert.equal(request.url, 'https://fetch.spec.whatwg.org/') }) + featureDependent(test, support.url, 'construct with URL instance', function() { + var url = new URL('https://fetch.spec.whatwg.org/') + url.pathname = 'cors' + var request = new Request(url) + assert.equal(request.url, 'https://fetch.spec.whatwg.org/cors') + }) + + test('construct with non-Request object', function() { + var url = { toString: function() { return 'https://fetch.spec.whatwg.org/' } } + var request = new Request(url) + assert.equal(request.url, 'https://fetch.spec.whatwg.org/') + }) + test('construct with Request', function() { var request1 = new Request('https://fetch.spec.whatwg.org/', { method: 'post', From b285e61fbc4dc21d4b5f7a498046bdff585abf1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 16 Jan 2017 18:07:14 +0100 Subject: [PATCH 2/2] Detect broken URL support in PhantomJS and skip test --- test/test.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index ea4caba3..280f0feb 100644 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,12 @@ var support = { searchParams: 'URLSearchParams' in self, - url: 'URL' in self, + url: (function(url) { + try { + return new URL(url).toString() === url + } catch(e) { + return false + } + })('http://example.com/'), blob: 'FileReader' in self && 'Blob' in self && (function() { try { new Blob()