File: test-httpModule.js

package info (click to toggle)
node-request 2.88.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 960 kB
  • sloc: javascript: 9,687; sh: 46; makefile: 2
file content (112 lines) | stat: -rw-r--r-- 3,056 bytes parent folder | download | duplicates (3)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict'

var http = require('http')
var https = require('https')
var destroyable = require('server-destroy')
var server = require('./server')
var request = require('../index')
var tape = require('tape')

var fauxRequestsMade

function clearFauxRequests () {
  fauxRequestsMade = { http: 0, https: 0 }
}

function wrapRequest (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] = function (/* options, callback */) {
        fauxRequestsMade[name] += 1
        return value.apply(this, arguments)
      }
    } else {
      wrapped[key] = value
    }
  })

  return wrapped
}

var fauxHTTP = wrapRequest('http', http)
var fauxHTTPS = wrapRequest('https', https)
var plainServer = server.createServer()
var httpsServer = server.createSSLServer()

destroyable(plainServer)
destroyable(httpsServer)

tape('setup', function (t) {
  plainServer.listen(0, function () {
    plainServer.on('/plain', function (req, res) {
      res.writeHead(200)
      res.end('plain')
    })
    plainServer.on('/to_https', function (req, res) {
      res.writeHead(301, { 'location': 'https://localhost:' + httpsServer.port + '/https' })
      res.end()
    })

    httpsServer.listen(0, function () {
      httpsServer.on('/https', function (req, res) {
        res.writeHead(200)
        res.end('https')
      })
      httpsServer.on('/to_plain', function (req, res) {
        res.writeHead(302, { 'location': 'http://localhost:' + plainServer.port + '/plain' })
        res.end()
      })

      t.end()
    })
  })
})

function runTests (name, httpModules) {
  tape(name, function (t) {
    var toHttps = 'http://localhost:' + plainServer.port + '/to_https'
    var toPlain = 'https://localhost:' + httpsServer.port + '/to_plain'
    var options = { httpModules: httpModules, strictSSL: false }
    var modulesTest = httpModules || {}

    clearFauxRequests()

    request(toHttps, options, function (err, res, body) {
      t.equal(err, null)
      t.equal(res.statusCode, 200)
      t.equal(body, 'https', 'Received HTTPS server body')

      t.equal(fauxRequestsMade.http, modulesTest['http:'] ? 1 : 0)
      t.equal(fauxRequestsMade.https, modulesTest['https:'] ? 1 : 0)

      request(toPlain, options, function (err, res, body) {
        t.equal(err, null)
        t.equal(res.statusCode, 200)
        t.equal(body, 'plain', 'Received HTTPS server body')

        t.equal(fauxRequestsMade.http, modulesTest['http:'] ? 2 : 0)
        t.equal(fauxRequestsMade.https, modulesTest['https:'] ? 2 : 0)

        t.end()
      })
    })
  })
}

runTests('undefined')
runTests('empty', {})
runTests('http only', { 'http:': fauxHTTP })
runTests('https only', { 'https:': fauxHTTPS })
runTests('http and https', { 'http:': fauxHTTP, 'https:': fauxHTTPS })

tape('cleanup', function (t) {
  plainServer.destroy(function () {
    httpsServer.destroy(function () {
      t.end()
    })
  })
})