-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrouting.spec.ts
138 lines (120 loc) · 3.68 KB
/
routing.spec.ts
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {
Controller,
Get,
Module,
RequestMethod,
Session,
} from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { SessionModule } from '../src';
import { platforms } from './utils/platforms';
import { doubleRequest } from './utils/request';
describe('routing', () => {
for (const PlatformAdapter of platforms) {
describe(PlatformAdapter.name, () => {
it('forRoutes', async () => {
@Controller('/')
class TestController {
@Get('with-session')
withSession(@Session() session?: { views?: number }) {
if (!session) {
return { views: 1 };
}
session.views = (session.views || 0) + 1;
return { views: session.views };
}
@Get('without-session')
withoutSession(@Session() session?: { views?: number }) {
if (!session) {
return { views: 1 };
}
session.views = (session.views || 0) + 1;
return { views: session.views };
}
}
@Module({
imports: [
SessionModule.forRoot({
session: { secret: 'test' },
forRoutes: [{ method: RequestMethod.ALL, path: 'with-session' }],
}),
],
controllers: [TestController],
})
class TestModule {}
const app = await NestFactory.create(
TestModule,
new PlatformAdapter(),
{ logger: false },
);
const server = app.getHttpServer();
await app.init();
const [sessRes1, sessRes2] = await doubleRequest(
server,
'/with-session',
);
const [noSessRes1, noSessRes2] = await doubleRequest(
server,
'/without-session',
);
await app.close();
expect(sessRes1.body.views).toBe(1);
expect(sessRes2.body.views).toBe(2);
expect(noSessRes1.body.views).toBe(1);
expect(noSessRes2.body.views).toBe(1);
});
it('exclude', async () => {
@Controller('/')
class TestController {
@Get('with-session')
withSession(@Session() session?: { views?: number }) {
if (!session) {
return { views: 1 };
}
session.views = (session.views || 0) + 1;
return { views: session.views };
}
@Get('without-session')
withoutSession(@Session() session?: { views?: number }) {
if (!session) {
return { views: 1 };
}
session.views = (session.views || 0) + 1;
return { views: session.views };
}
}
@Module({
imports: [
SessionModule.forRoot({
session: { secret: 'test' },
exclude: [{ method: RequestMethod.ALL, path: 'without-session' }],
forRoutes: [TestController],
}),
],
controllers: [TestController],
})
class TestModule {}
const app = await NestFactory.create(
TestModule,
new PlatformAdapter(),
{ logger: false },
);
const server = app.getHttpServer();
await app.init();
const [sessRes1, sessRes2] = await doubleRequest(
server,
'/with-session',
);
const [noSessRes1, noSessRes2] = await doubleRequest(
server,
'/without-session',
);
await app.close();
expect(sessRes1.body.views).toBe(1);
expect(sessRes2.body.views).toBe(2);
expect(noSessRes1.body.views).toBe(1);
expect(noSessRes2.body.views).toBe(1);
});
});
}
});