Skip to content

Commit

Permalink
Merge pull request #465 from github/url-to-string
Browse files Browse the repository at this point in the history
Treat any non-Request arg to `new Request()` as string url
  • Loading branch information
mislav authored Jan 19, 2017
2 parents 9240ef4 + b285e61 commit c64c8a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 3 additions & 3 deletions fetch.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c64c8a0

Please sign in to comment.