Last active
November 7, 2016 14:53
-
-
Save manchicken/9799567bf7cadcebedd057f9e8932866 to your computer and use it in GitHub Desktop.
This is a simple HTTP server which gives you predictable HTTP responses based on the URL. It's super simple. This requires `lodash`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const http = require('http') | |
const _ = require('lodash') | |
// Create an HTTP tunneling proxy | |
var svr = http.createServer( (req, res) => { | |
// Just some simple handler routines | |
const handlers = _.merge( | |
{ '/success': { status: 200, message: 'OK' }, }, | |
...[500, 404, 422, 409, 504, 503, 502, 403].map( | |
x => ({[`/${x}`]: { status: x, message: `Ack: ${x}` }}) | |
), | |
{ '/timeout': { status: null, message: null } } | |
) | |
console.info(req.url) | |
// Use a handler based on the URL, or default to success | |
let handler = handlers[ req.url || '/success' ] || handlers['/success'] | |
if ( ! handler.status ) { | |
console.info('Timing out on purpose') | |
req.socket.setTimeout(500) | |
return | |
} | |
res.writeHead(handler.status, {'Content-Type': 'text/plain'}); | |
return res.end(handler.message) | |
}); | |
svr.listen(6006, 'localhost') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment