@@ -15,6 +15,7 @@ var vows = require('vows'),
15
15
require . paths . unshift ( require ( 'path' ) . join ( __dirname , '../lib/' ) ) ;
16
16
17
17
var NodeProxy = require ( 'node-proxy' ) . NodeProxy ;
18
+ var testServers = { } ;
18
19
19
20
//
20
21
// Simple 'hello world' response for test purposes
@@ -28,47 +29,98 @@ var helloWorld = function(req, res) {
28
29
//
29
30
// Creates the reverse proxy server
30
31
//
31
- var startProxy = function ( server , port , proxy ) {
32
- http . createServer ( function ( req , res ) {
32
+ var startProxyServer = function ( server , port , proxy ) {
33
+ var proxyServer = http . createServer ( function ( req , res ) {
33
34
// Initialize the nodeProxy and start proxying the request
34
35
proxy . init ( req , res ) ;
35
36
proxy . proxyRequest ( server , port , req , res ) ;
36
- } ) . listen ( 8080 ) ;
37
+ } ) ;
38
+
39
+ proxyServer . listen ( 8080 ) ;
40
+ return proxyServer ;
41
+ } ;
42
+
43
+ //
44
+ // Creates the reverse proxy server with a specified latency
45
+ //
46
+ var startLatentProxyServer = function ( server , port , proxy , latency ) {
47
+ var proxyServer = http . createServer ( function ( req , res ) {
48
+ // Initialize the nodeProxy and start proxying the request
49
+ proxy . init ( req , res ) ;
50
+ setTimeout ( function ( ) {
51
+ proxy . proxyRequest ( server , port , req , res ) ;
52
+ } , latency ) ;
53
+ } ) ;
54
+
55
+ proxyServer . listen ( 8081 ) ;
56
+ return proxyServer ;
37
57
} ;
38
58
39
59
//
40
60
// Creates the 'hellonode' server
41
61
//
42
- var startProxyTarget = function ( ) {
43
- http . createServer ( function ( req , res ) {
62
+ var startTargetServer = function ( port ) {
63
+ var targetServer = http . createServer ( function ( req , res ) {
44
64
helloWorld ( req , res ) ;
45
- } ) . listen ( 8081 ) ;
65
+ } )
66
+
67
+ targetServer . listen ( port ) ;
68
+ return targetServer ;
46
69
} ;
47
70
48
71
//
49
- // The default test bootstrapper
72
+ // The default test bootstrapper with no latency
50
73
//
51
- var startProxyTest = function ( ) {
52
- var proxy = new ( NodeProxy ) ;
53
- startProxy ( '127.0.0.1' , 8081 , proxy ) ;
54
- startProxyTarget ( ) ;
55
- return proxy ;
74
+ var startTest = function ( proxy , port ) {
75
+ testServers . noLatency = [ ] ;
76
+ testServers . noLatency . push ( startProxyServer ( '127.0.0.1' , port , proxy ) ) ;
77
+ testServers . noLatency . push ( startTargetServer ( port ) ) ;
56
78
} ;
57
79
80
+ //
81
+ // The test bootstrapper with some latency
82
+ //
83
+ var startTestWithLatency = function ( proxy , port ) {
84
+ testServers . latency = [ ] ;
85
+ testServers . latency . push ( startLatentProxyServer ( '127.0.0.1' , port , proxy , 2000 ) ) ;
86
+ testServers . latency . push ( startTargetServer ( port ) ) ;
87
+ } ;
58
88
59
89
vows . describe ( 'node-proxy' ) . addBatch ( {
60
90
"When an incoming request is proxied to the helloNode server" : {
61
- topic : function ( ) {
62
- // Create the proxy and start listening
63
- var proxy = startProxyTest ( ) ;
64
- proxy . emitter . addListener ( 'end' , this . callback ) ;
91
+ "with no latency" : {
92
+ topic : function ( ) {
93
+ var proxy = new ( NodeProxy ) ;
94
+ startTest ( proxy , 8082 ) ;
95
+ proxy . emitter . addListener ( 'end' , this . callback ) ;
65
96
66
- var client = http . createClient ( 8080 , '127.0.0.1' ) ;
67
- var request = client . request ( 'GET' , '/' ) ;
68
- request . end ( ) ;
97
+ var client = http . createClient ( 8080 , '127.0.0.1' ) ;
98
+ var request = client . request ( 'GET' , '/' ) ;
99
+ request . end ( ) ;
100
+ } ,
101
+ "it should received 'hello world'" : function ( err , body ) {
102
+ assert . equal ( body , 'hello world' ) ;
103
+ testServers . noLatency . forEach ( function ( server ) {
104
+ server . close ( ) ;
105
+ } )
106
+ }
69
107
} ,
70
- "it should received 'hello world'" : function ( err , body ) {
71
- assert . equal ( body , 'hello world' ) ;
108
+ "with latency" : {
109
+ topic : function ( ) {
110
+ var proxy = new ( NodeProxy ) ;
111
+ startTestWithLatency ( proxy , 8083 ) ;
112
+ proxy . emitter . addListener ( 'end' , this . callback ) ;
113
+
114
+ var client = http . createClient ( 8081 , '127.0.0.1' ) ;
115
+ var request = client . request ( 'GET' , '/' ) ;
116
+ request . end ( ) ;
117
+ } ,
118
+ "it should receive 'hello world'" : function ( err , body ) {
119
+ assert . equal ( body , 'hello world' ) ;
120
+ testServers . latency . forEach ( function ( server ) {
121
+ server . close ( ) ;
122
+ } )
123
+ }
72
124
}
73
125
}
74
126
} ) . export ( module ) ;
0 commit comments