forked from koajs/session
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternalkey.test.ts
More file actions
60 lines (56 loc) · 1.66 KB
/
Copy pathexternalkey.test.ts
File metadata and controls
60 lines (56 loc) · 1.66 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
import { strict as assert } from 'node:assert';
import Koa from 'koa';
import { ZodError } from 'zod';
import { request } from '@eggjs/supertest';
import session, { type CreateSessionOptions } from '../src/index.js';
import store from './store.js';
const TOKEN_KEY = 'User-Token';
function App(options: CreateSessionOptions = {}) {
const app = new Koa();
app.keys = [ 'a', 'b' ];
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;
}
describe('Koa Session External Key', () => {
describe('when the external key set/get is invalid', () => {
it('should throw a error', () => {
assert.throws(() => {
App({
externalKey: {} as any,
});
}, err => {
assert(err instanceof ZodError);
assert.match(err.message, /externalKey/);
return true;
});
});
});
describe('custom get/set external key', () => {
it('should still work', async () => {
const app = App();
app.use(async function(ctx) {
if (ctx.method === 'POST') {
ctx.session.string = ';';
ctx.status = 204;
assert(ctx.session.externalKey);
} else {
ctx.body = ctx.session.string;
assert.equal(ctx.session.externalKey, ctx.get(TOKEN_KEY));
}
});
const res = await request(app.callback())
.post('/')
.expect(204);
const token = res.get(TOKEN_KEY)!;
await request(app.callback())
.get('/')
.set(TOKEN_KEY as any, token)
.expect(';');
});
});
});