Skip to content

Commit b4bf095

Browse files
committed
test(regexp): add tests and description for regexp
1 parent 37b6b7c commit b4bf095

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ app.post('/entry/:id', ({ pathname }) => {
4141
}
4242
})
4343

44+
// capture the parameters with RegExp
45+
app.get('/post/:date(\\d+)/:title([a-z]+)', ({ pathname }) => {
46+
const { date, title } = pathname.groups
47+
return { post: { date, title } }
48+
})
49+
4450
// get the query parameter
4551
app.get('/search', ({ search }) => {
4652
const params = new URLSearchParams(search.input)

test/pico.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3769
describe('All', () => {
3870
const app = new Pico()
3971
app.all('/abc', () => 'Hi')

0 commit comments

Comments
 (0)