Skip to content

Commit

Permalink
fix: #243
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Apr 8, 2021
1 parent eb64054 commit 72e0b0d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
20 changes: 20 additions & 0 deletions __tests__/core/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,26 @@ describe('HTTP methods', () => {

await fetch('/', { method: 'TRACE' }).expect(200, 'TRACE')
})
it('HEAD request works when any of the method handlers are defined', async () => {
const app = new App()

app.get('/', (_, res) => res.send('It works'))

const server = app.listen()
const fetch = makeFetch(server)

await fetch('/', { method: 'HEAD' }).expect(204)
})
it('HEAD request does not work for undefined handlers', async () => {
const app = new App()

app.get('/', (_, res) => res.send('It works'))

const server = app.listen()
const fetch = makeFetch(server)

await fetch('/hello', { method: 'HEAD' }).expect(404)
})
})

describe('Route handlers', () => {
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"pnpm": ">=5.3.0"
},
"devDependencies": {
"@changesets/cli": "^2.14.1",
"@changesets/cli": "^2.16.0",
"@commitlint/cli": "^12.1.1",
"@commitlint/config-conventional": "^12.1.1",
"@rollup/plugin-typescript": "^8.2.1",
"@types/jest": "^26.0.22",
"@types/node": "^14.14.37",
"@typescript-eslint/eslint-plugin": "^4.20.0",
"@typescript-eslint/parser": "^4.20.0",
"@typescript-eslint/eslint-plugin": "^4.21.0",
"@typescript-eslint/parser": "^4.21.0",
"colorette": "^1.2.2",
"decache": "^4.6.0",
"eslint": "^7.23.0",
Expand All @@ -33,8 +33,8 @@
"rollup": "^2.44.0",
"supertest-fetch": "^1.4.3",
"ts-jest": "^26.5.4",
"tslib": "^2.1.0",
"typescript": "^4.2.3"
"tslib": "^2.2.0",
"typescript": "^4.2.4"
},
"scripts": {
"prerelease": "pnpm lint && pnpm build && pnpm test",
Expand Down
31 changes: 23 additions & 8 deletions packages/app/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ export class App<
return app
}

find(url: string, method: string) {
find(url: string) {
return this.middleware.filter((m) => {
m.regex = m.type === 'mw' ? rg(m.path, true) : rg(m.path)

return (m.method ? m.method === method : true) && m.regex.pattern.test(url)
return m.regex.pattern.test(url)
})
}

Expand All @@ -237,21 +237,36 @@ export class App<

const pathname = getPathname(req.originalUrl)

const matched = this.find(pathname)

const mw: Middleware[] = [
{
handler: exts,
type: 'mw',
path: '/'
},
...matched.filter((x) => (x.method ? x.method === req.method : true))
]

...this.find(pathname, req.method),

{
handler: this.noMatchHandler,
if (matched[0] != null) {
mw.push({
type: 'mw',
handler: (req, res, next) => {
if (req.method === 'HEAD') {
res.statusCode = 204
return res.end('')
}
next()
},
path: '/'
}
]
})
}

mw.push({
handler: this.noMatchHandler,
type: 'mw',
path: '/'
})

const handle = (mw: Middleware) => async (req: Req, res: Res, next?: NextFunction) => {
const { path, handler, type, regex } = mw
Expand Down

0 comments on commit 72e0b0d

Please sign in to comment.