@@ -2,6 +2,7 @@ import supertest from "supertest";
2
2
import { describe , it , expect , beforeEach } from "vitest" ;
3
3
import {
4
4
createApp ,
5
+ createRouter ,
5
6
toNodeListener ,
6
7
App ,
7
8
eventHandler ,
@@ -12,18 +13,23 @@ import {
12
13
13
14
describe ( "session" , ( ) => {
14
15
let app : App ;
16
+ let router : ReturnType < typeof createRouter > ;
15
17
let request : ReturnType < typeof supertest > ;
16
- let cookie : string | undefined ;
18
+ let cookie = "" ;
19
+
20
+ let sessionIdCtr = 0 ;
21
+ const sessionConfig : SessionConfig = {
22
+ name : "h3-test" ,
23
+ password : "1234567123456712345671234567123456712345671234567" ,
24
+ generateId : ( ) => ++ sessionIdCtr + "" ,
25
+ } ;
17
26
18
27
beforeEach ( ( ) => {
19
- app = createApp ( { debug : true } ) ;
20
- let sessionIdCtr = 0 ;
21
- const sessionConfig : SessionConfig = {
22
- name : "h3-test" ,
23
- password : "1234567123456712345671234567123456712345671234567" ,
24
- generateId : ( ) => ++ sessionIdCtr + "" ,
25
- } ;
26
- app . use (
28
+ router = createRouter ( { preemptive : true } ) ;
29
+ app = createApp ( { debug : true } ) . use ( router ) ;
30
+ request = supertest ( toNodeListener ( app ) ) ;
31
+
32
+ router . use (
27
33
"/" ,
28
34
eventHandler ( async ( event ) => {
29
35
const session = await useSession ( event , sessionConfig ) ;
@@ -33,40 +39,37 @@ describe("session", () => {
33
39
return { session } ;
34
40
} ) ,
35
41
) ;
36
- request = supertest ( toNodeListener ( app ) ) ;
37
42
} ) ;
38
43
39
- describe ( "useSession" , ( ) => {
40
- it ( "initiates session" , async ( ) => {
41
- const result = await request . get ( "/" ) ;
42
- expect ( result . headers [ "set-cookie" ] ) . toHaveLength ( 1 ) ;
43
- cookie = result . headers [ "set-cookie" ] [ 0 ] ;
44
- expect ( result . body ) . toMatchObject ( {
45
- session : { id : "1" , data : { } } ,
46
- } ) ;
44
+ it ( "initiates session" , async ( ) => {
45
+ const result = await request . get ( "/" ) ;
46
+ expect ( result . headers [ "set-cookie" ] ) . toHaveLength ( 1 ) ;
47
+ cookie = result . headers [ "set-cookie" ] [ 0 ] ;
48
+ expect ( result . body ) . toMatchObject ( {
49
+ session : { id : "1" , data : { } } ,
47
50
} ) ;
51
+ } ) ;
48
52
49
- it ( "gets same session back" , async ( ) => {
50
- const result = await request . get ( "/" ) ;
51
- expect ( result . body ) . toMatchObject ( {
52
- session : { id : "1" , data : { } } ,
53
- } ) ;
53
+ it ( "gets same session back" , async ( ) => {
54
+ const result = await request . get ( "/" ) . set ( "Cookie" , cookie ) ;
55
+ expect ( result . body ) . toMatchObject ( {
56
+ session : { id : "1" , data : { } } ,
54
57
} ) ;
58
+ } ) ;
55
59
56
- it ( "set session data" , async ( ) => {
57
- const result = await request
58
- . post ( "/" )
59
- . set ( "Cookie" , cookie as string )
60
- . send ( { foo : "bar" } ) ;
61
- cookie = result . headers [ "set-cookie" ] [ 0 ] ;
62
- expect ( result . body ) . toMatchObject ( {
63
- session : { id : "1" , data : { foo : "bar" } } ,
64
- } ) ;
60
+ it ( "set session data" , async ( ) => {
61
+ const result = await request
62
+ . post ( "/" )
63
+ . set ( "Cookie" , cookie )
64
+ . send ( { foo : "bar" } ) ;
65
+ cookie = result . headers [ "set-cookie" ] [ 0 ] ;
66
+ expect ( result . body ) . toMatchObject ( {
67
+ session : { id : "1" , data : { foo : "bar" } } ,
68
+ } ) ;
65
69
66
- const result2 = await request . get ( "/" ) . set ( "Cookie" , cookie as string ) ;
67
- expect ( result2 . body ) . toMatchObject ( {
68
- session : { id : "1" , data : { foo : "bar" } } ,
69
- } ) ;
70
+ const result2 = await request . get ( "/" ) . set ( "Cookie" , cookie ) ;
71
+ expect ( result2 . body ) . toMatchObject ( {
72
+ session : { id : "1" , data : { foo : "bar" } } ,
70
73
} ) ;
71
74
} ) ;
72
75
} ) ;
0 commit comments