11import { INestApplication } from '@nestjs/common' ;
22import { IncomingMessage , request , RequestOptions } from 'http' ;
3+ import { URL } from 'url' ;
34
45export 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