forked from koajs/session
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore_with_ctx.test.ts
More file actions
78 lines (65 loc) · 2 KB
/
Copy pathstore_with_ctx.test.ts
File metadata and controls
78 lines (65 loc) · 2 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
import { strict as assert } from 'node:assert';
import Koa from 'koa';
import { request } from '@eggjs/supertest';
import session, { type CreateSessionOptions } from '../src/index.js';
import store from './store_with_ctx.js';
function App(options: CreateSessionOptions = {}) {
const app = new Koa();
app.keys = [ 'a', 'b' ];
options.store = store;
app.use(async (ctx, next) => {
await next();
ctx.body = ctx.state.test === undefined ? 'undefined' : ctx.state.test;
});
app.use(session(options, app));
return app;
}
describe('Koa Session External Store methods can access Koa context', () => {
let cookie: string;
describe('new session', () => {
describe('when not accessed', () => {
it('should not set ctx.state.test variable', async () => {
const app = App();
await request(app.callback())
.get('/')
.expect('undefined');
});
});
describe('when populated', () => {
it('should set ctx.state.test variable', async () => {
const app = App();
app.use(async ctx => {
if (ctx.path === '/set') ctx.session = { foo: 'bar' };
});
const res = await request(app.callback())
.get('/set')
.expect(200);
cookie = res.get('Set-Cookie')!.join(';');
assert.equal(res.text, 'set');
});
});
describe('when accessed', () => {
it('should access ctx.state.test variable', async () => {
const app = App();
await request(app.callback())
.get('/')
.set('Cookie', cookie)
.expect('get');
});
});
describe('session destroyed', () => {
it('should access ctx.state.test variable', async () => {
const app = App();
app.use(async ctx => {
if (ctx.path === '/destroy') {
ctx.session = null;
}
});
await request(app.callback())
.get('/destroy')
.set('Cookie', cookie)
.expect('destroyed');
});
});
});
});