| 35 | // for this request, or falsy to indicate that the proxy should not be used for |
| 36 | // this request. |
| 37 | function runTest (name, options, responseHandler) { |
| 38 | tape(name, function (t) { |
| 39 | proxyEnvVars.forEach(function (v) { |
| 40 | delete process.env[v] |
| 41 | }) |
| 42 | if (options.env) { |
| 43 | for (var v in options.env) { |
| 44 | process.env[v] = options.env[v] |
| 45 | } |
| 46 | delete options.env |
| 47 | } |
| 48 | |
| 49 | var called = false |
| 50 | currResponseHandler = function (req, res) { |
| 51 | if (responseHandler) { |
| 52 | called = true |
| 53 | t.equal(req.headers.host, 'google.com') |
| 54 | if (typeof responseHandler === 'function') { |
| 55 | responseHandler(t, req, res) |
| 56 | } |
| 57 | } else { |
| 58 | t.fail('proxy response should not be called') |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | options.url = options.url || 'http://google.com' |
| 63 | request(options, function (err, res, body) { |
| 64 | if (responseHandler && !called) { |
| 65 | t.fail('proxy response should be called') |
| 66 | } |
| 67 | t.equal(err, null) |
| 68 | t.equal(res.statusCode, 200) |
| 69 | if (responseHandler) { |
| 70 | if (body.length > 100) { |
| 71 | body = body.substring(0, 100) |
| 72 | } |
| 73 | t.equal(body, 'ok') |
| 74 | } else { |
| 75 | t.equal(/^<!doctype html>/i.test(body), true) |
| 76 | } |
| 77 | t.end() |
| 78 | }) |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | function addTests () { |
| 83 | // If the `runTest` function is changed, run the following command and make |