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

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

function proxyTarget(port) {

  var other = express();
  other.get('/', function (req, res) {
    res.send('Success');
  });
  return other.listen(port);
}

describe('proxies to requested port', function () {
  var other;
  var  http;

  beforeEach(function () {
    http = express();
    other = proxyTarget(56001);
  });

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


  function assertSuccess(server, done) {
    request(http)
      .get('/')
      .expect(200)
      .end(function (err, res) {
        if (err) { return done(err); }
        assert(res.text === 'Success');
        done();
      });
  }

  describe('when host is a String', function () {
    it('when passed as an option', function (done) {

      http.use(proxy('http://localhost', {
        port: 56001
      }));

      assertSuccess(http, done);
    });

    it('when passed on the host string', function (done) {

      http.use(proxy('http://localhost:56001'));

      assertSuccess(http, done);
    });

  });

  describe('when host is a function', function () {


    it('and port is on the String returned', function (done) {

      http.use(proxy(
        function () { return 'http://localhost:56001'; }
      ));

      assertSuccess(http, done);
    });

    it('and port passed as an option', function (done) {

      http.use(proxy(
        function () { return 'http://localhost'; },
        { port: 56001 }
      ));

      assertSuccess(http, done);
    });
  });

});