Skip to content

Commit

Permalink
Fix content-length header not being set when using custom `content-…
Browse files Browse the repository at this point in the history
…type`

Fixes #996
  • Loading branch information
szmarczak committed Dec 15, 2019
1 parent 7bf92f4 commit 3149340
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/normalize-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export const normalizeRequestArguments = async (options: NormalizedOptions): Pro
// Content-Length header field when the request message does not contain
// a payload body and the method semantics do not anticipate such a
// body.
if (noContentType && is.undefined(headers['transfer-encoding'])) {
if (is.undefined(headers['content-length']) && is.undefined(headers['transfer-encoding'])) {
if (
(options.method === 'POST' || options.method === 'PUT' || options.method === 'PATCH') &&
!is.undefined(uploadBodySize)
Expand Down
29 changes: 29 additions & 0 deletions test/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ test('`content-length` header with string body', withServer, async (t, server, g
t.is(headers['content-length'], '3');
});

test('`content-length` header with json body', withServer, async (t, server, got) => {
server.post('/', echoHeaders);

const {body} = await got.post({json: {foo: 'bar'}});
const headers = JSON.parse(body);
t.is(headers['content-length'], '13');
});

test('`content-length` header with form body', withServer, async (t, server, got) => {
server.post('/', echoHeaders);

const {body} = await got.post({form: {foo: 'bar'}});
const headers = JSON.parse(body);
t.is(headers['content-length'], '7');
});

test('`content-length` header with Buffer body', withServer, async (t, server, got) => {
server.post('/', echoHeaders);

Expand Down Expand Up @@ -143,6 +159,19 @@ test('`content-length` header is not overriden', withServer, async (t, server, g
t.is(headers['content-length'], '10');
});

test('`content-length` header is present when using custom content-type', withServer, async (t, server, got) => {
server.post('/', echoHeaders);

const {body} = await got.post({
json: {foo: 'bar'},
headers: {
'content-type': 'custom'
}
});
const headers = JSON.parse(body);
t.is(headers['content-length'], '13');
});

test('`content-length` header disabled for chunked transfer-encoding', withServer, async (t, server, got) => {
server.post('/', echoHeaders);

Expand Down

0 comments on commit 3149340

Please sign in to comment.