From 17befb7bd140effd385fa5b7efb92b90355d9f38 Mon Sep 17 00:00:00 2001 From: Baptiste Marchand Date: Mon, 28 Feb 2022 15:45:09 +0100 Subject: [PATCH 1/2] Fix encoding with json `responseType` --- source/core/response.ts | 2 +- test/encoding.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/encoding.ts diff --git a/source/core/response.ts b/source/core/response.ts index a6e66b5fd..413dcad4a 100644 --- a/source/core/response.ts +++ b/source/core/response.ts @@ -138,7 +138,7 @@ export const parseBody = (response: Response, responseType: ResponseType, parseJ } if (responseType === 'json') { - return rawBody.length === 0 ? '' : parseJson(rawBody.toString()); + return rawBody.length === 0 ? '' : parseJson(rawBody.toString(encoding)); } if (responseType === 'buffer') { diff --git a/test/encoding.ts b/test/encoding.ts new file mode 100644 index 000000000..8e6b1d68e --- /dev/null +++ b/test/encoding.ts @@ -0,0 +1,19 @@ +import {Buffer} from 'buffer'; +import test from 'ava'; +import withServer from './helpers/with-server.js'; + +test('encoding works with json', withServer, async (t, server, got) => { + const json = {data: 'é'}; + + server.get('/', (_request, response) => { + response.set('Content-Type', 'application-json'); + response.send(Buffer.from(JSON.stringify(json), 'latin1')); + }); + + const response = await got('', { + encoding: 'latin1', + responseType: 'json', + }); + + t.deepEqual(response.body, json); +}); From f39017d843e506b6cb747d92b8503aa8efff2d68 Mon Sep 17 00:00:00 2001 From: Baptiste Marchand Date: Mon, 28 Feb 2022 19:14:33 +0100 Subject: [PATCH 2/2] Update retry.ts --- test/retry.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/retry.ts b/test/retry.ts index 5a82c551b..b802c1c70 100644 --- a/test/retry.ts +++ b/test/retry.ts @@ -171,7 +171,9 @@ test('custom error codes', async t => { request: () => { const emitter = new EventEmitter() as http.ClientRequest; emitter.abort = () => {}; + // @ts-expect-error Imitating a stream emitter.end = () => {}; + // @ts-expect-error Imitating a stream emitter.destroy = () => {}; // @ts-expect-error Imitating a stream emitter.writable = true;