Skip to content
Myr

Fixed wildcard bugs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by Yusuke Wada · parent de6b5bd · Browse files · 5 files+43 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+43 added−9 removed

  • tests:+30 added−4 removed
  • other:+13 added−5 removed

5 files+43 added−9 removed

modified:example/basic/index.js+1 added-1 removed
modified:src/hono.js+4 added-2 removed
modified:src/hono.test.js+29 added-3 removed
60 unmodified lines
6162describe('Middleware', () => {63 const app = Hono()6465 const logger = (c, next) => {65 const logger = async (c, next) => {66 console.log(`${c.req.method} : ${c.req.url}`)67 next()68 }6970 const customHeader = (c, next) => {70 const rootHeader = async (c, next) => {71 next()72 c.res.headers.append('x-message', 'custom-header')72 await c.res.headers.append('x-custom', 'root')73 }7475 const customHeader = async (c, next) => {76 next()77 await c.res.headers.append('x-message', 'custom-header')78 }79 const customHeader2 = async (c, next) => {80 next()81 await c.res.headers.append('x-message-2', 'custom-header-2')82 }8384 app.use('*', logger)85 app.use('*', rootHeader)86 app.use('/hello', customHeader)87 app.use('/hello/*', customHeader2)88 app.get('/hello', () => {89 return new fetch.Response('hello')90 })91 app.get('/hello/:message', (c) => {92 const message = c.req.params('message')93 return new fetch.Response(`${message}`)94 })9596 it('logging and custom header', async () => {97 let req = new fetch.Request('https://example.com/hello')98 let res = await app.dispatch(req)99 expect(res.status).toBe(200)100 expect(await res.text()).toBe('hello')101 expect(await res.headers.get('x-custom')).toBe('root')102 expect(await res.headers.get('x-message')).toBe('custom-header')103 expect(await res.headers.get('x-message-2')).toBe('custom-header-2')104 })105106 it('logging and custom header with named params', async () => {107 let req = new fetch.Request('https://example.com/hello/message')108 let res = await app.dispatch(req)109 expect(res.status).toBe(200)110 expect(await res.text()).toBe('message')111 expect(await res.headers.get('x-custom')).toBe('root')112 expect(await res.headers.get('x-message-2')).toBe('custom-header-2')113 })114})
modified:src/node.js+8 added-2 removed
1'use strict'23const { splitPath, getPattern } = require('./util')45const METHOD_NAME_OF_ALL = 'all'5const METHOD_NAME_OF_ALL = 'ALL'67const createResult = (handler, params) => {8 return { handler: handler, params: params }9}
7 unmodified lines
17 this.method = {}18 if (method && handler) {19 this.method[method] = handler20 }21 this.middlewares = []22}2324Node.prototype.insert = function (method, path, handler) {25 let curNode = this
24 unmodified lines
50 curNode = nextNode51 continue52 }5354 let isWildcard = false55 let isParamMatch = false56 const keys = Object.keys(curNode.children)57 for (let j = 0; j < keys.length; j++) {58 const key = keys[j]59 // Wildcard60 if (key === '*') {61 curNode = curNode.children['*']60 isParamMatch = true62 isWildcard = true63 break64 }65 const pattern = getPattern(key)66 if (pattern) {
8 unmodified lines
75 return noRoute()76 }77 }7879 if (isWildcard) {80 break81 }8283 if (isParamMatch === false) {84 return noRoute()85 }86 }
11 unmodified lines
modified:src/node.test.js+1 added-1 removed
121 unmodified lines
122})123124describe('All', () => {125 const node = new Node()126 node.insert('all', '/all-methods', 'all methods')126 node.insert('ALL', '/all-methods', 'all methods') // ALL127 it('/all-methods', () => {128 res = node.search('get', '/all-methods')129 expect(res).not.toBeNull()130 expect(res.handler).toBe('all methods')
5 unmodified lines