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

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

describe('honors timeout option', function () {

  var other;
  var  http;

  beforeEach(function () {
    http = express();
    other = proxyTarget(56001, 1000, [{
      method: 'get',
      path: '/',
      fn: function (req, res) { res.sendStatus(200); }
    }]);
  });

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

  function assertSuccess(server, done) {
    request(http)
      .get('/')
      .expect(200)
      .end(done);
  }

  function assertConnectionTimeout(server, done) {
    request(http)
      .get('/')
      .expect(504)
      .expect('X-Timeout-Reason', 'express-http-proxy reset the request.')
      .end(done);
  }

  describe('when timeout option is set lower than server response time', function () {
    it('should fail with CONNECTION TIMEOUT', function (done) {

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

      assertConnectionTimeout(http, done);
    });
  });

  describe('when timeout option is set higher than server response time', function () {
    it('should succeed', function (done) {

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

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

});