1 | var http = require('http') |
---|
2 | , https = require('https') |
---|
3 | , server = require('./server') |
---|
4 | , assert = require('assert') |
---|
5 | , request = require('../index') |
---|
6 | |
---|
7 | |
---|
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. |
---|
11 | var wrapped = {} |
---|
12 | Object.keys(module).forEach(function(key) { |
---|
13 | var value = module[key]; |
---|
14 | |
---|
15 | if(key != 'request') |
---|
16 | wrapped[key] = value; |
---|
17 | else |
---|
18 | wrapped[key] = function(options, callback) { |
---|
19 | faux_requests_made[name] += 1 |
---|
20 | return value.apply(this, arguments) |
---|
21 | } |
---|
22 | }) |
---|
23 | |
---|
24 | return wrapped; |
---|
25 | } |
---|
26 | |
---|
27 | |
---|
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() |
---|
32 | |
---|
33 | |
---|
34 | plain_server.listen(plain_server.port, function() { |
---|
35 | plain_server.on('/plain', function (req, res) { |
---|
36 | res.writeHead(200) |
---|
37 | res.end('plain') |
---|
38 | }) |
---|
39 | plain_server.on('/to_https', function (req, res) { |
---|
40 | res.writeHead(301, {'location':'https://localhost:'+https_server.port + '/https'}) |
---|
41 | res.end() |
---|
42 | }) |
---|
43 | |
---|
44 | https_server.listen(https_server.port, function() { |
---|
45 | https_server.on('/https', function (req, res) { |
---|
46 | res.writeHead(200) |
---|
47 | res.end('https') |
---|
48 | }) |
---|
49 | https_server.on('/to_plain', function (req, res) { |
---|
50 | res.writeHead(302, {'location':'http://localhost:'+plain_server.port + '/plain'}) |
---|
51 | res.end() |
---|
52 | }) |
---|
53 | |
---|
54 | run_tests() |
---|
55 | run_tests({}) |
---|
56 | run_tests({'http:':faux_http}) |
---|
57 | run_tests({'https:':faux_https}) |
---|
58 | run_tests({'http:':faux_http, 'https:':faux_https}) |
---|
59 | }) |
---|
60 | }) |
---|
61 | |
---|
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' |
---|
65 | |
---|
66 | request(to_https, {'httpModules':httpModules, strictSSL:false}, function (er, res, body) { |
---|
67 | if (er) throw er |
---|
68 | assert.equal(body, 'https', 'Received HTTPS server body') |
---|
69 | done() |
---|
70 | }) |
---|
71 | |
---|
72 | request(to_plain, {'httpModules':httpModules, strictSSL:false}, function (er, res, body) { |
---|
73 | if (er) throw er |
---|
74 | assert.equal(body, 'plain', 'Received HTTPS server body') |
---|
75 | done() |
---|
76 | }) |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | var passed = 0; |
---|
81 | function done() { |
---|
82 | passed += 1 |
---|
83 | var expected = 10 |
---|
84 | |
---|
85 | if(passed == expected) { |
---|
86 | plain_server.close() |
---|
87 | https_server.close() |
---|
88 | |
---|
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') |
---|
91 | |
---|
92 | console.log((expected+2) + ' tests passed.') |
---|
93 | } |
---|
94 | } |
---|