@@ -34,6 +34,38 @@ describe('Basic', () => {
3434 } )
3535} )
3636
37+ describe ( 'RegExp' , ( ) => {
38+ const app = new Pico ( )
39+ app . get ( '/post/:date(\\d+)/:title([a-z]+)' , ( { pathname } ) => {
40+ const { date, title } = pathname . groups
41+ return { post : { date, title } }
42+ } )
43+ app . get ( '/assets/:filename(.*.png)' , ( { pathname } ) => {
44+ const { filename } = pathname . groups
45+ return { filename }
46+ } )
47+
48+ it ( 'Should capture regexp path parameters' , async ( ) => {
49+ const req = new Request ( 'http://localhost/post/20221124/hello' )
50+ const res = app . fetch ( req )
51+ expect ( res . status ) . toBe ( 200 )
52+ expect ( await res . json ( ) ) . toEqual ( { post : { date : '20221124' , title : 'hello' } } )
53+ } )
54+
55+ it ( 'Should return 404 response' , async ( ) => {
56+ const req = new Request ( 'http://localhost/post/onetwothree/hello' )
57+ const res = app . fetch ( req )
58+ expect ( res . status ) . toBe ( 404 )
59+ } )
60+
61+ it ( 'Should capture the path parameter with the wildcard' , async ( ) => {
62+ const req = new Request ( 'http://localhost/assets/animal.png' )
63+ const res = app . fetch ( req )
64+ expect ( res . status ) . toBe ( 200 )
65+ expect ( await res . json ( ) ) . toEqual ( { filename : 'animal.png' } )
66+ } )
67+ } )
68+
3769describe ( 'All' , ( ) => {
3870 const app = new Pico ( )
3971 app . all ( '/abc' , ( ) => 'Hi' )
0 commit comments