forked from bendrucker/hapi-require-https
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
132 lines (112 loc) · 2.72 KB
/
Copy pathtest.js
File metadata and controls
132 lines (112 loc) · 2.72 KB
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
132
'use strict'
var test = require('tape')
var hapi = require('hapi')
var http = require('http')
var plugin = require('./')
test('proxied requests', function (t) {
t.plan(2)
Server().inject({
url: '/',
headers: {
host: 'host',
'x-forwarded-proto': 'http'
}
}, function (response) {
t.equal(response.statusCode, 301, 'sets 301 code')
t.equal(response.headers.location, 'https://host/', 'sets Location header')
})
})
test('un-proxied requests: options = {proxy: false}', function (t) {
t.plan(2)
Server({proxy: false}).inject({
url: '/',
headers: {
host: 'host'
}
}, function (response) {
t.equal(response.statusCode, 301, 'sets 301 code')
t.equal(response.headers.location, 'https://host/', 'sets Location header')
})
})
test('query string', function (t) {
t.plan(2)
Server().inject({
url: '/?test=test&test2=test2',
headers: {
host: 'host',
'x-forwarded-proto': 'http'
}
}, function (response) {
t.equal(response.statusCode, 301, 'sets 301 code')
t.equal(
response.headers.location,
'https://host/?test=test&test2=test2',
'sets Location header with query string'
)
})
})
test('ignores unmatched', function (t) {
t.plan(2)
Server().inject({
url: '/',
headers: {
host: 'host',
'x-forwarded-proto': 'https'
}
}, function (response) {
t.equal(response.statusCode, 200, 'receives 200')
t.equal(response.result, 'Hello!', 'receives body')
})
})
test('multiple connections', function (t) {
t.plan(1)
var server = new hapi.Server()
var main = server.connection({labels: 'a'})
server.connection({labels: 'b'})
main.register({
register: plugin,
options: {
proxy: false
}
}, throwErr)
server.start(function (err) {
if (err) return t.end(err)
var info = main.info
var url = info.protocol + '://' + info.host + ':' + info.port
http.request(url, function (res) {
t.equal(res.statusCode, 301)
server.stop(t.end)
})
.end()
})
})
test('x-forward-host support', function (t) {
t.plan(2)
Server().inject({
url: '/',
headers: {
host: 'host',
'x-forwarded-proto': 'http',
'x-forwarded-host': 'host2'
}
}, function (response) {
t.equal(response.statusCode, 301, 'sets 301 code')
t.equal(response.headers.location, 'https://host2/', 'sets Location header')
})
})
function Server (options) {
var server = new hapi.Server()
server.connection()
server.register({register: plugin, options: options}, throwErr)
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply('Hello!')
}
})
return server
}
function throwErr (err) {
if (err) throw err
}