Skip to content

Commit f34fb1f

Browse files
authoredJun 23, 2022
Fix response details getting lost when using onDownloadProgress (#441)
1 parent e4077b9 commit f34fb1f

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed
 

‎source/core/Ky.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class Ky {
167167
// To provide correct form boundary, Content-Type header should be deleted each time when new Request instantiated from another one
168168
if (
169169
((supportsFormData && this._options.body instanceof globalThis.FormData)
170-
|| this._options.body instanceof URLSearchParams) && !(this._options.headers && (this._options.headers as Record<string, string>)['content-type'])
170+
|| this._options.body instanceof URLSearchParams) && !(this._options.headers && (this._options.headers as Record<string, string>)['content-type'])
171171
) {
172172
this.request.headers.delete('content-type');
173173
}
@@ -314,6 +314,11 @@ export class Ky {
314314
await read();
315315
},
316316
}),
317+
{
318+
status: response.status,
319+
statusText: response.statusText,
320+
headers: response.headers,
321+
},
317322
);
318323
}
319324
}

‎test/browser.ts

+37
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,43 @@ 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) => {
95+
const json = {hello: 'world'};
96+
const status = 202;
97+
const statusText = 'Accepted';
98+
const server = await createEsmTestServer();
99+
server.get('/', (_request, response) => {
100+
response.end('meow');
101+
});
102+
103+
server.get('/test', (_request, response) => {
104+
setTimeout(() => {
105+
response.statusMessage = statusText;
106+
response.status(status).header('X-ky-Header', 'ky').json(json);
107+
}, 500);
108+
});
109+
await page.goto(server.url);
110+
await addKyScriptToPage(page);
111+
const data = await page.evaluate(async url => {
112+
// eslint-disable-next-line @typescript-eslint/no-empty-function
113+
const request = window.ky.get(`${url}/test`, {onDownloadProgress() {}}).then(async v => ({
114+
headers: v.headers.get('X-ky-Header'),
115+
status: v.status,
116+
statusText: v.statusText,
117+
data: await v.json(),
118+
}));
119+
return request;
120+
}, server.url);
121+
122+
t.deepEqual(data, {
123+
status,
124+
headers: 'ky',
125+
statusText,
126+
data: json,
127+
});
128+
await server.close();
129+
});
130+
94131
test('aborting a request with onDonwloadProgress', withPage, async (t: ExecutionContext, page: Page) => {
95132
const server = await createEsmTestServer();
96133

0 commit comments

Comments
 (0)
Please sign in to comment.