File size: 3,652 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';

var assert = require('assert');
var express = require('express');
var http = require('http');
var startProxyTarget = require('./support/proxyTarget');
var proxy = require('../');

function chunkingProxyServer() {
  var proxyRouteFn = [{
    method: 'get',
    path: '/stream',
    fn: function (req, res) {
      res.write('0');
      setTimeout(function () { res.write('1'); }, 100);
      setTimeout(function () {  res.write('2'); }, 200);
      setTimeout(function () { res.write('3'); }, 300);
      setTimeout(function () { res.end(); }, 500);
    }
  }];

  return startProxyTarget(8309, 1000, proxyRouteFn);
}

function simulateUserRequest() {
  return new Promise(function (resolve, reject) {
    var req = http.request({ hostname: 'localhost', port: 8308, path: '/stream' }, function (res) {
      var chunks = [];
      res.on('data', function (chunk) { chunks.push(chunk.toString()); });
      res.on('end', function () { resolve(chunks); });
    });

    req.on('error', function (e) {
      reject('problem with request:', e.message);
    });

    req.end();
  });
}

function startLocalServer(proxyOptions) {
  var app = express();
  app.get('/stream', proxy('http://localhost:8309', proxyOptions));
  return app.listen(8308);
}

describe('streams / piped requests', function () {
  this.timeout(3000);

  var server;
  var  targetServer;

  beforeEach(function () {
    targetServer = chunkingProxyServer();
  });

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

  describe('when streaming options are truthy', function () {
    var TEST_CASES = [{
      name: 'vanilla, no options defined',
      options: {}
    }, {
      name: 'proxyReqOptDecorator is defined',
      options: { proxyReqOptDecorator: function (reqBuilder) { return reqBuilder; } }
    }, {
      //// Keep around this case for manually testing that this for sure fails for a few cycles.   2018 NMK
      //name: 'proxyReqOptDecorator never returns',
      //options: { proxyReqOptDecorator: function () { return new Promise(function () {}); } }
    //}, {

      name: 'proxyReqOptDecorator is a Promise',
      options: { proxyReqOptDecorator: function (reqBuilder) { return Promise.resolve(reqBuilder); } }
    }];

    TEST_CASES.forEach(function (testCase) {
      describe(testCase.name, function () {
        it('chunks are received without any buffering, e.g. before request end', function (done) {
          server = startLocalServer(testCase.options);
          simulateUserRequest()
            .then(function (res) {
              // Assume that if I'm getting a chunked response, it will be an array of length > 1;

              assert(res instanceof Array, 'res is an Array');
              assert.equal(res.length, 4);
              done();
            })
            .catch(done);
        });
      });
    });
  });

  describe('when streaming options are falsey', function () {
    var TEST_CASES = [{
      name: 'skipToNextHandler is defined',
      options: { skipToNextHandlerFilter: function () { return false; } }
    }];

    TEST_CASES.forEach(function (testCase) {
      describe(testCase.name, function () {
        it('response arrives in one large chunk', function (done) {
          server = startLocalServer(testCase.options);

          simulateUserRequest()
            .then(function (res) {
              // Assume that if I'm getting a un-chunked response, it will be an array of length = 1;

              assert(res instanceof Array);
              assert.equal(res.length, 1);
              done();
            })
            .catch(done);
        });
      });
    });
  });
});