-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
83 lines (80 loc) · 3.03 KB
/
test.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
'use strict';
const should = require('chai').should();
const hapi = require('hapi');
const plugin = require('.');
describe('Hapi Enforce HTTPS Plugin tests', function() {
[{
expect: (err) => should.not.exist(err, 'Error should be present')
},
{
options: { enforceHttps: true },
expect: (err) => should.not.exist(err, 'Error should be present')
},
{
options: { enforceHttps: false },
expect: (err) => should.not.exist(err, 'Error should be present')
}].forEach((dataSet, i) => {
it(`required valid input as a configuration object #${i}`, function() {
const server = new hapi.Server();
server.connection();
return server.register({register: plugin, options: dataSet.options})
.then(dataSet.expect);
});
});
[{
options: { enforceHttps: false },
headers: { 'x-forwarded-proto': 'http' },
expect: (response) => {
should.equal(response.statusCode, 200);
should.equal(response.result, 'Hello!');
}
}, {
options: { enforceHttps: true },
headers: { 'x-forwarded-proto': 'http' },
expect: (response) => {
should.equal(response.statusCode, 400);
should.equal(response.result.error, 'Bad Request');
should.equal(response.result.message, 'HTTPS required');
}
}, {
options: { enforceHttps: false },
headers: { 'x-forwarded-proto': 'https' },
expect: (response) => {
should.equal(response.statusCode, 200);
should.equal(response.result, 'Hello!');
}
}, {
options: { enforceHttps: true },
headers: { 'x-forwarded-proto': 'https' },
expect: (response) => {
should.equal(response.statusCode, 200);
should.equal(response.result, 'Hello!');
}
},
{
options: { enforceHttps: true, excludePaths: ['/'] },
headers: { 'x-forwarded-proto': 'http' },
expect: (response) => {
should.equal(response.statusCode, 200);
should.equal(response.result, 'Hello!');
}
},
{
options: { enforceHttps: true, excludePaths: ['/health'] },
headers: { 'x-forwarded-proto': 'http' },
expect: (response) => {
should.equal(response.statusCode, 400);
should.equal(response.result.error, 'Bad Request');
should.equal(response.result.message, 'HTTPS required');
}
}].forEach((dataSet, i) => {
it(`allows or blocks the resource depending on a given configuration #${i}`, function() {
const server = new hapi.Server();
server.connection();
return server.register({ register: plugin, options: dataSet.options })
.then(() => server.route({ method: 'GET', path: '/', handler: (request, reply) => reply('Hello!') }))
.then(() => server.inject({ url: '/', headers: dataSet.headers }))
.then(dataSet.expect);
});
});
});