Skip to content

Commit ccc6352

Browse files
cesinemonkpow
authored andcommitted
use body-parser to show a case where the body is passed as {}
when the original is an empty string
1 parent fe9b410 commit ccc6352

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

test/getBody.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var assert = require('assert');
4+
var bodyParser = require('body-parser');
45
var express = require('express');
56
var nock = require('nock');
67
var request = require('supertest');
@@ -20,6 +21,7 @@ describe('when proxy request is a GET', function () {
2021

2122
beforeEach(function () {
2223
localServer = createLocalApplicationServer();
24+
localServer.use(bodyParser.json());
2325
});
2426

2527
afterEach(function () {
@@ -58,7 +60,7 @@ describe('when proxy request is a GET', function () {
5860

5961
it('should deliver the get body when ' + test.name, function (done) {
6062
var nockedPostWithEncoding = nock('http://127.0.0.1:12345')
61-
.get('/', { name: 'tobi' })
63+
.get('/', test.encoding.includes('json') ? { name: 'tobi' } : '')
6264
.matchHeader('Content-Type', test.encoding)
6365
.reply(200, {
6466
name: 'tobi'
@@ -82,7 +84,7 @@ describe('when proxy request is a GET', function () {
8284

8385
it('should deliver empty string get body', function (done) {
8486
var nockedPostWithoutBody = nock('http://127.0.0.1:12345')
85-
.get('/', '')
87+
.get('/')
8688
.matchHeader('Content-Type', 'application/json')
8789
.reply(200, {
8890
name: 'get with string body'
@@ -94,7 +96,7 @@ describe('when proxy request is a GET', function () {
9496

9597
request(localServer)
9698
.get('/proxy')
97-
.send()
99+
.send('')
98100
.set('Content-Type', 'application/json')
99101
.expect(function (res) {
100102
assert(res.body.name === 'get with string body');
@@ -126,4 +128,31 @@ describe('when proxy request is a GET', function () {
126128
.end(done);
127129
});
128130

131+
it('should support parseReqBody', function (done) {
132+
var nockedPostWithBody = nock('http://127.0.0.1:12345')
133+
.get('/', '')
134+
.matchHeader('Content-Type', 'application/json')
135+
.reply(200, {
136+
name: 'get with parseReqBody false'
137+
});
138+
139+
localServer.use('/proxy', proxy('http://127.0.0.1:12345', {
140+
parseReqBody: false,
141+
}));
142+
localServer.use(function (req, res) { res.sendStatus(200); });
143+
localServer.use(function (err, req, res, next) { throw new Error(err, req, res, next); });
144+
145+
request(localServer)
146+
.get('/proxy')
147+
.send({
148+
name: 'tobi'
149+
})
150+
.set('Content-Type', 'application/json')
151+
.expect(function (res) {
152+
assert(res.body.name === 'get with parseReqBody false');
153+
nockedPostWithBody.done();
154+
})
155+
.end(done);
156+
});
157+
129158
});

test/postBody.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var assert = require('assert');
4+
var bodyParser = require('body-parser');
45
var express = require('express');
56
var nock = require('nock');
67
var request = require('supertest');
@@ -20,6 +21,7 @@ describe('when proxy request is a POST', function () {
2021

2122
beforeEach(function () {
2223
localServer = createLocalApplicationServer();
24+
localServer.use(bodyParser.json());
2325
});
2426

2527
afterEach(function () {
@@ -32,9 +34,33 @@ describe('when proxy request is a POST', function () {
3234
];
3335

3436
testCases.forEach(function (test) {
37+
it('should deliver the post query when ' + test.name, function (done) {
38+
var nockedPostWithEncoding = nock('http://127.0.0.1:12345')
39+
.post('/')
40+
.query({ name: 'tobi' })
41+
.matchHeader('Content-Type', test.encoding)
42+
.reply(200, {
43+
name: 'tobi'
44+
});
45+
46+
localServer.use('/proxy', proxy('http://127.0.0.1:12345'));
47+
localServer.use(function (req, res) { res.sendStatus(200); });
48+
localServer.use(function (err, req, res, next) { throw new Error(err, req, res, next); });
49+
50+
request(localServer)
51+
.post('/proxy')
52+
.query({ name: 'tobi' })
53+
.set('Content-Type', test.encoding)
54+
.expect(function (res) {
55+
assert(res.body.name === 'tobi');
56+
nockedPostWithEncoding.done();
57+
})
58+
.end(done);
59+
});
60+
3561
it('should deliver the post body when ' + test.name, function (done) {
3662
var nockedPostWithEncoding = nock('http://127.0.0.1:12345')
37-
.post('/', { name: 'tobi' })
63+
.post('/', test.encoding.includes('json') ? { name: 'tobi' } : {})
3864
.matchHeader('Content-Type', test.encoding)
3965
.reply(200, {
4066
name: 'tobi'
@@ -56,9 +82,9 @@ describe('when proxy request is a POST', function () {
5682
});
5783
});
5884

59-
it('should deliver empty string post body', function (done) {
85+
it.only('should deliver empty string post body', function (done) {
6086
var nockedPostWithoutBody = nock('http://127.0.0.1:12345')
61-
.post('/', '')
87+
.post('/')
6288
.matchHeader('Content-Type', 'application/json')
6389
.reply(200, {
6490
name: 'tobi'
@@ -70,7 +96,7 @@ describe('when proxy request is a POST', function () {
7096

7197
request(localServer)
7298
.post('/proxy')
73-
.send()
99+
.send('')
74100
.set('Content-Type', 'application/json')
75101
.expect(function (res) {
76102
assert(res.body.name === 'tobi');

0 commit comments

Comments
 (0)