Skip to content

Commit

Permalink
Merge pull request #179 from github/request-to-request
Browse files Browse the repository at this point in the history
Allow passing a Request instance to Request constructor
  • Loading branch information
mislav committed Jul 31, 2015
2 parents aa21bda + 52561a0 commit d16df66
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 10 deletions.
37 changes: 28 additions & 9 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,40 @@
return (methods.indexOf(upcased) > -1) ? upcased : method
}

function Request(url, options) {
function Request(input, options) {
options = options || {}
this.url = url
var body = options.body
if (Request.prototype.isPrototypeOf(input)) {
if (input.bodyUsed) {
throw new TypeError('Already read')
}
this.url = input.url
this.credentials = input.credentials
if (!options.headers) {
this.headers = new Headers(input.headers)
}
this.method = input.method
this.mode = input.mode
if (!body) {
body = input._bodyInit
input.bodyUsed = true
}
} else {
this.url = input
}

this.credentials = options.credentials || 'omit'
this.headers = new Headers(options.headers)
this.method = normalizeMethod(options.method || 'GET')
this.mode = options.mode || null
this.credentials = options.credentials || this.credentials || 'omit'
if (options.headers || !this.headers) {
this.headers = new Headers(options.headers)
}
this.method = normalizeMethod(options.method || this.method || 'GET')
this.mode = options.mode || this.mode || null
this.referrer = null

if ((this.method === 'GET' || this.method === 'HEAD') && options.body) {
if ((this.method === 'GET' || this.method === 'HEAD') && body) {
throw new TypeError('Body not allowed for GET or HEAD requests')
}
this._initBody(options.body)
this._initBody(body)
}

function decode(body) {
Expand Down Expand Up @@ -265,7 +285,6 @@
self.Response = Response;

self.fetch = function(input, init) {
// TODO: Request constructor should accept input, init
var request
if (Request.prototype.isPrototypeOf(input) && !init) {
request = input
Expand Down
82 changes: 81 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,82 @@ suite('Request', function() {
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',
body: 'I work out',
headers: {
accept: 'application/json',
'Content-Type': 'text/plain'
}
})
var request2 = new Request(request1)

return request2.text().then(function(body2) {
assert.equal(body2, 'I work out')
assert.equal(request2.method, 'POST')
assert.equal(request2.url, 'https://fetch.spec.whatwg.org/')
assert.equal(request2.headers.get('accept'), 'application/json')
assert.equal(request2.headers.get('content-type'), 'text/plain')

return request1.text().then(function() {
assert(false, 'original request body should have been consumed')
}, function(error) {
assert(error instanceof TypeError, 'expected TypeError for already read body')
})
})
})

test('construct with Request and override headers', function() {
var request1 = new Request('https://fetch.spec.whatwg.org/', {
method: 'post',
body: 'I work out',
headers: {
accept: 'application/json',
'Content-Type': 'text/plain'
}
})
var request2 = new Request(request1, {
headers: { 'x-test': '42' }
})

assert.equal(request2.headers.get('accept'), undefined)
assert.equal(request2.headers.get('content-type'), undefined)
assert.equal(request2.headers.get('x-test'), '42')
})

test('construct with Request and override body', function() {
var request1 = new Request('https://fetch.spec.whatwg.org/', {
method: 'post',
body: 'I work out',
headers: {
'Content-Type': 'text/plain'
}
})
var request2 = new Request(request1, {
body: '{"wiggles": 5}',
headers: { 'Content-Type': 'application/json' }
})

return request2.json().then(function(data) {
assert.equal(data.wiggles, 5)
assert.equal(request2.headers.get('content-type'), 'application/json')
})
})

;(/Chrome\//.test(navigator.userAgent) ? test.skip : test)('construct with used Request body', function() {
var request1 = new Request('https://fetch.spec.whatwg.org/', {
method: 'post',
body: 'I work out'
})

return request1.text().then(function() {
assert.throws(function() {
new Request(request1)
}, TypeError)
})
})

// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
suite('BodyInit extract', function() {
;(Request.prototype.blob ? suite : suite.skip)('type Blob', function() {
Expand Down Expand Up @@ -431,7 +507,11 @@ suite('Body mixin', function() {
response.formData()
return response.formData()
}).catch(function(error) {
assert(error instanceof TypeError, 'Promise rejected after body consumed')
if (error instanceof chai.AssertionError) {
throw error
} else {
assert(error instanceof TypeError, 'Promise rejected after body consumed')
}
})
})

Expand Down

0 comments on commit d16df66

Please sign in to comment.