mardi 3 novembre 2020

Testing http.Server.close error in NodeJS

I have a code in NodeJS responsible to close an http.Server connection and I would like to test the error scenario on the http.Server.close() method.

The problem is to do that, I need to simulate the return of the close method with the err object populated, and I don't know how to do it.

Below you can find my code and I would like to test the line where we can find the code reject(err);

Note: In my integration tests, I'm starting temp HTTP servers to simulate the real scenario. So as far as I understood I need to find a real scenario where the .close method will be rejected by the default implementation.

Thanks.

this._httpServer = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => this.handler(req, res));
...
disconnect(): Promise<void> {
    return new Promise((resolve, reject) => {
      this._httpServer.close((err) => {
        if (err) {
          reject(err);
        } else {
          resolve();
        }
      });
    });
  }

Aucun commentaire:

Enregistrer un commentaire