toxy
Version:
Hackable HTTP proxy to simulate server failure scenarios and network conditions
47 lines (38 loc) • 1.26 kB
JavaScript
const toxy = require('..')
const proxy = toxy()
const rules = toxy.rules
const poisons = toxy.poisons
proxy
.forward('http://httpbin.org')
.rule(rules.probability(50))
// Global, incoming only traffic poisioning
proxy
.poison(poisons.slowOpen({ delay: 500 }))
.withRule(rules.method('GET'))
// Route level, incoming and outgoing traffic poisioning
proxy
.get('/*')
// Define poison to infect incoming traffic
.poison(poisons.slowOpen({ delay: 500 }))
.withRule(rules.method('GET'))
// Define multiple poisons to infect outgoing traffic
.outgoingPoison(poisons.bandwidth({ bytes: 1024 }))
.withRule(rules.method('GET'))
// Randomly reply with an error
.outgoingPoison(function (req, res) {
res.writeHead(400, { 'Content-Length': 5 })
res.end('Error')
})
.withRule(rules.method('POST'))
.withRule(rules.probability(75))
.withRule(rules.responseHeaders({ 'Content-Type': /json/i }))
.withRule(function (req, res, next) {
// This rule evaluates the server response headers
// in order to determine if the poison should be applied or not
if (res.getHeader('server') === 'nginx') {
return next(null, false)
}
next()
})
proxy.listen(3000)
console.log('Server listening on port:', 3000)