Skip to content

Commit

Permalink
Fix copying response body with 204 status code when using `onDownload…
Browse files Browse the repository at this point in the history
…Progress` (#444)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
promer94 and sindresorhus committed Jul 20, 2022
1 parent b5a4dc6 commit d48ed95
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
15 changes: 15 additions & 0 deletions source/core/Ky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,21 @@ export class Ky {
const totalBytes = Number(response.headers.get('content-length')) || 0;
let transferredBytes = 0;

if (response.status === 204) {
if (onDownloadProgress) {
onDownloadProgress({percent: 1, totalBytes, transferredBytes}, new Uint8Array());
}

return new globalThis.Response(
null,
{
status: response.status,
statusText: response.statusText,
headers: response.headers,
},
);
}

return new globalThis.Response(
new globalThis.ReadableStream({
async start(controller) {
Expand Down
54 changes: 53 additions & 1 deletion test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ test('aborting a request', withPage, async (t: ExecutionContext, page: Page) =>
await server.close();
});

test('should copy origin response info when use onDownloadProgress', withPage, async (t: ExecutionContext, page: Page) => {
test('should copy origin response info when using `onDownloadProgress`', withPage, async (t: ExecutionContext, page: Page) => {
const json = {hello: 'world'};
const status = 202;
const statusText = 'Accepted';
Expand Down Expand Up @@ -128,6 +128,58 @@ test('should copy origin response info when use onDownloadProgress', withPage, a
await server.close();
});

test('should not copy response body with 204 status code when using `onDownloadProgress` ', withPage, async (t: ExecutionContext, page: Page) => {
const status = 204;
const statusText = 'No content';
const server = await createEsmTestServer();
server.get('/', (_request, response) => {
response.end('meow');
});

server.get('/test', (_request, response) => {
setTimeout(() => {
response.statusMessage = statusText;
response.status(status).header('X-ky-Header', 'ky').end(null);
}, 500);
});
await page.goto(server.url);
await addKyScriptToPage(page);
const data = await page.evaluate(async (url: string) => {
const progress: any = [];
let totalBytes = 0;
const response = await window.ky.get(`${url}/test`, {
onDownloadProgress(progressEvent) {
progress.push(progressEvent);
},
}).then(async v => {
totalBytes = Number(v.headers.get('content-length')) || 0;
return ({
headers: v.headers.get('X-ky-Header'),
status: v.status,
statusText: v.statusText,
});
});
return {
response,
progress,
totalBytes,
};
}, server.url);

t.deepEqual(data.response, {
status,
headers: 'ky',
statusText,
});
t.deepEqual(data.progress, [{
percent: 1,
totalBytes: data.totalBytes,
transferredBytes: 0,
}]);

await server.close();
});

test('aborting a request with onDonwloadProgress', withPage, async (t: ExecutionContext, page: Page) => {
const server = await createEsmTestServer();

Expand Down

0 comments on commit d48ed95

Please sign in to comment.