Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix encoding with json responseType #1996

Merged
merged 2 commits into from Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/core/response.ts
Expand Up @@ -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') {
Expand Down
19 changes: 19 additions & 0 deletions 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);
});
2 changes: 2 additions & 0 deletions test/retry.ts
Expand Up @@ -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;
Expand Down