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
97 lines (86 loc) · 2.24 KB
/
test.js
File metadata and controls
97 lines (86 loc) · 2.24 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
'use strict'
var test = require('tape')
var hapi = require('@hapi/hapi')
var http = require('http')
var plugin = require('./index.js')
test('proxied requests', async (t) => {
t.plan(2)
const server = await Server();
const response = await server.inject({
url: '/',
headers: {
host: 'host',
'x-forwarded-proto': 'http'
}
});
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}', async (t) => {
t.plan(2)
const server = await Server({ proxy: false });
const response = await server.inject({
url: '/',
headers: {
host: 'host'
}
});
t.equal(response.statusCode, 301, 'sets 301 code')
t.equal(response.headers.location, 'https://host/', 'sets Location header')
});
test('query string', async (t) => {
t.plan(2)
const server = await Server();
const response = await server.inject({
url: '/?test=test&test2=test2',
headers: {
host: 'host',
'x-forwarded-proto': 'http'
}
});
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', async (t) => {
t.plan(2)
const server = await Server();
const response = await server.inject({
url: '/',
headers: {
host: 'host',
'x-forwarded-proto': 'https'
}
});
t.equal(response.statusCode, 200, 'receives 200');
t.equal(response.result, 'Hello!', 'receives body');
});
test('x-forward-host support', async (t) => {
t.plan(2)
const server = await Server();
const response = await server.inject({
url: '/',
headers: {
host: 'host',
'x-forwarded-proto': 'http',
'x-forwarded-host': 'host2'
}
});
t.equal(response.statusCode, 301, 'sets 301 code')
t.equal(response.headers.location, 'https://host2/', 'sets Location header')
});
const Server = async (options) => {
const server = new hapi.Server();
await server.register({ plugin, options });
server.route({
method: 'GET',
path: '/',
handler: function (request, h) {
return 'Hello!';
}
})
return server;
}