1
1
var caronte = require ( '../lib/caronte' ) ,
2
- expect = require ( 'expect.js' ) ;
2
+ expect = require ( 'expect.js' ) ,
3
+ http = require ( 'http' ) ;
3
4
4
5
describe ( 'lib/caronte.js' , function ( ) {
5
6
describe ( '#createProxyServer' , function ( ) {
@@ -24,4 +25,155 @@ describe('lib/caronte.js', function() {
24
25
expect ( obj . listen ) . to . be . a ( Function ) ;
25
26
} ) ;
26
27
} ) ;
28
+
29
+ describe ( '#createProxyServer with forward options and using web-incoming passes' , function ( ) {
30
+ it ( 'should pipe the request using web-incoming#stream method' , function ( done ) {
31
+ var proxy = caronte . createProxyServer ( {
32
+ forward : 'http://127.0.0.1:8080'
33
+ } ) . listen ( '8081' )
34
+
35
+ var source = http . createServer ( function ( req , res ) {
36
+ expect ( req . method ) . to . eql ( 'GET' ) ;
37
+ expect ( req . headers . host . split ( ':' ) [ 1 ] ) . to . eql ( '8081' ) ;
38
+ source . close ( ) ;
39
+ proxy . close ( ) ;
40
+ done ( ) ;
41
+ } ) ;
42
+
43
+ source . listen ( '8080' ) ;
44
+
45
+ http . request ( 'http://127.0.0.1:8081' , function ( ) { } ) . end ( ) ;
46
+ } )
47
+ } ) ;
48
+
49
+ describe ( '#createProxyServer using the web-incoming passes' , function ( ) {
50
+ it ( 'should make the request on pipe and finish it' , function ( done ) {
51
+ var proxy = caronte . createProxyServer ( {
52
+ target : 'http://127.0.0.1:8080'
53
+ } ) . listen ( '8081' ) ;
54
+
55
+ var source = http . createServer ( function ( req , res ) {
56
+ expect ( req . method ) . to . eql ( 'POST' ) ;
57
+ expect ( req . headers [ 'x-forwarded-for' ] ) . to . eql ( '127.0.0.1' ) ;
58
+ expect ( req . headers . host . split ( ':' ) [ 1 ] ) . to . eql ( '8081' ) ;
59
+ source . close ( ) ;
60
+ proxy . close ( ) ;
61
+ done ( ) ;
62
+ } ) ;
63
+
64
+ source . listen ( '8080' ) ;
65
+
66
+ http . request ( {
67
+ hostname : '127.0.0.1' ,
68
+ port : '8081' ,
69
+ method : 'POST' ,
70
+ headers : {
71
+ 'x-forwarded-for' : '127.0.0.1'
72
+ }
73
+ } , function ( ) { } ) . end ( ) ;
74
+ } ) ;
75
+ } ) ;
76
+
77
+ describe ( '#createProxyServer using the web-incoming passes' , function ( ) {
78
+ it ( 'should make the request, handle response and finish it' , function ( done ) {
79
+ var proxy = caronte . createProxyServer ( {
80
+ target : 'http://127.0.0.1:8080'
81
+ } ) . listen ( '8081' ) ;
82
+
83
+ var source = http . createServer ( function ( req , res ) {
84
+ expect ( req . method ) . to . eql ( 'GET' ) ;
85
+ expect ( req . headers . host . split ( ':' ) [ 1 ] ) . to . eql ( '8081' ) ;
86
+ res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } )
87
+ res . end ( 'Hello from ' + source . address ( ) . port ) ;
88
+ } ) ;
89
+
90
+ source . listen ( '8080' ) ;
91
+
92
+ http . request ( {
93
+ hostname : '127.0.0.1' ,
94
+ port : '8081' ,
95
+ method : 'GET' ,
96
+ } , function ( res ) {
97
+ expect ( res . statusCode ) . to . eql ( 200 ) ;
98
+
99
+ res . on ( 'data' , function ( data ) {
100
+ expect ( data . toString ( ) ) . to . eql ( 'Hello from 8080' ) ;
101
+ } ) ;
102
+
103
+ res . on ( 'end' , function ( ) {
104
+ source . close ( ) ;
105
+ proxy . close ( ) ;
106
+ done ( ) ;
107
+ } ) ;
108
+ } ) . end ( ) ;
109
+ } ) ;
110
+ } ) ;
111
+
112
+ describe ( '#createProxyServer() method with error response' , function ( ) {
113
+ it ( 'should make the request and emit the error event' , function ( done ) {
114
+ var proxy = caronte . createProxyServer ( {
115
+ target : 'http://127.0.0.1:8080'
116
+ } ) ;
117
+
118
+ proxy . ee . on ( 'caronte:outgoing:web:error' , function ( err ) {
119
+ expect ( err ) . to . be . an ( Error ) ;
120
+ expect ( err . code ) . to . be ( 'ECONNREFUSED' ) ;
121
+ proxyServer . close ( ) ;
122
+ done ( ) ;
123
+ } )
124
+
125
+ var proxyServer = proxy . listen ( '8081' ) ;
126
+
127
+ http . request ( {
128
+ hostname : '127.0.0.1' ,
129
+ port : '8081' ,
130
+ method : 'GET' ,
131
+ } , function ( ) { } ) . end ( ) ;
132
+ } ) ;
133
+ } ) ;
134
+
135
+ describe ( '#createProxyServer using the web-incoming passes' , function ( ) {
136
+ it ( 'should emit events correclty' , function ( done ) {
137
+ var proxy = caronte . createProxyServer ( {
138
+ target : 'http://127.0.0.1:8080'
139
+ } ) ,
140
+
141
+ proxyServer = proxy . listen ( '8081' ) ,
142
+
143
+ source = http . createServer ( function ( req , res ) {
144
+ expect ( req . method ) . to . eql ( 'GET' ) ;
145
+ expect ( req . headers . host . split ( ':' ) [ 1 ] ) . to . eql ( '8081' ) ;
146
+ res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } )
147
+ res . end ( 'Hello from ' + source . address ( ) . port ) ;
148
+ } ) ,
149
+
150
+ events = [ ] ;
151
+
152
+ source . listen ( '8080' ) ;
153
+
154
+ proxy . ee . on ( 'caronte:**' , function ( uno , dos , tres ) {
155
+ events . push ( this . event ) ;
156
+ } )
157
+
158
+ http . request ( {
159
+ hostname : '127.0.0.1' ,
160
+ port : '8081' ,
161
+ method : 'GET' ,
162
+ } , function ( res ) {
163
+ expect ( res . statusCode ) . to . eql ( 200 ) ;
164
+
165
+ res . on ( 'data' , function ( data ) {
166
+ expect ( data . toString ( ) ) . to . eql ( 'Hello from 8080' ) ;
167
+ } ) ;
168
+
169
+ res . on ( 'end' , function ( ) {
170
+ expect ( events ) . to . contain ( 'caronte:outgoing:web:begin' ) ;
171
+ expect ( events ) . to . contain ( 'caronte:outgoing:web:end' ) ;
172
+ source . close ( ) ;
173
+ proxyServer . close ( ) ;
174
+ done ( ) ;
175
+ } ) ;
176
+ } ) . end ( ) ;
177
+ } ) ;
178
+ } ) ;
27
179
} ) ;
0 commit comments