Skip to content

Commit caf1d25

Browse files
committed
Add test for supertest call proxying for IPv6, make import style consistent
1 parent 5615e15 commit caf1d25

8 files changed

Lines changed: 39 additions & 17 deletions

File tree

test/helpers/echoserver.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import http from 'http';
1+
import { createServer } from 'node:http';
22
import { WebSocketServer } from 'ws';
33

44
export const echo = (ws, req) => {
@@ -31,7 +31,7 @@ export const echo = (ws, req) => {
3131
};
3232

3333
export default () => {
34-
const server = http.createServer();
34+
const server = createServer();
3535
new WebSocketServer({ server }).on('connection', echo);
3636
return server;
3737
};

test/helpers/errorserver.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import http from 'http';
1+
import { createServer } from 'node:http';
22

33
export default () => {
4-
const server = http.createServer((req, res) => {
4+
const server = createServer((req, res) => {
55
res.writeHead(404);
66
res.end();
77
});

test/helpers/subprotocolserver.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import http from 'http';
1+
import { createServer } from 'node:http';
22
import { WebSocketServer } from 'ws';
33

44
const SUPPORTED_SUBPROTOCOL = 'supported_subprotocol';
55

66
export default () => {
7-
const server = http.createServer();
7+
const server = createServer();
88
// eslint-disable-next-line no-new
99
new WebSocketServer({
1010
server,

test/package/commonjs.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { createServer } = require('node:http');
12
const defaultRequire = require('superwstest');
23

34
if (typeof defaultRequire !== 'function') {
@@ -8,6 +9,16 @@ if (typeof defaultRequire.default !== 'function') {
89
throw new Error("require('superwstest').default did not return superwstest function");
910
}
1011

11-
if (typeof defaultRequire('http://localhost').get !== 'function') {
12-
throw new Error('supertest proxy not connected');
13-
}
12+
// test proxying to supertest
13+
const server = createServer((req, res) => {
14+
res.writeHead(req.url === '/path' ? 200 : 404);
15+
res.end();
16+
});
17+
18+
server.listen(0, '::1', async (err) => {
19+
if (err) {
20+
throw err;
21+
}
22+
await defaultRequire(server).get('/path').expect(200);
23+
server.close();
24+
});

test/package/es6modules.mjs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
import { createServer } from 'node:http';
12
import defaultImport from 'superwstest';
23

34
if (typeof defaultImport !== 'function') {
45
throw new Error("import 'superwstest' did not return superwstest function");
56
}
67

7-
if (typeof defaultImport('http://localhost').get !== 'function') {
8-
throw new Error('supertest proxy not connected');
9-
}
8+
// test proxying to supertest
9+
const server = createServer((req, res) => {
10+
res.writeHead(req.url === '/path' ? 200 : 404);
11+
res.end();
12+
});
13+
14+
server.listen(0, '::1', async (err) => {
15+
if (err) {
16+
throw err;
17+
}
18+
await defaultImport(server).get('/path').expect(200);
19+
server.close();
20+
});

test/package/typescript.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import request from 'superwstest';
2-
import { createServer as httpCreateServer } from 'http';
3-
import { createServer as httpsCreateServer } from 'https';
2+
import { createServer as httpCreateServer } from 'node:http';
3+
import { createServer as httpsCreateServer } from 'node:https';
44
import { WebSocketServer } from 'ws';
55

66
// this file just checks types; the code is not executed

test/superwstest-servers.test.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import http from 'http';
2-
import https from 'https';
1+
import http from 'node:http';
2+
import https from 'node:https';
33
import { WebSocket, WebSocketServer } from 'ws';
44
import { echo } from './helpers/echoserver.mjs';
55
import withScopedRequest from './helpers/withScopedRequest.mjs';

test/superwstest.test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { promisify } from 'node:util';
12
import WebSocket from 'ws';
2-
import { promisify } from 'util';
33
import makeEchoServer from './helpers/echoserver.mjs';
44
import withServer from './helpers/withServer.mjs';
55
import withScopedRequest from './helpers/withScopedRequest.mjs';

0 commit comments

Comments
 (0)