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

var assert = require('assert');
var express = require('express');
var request = require('supertest');
var bodyParser = require('body-parser');
var proxy = require('../');
var proxyTarget = require('../test/support/proxyTarget');


var proxyRouteFn = [{
  method: 'post',
  path: '/poster',
  fn: function (req, res) {
    res.send(req.body);
  }
}];

describe('middleware compatibility', function () {
  var proxyServer;

  beforeEach(function () {
    proxyServer = proxyTarget(12346, 100, proxyRouteFn);
  });

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

  it('should use req.body if defined', function (done) {
    var app = express();

    // Simulate another middleware that puts req stream into the body

    app.use(function (req, res, next) {
      var received = [];
      req.on('data', function onData(chunk) {
        if (!chunk) { return; }
        received.push(chunk);
      });
      req.on('end', function onEnd() {
        received = Buffer.concat(received).toString('utf8');
        req.body = JSON.parse(received);
        req.body.foo = 1;
        next();
      });
    });

    app.use(proxy('localhost:12346', {
      userResDecorator: function (rsp, data, req) {
        assert(req.body);
        assert.equal(req.body.foo, 1);
        assert.equal(req.body.mypost, 'hello');
        return data;
      }
    }));

    request(app)
      .post('/poster')
      .send({ mypost: 'hello' })
      .expect(function (res) {
        assert.equal(res.body.foo, 1);
        assert.equal(res.body.mypost, 'hello');
      })
      .end(done);
  });

  it('should stringify req.body when it is a json body so it is written to proxy request', function (done) {
    var app = express();
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({
      extended: false
    }));
    app.use(proxy('localhost:12346'));
    request(app)
      .post('/poster')
      .send({
        mypost: 'hello',
        doorknob: 'wrect'
      })
      .expect(function (res) {
        assert.equal(res.body.doorknob, 'wrect');
        assert.equal(res.body.mypost, 'hello');
      })
      .end(done);
  });

  it('should convert req.body to a Buffer when reqAsBuffer is set', function (done) {
    var app = express();
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({
      extended: false
    }));
    app.use(proxy('localhost:12346', {
      reqAsBuffer: true
    }));
    request(app)
      .post('/poster')
      .send({
        mypost: 'hello',
        doorknob: 'wrect'
      })
      .expect(function (res) {
        assert.equal(res.body.doorknob, 'wrect');
        assert.equal(res.body.mypost, 'hello');
      })
      .end(done);
  });

});