Throw when a GET/HEAD Request is created with body
As mandated by the spec
This commit is contained in:
parent
70f61e0c7d
commit
3d676235a8
|
@ -38,17 +38,24 @@ export default class Request extends Body {
|
|||
parsedURL = parse_url(input.url);
|
||||
}
|
||||
|
||||
let method = init.method || input.method || 'GET';
|
||||
|
||||
if ((init.body != null || input instanceof Request && input.body != null) &&
|
||||
(method === 'GET' || method === 'HEAD')) {
|
||||
throw new TypeError('Request with GET/HEAD method cannot have body');
|
||||
}
|
||||
|
||||
super(init.body || clone(input), {
|
||||
timeout: init.timeout || input.timeout || 0,
|
||||
size: init.size || input.size || 0
|
||||
});
|
||||
|
||||
// fetch spec options
|
||||
this.method = init.method || input.method || 'GET';
|
||||
this.method = method;
|
||||
this.redirect = init.redirect || input.redirect || 'follow';
|
||||
this.headers = new Headers(init.headers || input.headers || {});
|
||||
|
||||
if (init.body) {
|
||||
if (init.body != null) {
|
||||
const contentType = extractContentType(this);
|
||||
if (contentType && !this.headers.has('Content-Type')) {
|
||||
this.headers.append('Content-Type', contentType);
|
||||
|
|
15
test/test.js
15
test/test.js
|
@ -1444,6 +1444,17 @@ describe(`node-fetch with FOLLOW_SPEC = ${defaultFollowSpec}`, () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('should throw error with GET/HEAD requests with body', function() {
|
||||
expect(() => new Request('.', { body: '' }))
|
||||
.to.throw(TypeError);
|
||||
expect(() => new Request('.', { body: 'a' }))
|
||||
.to.throw(TypeError);
|
||||
expect(() => new Request('.', { body: '', method: 'HEAD' }))
|
||||
.to.throw(TypeError);
|
||||
expect(() => new Request('.', { body: 'a', method: 'HEAD' }))
|
||||
.to.throw(TypeError);
|
||||
});
|
||||
|
||||
it('should support empty options in Response constructor', function() {
|
||||
let body = resumer().queue('a=1').end();
|
||||
body = body.pipe(new stream.PassThrough());
|
||||
|
@ -1557,6 +1568,7 @@ describe(`node-fetch with FOLLOW_SPEC = ${defaultFollowSpec}`, () => {
|
|||
it('should support arrayBuffer() method in Request constructor', function() {
|
||||
url = base;
|
||||
var req = new Request(url, {
|
||||
method: 'POST',
|
||||
body: 'a=1'
|
||||
});
|
||||
expect(req.url).to.equal(url);
|
||||
|
@ -1570,6 +1582,7 @@ describe(`node-fetch with FOLLOW_SPEC = ${defaultFollowSpec}`, () => {
|
|||
it('should support text() method in Request constructor', function() {
|
||||
url = base;
|
||||
const req = new Request(url, {
|
||||
method: 'POST',
|
||||
body: 'a=1'
|
||||
});
|
||||
expect(req.url).to.equal(url);
|
||||
|
@ -1581,6 +1594,7 @@ describe(`node-fetch with FOLLOW_SPEC = ${defaultFollowSpec}`, () => {
|
|||
it('should support json() method in Request constructor', function() {
|
||||
url = base;
|
||||
const req = new Request(url, {
|
||||
method: 'POST',
|
||||
body: '{"a":1}'
|
||||
});
|
||||
expect(req.url).to.equal(url);
|
||||
|
@ -1592,6 +1606,7 @@ describe(`node-fetch with FOLLOW_SPEC = ${defaultFollowSpec}`, () => {
|
|||
it('should support buffer() method in Request constructor', function() {
|
||||
url = base;
|
||||
const req = new Request(url, {
|
||||
method: 'POST',
|
||||
body: 'a=1'
|
||||
});
|
||||
expect(req.url).to.equal(url);
|
||||
|
|
Loading…
Reference in New Issue