Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat any non-Request arg to new Request() as string url #465

Merged
merged 2 commits into from Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
22 changes: 21 additions & 1 deletion test/test.js
@@ -1,5 +1,12 @@
var support = {
searchParams: 'URLSearchParams' 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()
Expand Down Expand Up @@ -283,11 +290,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