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 retries for requests with a body #231
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ import {serial as test} from 'ava'; | |
import createTestServer from 'create-test-server'; | ||
import withPage from './helpers/with-page'; | ||
|
||
const pBody = util.promisify(body); | ||
|
||
test('prefixUrl option', withPage, async (t, page) => { | ||
const server = await createTestServer(); | ||
server.get('/', (request, response) => { | ||
|
@@ -73,22 +75,24 @@ test('throws TimeoutError even though it does not support AbortController', with | |
response.end(); | ||
}); | ||
|
||
server.get('/endless', () => {}); | ||
server.get('/slow', (request, response) => { | ||
setTimeout(() => { | ||
response.end('ok'); | ||
}, 1000); | ||
}); | ||
|
||
await page.goto(server.url); | ||
await page.addScriptTag({path: './test/helpers/disable-abort-controller.js'}); | ||
await page.addScriptTag({path: './umd.js'}); | ||
|
||
// TODO: make set a timeout for this evaluation so we don't have to wait 30s | ||
const error = await page.evaluate(url => { | ||
window.ky = window.ky.default; | ||
|
||
const request = window.ky(`${url}/endless`, {timeout: 500}).text(); | ||
const request = window.ky(`${url}/slow`, {timeout: 500}).text(); | ||
return request.catch(error_ => error_.toString()); | ||
}, server.url); | ||
t.is(error, 'TimeoutError: Request timed out'); | ||
|
||
// A note from @szmarczak: await server.close() hangs on my machine | ||
await server.close(); | ||
}); | ||
|
||
|
@@ -180,12 +184,13 @@ test('throws if does not support ReadableStream', withPage, async (t, page) => { | |
}); | ||
|
||
test('FormData with searchParams', withPage, async (t, page) => { | ||
t.plan(2); | ||
|
||
const server = await createTestServer(); | ||
server.get('/', (request, response) => { | ||
response.end(); | ||
}); | ||
server.post('/', async (request, response) => { | ||
const pBody = util.promisify(body); | ||
const requestBody = await pBody(request); | ||
|
||
t.regex(requestBody, /bubblegum pie/); | ||
|
@@ -211,6 +216,8 @@ test('FormData with searchParams', withPage, async (t, page) => { | |
}); | ||
|
||
test('headers are preserved when input is a Request and there are searchParams in the options', withPage, async (t, page) => { | ||
t.plan(2); | ||
|
||
const server = await createTestServer(); | ||
server.get('/', (request, response) => { | ||
response.end(); | ||
|
@@ -236,3 +243,34 @@ test('headers are preserved when input is a Request and there are searchParams i | |
|
||
await server.close(); | ||
}); | ||
|
||
test('retry with body', withPage, async (t, page) => { | ||
let requestCount = 0; | ||
|
||
const server = await createTestServer(); | ||
server.get('/', (request, response) => { | ||
response.end('zebra'); | ||
}); | ||
server.put('/test', async (request, response) => { | ||
requestCount++; | ||
await pBody(request); | ||
response.sendStatus(408); | ||
}); | ||
|
||
await page.goto(server.url); | ||
await page.addScriptTag({path: './umd.js'}); | ||
|
||
const error = await page.evaluate(url => { | ||
window.ky = window.ky.default; | ||
const request = window.ky(url + '/test', { | ||
body: 'foo', | ||
method: 'PUT', | ||
retry: 2 | ||
}).text(); | ||
return request.catch(error_ => error_.toString()); | ||
}, server.url); | ||
t.is(error, 'HTTPError: Request Timeout'); | ||
t.is(requestCount, 2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reasons that I do not understand, the So, of course it seems like a bug in Ky. And sure enough, when I debugged it with The thing is, we have no special handling of I chose to just change the status code in commit 2808b0f. |
||
|
||
await server.close(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was hanging for me, too. I think because the server's
response
stream was never being closed. That behavior doesn't seem necessary for this test, so I simply made the Ky timeout less than the server timeout and it works as expected.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!☺️