1
+ var httpProxy = require ( '../lib/http-proxy' ) ,
2
+ expect = require ( 'expect.js' ) ,
3
+ http = require ( 'http' )
4
+ https = require ( 'https' ) ,
5
+ path = require ( 'path' ) ,
6
+ fs = require ( 'fs' ) ;
7
+
8
+ //
9
+ // Expose a port number generator.
10
+ // thanks to @3rd-Eden
11
+ //
12
+ var initialPort = 1024 , gen = { } ;
13
+ Object . defineProperty ( gen , 'port' , {
14
+ get : function get ( ) {
15
+ return initialPort ++ ;
16
+ }
17
+ } ) ;
18
+
19
+ describe ( 'lib/http-proxy.js' , function ( ) {
20
+ describe ( '#createProxyServer using HTTPS' , function ( ) {
21
+ describe ( 'HTTPS to HTTP' , function ( ) {
22
+ it ( 'should proxy the request en send back the response' , function ( done ) {
23
+ var ports = { source : gen . port , proxy : gen . port } ;
24
+ var source = http . createServer ( function ( req , res ) {
25
+ console . log ( 'Request:' , req . headers ) ;
26
+ expect ( req . method ) . to . eql ( 'GET' ) ;
27
+ expect ( req . headers . host . split ( ':' ) [ 1 ] ) . to . eql ( ports . proxy ) ;
28
+ res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
29
+ res . end ( 'Hello from ' + ports . source ) ;
30
+ } ) ;
31
+
32
+ source . listen ( ports . source ) ;
33
+
34
+ var proxy = httpProxy . createProxyServer ( {
35
+ forward : 'http://127.0.0.1:' + ports . source ,
36
+ ssl : {
37
+ key : fs . readFileSync ( path . join ( __dirname , 'fixtures' , 'agent2-key.pem' ) ) ,
38
+ cert : fs . readFileSync ( path . join ( __dirname , 'fixtures' , 'agent2-cert.pem' ) ) ,
39
+ }
40
+ } ) . listen ( ports . proxy ) ;
41
+
42
+ var req = https . request ( {
43
+ host : 'localhost' ,
44
+ port : ports . proxy ,
45
+ path : '/' ,
46
+ method : 'GET' ,
47
+ localAddress : '127.0.0.1' ,
48
+ rejectUnauthorized : false
49
+ } , function ( res ) {
50
+ console . log ( res ) ;
51
+ res . on ( 'data' , function ( ch ) {
52
+ console . log ( 'Chunks' , ch )
53
+ } )
54
+ console . log ( 'Response:' , res . statusCode ) ;
55
+ source . close ( ) ;
56
+ proxy . _server . close ( ) ;
57
+ done ( ) ;
58
+ } ) ;
59
+
60
+ req . on ( 'error' , function ( err ) { console . log ( 'Erroring' , err ) ; } ) ;
61
+ req . end ( ) ;
62
+ } )
63
+ } )
64
+ } ) ;
65
+ } ) ;
0 commit comments