File size: 2,212 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';

var express = require('express');
var request = require('supertest');
var proxy = require('../');
var startProxyTarget = require('./support/proxyTarget');
var expect = require('chai').expect;

describe('uses remote path', function () {

  this.timeout(10000);

  var app = express();
  var proxyRoutes = ['/somePath/', '/somePath/longer/path', '/somePath/long/path/with/many/tokens'];
  var proxyKeyPath = '/somePath';
  var server;

  afterEach(function () {
    server.close();
  });

  proxyRoutes.forEach(function (path) {
    it('uses path component from inbound request', function (done) {

      var modifiedPath = path.replace(new RegExp(proxyKeyPath), '');

      var proxyRouteFn = {
        method: 'get',
        path: modifiedPath,
        fn: function (req, res) {
          res.json({ path: path, modifiedPath: modifiedPath });
        }
      };

      server = startProxyTarget(8309, 1000, [proxyRouteFn]);

      app.use('/somePath/', proxy('http://localhost:8309'));

      request(app)
        .get(path)
        .expect(200)
        .end(function (err, response) {
          if (err) {
            return done(err);
          }
          expect(response.path === path);
          expect(response.modifiedPath === path);
          done();
        });
    });

  });
});


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

  this.timeout(10000);

  var app = express();
  var firstProxyApp = express();
  var secondProxyApp = express();
  var firstPort = 10031;
  var secondPort = 10032;

  app.use('/proxy/:port', proxy(function (req) {
    return 'localhost:' + req.params.port;
  }, {
    memoizeHost: false
  }));

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

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

  it('can proxy with session value', 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);
      });
  });
});