Skip to content

Instantly share code, notes, and snippets.

@swashcap
Created April 22, 2019 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swashcap/545ea386e1f453ac4a1f4ad610ef603b to your computer and use it in GitHub Desktop.
Save swashcap/545ea386e1f453ac4a1f4ad610ef603b to your computer and use it in GitHub Desktop.
const assert = require("assert");
const hapi = require("hapi");
const http = require("http");
const getRootHandler = response => ({
handler() {
return response;
},
method: "GET",
path: "/"
});
const listener = http.createServer();
const port = 3000;
const request = () =>
new Promise((resolve, reject) => {
const req = http.request(
{
hostname: "0.0.0.0",
port,
method: "GET",
path: "/"
},
res => {
let body = "";
res.setEncoding("utf8");
res.on("data", chunk => {
body += chunk;
});
res.on("end", () =>
resolve({
body,
headers: res.headers,
statusCode: res.statusCode
})
);
}
);
req.on("error", reject);
req.end();
});
const main = async () => {
// Create a server
const server1 = hapi.Server({
autoListen: false,
listener
});
// Register a `/` route that responds with "Server 1"
server1.route(getRootHandler("Server 1"));
// Start the `hapi.Server` and `http.Server`
await Promise.all([
server1.start(),
new Promise(resolve => listener.listen(port, resolve))
]);
// Check server1's response
const { body: body1, statusCode: statusCode1 } = await request();
assert.equal(statusCode1, 200);
assert.equal(body1, "Server 1");
console.log("server1 response good")
// Stop the server. This also appears to stop `listener`
await server1.stop();
// Create a new server and set its listener to the same `http.Server`
const server2 = hapi.Server({
autoListen: false,
listener
});
// Register a `/` route that responds with "Server 2"
server2.route(getRootHandler("Server 2"));
// Start everything
await Promise.all([
server2.start(),
new Promise(resolve => listener.listen(port, resolve))
])
// Check server2's response
const { body: body2, statusCode: statusCode2 } = await request();
assert.equal(statusCode2, 200);
assert.equal(body2, "Server 2");
console.log("server2 response good")
await server2.stop();
};
main()
.then(() => console.log("All good!"))
.catch(error => {
console.error(error);
process.exit(1);
});
@swashcap
Copy link
Author

Seeing this throw along with an unhandled rejection:

node hapi-listener-assertions.js
server1 response good
(node:2443) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:482:11)
    at Object.internals.writeHead (/Users/creed/Projects/hapijs/hapi/lib/transmit.js:326:21)
    at Object.internals.transmit (/Users/creed/Projects/hapijs/hapi/lib/transmit.js:105:15)
    at Object.internals.fail (/Users/creed/Projects/hapijs/hapi/lib/transmit.js:68:22)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
(node:2443) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2443) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
{ AssertionError [ERR_ASSERTION]: 'Server 1' == 'Server 2'
    at main (/Users/creed/Projects/hapijs/hapi/hapi-listener-assertions.js:90:10)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
  generatedMessage: true,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: 'Server 1',
  expected: 'Server 2',
  operator: '==' }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment