Created
April 1, 2012 23:12
-
-
Save gilmoreorless/2279344 to your computer and use it in GitHub Desktop.
Basic HTTP proxy that introduces random delays to responses, to help test network race conditions
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
#!/usr/bin/env node | |
/** | |
* INSTRUCTIONS | |
* | |
* npm install http-proxy | |
* node proxy.js | |
*/ | |
var config = { | |
// Port to access this proxy | |
proxyPort: 2991, | |
// Port of the real server | |
targetPort: 2990, | |
// Min/max values for request delays, in milliseconds | |
timeRange: [0, 3000] | |
}; | |
var http = require('http'), | |
httpProxy = require('http-proxy'); | |
function randomTime() { | |
var min = config.timeRange[0], | |
max = config.timeRange[1]; | |
return (Math.random() * (max - min) + min) | 0; | |
} | |
httpProxy.createServer(function (req, res, proxy) { | |
var buffer = httpProxy.buffer(req), | |
time = randomTime(); | |
console.log(req.method + ' ' + req.url); | |
setTimeout((function (time) { | |
return function () { | |
console.log('SERVE ' + req.url + ' (' + time + 'ms delay)'); | |
proxy.proxyRequest(req, res, { | |
host: 'localhost', | |
port: config.targetPort, | |
buffer: buffer | |
}); | |
}; | |
})(time), time); | |
}).listen(config.proxyPort); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment