File size: 1,546 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
'use strict';

var assert = require('assert');
var express = require('express');
var http = require('http');
var request = require('supertest');
var proxy = require('../');
var proxyTarget = require('../test/support/proxyTarget');

var aliases = ['forwardPath', 'forwardPathAsync', 'proxyReqPathResolver'];

describe('resolveProxyReqPath', function () {
  var server;

  this.timeout(10000);

  before(function () {
    var handlers = [{
      method: 'get',
      path: '/working',
      fn: function (req, res) {
        res.sendStatus(200);
      }
    }];

    server = proxyTarget(12345, 100, handlers);
  });

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

  aliases.forEach(function (alias) {
    describe('when author uses option ' + alias, function () {
      it('the proxy request path is the result of the function', function (done) {
        var app = express();
        var opts = {};
        opts[alias] = function () { return '/working'; };
        app.use(proxy('localhost:12345', opts));

        request(app)
          .get('/failing')
          .expect(200)
          .end(done);
      });

      it('the ' + alias + ' method has access to request object', function (done) {
        var app = express();
        app.use(proxy('localhost:12345', {
          forwardPath: function (req) {
            assert.ok(req instanceof http.IncomingMessage);
            return '/working';
          }
        }));

        request(app)
          .get('/foobar')
          .expect(200)
          .end(done);
      });

    });
  });
});