|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var assert = require('assert'); |
| 4 | +var express = require('express'); |
| 5 | +var http = require('http'); |
| 6 | +var startProxyTarget = require('./support/proxyTarget'); |
| 7 | +var proxy = require('../'); |
| 8 | + |
| 9 | +function fakeProxyServer({path, port, response}) { |
| 10 | + var proxyRouteFn = [{ |
| 11 | + method: 'get', |
| 12 | + path: path, |
| 13 | + fn: function (req, res) { |
| 14 | + res.write(response); |
| 15 | + res.end(); |
| 16 | + } |
| 17 | + }]; |
| 18 | + |
| 19 | + return startProxyTarget(port, 1000, proxyRouteFn); |
| 20 | +} |
| 21 | + |
| 22 | +function simulateUserRequest() { |
| 23 | + return new Promise(function (resolve, reject) { |
| 24 | + |
| 25 | + var req = http.request({ hostname: 'localhost', port: 8308, path: '/' }, function (res) { |
| 26 | + var chunks = []; |
| 27 | + res.on('data', function (chunk) { chunks.push(chunk.toString()); }); |
| 28 | + res.on('end', function () { resolve(chunks); }); |
| 29 | + }); |
| 30 | + |
| 31 | + req.on('error', function (e) { |
| 32 | + reject('problem with request:', e.message); |
| 33 | + }); |
| 34 | + |
| 35 | + req.end(); |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +describe('handle multiple proxies in the same runtime', function () { |
| 40 | + this.timeout(3000); |
| 41 | + |
| 42 | + var server; |
| 43 | + var targetServer, targetServer2; |
| 44 | + |
| 45 | + beforeEach(function () { |
| 46 | + targetServer = fakeProxyServer({path:'/', port: '8309', response: '8309_response'}); |
| 47 | + targetServer2 = fakeProxyServer({path: '/', port: '8310', response: '8310_response'}); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(function () { |
| 51 | + server.close(); |
| 52 | + targetServer.close(); |
| 53 | + targetServer2.close(); |
| 54 | + }); |
| 55 | + |
| 56 | + |
| 57 | + describe("When two distinct proxies are defined for the global route", () => { |
| 58 | + afterEach(() => server.close()) |
| 59 | + |
| 60 | + it('the first proxy definition should be used if it succeeds', function (done) { |
| 61 | + var app = express(); |
| 62 | + app.use(proxy('http://localhost:8309', {})); |
| 63 | + app.use(proxy('http://localhost:8310', {})); |
| 64 | + server = app.listen(8308) |
| 65 | + simulateUserRequest() |
| 66 | + .then(function (res) { |
| 67 | + assert.equal(res[0], '8309_response'); |
| 68 | + done(); |
| 69 | + }) |
| 70 | + .catch(done); |
| 71 | + }); |
| 72 | + |
| 73 | + it('the fall through definition should be used if the prior skipsToNext', function (done) { |
| 74 | + var app = express(); |
| 75 | + app.use(proxy('http://localhost:8309', { |
| 76 | + skipToNextHandlerFilter: () => { return true } // no matter what, reject this proxy request, and call next() |
| 77 | + })); |
| 78 | + app.use(proxy('http://localhost:8310')) |
| 79 | + server = app.listen(8308) |
| 80 | + simulateUserRequest() |
| 81 | + .then(function (res) { |
| 82 | + assert.equal(res[0], '8310_response'); |
| 83 | + done(); |
| 84 | + }) |
| 85 | + .catch(done); |
| 86 | + }); |
| 87 | + }) |
| 88 | +}); |
0 commit comments