1 var http
= require('http')
2 , https
= require('https')
3 , server
= require('./server')
4 , assert
= require('assert')
5 , request
= require('../main.js')
8 var faux_requests_made
= {'http':0, 'https':0}
9 function wrap_request(name
, module
) {
10 // Just like the http or https module, but note when a request is made.
12 Object
.keys(module
).forEach(function(key
) {
13 var value
= module
[key
];
18 wrapped
[key
] = function(options
, callback
) {
19 faux_requests_made
[name
] += 1
20 return value
.apply(this, arguments
)
28 var faux_http
= wrap_request('http', http
)
29 , faux_https
= wrap_request('https', https
)
30 , plain_server
= server
.createServer()
31 , https_server
= server
.createSSLServer()
34 plain_server
.listen(plain_server
.port
, function() {
35 plain_server
.on('/plain', function (req
, res
) {
39 plain_server
.on('/to_https', function (req
, res
) {
40 res
.writeHead(301, {'location':'https://localhost:'+https_server
.port
+ '/https'})
44 https_server
.listen(https_server
.port
, function() {
45 https_server
.on('/https', function (req
, res
) {
49 https_server
.on('/to_plain', function (req
, res
) {
50 res
.writeHead(302, {'location':'http://localhost:'+plain_server
.port
+ '/plain'})
56 run_tests({'http:':faux_http
})
57 run_tests({'https:':faux_https
})
58 run_tests({'http:':faux_http
, 'https:':faux_https
})
62 function run_tests(httpModules
) {
63 var to_https
= 'http://localhost:'+plain_server
.port
+'/to_https'
64 var to_plain
= 'https://localhost:'+https_server
.port
+'/to_plain'
66 request(to_https
, {'httpModules':httpModules
}, function (er
, res
, body
) {
67 assert
.ok(!er
, 'Bounce to SSL worked')
68 assert
.equal(body
, 'https', 'Received HTTPS server body')
72 request(to_plain
, {'httpModules':httpModules
}, function (er
, res
, body
) {
73 assert
.ok(!er
, 'Bounce to plaintext server worked')
74 assert
.equal(body
, 'plain', 'Received HTTPS server body')
85 if(passed
== expected
) {
89 assert
.equal(faux_requests_made
.http
, 4, 'Wrapped http module called appropriately')
90 assert
.equal(faux_requests_made
.https
, 4, 'Wrapped https module called appropriately')
92 console
.log((expected
+2) + ' tests passed.')