1
+ /*
2
+ node-http-proxy-test.js: http proxy for node.js
3
+
4
+ Copyright (c) 2010 Charlie Robbins & Marak Squires http://github.com/nodejitsu/node-http-proxy
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+ */
26
+
27
+ var vows = require ( 'vows' ) ,
28
+ sys = require ( 'sys' ) ,
29
+ assert = require ( 'assert' ) ,
30
+ http = require ( 'http' ) ;
31
+
32
+ var httpProxy = require ( './../lib/node-http-proxy' ) ;
33
+ var testServers = { } ;
34
+
35
+ //
36
+ // Creates the reverse proxy server
37
+ //
38
+ var startProxyServer = function ( port , server ) {
39
+ var proxyServer = httpProxy . createServer ( port , server ) ;
40
+ proxyServer . listen ( 8080 ) ;
41
+ return proxyServer ;
42
+ } ;
43
+
44
+ //
45
+ // Creates the reverse proxy server with a specified latency
46
+ //
47
+ var startLatentProxyServer = function ( port , server , latency ) {
48
+ // Initialize the nodeProxy and start proxying the request
49
+ var proxyServer = httpProxy . createServer ( function ( req , res , proxy ) {
50
+ setTimeout ( function ( ) {
51
+ proxy . proxyRequest ( port , server ) ;
52
+ } , latency ) ;
53
+ } ) ;
54
+
55
+ proxyServer . listen ( 8081 ) ;
56
+ return proxyServer ;
57
+ } ;
58
+
59
+ //
60
+ // Creates the 'hellonode' server
61
+ //
62
+ var startTargetServer = function ( port ) {
63
+ var targetServer = http . createServer ( function ( req , res ) {
64
+ res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
65
+ res . write ( 'hello world' )
66
+ res . end ( ) ;
67
+ } ) ;
68
+
69
+ targetServer . listen ( port ) ;
70
+ return targetServer ;
71
+ } ;
72
+
73
+ //
74
+ // The default test bootstrapper with no latency
75
+ //
76
+ var startTest = function ( port ) {
77
+ var proxyServer = startProxyServer ( port , 'localhost' ) ,
78
+ targetServer = startTargetServer ( port + 1000 ) ;
79
+
80
+ testServers . noLatency = [ ] ;
81
+ testServers . noLatency . push ( proxyServer ) ;
82
+ testServers . noLatency . push ( targetServer ) ;
83
+
84
+ return proxyServer ;
85
+ } ;
86
+
87
+ //
88
+ // The test bootstrapper with some latency
89
+ //
90
+ var startTestWithLatency = function ( port ) {
91
+ var proxyServer = startLatentProxyServer ( port , 'localhost' , 2000 ) ,
92
+ targetServer = startTargetServer ( port ) ;
93
+
94
+ testServers . latency = [ ] ;
95
+ testServers . latency . push ( proxyServer ) ;
96
+ testServers . latency . push ( targetServer ) ;
97
+
98
+ return proxyServer ;
99
+ } ;
100
+
101
+ vows . describe ( 'node-http-proxy' ) . addBatch ( {
102
+ "A node-http-proxy" : {
103
+ "when instantiated directly" : {
104
+ "and an incoming request is proxied to the helloNode server" : {
105
+ "with no latency" : {
106
+ topic : function ( ) {
107
+ var proxy = startTest ( 8082 ) ;
108
+ proxy . addListener ( 'proxy' , this . callback ) ;
109
+
110
+ var client = http . createClient ( 8080 , 'localhost' ) ;
111
+ var request = client . request ( 'GET' , '/' ) ;
112
+ request . end ( ) ;
113
+ } ,
114
+ "it should received 'hello world'" : function ( err , body ) {
115
+ assert . equal ( body , 'hello world' ) ;
116
+ testServers . noLatency . forEach ( function ( server ) {
117
+ server . close ( ) ;
118
+ } )
119
+ }
120
+ } ,
121
+ "with latency" : {
122
+ topic : function ( ) {
123
+ var proxy = startTestWithLatency ( 8083 ) ;
124
+ proxy . addListener ( 'proxy' , this . callback ) ;
125
+
126
+ var client = http . createClient ( 8081 , 'localhost' ) ;
127
+ var request = client . request ( 'GET' , '/' ) ;
128
+ request . end ( ) ;
129
+ } ,
130
+ "it should receive 'hello world'" : function ( err , body ) {
131
+ assert . equal ( body , 'hello world' ) ;
132
+ testServers . latency . forEach ( function ( server ) {
133
+ server . close ( ) ;
134
+ } )
135
+ }
136
+ }
137
+ }
138
+ }
139
+ }
140
+ } ) . export ( module ) ;
0 commit comments