Browse Source

fix accept and content-length headers overriding

http2
Vsevolod Strukchinsky 9 years ago
parent
commit
e45087ab04
  1. 6
      index.js
  2. 11
      test/headers.js

6
index.js

@ -231,8 +231,8 @@ function normalizeArguments(url, opts) {
delete opts.query;
}
if (opts.json) {
opts.headers.accept = opts.headers.accept || 'application/json';
if (opts.json && opts.headers.accept === undefined) {
opts.headers.accept = 'application/json';
}
var body = opts.body;
@ -249,7 +249,7 @@ function normalizeArguments(url, opts) {
body = opts.body = querystring.stringify(body);
}
if (!opts.headers['content-length'] && !opts.headers['transfer-encoding'] && !isStream.readable(body)) {
if (opts.headers['content-length'] === undefined && opts.headers['transfer-encoding'] === undefined && !isStream.readable(body)) {
var length = typeof body === 'string' ? Buffer.byteLength(body) : body.length;
opts.headers['content-length'] = length;
}

11
test/headers.js

@ -8,6 +8,7 @@ test.before('setup', async t => {
s = await createServer();
s.on('/', (req, res) => {
req.resume();
res.end(JSON.stringify(req.headers));
});
@ -25,8 +26,11 @@ test('accept-encoding', async t => {
});
test('accept header with json option', async t => {
const headers = (await got(s.url, {json: true})).body;
let headers = (await got(s.url, {json: true})).body;
t.is(headers.accept, 'application/json');
headers = (await got(s.url, {headers: {'accept': ''}, json: true})).body;
t.is(headers.accept, '');
});
test('host', async t => {
@ -39,6 +43,11 @@ test('transform names to lowercase', async t => {
t.is(headers['user-agent'], 'test');
});
test('zero content-length', async t => {
const headers = (await got(s.url, {headers: {'content-length': 0}, body: 'sup', json: true})).body;
t.is(headers['content-length'], '0');
});
test.after('cleanup', async t => {
await s.close();
});

Loading…
Cancel
Save