Skip to content

Commit b42e72e

Browse files
cesinemonkpow
authored andcommitted
use nock to define more details about the request to the proxy
1 parent 1d3da8c commit b42e72e

File tree

3 files changed

+116
-29
lines changed

3 files changed

+116
-29
lines changed

package-lock.json

Lines changed: 59 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"eslint": "^4.19.1",
3535
"express": "^4.15.4",
3636
"mocha": "^5.2.0",
37-
"supertest": "^3.0.0"
37+
"nock": "^10.0.6",
38+
"supertest": "^3.4.2"
3839
},
3940
"dependencies": {
4041
"debug": "^3.0.1",

test/postBody.js

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,28 @@
22

33
var assert = require('assert');
44
var express = require('express');
5+
var nock = require('nock');
56
var request = require('supertest');
67
var proxy = require('../');
7-
var bodyParser = require('body-parser');
88

99

1010
function createLocalApplicationServer() {
1111
var app = express();
1212
return app;
1313
}
1414

15-
function createProxyApplicationServer() {
16-
var pTarget = express();
17-
pTarget.use(bodyParser.json());
18-
pTarget.use(bodyParser.urlencoded({ extended: true }));
19-
pTarget.use(function (req, res) {
20-
assert(req.body.name === 'tobi'); //, 'Assert that the value posted to the local server is passed to the proxy');
21-
res.json(req.body);
22-
});
23-
return pTarget.listen(12345);
24-
}
25-
2615
describe('when proxy request is a POST', function () {
2716

2817
this.timeout(10000);
2918

3019
var localServer;
31-
var proxyServer;
3220

3321
beforeEach(function () {
3422
localServer = createLocalApplicationServer();
35-
proxyServer = createProxyApplicationServer();
3623
});
3724

3825
afterEach(function () {
39-
proxyServer.close();
26+
nock.cleanAll();
4027
});
4128

4229
var testCases = [
@@ -46,6 +33,12 @@ describe('when proxy request is a POST', function () {
4633

4734
testCases.forEach(function (test) {
4835
it('should deliver the post body when ' + test.name, function (done) {
36+
var nockedPostWithEncoding = nock('http://127.0.0.1:12345')
37+
.post('/', { name: 'tobi' })
38+
.matchHeader('Content-Type', test.encoding)
39+
.reply(200, {
40+
name: 'tobi'
41+
});
4942

5043
localServer.use('/proxy', proxy('http://127.0.0.1:12345'));
5144
localServer.use(function (req, res) { res.sendStatus(200); });
@@ -57,9 +50,56 @@ describe('when proxy request is a POST', function () {
5750
.set('Content-Type', test.encoding)
5851
.expect(function (res) {
5952
assert(res.body.name === 'tobi');
53+
nockedPostWithEncoding.done();
6054
})
6155
.end(done);
6256
});
6357
});
6458

59+
it('should deliver empty string post body', function (done) {
60+
var nockedPostWithoutBody = nock('http://127.0.0.1:12345')
61+
.post('/', '')
62+
.matchHeader('Content-Type', 'application/json')
63+
.reply(200, {
64+
name: 'tobi'
65+
});
66+
67+
localServer.use('/proxy', proxy('http://127.0.0.1:12345'));
68+
localServer.use(function (req, res) { res.sendStatus(200); });
69+
localServer.use(function (err, req, res, next) { throw new Error(err, req, res, next); });
70+
71+
request(localServer)
72+
.post('/proxy')
73+
.send()
74+
.set('Content-Type', 'application/json')
75+
.expect(function (res) {
76+
assert(res.body.name === 'tobi');
77+
nockedPostWithoutBody.done();
78+
})
79+
.end(done);
80+
});
81+
82+
it('should deliver empty object post body', function (done) {
83+
var nockedPostWithoutBody = nock('http://127.0.0.1:12345')
84+
.post('/', {})
85+
.matchHeader('Content-Type', 'application/json')
86+
.reply(200, {
87+
name: 'tobi'
88+
});
89+
90+
localServer.use('/proxy', proxy('http://127.0.0.1:12345'));
91+
localServer.use(function (req, res) { res.sendStatus(200); });
92+
localServer.use(function (err, req, res, next) { throw new Error(err, req, res, next); });
93+
94+
request(localServer)
95+
.post('/proxy')
96+
.send({})
97+
.set('Content-Type', 'application/json')
98+
.expect(function (res) {
99+
assert(res.body.name === 'tobi');
100+
nockedPostWithoutBody.done();
101+
})
102+
.end(done);
103+
});
104+
65105
});

0 commit comments

Comments
 (0)