Skip to content

Commit bc20f2d

Browse files
fehmersindresorhus
andauthored
Add statusCodes option (#18)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 095d1fc commit bc20f2d

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ export interface Options {
1919
@default false
2020
*/
2121
useGet?: boolean;
22+
23+
/**
24+
HTTP status codes to consider as successful responses.
25+
26+
@default [200]
27+
*/
28+
statusCodes?: readonly number[];
2229
}
2330

2431
/**

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import http from 'node:http';
22

3-
export default function waitForLocalhost({port, path, useGet} = {}) {
3+
export default function waitForLocalhost({port, path, useGet, statusCodes = [200]} = {}) {
44
return new Promise(resolve => {
55
const retry = () => {
66
setTimeout(main, 200);
@@ -10,7 +10,7 @@ export default function waitForLocalhost({port, path, useGet} = {}) {
1010

1111
const doRequest = (ipVersion, next) => {
1212
const request = http.request({method, port, path, family: ipVersion}, response => {
13-
if (response.statusCode === 200) {
13+
if (statusCodes.includes(response.statusCode)) {
1414
resolve({ipVersion});
1515
return;
1616
}

readme.md

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ Default: `false`
5252

5353
Use the `GET` HTTP-method instead of `HEAD` to check if the server is running.
5454

55+
##### statusCodes
56+
57+
Type: `number[]`\
58+
Default: `[200]`
59+
60+
HTTP status codes to consider as successful responses.
61+
5562
## Related
5663

5764
- [wait-for-localhost-cli](https://github.com/sindresorhus/wait-for-localhost-cli) - CLI for this module

test.js

+20
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,23 @@ test('use custom path', async t => {
5959

6060
await server.close();
6161
});
62+
63+
test('use custom statusCodes', async t => {
64+
t.plan(2);
65+
66+
const server = await createTestServer();
67+
server.get('/', async (request, response) => {
68+
await delay(1000);
69+
response.status(202).end();
70+
t.pass();
71+
});
72+
73+
await waitForLocalhost({
74+
port: server.port,
75+
statusCodes: [201, 202],
76+
});
77+
78+
t.pass();
79+
80+
await server.close();
81+
});

0 commit comments

Comments
 (0)