Skip to content

Commit d48ed95

Browse files
promer94sindresorhus
andauthoredJul 20, 2022
Fix copying response body with 204 status code when using onDownloadProgress (#444)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent b5a4dc6 commit d48ed95

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed
 

‎source/core/Ky.ts

+15
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,21 @@ export class Ky {
285285
const totalBytes = Number(response.headers.get('content-length')) || 0;
286286
let transferredBytes = 0;
287287

288+
if (response.status === 204) {
289+
if (onDownloadProgress) {
290+
onDownloadProgress({percent: 1, totalBytes, transferredBytes}, new Uint8Array());
291+
}
292+
293+
return new globalThis.Response(
294+
null,
295+
{
296+
status: response.status,
297+
statusText: response.statusText,
298+
headers: response.headers,
299+
},
300+
);
301+
}
302+
288303
return new globalThis.Response(
289304
new globalThis.ReadableStream({
290305
async start(controller) {

‎test/browser.ts

+53-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ test('aborting a request', withPage, async (t: ExecutionContext, page: Page) =>
9191
await server.close();
9292
});
9393

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

131+
test('should not copy response body with 204 status code when using `onDownloadProgress` ', withPage, async (t: ExecutionContext, page: Page) => {
132+
const status = 204;
133+
const statusText = 'No content';
134+
const server = await createEsmTestServer();
135+
server.get('/', (_request, response) => {
136+
response.end('meow');
137+
});
138+
139+
server.get('/test', (_request, response) => {
140+
setTimeout(() => {
141+
response.statusMessage = statusText;
142+
response.status(status).header('X-ky-Header', 'ky').end(null);
143+
}, 500);
144+
});
145+
await page.goto(server.url);
146+
await addKyScriptToPage(page);
147+
const data = await page.evaluate(async (url: string) => {
148+
const progress: any = [];
149+
let totalBytes = 0;
150+
const response = await window.ky.get(`${url}/test`, {
151+
onDownloadProgress(progressEvent) {
152+
progress.push(progressEvent);
153+
},
154+
}).then(async v => {
155+
totalBytes = Number(v.headers.get('content-length')) || 0;
156+
return ({
157+
headers: v.headers.get('X-ky-Header'),
158+
status: v.status,
159+
statusText: v.statusText,
160+
});
161+
});
162+
return {
163+
response,
164+
progress,
165+
totalBytes,
166+
};
167+
}, server.url);
168+
169+
t.deepEqual(data.response, {
170+
status,
171+
headers: 'ky',
172+
statusText,
173+
});
174+
t.deepEqual(data.progress, [{
175+
percent: 1,
176+
totalBytes: data.totalBytes,
177+
transferredBytes: 0,
178+
}]);
179+
180+
await server.close();
181+
});
182+
131183
test('aborting a request with onDonwloadProgress', withPage, async (t: ExecutionContext, page: Page) => {
132184
const server = await createEsmTestServer();
133185

0 commit comments

Comments
 (0)
Please sign in to comment.