You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

95 lines
2.5 KiB

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