Skip to content

Commit

Permalink
Improve Node.js test stub
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
  • Loading branch information
jviotti committed Nov 22, 2023
1 parent 14889ae commit 84c586b
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions test/http/stub.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
const http = require('http');
const PORT = 9999;

const server = http.createServer((req, res) => {
// Set the response header to indicate JSON content
res.setHeader('Content-Type', 'application/json');

// Define a simple JSON response
const jsonResponse = {
message: 'Hello, JSON World!',
timestamp: new Date().toISOString(),
};
http.createServer((request, response) => {
for (const [key, value] of Object.entries(request.headers)) {
response.setHeader(`X-${key}`, value);
}

// Send the JSON response
res.end(JSON.stringify(jsonResponse));
});
if (request.headers['x-code']) {
response.statusCode = parseInt(request.headers['x-code'], 10);
}

const PORT = 9999;
server.listen(PORT, () => {
response.setHeader('Content-Type', 'text/plain');
response.end(`RECEIVED ${request.method} ${request.url}`);
}).listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});

0 comments on commit 84c586b

Please sign in to comment.