Skip to content

Commit 195e0e2

Browse files
authoredOct 31, 2022
Don't return empty string for .json() if Transfer-Encoding is chunked (#464)
1 parent 4987612 commit 195e0e2

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
 

‎source/core/Ky.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ export class Ky {
9292
}
9393

9494
const contentLength = response.headers.get('Content-Length');
95-
if (contentLength === null || contentLength === '0') {
95+
const transferEncoding = response.headers.get('Transfer-Encoding');
96+
if ((contentLength === null || contentLength === '0') && transferEncoding !== 'chunked') {
9697
return '';
9798
}
9899

‎test/main.ts

+16
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,22 @@ test('.json() with custom accept header', async t => {
236236
await server.close();
237237
});
238238

239+
test('.json() when response is chunked', async t => {
240+
const server = await createHttpTestServer();
241+
server.get('/', async (request, response) => {
242+
response.write('[');
243+
response.write('"one",');
244+
response.write('"two"');
245+
response.end(']');
246+
});
247+
248+
const responseJson = await ky.get(server.url).json();
249+
250+
t.deepEqual(responseJson, ['one', 'two']);
251+
252+
await server.close();
253+
});
254+
239255
test('.json() with invalid JSON body', async t => {
240256
const server = await createHttpTestServer();
241257
server.get('/', async (request, response) => {

0 commit comments

Comments
 (0)
Please sign in to comment.