1- process . env . TEST = true
2- process . env . PORT = 4443
3- process . env . HTTP_PORT = 8080
1+ process . env . USE_STATIC = true
42
53const assert = require ( "assert" )
6- const childProcess = require ( "child_process" )
74const fs = require ( "fs" )
85const http = require ( "http" )
96const https = require ( "https" )
107const app = require ( "../index.js" )
118
12- // run an external script located in the scriptPath
13- function runScript ( scriptPath , args = [ ] , background = false ) {
14- return new Promise ( ( resolve , reject ) => {
15- const process = childProcess . fork ( scriptPath , args )
16- process . on ( "error" , err => reject ( err ) )
17- process . on ( "exit" , code => {
18- if ( code === 0 ) resolve ( )
19- else reject ( code )
20- } )
21- if ( background ) resolve ( process )
22- } )
23- }
24-
25- // sleep function, must be used with await
26- async function sleep ( ms = 0 ) {
27- return new Promise ( resolve => setTimeout ( resolve , ms ) )
28- }
29-
309// make an http request on the specified path
3110function makeRequest ( path = "/" , secure = false ) {
3211 const agentOptions = {
@@ -37,7 +16,7 @@ function makeRequest(path = "/", secure = false) {
3716 }
3817 const options = {
3918 host : "localhost" ,
40- port : process . env . HTTP_PORT || 80 ,
19+ port : 80 ,
4120 path : path ,
4221 method : "GET" ,
4322 headers : { }
@@ -67,20 +46,14 @@ describe("Testing https-localhost", () => {
6746 . then ( res => assert ( res . statusCode === 301 ) )
6847 } )
6948 it ( "works as a module" , async function ( ) {
70- app . get ( "/test" , ( req , res ) => res . send ( "TEST" ) )
71- await makeRequest ( "/test" , true )
49+ app . get ( "/test/module " , ( req , res ) => res . send ( "TEST" ) )
50+ await makeRequest ( "/test/module " , true )
7251 . then ( res => assert ( res . data === "TEST" ) )
7352 } )
7453 it ( "serve static file used as standalone tool" , async function ( ) {
75- process . env . PORT = 4444
76- process . env . HTTP_PORT = 8081
77- runScript ( "index.js" , [ "test" ] , true )
78- . then ( process => proc = process ) // eslint-disable-line no-return-assign
79- . catch ( err => console . error ( err ) )
80- await sleep ( 500 )
81- await makeRequest ( "/test.html" , true )
54+ await makeRequest ( "/test/static.html" , true )
8255 . then ( res => assert (
83- res . data . toString ( ) === fs . readFileSync ( "test/test .html" ) . toString ( ) ) )
56+ res . data . toString ( ) === fs . readFileSync ( "test/static .html" ) . toString ( ) ) )
8457 if ( proc ) proc . kill ( "SIGINT" )
8558 } )
8659} )
0 commit comments