File: test-redirect-auth.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 (131 lines) | stat: -rw-r--r-- 3,428 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict'

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

var s = server.createServer()
var ss = server.createSSLServer()

destroyable(s)
destroyable(ss)

// always send basic auth and allow non-strict SSL
request = request.defaults({
  auth: {
    user: 'test',
    pass: 'testing'
  },
  rejectUnauthorized: false
})

// redirect.from(proto, host).to(proto, host) returns an object with keys:
//   src : source URL
//   dst : destination URL
var redirect = {
  from: function (fromProto, fromHost) {
    return {
      to: function (toProto, toHost) {
        var fromPort = (fromProto === 'http' ? s.port : ss.port)
        var toPort = (toProto === 'http' ? s.port : ss.port)
        return {
          src: util.format(
            '%s://%s:%d/to/%s/%s',
            fromProto, fromHost, fromPort, toProto, toHost),
          dst: util.format(
            '%s://%s:%d/from/%s/%s',
            toProto, toHost, toPort, fromProto, fromHost)
        }
      }
    }
  }
}

function handleRequests (srv) {
  ['http', 'https'].forEach(function (proto) {
    ['localhost', '127.0.0.1'].forEach(function (host) {
      srv.on(util.format('/to/%s/%s', proto, host), function (req, res) {
        var r = redirect
          .from(srv.protocol, req.headers.host.split(':')[0])
          .to(proto, host)
        res.writeHead(301, {
          location: r.dst
        })
        res.end()
      })

      srv.on(util.format('/from/%s/%s', proto, host), function (req, res) {
        res.end('auth: ' + (req.headers.authorization || '(nothing)'))
      })
    })
  })
}

handleRequests(s)
handleRequests(ss)

function runTest (name, redir, expectAuth) {
  tape('redirect to ' + name, function (t) {
    request(redir.src, function (err, res, body) {
      t.equal(err, null)
      t.equal(res.request.uri.href, redir.dst)
      t.equal(res.statusCode, 200)
      t.equal(body, expectAuth
        ? 'auth: Basic dGVzdDp0ZXN0aW5n'
        : 'auth: (nothing)')
      t.end()
    })
  })
}

function addTests () {
  runTest('same host and protocol',
    redirect.from('http', 'localhost').to('http', 'localhost'),
    true)

  runTest('same host different protocol',
    redirect.from('http', 'localhost').to('https', 'localhost'),
    true)

  runTest('different host same protocol',
    redirect.from('https', '127.0.0.1').to('https', 'localhost'),
    false)

  runTest('different host and protocol',
    redirect.from('http', 'localhost').to('https', '127.0.0.1'),
    false)
}

tape('setup', function (t) {
  s.listen(0, function () {
    ss.listen(0, function () {
      addTests()
      tape('cleanup', function (t) {
        s.destroy(function () {
          ss.destroy(function () {
            t.end()
          })
        })
      })
      t.end()
    })
  })
})

tape('redirect URL helper', function (t) {
  t.deepEqual(
    redirect.from('http', 'localhost').to('https', '127.0.0.1'),
    {
      src: util.format('http://localhost:%d/to/https/127.0.0.1', s.port),
      dst: util.format('https://127.0.0.1:%d/from/http/localhost', ss.port)
    })
  t.deepEqual(
    redirect.from('https', 'localhost').to('http', 'localhost'),
    {
      src: util.format('https://localhost:%d/to/http/localhost', ss.port),
      dst: util.format('http://localhost:%d/from/https/localhost', s.port)
    })
  t.end()
})