A dummy https server. The server is signed with a temporary, in memory, self signed certificate.
When requesting a server signed with a self signed certificate from node.js do set rejectUnauthorized to false when doing the request to ignore the self signed certificate warning.
$ npm install @podium/test-utils --save-devStart a https server at any random available http port
const{ HttpsServer }=require('@podium/test-utils');constserver=newHttpsServer();server.request=(req,res)=>{res.statusCode=200;res.setHeader('Content-Type','text/plain');res.end('hello world');};constservice=awaitserver.listen();console.log(service)// addresses to server.awaitserver.close();Create a new https server.
constserver=newHttpsServer();The https server instance has the following API:
Starts the https server listening on a random available port. Returns a
promise which will resolve with the address of the running instance.
constserver=newHttpsServer();constservice=awaitserver.listen();console.log(service)// addresses to server etc.Closes the running instance. Returns a promise which will resolve when all
open connections to the instance is closed and the server is properly closed.
Helpe method to request any path on the server instance. Returns a promise
which will resolve with the data of the request.
Example:
constserver=newHttpsServer();server.request=(req,res)=>{res.statusCode=200;res.setHeader('Content-Type','text/plain');res.end(JSON.stringify({data: 'hello world'}));};awaitserver.listen();constdata=awaitserver.get({pathname: '/foo'});console.log(data);awaitserver.close();The method takes the following options:
pathname-String- A pathname on the server to request. Defaults to/.
A setter for a function to be run on each request. Function will be called
with request and response object.
constserver=newHttpsServer();server.request=(req,res)=>{res.statusCode=200;res.setHeader('Content-Type','application/json');res.end(JSON.stringify({status: 'ok'}));}