/
index.js
93 lines (86 loc) · 2.17 KB
/
index.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
/**
* hapi-session-inject test
*/
'use strict';
const Hapi = require('hapi');
const Session = require('../lib');
const Code = require('code');
const Lab = require('lab');
const lab = exports.lab = Lab.script();
const describe = lab.describe;
const it = lab.it;
const before = lab.before;
const expect = Code.expect;
const fail = Code.fail;
const internals = {
regex: new RegExp('(session=[^\x00-\x20\"\,\;\\\x7F]*)'),
password: 'passwordmustbelongerthan32characterssowejustmakethislonger'
};
function init(server, maxCookieSize) {
server.connection();
server.route([
{
method: 'POST', path: '/',
handler: (request, reply) => {
request.yar.set('val', request.payload);
reply();
}
},
{
method: 'GET', path: '/',
handler: (request, reply) => {
reply(request.yar.get('val'));
}
},
]);
const options = {
maxCookieSize: maxCookieSize,
cookieOptions: {
password: internals.password,
isSecure: false
}
};
return server.register({ register: require('yar'), options })
.then(() => {
return server.initialize();
});
}
describe("Client side storage with callback", () => {
const server = new Hapi.Server();
before(() => {
return init(server, 1024);
});
it('should get back stored value', (done) => {
const session = new Session(server);
session.inject({ method: 'POST', url: '/', payload: '"one"'}, (res) => {
session.inject({ method: 'GET', url: '/' }, (res2) => {
expect(res2.result).to.equal('one');
done();
});
});
});
});
describe("Server side storage with promises", () => {
const server = new Hapi.Server();
before(() => {
return init(server, 0);
});
it('should keep sessions separate', () => {
const session1 = new Session(server);
const session2 = new Session(server);
return session1.inject({ method: 'POST', url: '/', payload: '"one"'})
.then(() => {
return session2.inject({ method: 'POST', url: '/', payload: '"two"'});
})
.then(() => {
return session1.inject({ method: 'GET', url: '/' })
})
.then((res) => {
expect(res.result).to.equal('one');
return session2.inject({ method: 'GET', url: '/' })
})
.then((res) => {
expect(res.result).to.equal('two');
});
});
});