Skip to content

Commit

Permalink
Treat any non-Request arg to new Request() as string url
Browse files Browse the repository at this point in the history
This aims to match the behavior that the IDL of the spec defines:
whatwg/fetch#452 (comment)
  • Loading branch information
mislav committed Jan 16, 2017
1 parent 9240ef4 commit 0e849ed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 3 additions & 3 deletions fetch.js
Expand Up @@ -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')
}
Expand All @@ -320,6 +318,8 @@
body = input._bodyInit
input.bodyUsed = true
}
} else {
this.url = String(input)
}

this.credentials = options.credentials || this.credentials || 'omit'
Expand Down
16 changes: 15 additions & 1 deletion 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()
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 0e849ed

Please sign in to comment.