Skip to content

Commit 78fc6be

Browse files
committed
test: use url class instead of request options
1 parent e66423b commit 78fc6be

2 files changed

Lines changed: 10 additions & 24 deletions

File tree

integration/send-files/e2e/express.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ describe('Express FileSend', () => {
7575
});
7676
it('should allow for the client to end the response and be able to make another', async () => {
7777
await app.listen(0);
78-
const httpOptions = await getHttpBaseOptions(app);
79-
await sendCanceledHttpRequest(httpOptions, '/file/slow');
80-
const res = await sendHttpRequest(httpOptions, '/file/stream');
78+
const url = await getHttpBaseOptions(app);
79+
await sendCanceledHttpRequest(new URL('/file/slow', url));
80+
const res = await sendHttpRequest(new URL('/file/stream', url));
8181
expect(res.statusCode).to.be.eq(200);
8282
}).timeout(5000);
8383
});

integration/send-files/e2e/utils.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
import { INestApplication } from '@nestjs/common';
22
import { IncomingMessage, request, RequestOptions } from 'http';
3+
import { URL } from 'url';
34

45
export const getHttpBaseOptions = async (
56
app: INestApplication,
6-
): Promise<RequestOptions> => {
7+
): Promise<URL> => {
78
const url = await app.getUrl();
8-
// replace IPv6 localhost with IPv4 localhost alias and split URL on : to get the protocol (http), the host (localhost) and the port (random port)
9-
const [protocol, host, port] = url.replace('[::1]', 'localhost').split(':');
10-
return {
11-
// protocol is expected to be http: or https:
12-
protocol: `${protocol}:`,
13-
// remove the // prefix left over from the split(':')
14-
host: host.replace('//', ''),
15-
port,
16-
method: 'GET',
17-
};
9+
return new URL(url);
1810
};
1911

20-
export const sendCanceledHttpRequest = async (
21-
options: RequestOptions,
22-
path: string,
23-
) => {
12+
export const sendCanceledHttpRequest = async (url: URL) => {
2413
return new Promise((resolve, reject) => {
25-
const req = request({ ...options, path }, res => {
14+
const req = request(url, res => {
2615
// close the request once we get the first response of data
2716
res.on('data', () => {
2817
req.destroy();
@@ -35,12 +24,9 @@ export const sendCanceledHttpRequest = async (
3524
});
3625
};
3726

38-
export const sendHttpRequest = async (
39-
options: RequestOptions,
40-
path: string,
41-
) => {
27+
export const sendHttpRequest = async (url: URL) => {
4228
return new Promise<IncomingMessage>((resolve, reject) => {
43-
const req = request({ ...options, path }, res => {
29+
const req = request(url, res => {
4430
// this makes sure that the response actually starts and is read. We could verify this value against the same
4531
// that is in an earlier test, but all we care about in _this_ test is that the status code is 200
4632
res.on('data', chunk => {

0 commit comments

Comments
 (0)