forked from koajs/session
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternalkey.test.js
More file actions
56 lines (56 loc) · 1.49 KB
/
Copy pathexternalkey.test.js
File metadata and controls
56 lines (56 loc) · 1.49 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
'use strict';
const Koa = require('koa');
const request = require('supertest');
const assert = require('assert');
const session = require('..');
const store = require('./store');
const TOKEN_KEY = 'User-Token';
describe('Koa Session External Key', () => {
describe('when the external key set/get is invalid', () => {
it('should throw a error', () => {
try {
new App({
externalKey: {},
});
} catch (err) {
assert.equal(err.message, 'externalKey.get must be function');
}
});
});
describe('custom get/set external key', () => {
it('should still work', done => {
const app = App();
app.use(async function(ctx) {
if (ctx.method === 'POST') {
ctx.session.string = ';';
ctx.status = 204;
} else {
ctx.body = ctx.session.string;
}
});
const server = app.listen();
request(server)
.post('/')
.expect(204, (err, res) => {
if (err) return done(err);
const token = res.get(TOKEN_KEY);
request(server)
.get('/')
.set(TOKEN_KEY, token)
.expect(';', done);
});
});
});
});
function App(options) {
const app = new Koa();
app.keys = [ 'a', 'b' ];
options = options || {};
options.store = store;
options.externalKey = options.externalKey || {
get: ctx => ctx.get(TOKEN_KEY),
set: (ctx, value) => ctx.set(TOKEN_KEY, value),
};
app.use(session(options, app));
return app;
}