提交 d16df665 编写于 作者: M Mislav Marohnić

Merge pull request #179 from github/request-to-request

Allow passing a Request instance to Request constructor
......@@ -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) {
......@@ -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
......
......@@ -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() {
......@@ -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')
}
})
})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册