2d19b02a0454876f0d40fc2f2d34d8fbd2c8ed0a.svn-base
2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var http = require('http')
, https = require('https')
, server = require('./server')
, assert = require('assert')
, request = require('../main.js')
var faux_requests_made = {'http':0, 'https':0}
function wrap_request(name, module) {
// Just like the http or https module, but note when a request is made.
var wrapped = {}
Object.keys(module).forEach(function(key) {
var value = module[key];
if(key != 'request')
wrapped[key] = value;
else
wrapped[key] = function(options, callback) {
faux_requests_made[name] += 1
return value.apply(this, arguments)
}
})
return wrapped;
}
var faux_http = wrap_request('http', http)
, faux_https = wrap_request('https', https)
, plain_server = server.createServer()
, https_server = server.createSSLServer()
plain_server.listen(plain_server.port, function() {
plain_server.on('/plain', function (req, res) {
res.writeHead(200)
res.end('plain')
})
plain_server.on('/to_https', function (req, res) {
res.writeHead(301, {'location':'https://localhost:'+https_server.port + '/https'})
res.end()
})
https_server.listen(https_server.port, function() {
https_server.on('/https', function (req, res) {
res.writeHead(200)
res.end('https')
})
https_server.on('/to_plain', function (req, res) {
res.writeHead(302, {'location':'http://localhost:'+plain_server.port + '/plain'})
res.end()
})
run_tests()
run_tests({})
run_tests({'http:':faux_http})
run_tests({'https:':faux_https})
run_tests({'http:':faux_http, 'https:':faux_https})
})
})
function run_tests(httpModules) {
var to_https = 'http://localhost:'+plain_server.port+'/to_https'
var to_plain = 'https://localhost:'+https_server.port+'/to_plain'
request(to_https, {'httpModules':httpModules}, function (er, res, body) {
assert.ok(!er, 'Bounce to SSL worked')
assert.equal(body, 'https', 'Received HTTPS server body')
done()
})
request(to_plain, {'httpModules':httpModules}, function (er, res, body) {
assert.ok(!er, 'Bounce to plaintext server worked')
assert.equal(body, 'plain', 'Received HTTPS server body')
done()
})
}
var passed = 0;
function done() {
passed += 1
var expected = 10
if(passed == expected) {
plain_server.close()
https_server.close()
assert.equal(faux_requests_made.http, 4, 'Wrapped http module called appropriately')
assert.equal(faux_requests_made.https, 4, 'Wrapped https module called appropriately')
console.log((expected+2) + ' tests passed.')
}
}