2
2
3
3
var assert = require ( 'assert' ) ;
4
4
var express = require ( 'express' ) ;
5
+ var nock = require ( 'nock' ) ;
5
6
var request = require ( 'supertest' ) ;
6
7
var proxy = require ( '../' ) ;
7
- var bodyParser = require ( 'body-parser' ) ;
8
8
9
9
10
10
function createLocalApplicationServer ( ) {
11
11
var app = express ( ) ;
12
12
return app ;
13
13
}
14
14
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
-
26
15
describe ( 'when proxy request is a POST' , function ( ) {
27
16
28
17
this . timeout ( 10000 ) ;
29
18
30
19
var localServer ;
31
- var proxyServer ;
32
20
33
21
beforeEach ( function ( ) {
34
22
localServer = createLocalApplicationServer ( ) ;
35
- proxyServer = createProxyApplicationServer ( ) ;
36
23
} ) ;
37
24
38
25
afterEach ( function ( ) {
39
- proxyServer . close ( ) ;
26
+ nock . cleanAll ( ) ;
40
27
} ) ;
41
28
42
29
var testCases = [
@@ -46,6 +33,12 @@ describe('when proxy request is a POST', function () {
46
33
47
34
testCases . forEach ( function ( test ) {
48
35
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
+ } ) ;
49
42
50
43
localServer . use ( '/proxy' , proxy ( 'http://127.0.0.1:12345' ) ) ;
51
44
localServer . use ( function ( req , res ) { res . sendStatus ( 200 ) ; } ) ;
@@ -57,9 +50,56 @@ describe('when proxy request is a POST', function () {
57
50
. set ( 'Content-Type' , test . encoding )
58
51
. expect ( function ( res ) {
59
52
assert ( res . body . name === 'tobi' ) ;
53
+ nockedPostWithEncoding . done ( ) ;
60
54
} )
61
55
. end ( done ) ;
62
56
} ) ;
63
57
} ) ;
64
58
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
+
65
105
} ) ;
0 commit comments