@@ -17,6 +17,208 @@ describe('Router', () => {
17
17
const json = await response . json ( ) ;
18
18
expect ( json . message ) . toBe ( 'Hello /greetings/John!' ) ;
19
19
} ) ;
20
+ it ( 'should process parameters in the path' , async ( ) => {
21
+ const router = createRouter ( ) ;
22
+ router . get (
23
+ '/greetings/:name' ,
24
+ request =>
25
+ new Response (
26
+ JSON . stringify ( {
27
+ message : `Hello ${ request . params . name } !` ,
28
+ } )
29
+ )
30
+ ) ;
31
+ const response = await router . fetch ( 'http://localhost/greetings/John' ) ;
32
+ const json = await response . json ( ) ;
33
+ expect ( json . message ) . toBe ( 'Hello John!' ) ;
34
+ } ) ;
35
+ it ( 'should process query parameters' , async ( ) => {
36
+ const router = createRouter ( ) ;
37
+ router . get (
38
+ '/greetings' ,
39
+ request =>
40
+ new Response (
41
+ JSON . stringify ( {
42
+ message : `Hello ${ request . query . name } !` ,
43
+ } )
44
+ )
45
+ ) ;
46
+ const response = await router . fetch ( 'http://localhost/greetings?name=John' ) ;
47
+ const json = await response . json ( ) ;
48
+ expect ( json . message ) . toBe ( 'Hello John!' ) ;
49
+ } ) ;
50
+ it ( 'should process multiple handlers for the same route' , async ( ) => {
51
+ const router = createRouter ( ) ;
52
+ router . get (
53
+ '/greetings' ,
54
+ ( request : any ) => {
55
+ request . message = 'Hello' ;
56
+ } ,
57
+ ( request : any ) => {
58
+ request . message += ` ${ request . query . name } !` ;
59
+ return new Response ( JSON . stringify ( { message : request . message } ) ) ;
60
+ }
61
+ ) ;
62
+ const response = await router . fetch ( 'http://localhost/greetings?name=John' ) ;
63
+ const json = await response . json ( ) ;
64
+ expect ( json . message ) . toBe ( 'Hello John!' ) ;
65
+ } ) ;
66
+
67
+ it ( 'can match multiple routes if earlier handlers do not return (as middleware)' , async ( ) => {
68
+ const router = createRouter ( ) ;
69
+ router . get (
70
+ '/greetings' ,
71
+ ( request : any ) => {
72
+ request . message = 'Hello' ;
73
+ } ,
74
+ ( request : any ) => {
75
+ request . message += ` to you` ;
76
+ }
77
+ ) ;
78
+ router . get ( '/greetings' , ( request : any ) => {
79
+ request . message += ` ${ request . query . name } !` ;
80
+ return new Response ( JSON . stringify ( { message : request . message } ) ) ;
81
+ } ) ;
82
+ const response = await router . fetch ( 'http://localhost/greetings?name=John' ) ;
83
+ const json = await response . json ( ) ;
84
+ expect ( json . message ) . toBe ( 'Hello to you John!' ) ;
85
+ } ) ;
86
+ it ( 'can pull route params from the basepath as well' , async ( ) => {
87
+ const router = createRouter ( { base : '/api' } ) ;
88
+ router . get (
89
+ '/greetings/:name' ,
90
+ request =>
91
+ new Response (
92
+ JSON . stringify ( {
93
+ message : `Hello ${ request . params . name } !` ,
94
+ } )
95
+ )
96
+ ) ;
97
+ const response = await router . fetch ( 'http://localhost/api/greetings/John' ) ;
98
+ const json = await response . json ( ) ;
99
+ expect ( json . message ) . toBe ( 'Hello John!' ) ;
100
+ } ) ;
101
+
102
+ it ( 'can handle nested routers' , async ( ) => {
103
+ const router = createRouter ( ) ;
104
+ const nested = createRouter ( {
105
+ base : '/api' ,
106
+ } ) ;
107
+ nested . get (
108
+ '/greetings/:name' ,
109
+ request =>
110
+ new Response (
111
+ JSON . stringify ( {
112
+ message : `Hello ${ request . params . name } !` ,
113
+ } )
114
+ )
115
+ ) ;
116
+ router . get ( '/api/*' , nested ) ;
117
+ const response = await router . fetch ( 'http://localhost/api/greetings/John' ) ;
118
+ const json = await response . json ( ) ;
119
+ expect ( json . message ) . toBe ( 'Hello John!' ) ;
120
+ } ) ;
121
+
122
+ it ( 'can get query params' , async ( ) => {
123
+ const router = createRouter ( ) ;
124
+ router . get (
125
+ '/foo' ,
126
+ request =>
127
+ new Response (
128
+ JSON . stringify ( {
129
+ cat : request . query . cat ,
130
+ foo : request . query . foo ,
131
+ missing : request . query . missing ,
132
+ } )
133
+ )
134
+ ) ;
135
+ const response = await router . fetch ( 'https://foo.com/foo?cat=dog&foo=bar&foo=baz&missing=' ) ;
136
+ const json = await response . json ( ) ;
137
+ expect ( json ) . toMatchObject ( { cat : 'dog' , foo : [ 'bar' , 'baz' ] , missing : '' } ) ;
138
+ } ) ;
139
+ it ( 'supports "/" with base' , async ( ) => {
140
+ const router = createRouter ( {
141
+ base : '/api' ,
142
+ } ) ;
143
+ router . get (
144
+ '/' ,
145
+ ( ) =>
146
+ new Response (
147
+ JSON . stringify ( {
148
+ message : `Hello Root!` ,
149
+ } )
150
+ )
151
+ ) ;
152
+ const response = await router . fetch ( 'http://localhost/api' ) ;
153
+ const json = await response . json ( ) ;
154
+ expect ( json . message ) . toBe ( 'Hello Root!' ) ;
155
+ } ) ;
156
+ it ( 'supports "/" without base' , async ( ) => {
157
+ const router = createRouter ( ) ;
158
+ router . get (
159
+ '/' ,
160
+ ( ) =>
161
+ new Response (
162
+ JSON . stringify ( {
163
+ message : `Hello Root!` ,
164
+ } )
165
+ )
166
+ ) ;
167
+ const response = await router . fetch ( 'http://localhost' ) ;
168
+ const json = await response . json ( ) ;
169
+ expect ( json . message ) . toBe ( 'Hello Root!' ) ;
170
+ } ) ;
171
+ it ( 'supports "/" in the base' , async ( ) => {
172
+ const router = createRouter ( {
173
+ base : '/' ,
174
+ } ) ;
175
+ router . get (
176
+ '/greetings' ,
177
+ ( ) =>
178
+ new Response (
179
+ JSON . stringify ( {
180
+ message : `Hello World!` ,
181
+ } )
182
+ )
183
+ ) ;
184
+ const response = await router . fetch ( 'http://localhost/greetings' ) ;
185
+ const json = await response . json ( ) ;
186
+ expect ( json . message ) . toBe ( 'Hello World!' ) ;
187
+ } ) ;
188
+ it ( 'supports "/" both in the base and in the route' , async ( ) => {
189
+ const router = createRouter ( {
190
+ base : '/' ,
191
+ } ) ;
192
+ router . get (
193
+ '/' ,
194
+ ( ) =>
195
+ new Response (
196
+ JSON . stringify ( {
197
+ message : `Hello World!` ,
198
+ } )
199
+ )
200
+ ) ;
201
+ const response = await router . fetch ( 'http://localhost' ) ;
202
+ const json = await response . json ( ) ;
203
+ expect ( json . message ) . toBe ( 'Hello World!' ) ;
204
+ } ) ;
205
+ it ( 'handles POST bodies' , async ( ) => {
206
+ const router = createRouter ( ) ;
207
+ router . post ( '/greetings' , async request => {
208
+ const json = await request . json ( ) ;
209
+ return new Response (
210
+ JSON . stringify ( {
211
+ message : `Hello ${ json . name } !` ,
212
+ } )
213
+ ) ;
214
+ } ) ;
215
+ const response = await router . fetch ( 'http://localhost/greetings' , {
216
+ method : 'POST' ,
217
+ body : JSON . stringify ( { name : 'John' } ) ,
218
+ } ) ;
219
+ const json = await response . json ( ) ;
220
+ expect ( json . message ) . toBe ( 'Hello John!' ) ;
221
+ } ) ;
20
222
} ) ;
21
223
describe ( 'withErrorHandling' , ( ) => {
22
224
it ( 'should return 500 when error is thrown' , async ( ) => {
0 commit comments