-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
test-redirect-auth.js
126 lines (111 loc) 路 3.2 KB
/
test-redirect-auth.js
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
'use strict'
var server = require('./server')
, request = require('../index')
, util = require('util')
, events = require('events')
, tape = require('tape')
var s = server.createServer()
, ss = server.createSSLServer()
// 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)
, 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)
tape('setup', function(t) {
s.listen(s.port, function() {
ss.listen(ss.port, function() {
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()
})
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()
})
})
}
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('cleanup', function(t) {
s.close(function() {
ss.close(function() {
t.end()
})
})
})