File size: 1,791 Bytes
befd7da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';

var express = require('express');
var request = require('supertest');
var proxy = require('../');

describe('host can be a dynamic function', function () {

  this.timeout(10000);

  var app = express();
  describe('and memoization can be disabled', function () {
    var firstProxyApp = express();
    var secondProxyApp = express();
    // TODO: This seems like a bug factory.  We will have intermittent port conflicts, yeah?

    function randomNumberInPortRange() {
      return Math.floor(Math.random() * 48000) + 1024;
    }
    var firstPort = randomNumberInPortRange();
    var secondPort = randomNumberInPortRange();

    var hostFn = function (req) {
      return 'localhost:' + req.params.port;
    };

    app.use('/proxy/:port', proxy(hostFn, { memoizeHost: false }));

    firstProxyApp
      .get('/', function (req, res) { res.sendStatus(204); })
      .listen(firstPort);

    secondProxyApp
      .get('/', function (req, res) { res.sendStatus(200); })
      .listen(secondPort);

    it('when not memoized, host resolves to a second value on the seecond call', function (done) {
      request(app)
        .get('/proxy/' + firstPort)
        .expect(204)
        .end(function (err) {
          if (err) {
            return done(err);
          }
          request(app)
            .get('/proxy/' + secondPort)
            .expect(200, done);
        });
    });
  });
});

describe('host can be an ip address', function () {
  it('with a port', function (done) {
    var app = express();
    app.use('/proxy/', proxy('127.0.0.1:3020'));

    var targetApp = express();
    targetApp
      .get('/', function (req, res) { res.sendStatus(211); })
      .listen(3020);


    request(app)
      .get('/proxy/')
      .expect(211)
      .end(done);
  });
});