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

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

describe('when server responds with an error', function () {

  this.timeout(10000);

  var app;
  var slowTarget;
  var serverReference;

  beforeEach(function () {
    app = express();
  });

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

  var STATUS_CODES = [
    { code: 403, text: 'Forbidden', toString: 'Error: cannot GET /proxy (403)' },
    { code: 404, text: 'Not Found', toString: 'Error: cannot GET /proxy (404)' },
    { code: 500, text: 'Internal Server Error', toString: 'Error: cannot GET /proxy (500)' }
  ];

  STATUS_CODES.forEach(function (statusCode) {
    it('express-http-proxy responds with ' + statusCode.text +
      'when proxy server responds ' + statusCode.code, function (done) {
      slowTarget = express();
      slowTarget.use(function (req, res) { res.sendStatus(statusCode.code); });
      serverReference = slowTarget.listen(12345);

      app.use('/proxy', proxy('http://127.0.0.1:12345', {
        reqAsBuffer: true,
        reqBodyEncoding: null,
        parseReqBody: false
      }));

      request(app)
        .get('/proxy')
        .expect(statusCode.code)
        .end(function (err, res) {
          assert(err === null);
          assert(res.error);
          assert(res.error.text === statusCode.text);
          assert(res.error.toString() === statusCode.toString);
          done();
        });
    });
  });
});