1
1
import { describe , it , expect , run } from 'https://deno.land/x/wizard@0.1.0/mod.ts'
2
- import { App } from '../../mod .ts'
2
+ import { App } from '../../app .ts'
3
3
import { BindToSuperDeno , InitAppAndTest } from '../util.ts'
4
4
5
5
describe ( 'Testing App' , ( ) => {
@@ -22,18 +22,24 @@ describe('Testing App', () => {
22
22
23
23
expect ( app . locals . hello ) . toBe ( 'world' )
24
24
} )
25
- /* it('Custom noMatchHandler works', async () => {
26
- const { fetch } = InitAppAndTest(() => {}, undefined, {
27
- noMatchHandler: (req) => {
28
- req.respond({
29
- status: 404,
30
- body: `Oopsie! Page ${req.url} is lost.`
31
- })
25
+ it ( 'Custom noMatchHandler works' , async ( ) => {
26
+ const { fetch } = InitAppAndTest (
27
+ ( _req , _res , next ) => {
28
+ next ( )
29
+ } ,
30
+ undefined ,
31
+ {
32
+ noMatchHandler : ( req ) => {
33
+ req . respond ( {
34
+ status : 404 ,
35
+ body : `Oopsie! Page ${ req . url } is lost.`
36
+ } )
37
+ }
32
38
}
33
- } )
39
+ )
34
40
35
41
await fetch . get ( '/' ) . expect ( 404 , 'Oopsie! Page / is lost.' )
36
- }) */
42
+ } )
37
43
it ( 'Custom onError works' , async ( ) => {
38
44
const app = new App ( {
39
45
onError : ( err , req ) => {
@@ -50,6 +56,54 @@ describe('Testing App', () => {
50
56
51
57
await fetch . get ( '/' ) . expect ( 500 , 'Ouch, you hurt me on / page.' )
52
58
} )
59
+ it ( 'req and res inherit properties from previous middlewares' , async ( ) => {
60
+ const app = new App ( )
61
+
62
+ app
63
+ . use ( ( _req , res , next ) => {
64
+ res . locals . hello = 'world'
65
+
66
+ next ( )
67
+ } )
68
+ . get ( '/' , ( _ , res ) => {
69
+ res . send ( res . locals )
70
+ } )
71
+
72
+ const fetch = BindToSuperDeno ( app )
73
+
74
+ await fetch . get ( '/' ) . expect ( 200 , '{\n "hello": "world"\n}' )
75
+ } )
76
+ } )
77
+
78
+ describe ( 'Testing App routing' , ( ) => {
79
+ it ( 'should respond on matched route' , async ( ) => {
80
+ const { fetch } = InitAppAndTest ( ( _req , res ) => void res . send ( 'Hello world' ) , '/route' )
81
+
82
+ await fetch . get ( '/route' ) . expect ( 200 , 'Hello world' )
83
+ } )
84
+ it ( 'should match wares containing base path' , async ( ) => {
85
+ const app = new App ( )
86
+
87
+ app . use ( '/abc' , ( _req , res ) => void res . send ( 'Hello world' ) )
88
+
89
+ const fetch = BindToSuperDeno ( app )
90
+
91
+ await fetch . get ( '/abc/def' ) . expect ( 200 , 'Hello world' )
92
+ } )
93
+ it ( '"*" should catch all undefined routes' , async ( ) => {
94
+ const app = new App ( )
95
+
96
+ app
97
+ . get ( '/route' , ( _req , res ) => void res . send ( 'A different route' ) )
98
+ . all ( '*' , ( _req , res ) => void res . send ( 'Hello world' ) )
99
+
100
+ const fetch = BindToSuperDeno ( app )
101
+
102
+ await fetch . get ( '/test' ) . expect ( 200 , 'Hello world' )
103
+ } )
104
+ it ( 'should throw 404 on no routes' , async ( ) => {
105
+ await BindToSuperDeno ( new App ( ) ) . get ( '/' ) . expect ( 404 )
106
+ } )
53
107
} )
54
108
55
109
run ( )
0 commit comments