diff --git a/src/targets/http/http1.1/client.test.ts b/src/targets/http/http1.1/client.test.ts new file mode 100644 index 000000000..6d5d62d9e --- /dev/null +++ b/src/targets/http/http1.1/client.test.ts @@ -0,0 +1,24 @@ +import { HTTPSnippet, Request } from '../../../httpsnippet'; + +const unicodeRequest = { + method: 'POST', + url: 'http://mockbin.com/har', + headers: [ + { + name: 'content-type', + value: 'text/plain', + }, + ], + postData: { + mimeType: 'text/plain', + text: 'äöü', + }, +} as Request; + +describe('http:http1.1', () => { + it('should set Content-Length to the byte length of a non-ascii body', () => { + const result = String(new HTTPSnippet(unicodeRequest).convert('http', 'http1.1')); + + expect(result).toContain('Content-Length: 6'); + }); +}); diff --git a/src/targets/http/http1.1/client.ts b/src/targets/http/http1.1/client.ts index 0b28ef079..3ce8bc955 100644 --- a/src/targets/http/http1.1/client.ts +++ b/src/targets/http/http1.1/client.ts @@ -69,7 +69,7 @@ export const http11: Client = { // RFC 7230 Section 3.3.3. Message Body Length // Automatically set Content-Length header if option is on, postData is present and no header already exists. if (opts.autoContentLength && postData.text && !headerKeys.includes('content-length')) { - const length = Buffer.byteLength(postData.text, 'ascii').toString(); + const length = Buffer.byteLength(postData.text, 'utf8').toString(); push(`Content-Length: ${length}`); }