Skip to content
Myr

test: add `router/common.case.test.ts` (#2258)

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

* test: remove unused spy * refactor: add "@typescript-eslint/no-explicit-any" for avoiding lint error * test: exclude *.case.test.ts files from vitest These special files are used to share test cases between different modules or runtime environments. * shared among multiple routers * shared by renderToString and renderToDom * test: rename helper/css/common.test.tsx to helper/css/common.case.test.tsx * test: add router/common.case.test.ts * test: remove redundant skip field

authored by Taku Amano · committed by GitHub · parent c19fad7 · Browse files · 14 files+1400 added−2394 removed
This commit

State

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

Diff stats

14 files+1400 added−2394 removed

  • tests:+1397 added−2393 removed
  • other:+3 added−1 removed

14 files+1400 added−2394 removed

modified:deno_dist/middleware/jsx-renderer/index.ts+1 added
added:src/helper/css/common.case.test.tsx+494 added
1/* eslint-disable quotes */2// eslint-disable-next-line @typescript-eslint/no-unused-vars3import { jsx, Fragment } from '../../jsx'4import type {5 css as cssHelper,6 keyframes as keyframesHelper,7 viewTransition as viewTransitionHelper,8 rawCssString as rawCssStringHelper,9 Style as StyleComponent,10} from './index'1112interface Support {13 nest: boolean14}15export const renderTest = (16 getEnv: () => {17 css: typeof cssHelper18 keyframes: typeof keyframesHelper19 viewTransition: typeof viewTransitionHelper20 rawCssString: typeof rawCssStringHelper21 // eslint-disable-next-line @typescript-eslint/no-explicit-any22 toString: (template: any) => Promise<string>23 Style: typeof StyleComponent24 support: Support25 }26) => {27 const { support } = getEnv()2829 let css: typeof cssHelper30 let keyframes: typeof keyframesHelper31 let viewTransition: typeof viewTransitionHelper32 let rawCssString: typeof rawCssStringHelper33 // eslint-disable-next-line @typescript-eslint/no-explicit-any34 let toString: (template: any) => Promise<string>35 let Style: typeof StyleComponent36 beforeEach(() => {37 ;({ css, keyframes, viewTransition, rawCssString, toString, Style } = getEnv())38 })3940 describe('render css', () => {41 it('Should render CSS styles with JSX', async () => {42 const headerClass = css`43 background-color: blue;44 `45 const template = (46 <>47 <Style />48 <h1 class={headerClass}>Hello!</h1>49 </>50 )51 expect(await toString(template)).toBe(52 '<style id="hono-css">.css-2458908649{background-color:blue}</style><h1 class="css-2458908649">Hello!</h1>'53 )54 })5556 it('Should render CSS with keyframes', async () => {57 const animation = keyframes`58 from {59 opacity: 0;60 }61 to {62 opacity: 1;63 }64 `65 const headerClass = css`66 background-color: blue;67 animation: ${animation} 1s ease-in-out;68 `69 const template = (70 <>71 <Style />72 <h1 class={headerClass}>Hello!</h1>73 </>74 )75 expect(await toString(template)).toBe(76 '<style id="hono-css">.css-1580801783{background-color:blue;animation:css-9294673 1s ease-in-out}@keyframes css-9294673{from{opacity:0}to{opacity:1}}</style><h1 class="css-1580801783">Hello!</h1>'77 )78 })7980 it('Should not output the same class name multiple times.', async () => {81 const headerClass = css`82 background-color: blue;83 `84 const headerClass2 = css`85 background-color: blue;86 `87 const template = (88 <>89 <Style />90 <h1 class={headerClass}>Hello!</h1>91 <h1 class={headerClass2}>Hello2!</h1>92 </>93 )94 expect(await toString(template)).toBe(95 '<style id="hono-css">.css-2458908649{background-color:blue}</style><h1 class="css-2458908649">Hello!</h1><h1 class="css-2458908649">Hello2!</h1>'96 )97 })9899 it('Should render CSS with variable', async () => {100 const headerClass = css`101 background-color: blue;102 content: '${"I'm a variable!"}';103 `104 const template = (105 <>106 <Style />107 <h1 class={headerClass}>Hello!</h1>108 </>109 )110 expect(await toString(template)).toBe(111 '<style id="hono-css">.css-4027435072{background-color:blue;content:\'I\\\'m a variable!\'}</style><h1 class="css-4027435072">Hello!</h1>'112 )113 })114115 it('Should escape </style>', async () => {116 const headerClass = css`117 background-color: blue;118 content: '${'</style>'}';119 `120 const template = (121 <>122 <Style />123 <h1 class={headerClass}>Hello!</h1>124 </>125 )126 expect(await toString(template)).toBe(127 '<style id="hono-css">.css-372954897{background-color:blue;content:\'<\\/style>\'}</style><h1 class="css-372954897">Hello!</h1>'128 )129 })130131 it('Should not escape URL', async () => {132 const headerClass = css`133 background-color: blue;134 background: url('${'http://www.example.com/path/to/file.jpg'}');135 `136 const template = (137 <>138 <Style />139 <h1 class={headerClass}>Hello!</h1>140 </>141 )142 expect(await toString(template)).toBe(143 '<style id="hono-css">.css-1321888780{background-color:blue;background:url(\'http://www.example.com/path/to/file.jpg\')}</style><h1 class="css-1321888780">Hello!</h1>'144 )145 })146147 it('Should render CSS with escaped variable', async () => {148 const headerClass = css`149 background-color: blue;150 content: '${rawCssString('say "Hello!"')}';151 `152 const template = (153 <>154 <Style />155 <h1 class={headerClass}>Hello!</h1>156 </>157 )158 expect(await toString(template)).toBe(159 '<style id="hono-css">.css-2238574885{background-color:blue;content:\'say "Hello!"\'}</style><h1 class="css-2238574885">Hello!</h1>'160 )161 })162163 it('Should render CSS with number', async () => {164 const headerClass = css`165 background-color: blue;166 font-size: ${1}rem;167 `168 const template = (169 <>170 <Style />171 <h1 class={headerClass}>Hello!</h1>172 </>173 )174 expect(await toString(template)).toBe(175 '<style id="hono-css">.css-1847536026{background-color:blue;font-size:1rem}</style><h1 class="css-1847536026">Hello!</h1>'176 )177 })178179 it('Should render CSS with array', async () => {180 const animation = keyframes`181 from {182 opacity: 0;183 }184 to {185 opacity: 1;186 }187 `188 const headerClass = css`189 background-color: blue;190 animation: ${animation} 1s ease-in-out;191 `192 const extendedHeaderClass = css`193 ${headerClass}194 color: red;195 `196 const template = (197 <>198 <Style />199 <h1 class={extendedHeaderClass}>Hello!</h1>200 </>201 )202 expect(await toString(template)).toBe(203 '<style id="hono-css">.css-2558359670{background-color:blue;animation:css-9294673 1s ease-in-out;color:red}@keyframes css-9294673{from{opacity:0}to{opacity:1}}</style><h1 class="css-2558359670">Hello!</h1>'204 )205 })206207 it.runIf(support.nest)(208 'Should be used as a class name for syntax `${className} {`',209 async () => {210 const headerClass = css`211 font-weight: bold;212 `213 const containerClass = css`214 ${headerClass} {215 h1 {216 color: red;217 }218 }219 `220 const template = (221 <>222 <Style />223 <div class={containerClass}>224 <h1 class={headerClass}>Hello!</h1>225 </div>226 </>227 )228 expect(await toString(template)).toBe(229 '<style id="hono-css">.css-4220297002{.css-1032195302{h1{color:red}}}.css-1032195302{font-weight:bold}</style><div class="css-4220297002"><h1 class="css-1032195302">Hello!</h1></div>'230 )231 }232 )233234 it('Should be inserted to global if style string starts with :-hono-root', async () => {235 const globalClass = css`236 :-hono-global {237 html {238 color: red;239 }240 body {241 display: flex;242 }243 }244 `245 const template = (246 <>247 <Style />248 <div class={globalClass}>249 <h1>Hello!</h1>250 </div>251 </>252 )253 expect(await toString(template)).toBe(254 '<style id="hono-css">html{color:red}body{display:flex}</style><div class=""><h1>Hello!</h1></div>'255 )256 })257258 it.runIf(support.nest)(259 'Should be inserted to global if style string starts with :-hono-root and extends class name',260 async () => {261 const headerClass = css`262 display: flex;263 `264 const specialHeaderClass = css`265 :-hono-global {266 ${headerClass} {267 h1 {268 color: red;269 }270 }271 }272 `273 const template = (274 <>275 <Style />276 <div class={specialHeaderClass}>277 <h1>Hello!</h1>278 </div>279 </>280 )281 expect(await toString(template)).toBe(282 '<style id="hono-css">.css-3980466870{h1{color:red}}.css-3980466870{display:flex}</style><div class="css-3980466870"><h1>Hello!</h1></div>'283 )284 }285 )286287 it('Should be inserted as global css if passed css`` to Style component', async () => {288 const headerClass = css`289 font-size: 1rem;290 `291 const template = (292 <>293 <Style>{css`294 html {295 color: red;296 }297 body {298 display: flex;299 }300 `}</Style>301 <div>302 <h1 class={headerClass}>Hello!</h1>303 </div>304 </>305 )306 expect(await toString(template)).toBe(307 '<style id="hono-css">html{color:red}body{display:flex}.css-1740067317{font-size:1rem}</style><div><h1 class="css-1740067317">Hello!</h1></div>'308 )309 })310311 it('Should be ignored :-hono-root inside Style component', async () => {312 const headerClass = css`313 font-size: 1rem;314 `315 const template = (316 <>317 <Style>{css`318 :-hono-global {319 html {320 color: red;321 }322 body {323 display: flex;324 }325 }326 `}</Style>327 <div>328 <h1 class={headerClass}>Hello!</h1>329 </div>330 </>331 )332 expect(await toString(template)).toBe(333 '<style id="hono-css">html{color:red}body{display:flex}.css-1740067317{font-size:1rem}</style><div><h1 class="css-1740067317">Hello!</h1></div>'334 )335 })336337 describe('viewTransition', () => {338 it('Should render CSS with unique view-transition-name', async () => {339 const transition = viewTransition()340 const template = (341 <>342 <Style />343 <h1 class={transition}>Hello!</h1>344 </>345 )346 expect(await toString(template)).toBe(347 '<style id="hono-css">.css-1644952339{view-transition-name:css-1644952339}</style><h1 class="css-1644952339">Hello!</h1>'348 )349 })350351 it('Should render CSS with css and keyframes', async () => {352 const kf = keyframes`353 from {354 opacity: 0;355 }356 to {357 opacity: 1;358 }359 `360 const transition = viewTransition(css`361 ::view-transition-old() {362 animation-name: ${kf};363 }364 ::view-transition-new() {365 animation-name: ${kf};366 }367 `)368 const headerClass = css`369 ${transition}370 background-color: blue;371 `372 const template = (373 <>374 <Style />375 <h1 class={headerClass}>Hello!</h1>376 </>377 )378 expect(await toString(template)).toBe(379 '<style id="hono-css">.css-1245070278{view-transition-name:css-399742870;background-color:blue}@keyframes css-9294673{from{opacity:0}to{opacity:1}}::view-transition-old(css-399742870){animation-name:css-9294673}::view-transition-new(css-399742870){animation-name:css-9294673}</style><h1 class="css-1245070278">Hello!</h1>'380 )381 })382383 it('Should works as a template tag function', async () => {384 const kf = keyframes`385 from {386 opacity: 0;387 }388 to {389 opacity: 1;390 }391 `392 const transition = viewTransition`393 ::view-transition-old() {394 animation-name: ${kf};395 }396 ::view-transition-new() {397 animation-name: ${kf};398 }399 `400 const headerClass = css`401 ${transition}402 background-color: blue;403 `404 const template = (405 <>406 <Style />407 <h1 class={headerClass}>Hello!</h1>408 </>409 )410 expect(await toString(template)).toBe(411 '<style id="hono-css">.css-1245070278{view-transition-name:css-399742870;background-color:blue}@keyframes css-9294673{from{opacity:0}to{opacity:1}}::view-transition-old(css-399742870){animation-name:css-9294673}::view-transition-new(css-399742870){animation-name:css-9294673}</style><h1 class="css-1245070278">Hello!</h1>'412 )413 })414 })415416 it.runIf(support.nest)('Should render sub CSS with keyframe', async () => {417 const headerClass = css`418 background-color: blue;419 ${[1, 2].map(420 (i) =>421 css`422 :nth-child(${i}) {423 color: red;424 }425 `426 )}427 `428 const template = (429 <>430 <Style />431 <h1 class={headerClass}>Hello!</h1>432 </>433 )434 expect(await toString(template)).toBe(435 '<style id="hono-css">.css-1539881271{background-color:blue;:nth-child(1){color:red}:nth-child(2){color:red}}</style><h1 class="css-1539881271">Hello!</h1>'436 )437 })438439 it('Should be generated deferent class name for deferent first line comment even if the content is the same', async () => {440 const headerClassA = css`441 /* class A */442 display: flex;443 `444 const headerClassB = css`445 /* class B */446 display: flex;447 `448 const template = (449 <>450 <Style />451 <h1 class={headerClassA}>Hello!</h1>452 <h1 class={headerClassB}>Hello!</h1>453 </>454 )455 expect(await toString(template)).toBe(456 '<style id="hono-css">.css-3170754153{display:flex}.css-896513246{display:flex}</style><h1 class="css-3170754153">Hello!</h1><h1 class="css-896513246">Hello!</h1>'457 )458 })459460 describe('Booleans, Null, and Undefined Are Ignored', () => {461 it.each([true, false, undefined, null])('%s', async (value) => {462 const headerClass = css`463 ${value}464 background-color: blue;465 `466 const template = (467 <>468 <Style />469 <h1 class={headerClass}>Hello!</h1>470 </>471 )472 expect(await toString(template)).toBe(473 '<style id="hono-css">.css-2458908649{background-color:blue}</style><h1 class="css-2458908649">Hello!</h1>'474 )475 })476477 it('falsy value', async () => {478 const value = 0479 const headerClass = css`480 padding: ${value};481 `482 const template = (483 <>484 <Style />485 <h1 class={headerClass}>Hello!</h1>486 </>487 )488 expect(await toString(template)).toBe(489 '<style id="hono-css">.css-478287868{padding:0}</style><h1 class="css-478287868">Hello!</h1>'490 )491 })492 })493 })494}
deleted:src/helper/css/common.test.tsx-496 removed
1/* eslint-disable quotes */2// eslint-disable-next-line @typescript-eslint/no-unused-vars3import { jsx, Fragment } from '../../jsx'4import type {5 css as cssHelper,6 keyframes as keyframesHelper,7 viewTransition as viewTransitionHelper,8 rawCssString as rawCssStringHelper,9 Style as StyleComponent,10} from './index'1112it.skip('this file only exports test runners.', () => {})1314interface Support {15 nest: boolean16}17export const renderTest = (18 getEnv: () => {19 css: typeof cssHelper20 keyframes: typeof keyframesHelper21 viewTransition: typeof viewTransitionHelper22 rawCssString: typeof rawCssStringHelper23 // eslint-disable-next-line @typescript-eslint/no-explicit-any24 toString: (template: any) => Promise<string>25 Style: typeof StyleComponent26 support: Support27 }28) => {29 const { support } = getEnv()3031 let css: typeof cssHelper32 let keyframes: typeof keyframesHelper33 let viewTransition: typeof viewTransitionHelper34 let rawCssString: typeof rawCssStringHelper35 // eslint-disable-next-line @typescript-eslint/no-explicit-any36 let toString: (template: any) => Promise<string>37 let Style: typeof StyleComponent38 beforeEach(() => {39 ;({ css, keyframes, viewTransition, rawCssString, toString, Style } = getEnv())40 })4142 describe('render css', () => {43 it('Should render CSS styles with JSX', async () => {44 const headerClass = css`45 background-color: blue;46 `47 const template = (48 <>49 <Style />50 <h1 class={headerClass}>Hello!</h1>51 </>52 )53 expect(await toString(template)).toBe(54 '<style id="hono-css">.css-2458908649{background-color:blue}</style><h1 class="css-2458908649">Hello!</h1>'55 )56 })5758 it('Should render CSS with keyframes', async () => {59 const animation = keyframes`60 from {61 opacity: 0;62 }63 to {64 opacity: 1;65 }66 `67 const headerClass = css`68 background-color: blue;69 animation: ${animation} 1s ease-in-out;70 `71 const template = (72 <>73 <Style />74 <h1 class={headerClass}>Hello!</h1>75 </>76 )77 expect(await toString(template)).toBe(78 '<style id="hono-css">.css-1580801783{background-color:blue;animation:css-9294673 1s ease-in-out}@keyframes css-9294673{from{opacity:0}to{opacity:1}}</style><h1 class="css-1580801783">Hello!</h1>'79 )80 })8182 it('Should not output the same class name multiple times.', async () => {83 const headerClass = css`84 background-color: blue;85 `86 const headerClass2 = css`87 background-color: blue;88 `89 const template = (90 <>91 <Style />92 <h1 class={headerClass}>Hello!</h1>93 <h1 class={headerClass2}>Hello2!</h1>94 </>95 )96 expect(await toString(template)).toBe(97 '<style id="hono-css">.css-2458908649{background-color:blue}</style><h1 class="css-2458908649">Hello!</h1><h1 class="css-2458908649">Hello2!</h1>'98 )99 })100101 it('Should render CSS with variable', async () => {102 const headerClass = css`103 background-color: blue;104 content: '${"I'm a variable!"}';105 `106 const template = (107 <>108 <Style />109 <h1 class={headerClass}>Hello!</h1>110 </>111 )112 expect(await toString(template)).toBe(113 '<style id="hono-css">.css-4027435072{background-color:blue;content:\'I\\\'m a variable!\'}</style><h1 class="css-4027435072">Hello!</h1>'114 )115 })116117 it('Should escape </style>', async () => {118 const headerClass = css`119 background-color: blue;120 content: '${'</style>'}';121 `122 const template = (123 <>124 <Style />125 <h1 class={headerClass}>Hello!</h1>126 </>127 )128 expect(await toString(template)).toBe(129 '<style id="hono-css">.css-372954897{background-color:blue;content:\'<\\/style>\'}</style><h1 class="css-372954897">Hello!</h1>'130 )131 })132133 it('Should not escape URL', async () => {134 const headerClass = css`135 background-color: blue;136 background: url('${'http://www.example.com/path/to/file.jpg'}');137 `138 const template = (139 <>140 <Style />141 <h1 class={headerClass}>Hello!</h1>142 </>143 )144 expect(await toString(template)).toBe(145 '<style id="hono-css">.css-1321888780{background-color:blue;background:url(\'http://www.example.com/path/to/file.jpg\')}</style><h1 class="css-1321888780">Hello!</h1>'146 )147 })148149 it('Should render CSS with escaped variable', async () => {150 const headerClass = css`151 background-color: blue;152 content: '${rawCssString('say "Hello!"')}';153 `154 const template = (155 <>156 <Style />157 <h1 class={headerClass}>Hello!</h1>158 </>159 )160 expect(await toString(template)).toBe(161 '<style id="hono-css">.css-2238574885{background-color:blue;content:\'say "Hello!"\'}</style><h1 class="css-2238574885">Hello!</h1>'162 )163 })164165 it('Should render CSS with number', async () => {166 const headerClass = css`167 background-color: blue;168 font-size: ${1}rem;169 `170 const template = (171 <>172 <Style />173 <h1 class={headerClass}>Hello!</h1>174 </>175 )176 expect(await toString(template)).toBe(177 '<style id="hono-css">.css-1847536026{background-color:blue;font-size:1rem}</style><h1 class="css-1847536026">Hello!</h1>'178 )179 })180181 it('Should render CSS with array', async () => {182 const animation = keyframes`183 from {184 opacity: 0;185 }186 to {187 opacity: 1;188 }189 `190 const headerClass = css`191 background-color: blue;192 animation: ${animation} 1s ease-in-out;193 `194 const extendedHeaderClass = css`195 ${headerClass}196 color: red;197 `198 const template = (199 <>200 <Style />201 <h1 class={extendedHeaderClass}>Hello!</h1>202 </>203 )204 expect(await toString(template)).toBe(205 '<style id="hono-css">.css-2558359670{background-color:blue;animation:css-9294673 1s ease-in-out;color:red}@keyframes css-9294673{from{opacity:0}to{opacity:1}}</style><h1 class="css-2558359670">Hello!</h1>'206 )207 })208209 it.runIf(support.nest)(210 'Should be used as a class name for syntax `${className} {`',211 async () => {212 const headerClass = css`213 font-weight: bold;214 `215 const containerClass = css`216 ${headerClass} {217 h1 {218 color: red;219 }220 }221 `222 const template = (223 <>224 <Style />225 <div class={containerClass}>226 <h1 class={headerClass}>Hello!</h1>227 </div>228 </>229 )230 expect(await toString(template)).toBe(231 '<style id="hono-css">.css-4220297002{.css-1032195302{h1{color:red}}}.css-1032195302{font-weight:bold}</style><div class="css-4220297002"><h1 class="css-1032195302">Hello!</h1></div>'232 )233 }234 )235236 it('Should be inserted to global if style string starts with :-hono-root', async () => {237 const globalClass = css`238 :-hono-global {239 html {240 color: red;241 }242 body {243 display: flex;244 }245 }246 `247 const template = (248 <>249 <Style />250 <div class={globalClass}>251 <h1>Hello!</h1>252 </div>253 </>254 )255 expect(await toString(template)).toBe(256 '<style id="hono-css">html{color:red}body{display:flex}</style><div class=""><h1>Hello!</h1></div>'257 )258 })259260 it.runIf(support.nest)(261 'Should be inserted to global if style string starts with :-hono-root and extends class name',262 async () => {263 const headerClass = css`264 display: flex;265 `266 const specialHeaderClass = css`267 :-hono-global {268 ${headerClass} {269 h1 {270 color: red;271 }272 }273 }274 `275 const template = (276 <>277 <Style />278 <div class={specialHeaderClass}>279 <h1>Hello!</h1>280 </div>281 </>282 )283 expect(await toString(template)).toBe(284 '<style id="hono-css">.css-3980466870{h1{color:red}}.css-3980466870{display:flex}</style><div class="css-3980466870"><h1>Hello!</h1></div>'285 )286 }287 )288289 it('Should be inserted as global css if passed css`` to Style component', async () => {290 const headerClass = css`291 font-size: 1rem;292 `293 const template = (294 <>295 <Style>{css`296 html {297 color: red;298 }299 body {300 display: flex;301 }302 `}</Style>303 <div>304 <h1 class={headerClass}>Hello!</h1>305 </div>306 </>307 )308 expect(await toString(template)).toBe(309 '<style id="hono-css">html{color:red}body{display:flex}.css-1740067317{font-size:1rem}</style><div><h1 class="css-1740067317">Hello!</h1></div>'310 )311 })312313 it('Should be ignored :-hono-root inside Style component', async () => {314 const headerClass = css`315 font-size: 1rem;316 `317 const template = (318 <>319 <Style>{css`320 :-hono-global {321 html {322 color: red;323 }324 body {325 display: flex;326 }327 }328 `}</Style>329 <div>330 <h1 class={headerClass}>Hello!</h1>331 </div>332 </>333 )334 expect(await toString(template)).toBe(335 '<style id="hono-css">html{color:red}body{display:flex}.css-1740067317{font-size:1rem}</style><div><h1 class="css-1740067317">Hello!</h1></div>'336 )337 })338339 describe('viewTransition', () => {340 it('Should render CSS with unique view-transition-name', async () => {341 const transition = viewTransition()342 const template = (343 <>344 <Style />345 <h1 class={transition}>Hello!</h1>346 </>347 )348 expect(await toString(template)).toBe(349 '<style id="hono-css">.css-1644952339{view-transition-name:css-1644952339}</style><h1 class="css-1644952339">Hello!</h1>'350 )351 })352353 it('Should render CSS with css and keyframes', async () => {354 const kf = keyframes`355 from {356 opacity: 0;357 }358 to {359 opacity: 1;360 }361 `362 const transition = viewTransition(css`363 ::view-transition-old() {364 animation-name: ${kf};365 }366 ::view-transition-new() {367 animation-name: ${kf};368 }369 `)370 const headerClass = css`371 ${transition}372 background-color: blue;373 `374 const template = (375 <>376 <Style />377 <h1 class={headerClass}>Hello!</h1>378 </>379 )380 expect(await toString(template)).toBe(381 '<style id="hono-css">.css-1245070278{view-transition-name:css-399742870;background-color:blue}@keyframes css-9294673{from{opacity:0}to{opacity:1}}::view-transition-old(css-399742870){animation-name:css-9294673}::view-transition-new(css-399742870){animation-name:css-9294673}</style><h1 class="css-1245070278">Hello!</h1>'382 )383 })384385 it('Should works as a template tag function', async () => {386 const kf = keyframes`387 from {388 opacity: 0;389 }390 to {391 opacity: 1;392 }393 `394 const transition = viewTransition`395 ::view-transition-old() {396 animation-name: ${kf};397 }398 ::view-transition-new() {399 animation-name: ${kf};400 }401 `402 const headerClass = css`403 ${transition}404 background-color: blue;405 `406 const template = (407 <>408 <Style />409 <h1 class={headerClass}>Hello!</h1>410 </>411 )412 expect(await toString(template)).toBe(413 '<style id="hono-css">.css-1245070278{view-transition-name:css-399742870;background-color:blue}@keyframes css-9294673{from{opacity:0}to{opacity:1}}::view-transition-old(css-399742870){animation-name:css-9294673}::view-transition-new(css-399742870){animation-name:css-9294673}</style><h1 class="css-1245070278">Hello!</h1>'414 )415 })416 })417418 it.runIf(support.nest)('Should render sub CSS with keyframe', async () => {419 const headerClass = css`420 background-color: blue;421 ${[1, 2].map(422 (i) =>423 css`424 :nth-child(${i}) {425 color: red;426 }427 `428 )}429 `430 const template = (431 <>432 <Style />433 <h1 class={headerClass}>Hello!</h1>434 </>435 )436 expect(await toString(template)).toBe(437 '<style id="hono-css">.css-1539881271{background-color:blue;:nth-child(1){color:red}:nth-child(2){color:red}}</style><h1 class="css-1539881271">Hello!</h1>'438 )439 })440441 it('Should be generated deferent class name for deferent first line comment even if the content is the same', async () => {442 const headerClassA = css`443 /* class A */444 display: flex;445 `446 const headerClassB = css`447 /* class B */448 display: flex;449 `450 const template = (451 <>452 <Style />453 <h1 class={headerClassA}>Hello!</h1>454 <h1 class={headerClassB}>Hello!</h1>455 </>456 )457 expect(await toString(template)).toBe(458 '<style id="hono-css">.css-3170754153{display:flex}.css-896513246{display:flex}</style><h1 class="css-3170754153">Hello!</h1><h1 class="css-896513246">Hello!</h1>'459 )460 })461462 describe('Booleans, Null, and Undefined Are Ignored', () => {463 it.each([true, false, undefined, null])('%s', async (value) => {464 const headerClass = css`465 ${value}466 background-color: blue;467 `468 const template = (469 <>470 <Style />471 <h1 class={headerClass}>Hello!</h1>472 </>473 )474 expect(await toString(template)).toBe(475 '<style id="hono-css">.css-2458908649{background-color:blue}</style><h1 class="css-2458908649">Hello!</h1>'476 )477 })478479 it('falsy value', async () => {480 const value = 0481 const headerClass = css`482 padding: ${value};483 `484 const template = (485 <>486 <Style />487 <h1 class={headerClass}>Hello!</h1>488 </>489 )490 expect(await toString(template)).toBe(491 '<style id="hono-css">.css-478287868{padding:0}</style><h1 class="css-478287868">Hello!</h1>'492 )493 })494 })495 })496}
modified:src/helper/css/index.test.tsx+1 added-1 removed
5 unmodified lines
6import type { JSXNode } from '../../jsx'7import { Suspense, renderToReadableStream } from '../../jsx/streaming'8import type { HtmlEscapedString } from '../../utils/html'9import { HtmlEscapedCallbackPhase, resolveCallback } from '../../utils/html'10import { renderTest } from './common.test'10import { renderTest } from './common.case.test'11import { css, cx, keyframes, viewTransition, rawCssString, Style, createCssContext } from './index'1213async function toString(14 template: JSXNode | Promise<HtmlEscapedString> | Promise<string> | HtmlEscapedString
241 unmodified lines
modified:src/jsx/dom/css.test.tsx+1 added-1 removed
4 unmodified lines
5import { jsx } from '..'6import type { JSXNode } from '..'7import { Style, css, rawCssString, createCssContext } from '../../helper/css'8import { minify } from '../../helper/css/common'9import { renderTest } from '../../helper/css/common.test'9import { renderTest } from '../../helper/css/common.case.test'10import { render } from '.'1112describe('Style and css for jsx/dom', () => {13 beforeAll(() => {
96 unmodified lines
modified:src/jsx/dom/index.test.tsx-3 removed
1065 unmodified lines
1066 })10671068 describe('SVG', () => {1069 it('simple', () => {1070 const createElementSpy = vi.spyOn(dom.window.document, 'createElement')1071 const createElementNSSpy = vi.spyOn(dom.window.document, 'createElementNS')10721070 const App = () => {1071 return (1072 <svg>1073 <circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />
52 unmodified lines
modified:src/middleware/jsx-renderer/index.ts+1 added
modified:src/router/linear-router/router.test.ts+38 added-347 removed
1import { UnsupportedPathError } from '../../router'2import { runTest } from '../common.case.test'3import { LinearRouter } from './router'43describe('Basic Usage', () => {4 const router = new LinearRouter<string>()56 router.add('GET', '/hello', 'get hello')7 router.add('POST', '/hello', 'post hello')8 router.add('PURGE', '/hello', 'purge hello')910 it('get, post hello', async () => {11 let [res] = router.match('GET', '/hello')12 expect(res.length).toBe(1)13 expect(res[0][0]).toEqual('get hello')14 ;[res] = router.match('POST', '/hello')15 expect(res.length).toBe(1)16 expect(res[0][0]).toEqual('post hello')17 ;[res] = router.match('PURGE', '/hello')18 expect(res.length).toBe(1)19 expect(res[0][0]).toEqual('purge hello')20 ;[res] = router.match('PUT', '/hello')21 expect(res.length).toBe(0)22 ;[res] = router.match('GET', '/')23 expect(res.length).toBe(0)5describe('LinearRouter', () => {6 runTest({7 skip: [8 {9 reason: 'UnsupportedPath',10 tests: ['Multi match > `params` per a handler > GET /entry/123/show'],11 },12 {13 reason: 'LinearRouter allows trailing slashes',14 tests: ['Trailing slash > GET /book/'],15 },16 ],17 newRouter: () => new LinearRouter(),18 })25})1927describe('Complex', () => {28 let router: LinearRouter<string>29 beforeEach(() => {30 router = new LinearRouter<string>()31 })20 describe('Multi match', () => {21 describe('`params` per a handler', () => {22 const router = new LinearRouter<string>()2333 it('Named Param', async () => {34 router.add('GET', '/entry/:id', 'get entry')35 const [res] = router.match('GET', '/entry/123')36 expect(res.length).toBe(1)37 expect(res[0][0]).toEqual('get entry')38 expect(res[0][1]['id']).toBe('123')39 })24 beforeEach(() => {25 router.add('ALL', '*', 'middleware a')26 router.add('GET', '/entry/:id/*', 'middleware b')27 router.add('GET', '/entry/:id/:action', 'action')28 })2941 it('Wildcard', async () => {42 router.add('GET', '/wild/*/card', 'get wildcard')4344 let [res] = router.match('GET', '/wild/xxx/card')45 expect(res.length).toBe(1)46 expect(res[0][0]).toEqual('get wildcard')47 ;[res] = router.match('GET', '/wild/xxx/card/yyy')48 expect(res.length).toBe(0)30 it('GET /entry/123/show', async () => {31 expect(() => {32 router.match('GET', '/entry/123/show')33 }).toThrowError(UnsupportedPathError)34 })35 })36 })3751 it('Default', async () => {52 router.add('GET', '/api/abc', 'get api')53 router.add('GET', '/api/*', 'fallback')54 let [res] = router.match('GET', '/api/abc')55 expect(res.length).toBe(2)56 expect(res[0][0]).toEqual('get api')57 expect(res[1][0]).toEqual('fallback')58 ;[res] = router.match('GET', '/api/def')59 expect(res.length).toBe(1)60 expect(res[0][0]).toEqual('fallback')61 })38 describe('Trailing slash', () => {39 const router = new LinearRouter<string>()4063 it('Regexp', async () => {64 router.add('GET', '/post/:date{[0-9]+}/:title{[a-z]+}', 'get post')65 let [res] = router.match('GET', '/post/20210101/hello')66 expect(res.length).toBe(1)67 expect(res[0][0]).toEqual('get post')68 expect(res[0][1]['date']).toBe('20210101')69 expect(res[0][1]['title']).toBe('hello')70 ;[res] = router.match('GET', '/post/onetwothree')71 expect(res.length).toBe(0)72 ;[res] = router.match('GET', '/post/123/123')73 expect(res.length).toBe(0)74 })7576 it('/*', async () => {77 router.add('GET', '/api/*', 'auth middleware')78 router.add('GET', '/api', 'top')79 router.add('GET', '/api/posts', 'posts')80 router.add('GET', '/api/*', 'fallback')8182 let [res] = router.match('GET', '/api')83 expect(res.length).toBe(3)84 expect(res[0][0]).toEqual('auth middleware')85 expect(res[1][0]).toEqual('top')86 expect(res[2][0]).toEqual('fallback')87 ;[res] = router.match('GET', '/api/posts')88 expect(res.length).toBe(3)89 expect(res[0][0]).toEqual('auth middleware')90 expect(res[1][0]).toEqual('posts')91 expect(res[2][0]).toEqual('fallback')92 })9394 it('Should support regexp quantifiers', async () => {95 router.add('GET', '/year/:year{[0-9]{4}}/month/:month{[0-9]{1,2}}', 'handler')96 const [res] = router.match('GET', '/year/2024/month/2')97 expect(res.length).toBe(1)98 expect(res[0][0]).toBe('handler')99 expect(res[0][1]['year']).toBe('2024')100 expect(res[0][1]['month']).toBe('2')101 })102})103104describe('Multi match', () => {105 const router = new LinearRouter<string>()106107 describe('Blog', () => {108 router.add('ALL', '*', 'middleware a')109 router.add('GET', '*', 'middleware b')110 router.add('GET', '/entry', 'get entries')111 router.add('POST', '/entry/*', 'middleware c')112 router.add('POST', '/entry', 'post entry')113 router.add('GET', '/entry/:id', 'get entry')114 router.add('GET', '/entry/:id/comment/:comment_id', 'get comment')115 it('GET /', async () => {116 const [res] = router.match('GET', '/')117 expect(res.length).toBe(2)118 expect(res[0][0]).toEqual('middleware a')119 expect(res[0][1]).toEqual({})120 expect(res[1][0]).toEqual('middleware b')121 expect(res[1][1]).toEqual({})41 beforeEach(() => {42 router.add('GET', '/book', 'GET /book')43 router.add('GET', '/book/:id', 'GET /book/:id')44 })123 it('GET /entry/123', async () => {124 const [res] = router.match('GET', '/entry/123')125 expect(res.length).toBe(3)126 expect(res[0][0]).toEqual('middleware a')127 expect(res[0][1]).toEqual({})128 expect(res[1][0]).toEqual('middleware b')129 expect(res[1][1]).toEqual({})130 expect(res[2][0]).toEqual('get entry')131 expect(res[2][1]['id']).toBe('123')132 })133 it('GET /entry/123/comment/456', async () => {134 const [res] = router.match('GET', '/entry/123/comment/456')135 expect(res.length).toBe(3)136 expect(res[0][0]).toEqual('middleware a')137 expect(res[0][1]).toEqual({})138 expect(res[1][0]).toEqual('middleware b')139 expect(res[1][1]).toEqual({})140 expect(res[2][0]).toEqual('get comment')141 expect(res[2][1]['id']).toBe('123')142 expect(res[2][1]['comment_id']).toBe('456')143 })144 it('POST /entry', async () => {145 const [res] = router.match('POST', '/entry')146 expect(res.length).toBe(3)147 expect(res[0][0]).toEqual('middleware a')148 expect(res[1][0]).toEqual('middleware c')149 expect(res[2][0]).toEqual('post entry')150 })151 it('DELETE /entry', async () => {152 const [res] = router.match('DELETE', '/entry')4546 it('GET /book/', () => {47 const [res] = router.match('GET', '/book/')48 expect(res.length).toBe(1)154 expect(res[0][0]).toEqual('middleware a')49 expect(res[0][0]).toBe('GET /book')50 })51 })52})158159describe('page', () => {160 const router = new LinearRouter<string>()161 router.add('GET', '/page', 'page')162 router.add('ALL', '*', 'fallback') // or '*'163 it('GET /page', async () => {164 const [res] = router.match('GET', '/page')165 expect(res.length).toBe(2)166 expect(res[0][0]).toEqual('page')167 expect(res[1][0]).toEqual('fallback')168 })169})170171describe('Optional route', () => {172 const router = new LinearRouter<string>()173 router.add('GET', '/api/animals/:type?', 'animals')174 it('GET /api/animals/dog', async () => {175 const [res] = router.match('GET', '/api/animals/dog')176 expect(res[0][0]).toEqual('animals')177 expect(res[0][1]['type']).toBe('dog')178 })179 it('GET /api/animals', async () => {180 const [res] = router.match('GET', '/api/animals')181 expect(res[0][0]).toEqual('animals')182 expect(res[0][1]['type']).toBeUndefined()183 })184 router.add('GET', '/v1/:version?/:platform?', 'result')185 it('GET /v1/123/abc', () => {186 const [res] = router.match('GET', '/v1/123/abc')187 expect(res.length).toBe(1)188 expect(res[0][0]).toEqual('result')189 expect(res[0][1]['version']).toBe('123')190 expect(res[0][1]['platform']).toBe('abc')191 })192 it('GET /v1/123', () => {193 const [res] = router.match('GET', '/v1/123')194 expect(res.length).toBe(1)195 expect(res[0][0]).toEqual('result')196 expect(res[0][1]['version']).toBe('123')197 expect(res[0][1]['platform']).toBeUndefined()198 })199 it('GET /v1', () => {200 const [res] = router.match('GET', '/v1')201 expect(res.length).toBe(1)202 expect(res[0][0]).toEqual('result')203 expect(res[0][1]['version']).toBeUndefined()204 expect(res[0][1]['platform']).toBeUndefined()205 })206})207208describe('Trailing slash', () => {209 const router = new LinearRouter<string>()210 router.add('GET', '/book', 'GET /book')211 router.add('GET', '/book/:id', 'GET /book/:id')212 it('GET /book', () => {213 const [res] = router.match('GET', '/book')214 expect(res[0][0]).toEqual('GET /book')215 })216 it('GET /book/', () => {217 const [res] = router.match('GET', '/book/')218 expect(res[0][0]).toEqual('GET /book')219 })220})221222describe('Same path', () => {223 const router = new LinearRouter<string>()224 router.add('GET', '/hey', 'Middleware A')225 router.add('GET', '/hey', 'Middleware B')226 it('GET /hey', () => {227 const [res] = router.match('GET', '/hey')228 expect(res.length).toBe(2)229 expect(res[0][0]).toEqual('Middleware A')230 expect(res[1][0]).toEqual('Middleware B')231 })232})233234describe('Including slashes', () => {235 const router = new LinearRouter<string>()236 router.add('GET', '/js/:filename{[a-z0-9/]+.js}', 'any file')237 router.add('GET', '/js/main.js', 'main.js')238239 it('GET /js/main.js', () => {240 const [res] = router.match('GET', '/js/main.js')241 expect(res.length).toBe(2)242 expect(res[0][0]).toEqual('any file')243 expect(res[0][1]).toEqual({ filename: 'main.js' })244 expect(res[1][0]).toEqual('main.js')245 expect(res[1][1]).toEqual({})246 })247248 it('get /js/chunk/123.js', () => {249 const [res] = router.match('GET', '/js/chunk/123.js')250 expect(res[0][0]).toEqual('any file')251 expect(res[0][1]).toEqual({ filename: 'chunk/123.js' })252 })253254 it('get /js/chunk/nest/123.js', () => {255 const [res] = router.match('GET', '/js/chunk/nest/123.js')256 expect(res[0][0]).toEqual('any file')257 expect(res[0][1]).toEqual({ filename: 'chunk/nest/123.js' })258 })259})260261describe('REST API', () => {262 const router = new LinearRouter<string>()263 router.add('GET', '/users/:username{[a-z]+}', 'profile')264 router.add('GET', '/users/:username{[a-z]+}/posts', 'posts')265266 it('GET /users/hono', () => {267 const [res] = router.match('GET', '/users/hono')268 expect(res.length).toBe(1)269 expect(res[0][0]).toEqual('profile')270 })271272 it('GET /users/hono/posts', () => {273 const [res] = router.match('GET', '/users/hono/posts')274 expect(res.length).toBe(1)275 expect(res[0][0]).toEqual('posts')276 })277})278279describe('star', () => {280 const router = new LinearRouter<string>()281 router.add('get', '/', '/')282 router.add('get', '/*', '/*')283 router.add('get', '*', '*')284285 router.add('get', '/x', '/x')286 router.add('get', '/x/*', '/x/*')287288 it('top', async () => {289 const [res] = router.match('get', '/')290 expect(res.length).toBe(3)291 expect(res[0][0]).toEqual('/')292 expect(res[1][0]).toEqual('/*')293 expect(res[2][0]).toEqual('*')294 })295296 it('Under a certain path', async () => {297 const [res] = router.match('get', '/x')298 expect(res.length).toBe(4)299 expect(res[0][0]).toEqual('/*')300 expect(res[1][0]).toEqual('*')301 expect(res[2][0]).toEqual('/x')302 expect(res[3][0]).toEqual('/x/*')303 })304})305306describe('Duplicate param name', () => {307 it('self', () => {308 const router = new LinearRouter<string>()309 router.add('GET', '/:id/:id', 'foo')310 const [res] = router.match('GET', '/123/456')311 expect(res.length).toBe(1)312 expect(res[0][0]).toEqual('foo')313 expect(res[0][1]['id']).toBe('123')314 })315316 it('parent', () => {317 const router = new LinearRouter<string>()318 router.add('GET', '/:id/:action', 'foo')319 router.add('GET', '/posts/:id', 'bar')320 const [res] = router.match('GET', '/posts/get')321 expect(res.length).toBe(2)322 expect(res[0][0]).toEqual('foo')323 expect(res[0][1]['id']).toBe('posts')324 expect(res[0][1]['action']).toBe('get')325 expect(res[1][0]).toEqual('bar')326 expect(res[1][1]['id']).toBe('get')327 })328329 it('child', () => {330 const router = new LinearRouter<string>()331 router.add('GET', '/posts/:id', 'foo')332 router.add('GET', '/:id/:action', 'bar')333 const [res] = router.match('GET', '/posts/get')334 expect(res.length).toBe(2)335 expect(res[0][0]).toEqual('foo')336 expect(res[0][1]['id']).toBe('get')337 expect(res[1][0]).toEqual('bar')338 expect(res[1][1]['id']).toBe('posts')339 expect(res[1][1]['action']).toBe('get')340 })341})342343describe('Routing with a hostname', () => {344 const router = new LinearRouter<string>()345 router.add('get', 'www1.example.com/hello', 'www1')346 router.add('get', 'www2.example.com/hello', 'www2')347 it('GET www1.example.com/hello', () => {348 const [res] = router.match('get', 'www1.example.com/hello')349 expect(res.length).toBe(1)350 expect(res[0][0]).toEqual('www1')351 })352 it('GET www2.example.com/hello', () => {353 const [res] = router.match('get', 'www2.example.com/hello')354 expect(res.length).toBe(1)355 expect(res[0][0]).toEqual('www2')356 })357 it('GET /hello', () => {358 const [res] = router.match('get', '/hello')359 expect(res.length).toBe(0)360 })361})
modified:src/router/pattern-router/router.test.ts+28 added-565 removed
1import { UnsupportedPathError } from '../../router'2import { runTest } from '../common.case.test'3import { PatternRouter } from './router'44describe('Basic', () => {5 const router = new PatternRouter<string>()6 router.add('GET', '/hello', 'get hello')7 router.add('POST', '/hello', 'post hello')8 router.add('PURGE', '/hello', 'purge hello')910 it('get, post, purge hello', async () => {11 let [res] = router.match('GET', '/hello')12 expect(res.length).toBe(1)13 expect(res[0][0]).toEqual('get hello')14 ;[res] = router.match('POST', '/hello')15 expect(res.length).toBe(1)16 expect(res[0][0]).toEqual('post hello')17 ;[res] = router.match('PURGE', '/hello')18 expect(res.length).toBe(1)19 expect(res[0][0]).toEqual('purge hello')20 ;[res] = router.match('PUT', '/hello')21 expect(res.length).toBe(0)22 ;[res] = router.match('GET', '/')23 expect(res.length).toBe(0)5describe('Pattern', () => {6 runTest({7 skip: [8 {9 reason: 'UnsupportedPath',10 tests: ['Duplicate param name > self'],11 },12 {13 reason: 'PatternRouter allows trailing slashes',14 tests: ['Trailing slash > GET /book/'],15 },16 ],17 newRouter: () => new PatternRouter(),18 })25})1927describe('Complex', () => {28 const router = new PatternRouter<string>()2930 it('Named Param', async () => {31 router.add('GET', '/entry/:id', 'get entry')32 const [res] = router.match('GET', '/entry/123')33 expect(res.length).toBe(1)34 expect(res[0][0]).toEqual('get entry')35 expect(res[0][1]['id']).toBe('123')36 })3738 it('Named param with trailing wildcard', async () => {39 router.add('GET', '/article/:id/*', 'get article with wildcard')40 let [res] = router.match('GET', '/article/123')41 expect(res.length).toBe(1)42 expect(res[0][0]).toEqual('get article with wildcard')43 expect(res[0][1]['id']).toBe('123')44 ;[res] = router.match('GET', '/article/123/action')45 expect(res.length).toBe(1)46 expect(res[0][0]).toEqual('get article with wildcard')47 expect(res[0][1]['id']).toBe('123')48 })4950 it('Wildcard', async () => {51 router.add('GET', '/wild/*/card', 'get wildcard')52 const [res] = router.match('GET', '/wild/xxx/card')53 expect(res.length).toBe(1)54 expect(res[0][0]).toEqual('get wildcard')55 })5657 it('Default', async () => {58 router.add('GET', '/api/*', 'fallback')59 router.add('GET', '/api/abc', 'get api')60 let [res] = router.match('GET', '/api/abc')61 expect(res.length).toBe(2)62 expect(res[0][0]).toEqual('fallback')63 expect(res[1][0]).toEqual('get api')64 ;[res] = router.match('GET', '/api/def')65 expect(res.length).toBe(1)66 expect(res[0][0]).toEqual('fallback')67 })6869 it('Regexp', async () => {70 router.add('GET', '/post/:date{[0-9]+}/:title{[a-z]+}', 'get post')71 let [res] = router.match('GET', '/post/20210101/hello')72 expect(res.length).toBe(1)73 expect(res[0][0]).toEqual('get post')74 expect(res[0][1]['date']).toBe('20210101')75 expect(res[0][1]['title']).toBe('hello')76 ;[res] = router.match('GET', '/post/onetwothree')77 expect(res.length).toBe(0)78 ;[res] = router.match('GET', '/post/123/123')79 expect(res.length).toBe(0)80 })8182 it('Should support regexp quantifiers', async () => {83 router.add('GET', '/year/:year{[0-9]{4}}/month/:month{[0-9]{1,2}}', 'handler')84 const [res] = router.match('GET', '/year/2024/month/2')85 expect(res.length).toBe(1)86 expect(res[0][0]).toBe('handler')87 expect(res[0][1]['year']).toBe('2024')88 expect(res[0][1]['month']).toBe('2')89 })90})9192describe('Multi match', () => {93 const router = new PatternRouter<string>()9495 describe('Blog', () => {96 router.add('ALL', '*', 'middleware a')97 router.add('GET', '*', 'middleware b')98 router.add('GET', '/entry', 'get entries')99 router.add('POST', '/entry/*', 'middleware c')100 router.add('POST', '/entry', 'post entry')101 router.add('GET', '/entry/:id', 'get entry')102 router.add('GET', '/entry/:id/comment/:comment_id', 'get comment')103 it('GET /', async () => {104 const [res] = router.match('GET', '/')105 expect(res.length).toBe(2)106 expect(res[0][0]).toEqual('middleware a')107 expect(res[1][0]).toEqual('middleware b')20 describe('Duplicate param name', () => {21 it('self', () => {22 const router = new PatternRouter<string>()23 expect(() => {24 router.add('GET', '/:id/:id', 'foo')25 }).toThrowError(UnsupportedPathError)26 })109 it('GET /entry/123', async () => {110 const [res] = router.match('GET', '/entry/123')111 expect(res.length).toBe(3)112 expect(res[0][0]).toEqual('middleware a')113 expect(res[0][1]).toEqual({})114 expect(res[1][0]).toEqual('middleware b')115 expect(res[1][1]).toEqual({})116 expect(res[2][0]).toEqual('get entry')117 expect(res[2][1]['id']).toBe('123')118 })119 it('GET /entry/123/comment/456', async () => {120 const [res] = router.match('GET', '/entry/123/comment/456')121 expect(res.length).toBe(3)122 expect(res[0][0]).toEqual('middleware a')123 expect(res[1][1]).toEqual({})124 expect(res[1][0]).toEqual('middleware b')125 expect(res[1][1]).toEqual({})126 expect(res[2][0]).toEqual('get comment')127 expect(res[2][1]['id']).toBe('123')128 expect(res[2][1]['comment_id']).toBe('456')129 })130 it('POST /entry', async () => {131 const [res] = router.match('POST', '/entry')132 expect(res.length).toBe(3)133 expect(res[0][0]).toEqual('middleware a')134 expect(res[1][0]).toEqual('middleware c')135 expect(res[2][0]).toEqual('post entry')136 })137 it('DELETE /entry', async () => {138 const [res] = router.match('DELETE', '/entry')139 expect(res.length).toBe(1)140 expect(res[0][0]).toEqual('middleware a')141 })27 })143})144145describe('Fallback', () => {146 const router = new PatternRouter<string>()147 router.add('POST', '/entry', 'post entry')148 router.add('POST', '/entry/*', 'fallback')149 router.add('GET', '/entry/:id', 'get entry')150 it('POST /entry', async () => {151 const [res] = router.match('POST', '/entry')152 expect(res.length).toBe(2)153 expect(res[0][0]).toEqual('post entry')154 expect(res[1][0]).toEqual('fallback')155 })156})157158describe('page', () => {159 const router = new PatternRouter<string>()160 router.add('GET', '/page', 'page')161 router.add('ALL', '*', 'fallback') // or '*'162 it('GET /page', async () => {163 const [res] = router.match('GET', '/page')164 expect(res.length).toBe(2)165 expect(res[0][0]).toEqual('page')166 expect(res[1][0]).toEqual('fallback')167 })168})169170describe('Optional route', () => {171 const router = new PatternRouter<string>()172 router.add('GET', '/api/animals/:type?', 'animals')173 it('GET /api/animals/dog', async () => {174 const [res] = router.match('GET', '/api/animals/dog')175 expect(res[0][0]).toEqual('animals')176 expect(res[0][1]['type']).toBe('dog')177 })178 it('GET /api/animals', async () => {179 const [res] = router.match('GET', '/api/animals')180 expect(res[0][0]).toEqual('animals')181 expect(res[0][1]['type']).toBeUndefined()182 })183 router.add('GET', '/v1/:version?/:platform?', 'result')184 it('GET /v1/123/abc', () => {185 const [res] = router.match('GET', '/v1/123/abc')186 expect(res.length).toBe(1)187 expect(res[0][0]).toEqual('result')188 expect(res[0][1]['version']).toBe('123')189 expect(res[0][1]['platform']).toBe('abc')190 })191 it('GET /v1/123', () => {192 const [res] = router.match('GET', '/v1/123')193 expect(res.length).toBe(1)194 expect(res[0][0]).toEqual('result')195 expect(res[0][1]['version']).toBe('123')196 expect(res[0][1]['platform']).toBeUndefined()197 })198 it('GET /v1', () => {199 const [res] = router.match('GET', '/v1')200 expect(res.length).toBe(1)201 expect(res[0][0]).toEqual('result')202 expect(res[0][1]['version']).toBeUndefined()203 expect(res[0][1]['platform']).toBeUndefined()204 })205})206207describe('routing order with named parameters', () => {208 const router = new PatternRouter<string>()209 router.add('GET', '/book/a', 'no-slug')210 router.add('GET', '/book/:slug', 'slug')211 router.add('GET', '/book/b', 'no-slug-b')212 it('GET /book/a', async () => {213 const [res] = router.match('GET', '/book/a')214 expect(res[0][0]).toEqual('no-slug')215 expect(res[0][1]).toEqual({})216 expect(res[1][0]).toEqual('slug')217 expect(res[1][1]['slug']).toBe('a')218 })219 it('GET /book/foo', async () => {220 const [res] = router.match('GET', '/book/foo')221 expect(res[0][0]).toEqual('slug')222 expect(res[0][1]['slug']).toBe('foo')223 })224 it('GET /book/b', async () => {225 const [res] = router.match('GET', '/book/b')226 expect(res.length).toBe(2)227 expect(res[0][0]).toEqual('slug')228 expect(res[0][1]['slug']).toBe('b')229 expect(res[1][0]).toEqual('no-slug-b')230 expect(res[1][1]).toEqual({})231 })232})233234describe('Trailing slash', () => {28 describe('Trailing slash', () => {235 const router = new PatternRouter<string>()236 router.add('GET', '/book', 'GET /book')237 router.add('GET', '/book/:id', 'GET /book/:id')238 it('GET /book', () => {239 const [res] = router.match('GET', '/book')240 expect(res.length).toBe(1)241 expect(res[0][0]).toEqual('GET /book')242 })243 it('GET /book/', () => {244 const [res] = router.match('GET', '/book/')245 expect(res.length).toBe(1)246 expect(res[0][0]).toEqual('GET /book')247 })248})249250describe('Same path', () => {251 const router = new PatternRouter<string>()252 router.add('GET', '/hey', 'Middleware A')253 router.add('GET', '/hey', 'Middleware B')254 it('GET /hey', () => {255 const [res] = router.match('GET', '/hey')256 expect(res.length).toBe(2)257 expect(res[0][0]).toEqual('Middleware A')258 expect(res[1][0]).toEqual('Middleware B')259 })260})261262describe('Including slashes', () => {263 const router = new PatternRouter<string>()264 router.add('GET', '/js/:filename{[a-z0-9/]+.js}', 'any file')265 router.add('GET', '/js/main.js', 'main.js')266267 it('GET /js/main.js', () => {268 const [res] = router.match('GET', '/js/main.js')269 expect(res[0][0]).toEqual('any file')270 expect(res[0][1]).toEqual({ filename: 'main.js' })271 expect(res[1][0]).toEqual('main.js')272 expect(res[1][1]).toEqual({})273 })274275 it('get /js/chunk/123.js', () => {276 const [res] = router.match('GET', '/js/chunk/123.js')277 expect(res[0][0]).toEqual('any file')278 expect(res[0][1]).toEqual({ filename: 'chunk/123.js' })279 })280281 it('get /js/chunk/nest/123.js', () => {282 const [res] = router.match('GET', '/js/chunk/nest/123.js')283 expect(res[0][0]).toEqual('any file')284 expect(res[0][1]).toEqual({ filename: 'chunk/nest/123.js' })285 })286})287288describe('REST API', () => {289 const router = new PatternRouter<string>()290 router.add('GET', '/users/:username{[a-z]+}', 'profile')291 router.add('GET', '/users/:username{[a-z]+}/posts', 'posts')292293 it('GET /users/hono', () => {294 const [res] = router.match('GET', '/users/hono')295 expect(res.length).toBe(1)296 expect(res[0][0]).toEqual('profile')297 })298299 it('GET /users/hono/posts', () => {300 const [res] = router.match('GET', '/users/hono/posts')301 expect(res.length).toBe(1)302 expect(res[0][0]).toEqual('posts')303 })304})305306describe('Duplicate param name', () => {307 it('self', () => {29 const router = new PatternRouter<string>()309 expect(() => {310 router.add('GET', '/:id/:id', 'foo')311 }).toThrowError(UnsupportedPathError)312 })30314 it('parent', () => {315 const router = new PatternRouter<string>()316 router.add('GET', '/:id/:action', 'foo')317 router.add('GET', '/posts/:id', 'bar')318 const [res] = router.match('GET', '/posts/get')319 expect(res.length).toBe(2)320 expect(res[0][0]).toEqual('foo')321 expect(res[0][1]['id']).toBe('posts')322 expect(res[0][1]['action']).toBe('get')323 expect(res[1][0]).toEqual('bar')324 expect(res[1][1]['id']).toBe('get')325 })326327 it('child', () => {328 const router = new PatternRouter<string>()329 router.add('GET', '/posts/:id', 'foo')330 router.add('GET', '/:id/:action', 'bar')331 const [res] = router.match('GET', '/posts/get')332 expect(res.length).toBe(2)333 expect(res[0][0]).toEqual('foo')334 expect(res[0][1]['id']).toBe('get')335 expect(res[1][0]).toEqual('bar')336 expect(res[1][1]['id']).toBe('posts')337 expect(res[1][1]['action']).toBe('get')338 })339340 it('hierarchy', () => {341 const router = new PatternRouter<string>()342 router.add('get', '/posts/:id/comments/:comment_id', 'foo')343 expect(() => {344 router.add('get', '/posts/:id', 'bar')345 }).not.toThrowError()346 })347348 it('regular expression', () => {349 const router = new PatternRouter<string>()350 router.add('get', '/:id/:action{create|update}', 'foo')351 expect(() => {352 router.add('get', '/:id/:action{delete}', 'bar')353 }).not.toThrowError()354 })355})356357describe('Sort Order', () => {358 describe('Basic', () => {359 const router = new PatternRouter<string>()360 router.add('get', '*', 'a')361 router.add('get', '/page', '/page')362 router.add('get', '/:slug', '/:slug')363364 it('get /page', () => {365 const [res] = router.match('get', '/page')366 expect(res.length).toBe(3)367 expect(res[0][0]).toEqual('a')368 expect(res[1][0]).toEqual('/page')369 expect(res[2][0]).toEqual('/:slug')31 beforeEach(() => {32 router.add('GET', '/book', 'GET /book')33 router.add('GET', '/book/:id', 'GET /book/:id')34 })371 })35373 describe('With Named path', () => {374 const router = new PatternRouter<string>()375 router.add('get', '*', 'a')376 router.add('get', '/posts/:id', '/posts/:id')377 router.add('get', '/:type/:id', '/:type/:id')378379 it('get /posts/123', () => {380 const [res] = router.match('get', '/posts/123')381 expect(res.length).toBe(3)382 expect(res[0][0]).toEqual('a')36 it('GET /book/', () => {37 const [res] = router.match('GET', '/book/')38 expect(res.length).toBe(1)39 expect(res[0][0]).toBe('GET /book')383 expect(res[1][0]).toEqual('/posts/:id')384 expect(res[2][0]).toEqual('/:type/:id')40 })41 })387388 describe('With Wildcards', () => {389 const router = new PatternRouter<string>()390 router.add('get', '/api/*', '1st')391 router.add('get', '/api/*', '2nd')392 router.add('get', '/api/posts/:id', '3rd')393 router.add('get', '/api/*', '4th')394395 it('get /api/posts/123', () => {396 const [res] = router.match('get', '/api/posts/123')397 expect(res.length).toBe(4)398 expect(res[0][0]).toEqual('1st')399 expect(res[1][0]).toEqual('2nd')400 expect(res[2][0]).toEqual('3rd')401 expect(res[3][0]).toEqual('4th')402 })403 })404405 describe('With special Wildcard', () => {406 const router = new PatternRouter<string>()407 router.add('get', '/posts', '/posts') // 1.1408 router.add('get', '/posts/*', '/posts/*') // 1.2409 router.add('get', '/posts/:id', '/posts/:id') // 2.3410411 it('get /posts', () => {412 const [res] = router.match('get', '/posts')413414 expect(res.length).toBe(2)415 expect(res[0][0]).toEqual('/posts')416 expect(res[1][0]).toEqual('/posts/*')417 })418 })419420 describe('Complex', () => {421 const router = new PatternRouter<string>()422 router.add('get', '/api', 'a') // not match423 router.add('get', '/api/*', 'b') // match424 router.add('get', '/api/:type', 'c') // not match425 router.add('get', '/api/:type/:id', 'd') // match426 router.add('get', '/api/posts/:id', 'e') // match427 router.add('get', '/api/posts/123', 'f') // match428 router.add('get', '/*/*/:id', 'g') // match429 router.add('get', '/api/posts/*/comment', 'h') // not match430 router.add('get', '*', 'i') // match431 router.add('get', '*', 'j') // match432433 it('get /api/posts/123', () => {434 const [res] = router.match('get', '/api/posts/123')435 expect(res.length).toBe(7)436 expect(res[0][0]).toEqual('b')437 expect(res[1][0]).toEqual('d')438 expect(res[2][0]).toEqual('e')439 expect(res[3][0]).toEqual('f')440 expect(res[4][0]).toEqual('g')441 expect(res[5][0]).toEqual('i')442 expect(res[6][0]).toEqual('j')443 })444 })445446 describe('Multi match', () => {447 const router = new PatternRouter<string>()448 router.add('get', '*', 'GET *') // 0.1449 router.add('get', '/abc/*', 'GET /abc/*') // 1.2450 router.add('get', '/abc/edf', 'GET /abc/edf') // 2.3451 router.add('get', '/abc/*/ghi/jkl', 'GET /abc/*/ghi/jkl') // 4.4452 it('get /abc/edf', () => {453 const [res] = router.match('get', '/abc/edf')454 expect(res.length).toBe(3)455 expect(res[0][0]).toEqual('GET *')456 expect(res[1][0]).toEqual('GET /abc/*')457 expect(res[2][0]).toEqual('GET /abc/edf')458 })459 })460461 describe('Multi match', () => {462 const router = new PatternRouter<string>()463464 router.add('get', '/api/*', 'a') // 2.1 for /api/entry465 router.add('get', '/api/entry', 'entry') // 2.2466 router.add('ALL', '/api/*', 'b') // 2.3 for /api/entry467468 it('get /api/entry', async () => {469 const [res] = router.match('get', '/api/entry')470 expect(res.length).toBe(3)471 expect(res[0][0]).toEqual('a')472 expect(res[1][0]).toEqual('entry')473 expect(res[2][0]).toEqual('b')474 })475 })476477 describe('fallback', () => {478 describe('Blog - failed', () => {479 const router = new PatternRouter<string>()480 router.add('post', '/entry', 'post entry') // 1.1481 router.add('post', '/entry/*', 'fallback') // 1.2482 router.add('get', '/entry/:id', 'get entry') // 2.3483 it('post /entry', async () => {484 const [res] = router.match('post', '/entry')485 expect(res.length).toBe(2)486 expect(res[0][0]).toEqual('post entry')487 expect(res[1][0]).toEqual('fallback')488 })489 })490 })491 describe('page', () => {492 const router = new PatternRouter<string>()493 router.add('get', '/page', 'page') // 1.1494 router.add('ALL', '/*', 'fallback') // 1.2495 it('get /page', async () => {496 const [res] = router.match('get', '/page')497 expect(res.length).toBe(2)498 expect(res[0][0]).toEqual('page')499 expect(res[1][0]).toEqual('fallback')500 })501 })42})503504describe('star', () => {505 const router = new PatternRouter<string>()506 router.add('get', '/', '/')507 router.add('get', '/*', '/*')508 router.add('get', '*', '*')509510 router.add('get', '/x', '/x')511 router.add('get', '/x/*', '/x/*')512513 it('top', async () => {514 const [res] = router.match('get', '/')515 expect(res.length).toBe(3)516 expect(res[0][0]).toEqual('/')517 expect(res[1][0]).toEqual('/*')518 expect(res[2][0]).toEqual('*')519 })520521 it('Under a certain path', async () => {522 const [res] = router.match('get', '/x')523 expect(res.length).toBe(4)524 expect(res[0][0]).toEqual('/*')525 expect(res[1][0]).toEqual('*')526 expect(res[2][0]).toEqual('/x')527 expect(res[3][0]).toEqual('/x/*')528 })529})530531describe('Routing order With named parameters', () => {532 const router = new PatternRouter<string>()533 router.add('get', '/book/a', 'no-slug')534 router.add('get', '/book/:slug', 'slug')535 router.add('get', '/book/b', 'no-slug-b')536 it('/book/a', () => {537 const [res] = router.match('get', '/book/a')538 expect(res.length).toBe(2)539 expect(res[0][0]).toEqual('no-slug')540 expect(res[0][1]).toEqual({})541 expect(res[1][0]).toEqual('slug')542 expect(res[1][1]['slug']).toBe('a')543 })544545 it('/book/foo', () => {546 const [res] = router.match('get', '/book/foo')547 expect(res.length).toBe(1)548 expect(res[0][0]).toEqual('slug')549 expect(res[0][1]['slug']).toBe('foo')550 })551 it('/book/b', () => {552 const [res] = router.match('get', '/book/b')553 expect(res.length).toBe(2)554 expect(res[0][0]).toEqual('slug')555 expect(res[0][1]['slug']).toBe('b')556 expect(res[1][0]).toEqual('no-slug-b')557 expect(res[1][1]).toEqual({})558 })559})560561describe('Routing with a hostname', () => {562 const router = new PatternRouter<string>()563 router.add('get', 'www1.example.com/hello', 'www1')564 router.add('get', 'www2.example.com/hello', 'www2')565 it('GET www1.example.com/hello', () => {566 const [res] = router.match('get', 'www1.example.com/hello')567 expect(res.length).toBe(1)568 expect(res[0][0]).toEqual('www1')569 })570 it('GET www2.example.com/hello', () => {571 const [res] = router.match('get', 'www2.example.com/hello')572 expect(res.length).toBe(1)573 expect(res[0][0]).toEqual('www2')574 })575 it('GET /hello', () => {576 const [res] = router.match('get', '/hello')577 expect(res.length).toBe(0)578 })579})
modified:src/router/reg-exp-router/router.test.ts+94 added-653 removed
1import type { ParamStash } from '../../router'2import { UnsupportedPathError } from '../../router'3import { runTest } from '../common.case.test'4import { RegExpRouter } from './router'54describe('Basic Usage', () => {5 const router = new RegExpRouter<string>()67 router.add('GET', '/hello', 'get hello')8 router.add('POST', '/hello', 'post hello')9 router.add('PURGE', '/hello', 'purge hello')1011 it('get, post hello', async () => {12 let [res] = router.match('GET', '/hello')13 expect(res.length).toBe(1)14 expect(res[0][0]).toEqual('get hello')15 ;[res] = router.match('POST', '/hello')16 expect(res.length).toBe(1)17 expect(res[0][0]).toEqual('post hello')18 ;[res] = router.match('PURGE', '/hello')19 expect(res.length).toBe(1)20 expect(res[0][0]).toEqual('purge hello')21 ;[res] = router.match('PUT', '/hello')22 expect(res.length).toBe(0)23 ;[res] = router.match('GET', '/')24 expect(res.length).toBe(0)6describe('RegExpRouter', () => {7 runTest({8 skip: [9 {10 reason: 'UnsupportedPath',11 tests: [12 'Duplicate param name > parent',13 'Duplicate param name > child',14 'Capture Group > Complex capturing group > GET request',15 ],16 },17 {18 reason: 'This route can not be added with `:label` to RegExpRouter. This is ambiguous',19 tests: ['Including slashes > GET /js/main.js'],20 },21 ],22 newRouter: () => new RegExpRouter(),23 })26})2428describe('Complex', () => {29 let router: RegExpRouter<string>30 beforeEach(() => {31 router = new RegExpRouter<string>()32 })25 describe('Return value type', () => {26 it('Should return [[T, ParamIndexMap][], ParamStash]', async () => {27 const router = new RegExpRouter<string>()28 router.add('GET', '/posts/:id', 'get post')2934 it('Named Param', async () => {35 router.add('GET', '/entry/:id', 'get entry')36 const [res, stash] = router.match('GET', '/entry/123')30 const [res, stash] = router.match('GET', '/posts/1')37 expect(res.length).toBe(1)38 expect(res[0][0]).toEqual('get entry')39 expect(stash?.[res[0][1]['id'] as number]).toBe('123')40 })4142 it('Wildcard', async () => {43 router.add('GET', '/wild/*/card', 'get wildcard')44 const [res] = router.match('GET', '/wild/xxx/card')45 expect(res.length).toBe(1)46 expect(res[0][0]).toEqual('get wildcard')47 })4849 it('Default', async () => {50 router.add('GET', '/api/abc', 'get api')51 router.add('GET', '/api/*', 'fallback')52 let [res] = router.match('GET', '/api/abc')53 expect(res.length).toBe(2)54 expect(res[0][0]).toEqual('get api')55 expect(res[1][0]).toEqual('fallback')56 ;[res] = router.match('GET', '/api/def')57 expect(res.length).toBe(1)58 expect(res[0][0]).toEqual('fallback')59 })6061 it('Regexp', async () => {62 router.add('GET', '/post/:date{[0-9]+}/:title{[a-z]+}', 'get post')63 let [res, stash] = router.match('GET', '/post/20210101/hello')64 expect(res.length).toBe(1)65 expect(res[0][0]).toEqual('get post')66 expect(stash?.[res[0][1]['date'] as number]).toBe('20210101')67 expect(stash?.[res[0][1]['title'] as number]).toBe('hello')68 ;[res] = router.match('GET', '/post/onetwothree')69 expect(res.length).toBe(0)70 ;[res] = router.match('GET', '/post/123/123')71 expect(res.length).toBe(0)72 })7374 it('/*', async () => {75 router.add('GET', '/api/*', 'auth middleware')76 router.add('GET', '/api', 'top')77 router.add('GET', '/api/posts', 'posts')78 router.add('GET', '/api/*', 'fallback')7980 let [res] = router.match('GET', '/api')81 expect(res.length).toBe(3)82 expect(res[0][0]).toEqual('auth middleware')83 expect(res[1][0]).toEqual('top')84 expect(res[2][0]).toEqual('fallback')85 ;[res] = router.match('GET', '/api/posts')86 expect(res.length).toBe(3)87 expect(res[0][0]).toEqual('auth middleware')88 expect(res[1][0]).toEqual('posts')89 expect(res[2][0]).toEqual('fallback')90 })91})9293describe('Registration order', () => {94 let router: RegExpRouter<string>95 beforeEach(() => {96 router = new RegExpRouter<string>()97 })9899 it('middleware -> handler', async () => {100 router.add('GET', '*', 'bar')101 router.add('GET', '/:type/:action', 'foo')102 const [res] = router.match('GET', '/posts/123')103 expect(res.length).toBe(2)104 expect(res[0][0]).toEqual('bar')105 expect(res[1][0]).toEqual('foo')106 })107108 it('handler -> fallback', async () => {109 router.add('GET', '/:type/:action', 'foo')110 router.add('GET', '*', 'fallback')111 const [res] = router.match('GET', '/posts/123')112 expect(res.length).toBe(2)113 expect(res[0][0]).toEqual('foo')114 expect(res[1][0]).toEqual('fallback')115 })116})117118describe('Multi match', () => {119 describe('Blog', () => {120 const router = new RegExpRouter<string>()121122 router.add('ALL', '*', 'middleware a')123 router.add('GET', '*', 'middleware b')124 router.add('GET', '/entry', 'get entries')125 router.add('POST', '/entry/*', 'middleware c')126 router.add('POST', '/entry', 'post entry')127 router.add('GET', '/entry/:id', 'get entry')128 router.add('GET', '/entry/:id/comment/:comment_id', 'get comment')129 it('GET /', async () => {130 const [res] = router.match('GET', '/')131 expect(res.length).toBe(2)132 expect(res[0][0]).toEqual('middleware a')133 expect(res[1][0]).toEqual('middleware b')134 })135 it('GET /entry/123', async () => {136 const [res, stash] = router.match('GET', '/entry/123')137 expect(res.length).toBe(3)138 expect(res[0][0]).toEqual('middleware a')139 expect(stash?.[res[0][1]['id'] as number]).toBe(undefined)140 expect(res[1][0]).toEqual('middleware b')141 expect(stash?.[res[1][1]['id'] as number]).toBe(undefined)142 expect(res[2][0]).toEqual('get entry')143 expect(stash?.[res[2][1]['id'] as number]).toBe('123')144 })145 it('GET /entry/123/comment/456', async () => {146 const [res, stash] = router.match('GET', '/entry/123/comment/456')147 expect(res.length).toBe(3)148 expect(res[0][0]).toEqual('middleware a')149 expect(stash?.[res[0][1]['id'] as number]).toBe(undefined)150 expect(stash?.[res[0][1]['comment_id'] as number]).toBe(undefined)151 expect(res[1][0]).toEqual('middleware b')152 expect(stash?.[res[1][1]['id'] as number]).toBe(undefined)153 expect(stash?.[res[1][1]['comment_id'] as number]).toBe(undefined)154 expect(res[2][0]).toEqual('get comment')155 expect(stash?.[res[2][1]['id'] as number]).toBe('123')156 expect(stash?.[res[2][1]['comment_id'] as number]).toBe('456')157 })158 it('POST /entry', async () => {159 const [res] = router.match('POST', '/entry')160 expect(res.length).toBe(3)161 expect(res[0][0]).toEqual('middleware a')162 expect(res[1][0]).toEqual('middleware c')163 expect(res[2][0]).toEqual('post entry')164 })165 it('DELETE /entry', async () => {166 const [res] = router.match('DELETE', '/entry')31 expect(res.length).toBe(1)168 expect(res[0][0]).toEqual('middleware a')32 expect(res).toEqual([['get post', { id: 1 }]])33 expect((stash as ParamStash)[1]).toBe('1')34 })35 })36172 describe('`params` per a handler', () => {173 const router = new RegExpRouter<string>()37 describe('UnsupportedPathError', () => {38 describe('Ambiguous', () => {39 const router = new RegExpRouter<string>()40175 router.add('ALL', '*', 'middleware a')176 router.add('GET', '/entry/:id/*', 'middleware b')177 router.add('GET', '/entry/:id/:action', 'action')41 router.add('GET', '/:user/entries', 'get user entries')42 router.add('GET', '/entry/:name', 'get entry')43 router.add('POST', '/entry', 'create entry')44179 it('GET /entry/123/show', async () => {180 const [res, stash] = router.match('GET', '/entry/123/show')181 expect(res.length).toBe(3)182 expect(res[0][0]).toEqual('middleware a')183 expect(stash?.[res[0][1]['id'] as number]).toBe(undefined)184 expect(stash?.[res[0][1]['action'] as number]).toBe(undefined)185 expect(res[1][0]).toEqual('middleware b')186 expect(stash?.[res[1][1]['id'] as number]).toBe('123')187 expect(stash?.[res[1][1]['comment_id'] as number]).toBe(undefined)188 expect(res[2][0]).toEqual('action')189 expect(stash?.[res[2][1]['id'] as number]).toBe('123')190 expect(stash?.[res[2][1]['action'] as number]).toBe('show')45 it('GET /entry/entries', async () => {46 expect(() => {47 router.match('GET', '/entry/entries')48 }).toThrowError(UnsupportedPathError)49 })50 })192 })51194 it('hierarchy', () => {195 const router = new RegExpRouter<string>()196 router.add('GET', '/posts/:id/comments/:comment_id', 'foo')197 router.add('GET', '/posts/:id', 'bar')198 expect(() => {199 router.match('GET', '/')200 }).not.toThrow()201 })202})52 describe('Multiple handlers with different label', () => {53 const router = new RegExpRouter<string>()54204describe('Check for duplicate parameter names', () => {205 it('self', () => {206 const router = new RegExpRouter<string>()207 router.add('GET', '/:id/:id', 'get')208 const [res, stash] = router.match('GET', '/123/456')209 expect(res.length).toBe(1)210 expect(stash?.[res[0][1]['id'] as number]).toBe('123')211 })212})55 router.add('GET', '/:type/:id', ':type')56 router.add('GET', '/:class/:id', ':class')57 router.add('GET', '/:model/:id', ':model')58214describe('UnsupportedPathError', () => {215 describe('Ambiguous', () => {216 const router = new RegExpRouter<string>()217218 router.add('GET', '/:user/entries', 'get user entries')219 router.add('GET', '/entry/:name', 'get entry')220 router.add('POST', '/entry', 'create entry')221222 it('GET /entry/entries', async () => {223 expect(() => {224 router.match('GET', '/entry/entries')225 }).toThrowError(UnsupportedPathError)59 it('GET /entry/123', async () => {60 expect(() => {61 router.match('GET', '/entry/123')62 }).toThrowError(UnsupportedPathError)63 })64 })227 })65229 describe('Multiple handlers with different label', () => {230 const router = new RegExpRouter<string>()231232 router.add('GET', '/:type/:id', ':type')233 router.add('GET', '/:class/:id', ':class')234 router.add('GET', '/:model/:id', ':model')235236 it('GET /entry/123', async () => {237 expect(() => {238 router.match('GET', '/entry/123')239 }).toThrowError(UnsupportedPathError)240 })241 })242243 it('parent', () => {66 it('parent', () => {244 const router = new RegExpRouter<string>()245 router.add('GET', '/:id/:action', 'foo')246 router.add('GET', '/posts/:id', 'bar')247 expect(() => {248 router.match('GET', '/')249 }).toThrowError(UnsupportedPathError)250 })251252 it('child', () => {253 const router = new RegExpRouter<string>()254 router.add('GET', '/posts/:id', 'foo')255 router.add('GET', '/:id/:action', 'bar')256257 expect(() => {258 router.match('GET', '/')259 }).toThrowError(UnsupportedPathError)260 })261262 describe('static and dynamic', () => {263 it('static first', () => {67 const router = new RegExpRouter<string>()265 router.add('GET', '/reg-exp/router', 'foo')266 router.add('GET', '/reg-exp/:id', 'bar')26768 router.add('GET', '/:id/:action', 'foo')69 router.add('GET', '/posts/:id', 'bar')70 expect(() => {71 router.match('GET', '/')72 }).toThrowError(UnsupportedPathError)73 })74273 it('long label', () => {75 it('child', () => {76 const router = new RegExpRouter<string>()275 router.add('GET', '/reg-exp/router', 'foo')276 router.add('GET', '/reg-exp/:service', 'bar')77 router.add('GET', '/posts/:id', 'foo')78 router.add('GET', '/:id/:action', 'bar')7980 expect(() => {81 router.match('GET', '/')82 }).toThrowError(UnsupportedPathError)83 })84283 it('dynamic first', () => {284 const router = new RegExpRouter<string>()285 router.add('GET', '/reg-exp/:id', 'bar')286 router.add('GET', '/reg-exp/router', 'foo')85 describe('static and dynamic', () => {86 it('static first', () => {87 const router = new RegExpRouter<string>()88 router.add('GET', '/reg-exp/router', 'foo')89 router.add('GET', '/reg-exp/:id', 'bar')90288 expect(() => {289 router.match('GET', '/')290 }).toThrowError(UnsupportedPathError)291 })292 })91 expect(() => {92 router.match('GET', '/')93 }).toThrowError(UnsupportedPathError)94 })95294 it('different regular expression', () => {295 const router = new RegExpRouter<string>()296 router.add('GET', '/:id/:action{create|update}', 'foo')297 router.add('GET', '/:id/:action{delete}', 'bar')298 expect(() => {299 router.match('GET', '/')300 }).toThrowError(UnsupportedPathError)301 })302})96 it('long label', () => {97 const router = new RegExpRouter<string>()98 router.add('GET', '/reg-exp/router', 'foo')99 router.add('GET', '/reg-exp/:service', 'bar')100304describe('star', () => {305 const router = new RegExpRouter<string>()101 expect(() => {102 router.match('GET', '/')103 }).toThrowError(UnsupportedPathError)104 })105307 router.add('GET', '/', '/')308 router.add('GET', '/*', '/*')309 router.add('GET', '*', '*')106 it('dynamic first', () => {107 const router = new RegExpRouter<string>()108 router.add('GET', '/reg-exp/:id', 'bar')109 router.add('GET', '/reg-exp/router', 'foo')110311 router.add('GET', '/x', '/x')312 router.add('GET', '/x/*', '/x/*')313314 it('top', async () => {315 const [res] = router.match('GET', '/')316 expect(res.length).toBe(3)317 expect(res[0][0]).toEqual('/')318 expect(res[1][0]).toEqual('/*')319 expect(res[2][0]).toEqual('*')320 })321322 it('Under a certain path', async () => {323 const [res] = router.match('GET', '/x')324 expect(res.length).toBe(4)325 expect(res[0][0]).toEqual('/*')326 expect(res[1][0]).toEqual('*')327 expect(res[2][0]).toEqual('/x')328 expect(res[3][0]).toEqual('/x/*')329 })330})331332describe('Optional route', () => {333 const router = new RegExpRouter<string>()334 router.add('GET', '/api/animals/:type?', 'animals')335 it('GET /api/animals/dog', async () => {336 const [res, stash] = router.match('GET', '/api/animals/dog')337 expect(res.length).toBe(1)338 expect(res[0][0]).toEqual('animals')339 expect(stash?.[res[0][1]['type'] as number]).toBe('dog')340 })341 it('GET /api/animals', async () => {342 const [res, stash] = router.match('GET', '/api/animals')343 expect(res.length).toBe(1)344 expect(res[0][0]).toEqual('animals')345 expect(stash?.[res[0][1]['type'] as number]).toBeUndefined()346 })347 router.add('GET', '/v1/:version?/:platform?', 'result')348 it('GET /v1/123/abc', () => {349 const [res, stash] = router.match('GET', '/v1/123/abc')350 expect(res.length).toBe(1)351 expect(res[0][0]).toEqual('result')352 expect(stash?.[res[0][1]['version'] as number]).toBe('123')353 expect(stash?.[res[0][1]['platform'] as number]).toBe('abc')354 })355 it('GET /v1/123', () => {356 const [res, stash] = router.match('GET', '/v1/123')357 expect(res.length).toBe(1)358 expect(res[0][0]).toEqual('result')359 expect(stash?.[res[0][1]['version'] as number]).toBe('123')360 expect(stash?.[res[0][1]['platform'] as number]).toBeUndefined()361 })362 it('GET /v1', () => {363 const [res, stash] = router.match('GET', '/v1')364 expect(res.length).toBe(1)365 expect(res[0][0]).toEqual('result')366 expect(stash?.[res[0][1]['version'] as number]).toBeUndefined()367 expect(stash?.[res[0][1]['platform'] as number]).toBeUndefined()368 })369})370371describe('All', () => {372 const router = new RegExpRouter<string>()373374 router.add('GET', '/hello', 'get hello')375 router.add('ALL', '/all', 'get all')376377 it('get, all hello', async () => {378 const [res] = router.match('GET', '/all')379 expect(res.length).toBe(1)380 })381})382383describe('long prefix, then star', () => {384 describe('GET only', () => {385 const router = new RegExpRouter<string>()386387 router.add('GET', '/long/prefix/*', 'long-prefix')388 router.add('GET', '/long/*', 'long')389 router.add('GET', '*', 'star1')390 router.add('GET', '*', 'star2')391392 it('get /', () => {393 const [res] = router.match('GET', '/')394 expect(res.length).toBe(2)395 expect(res[0][0]).toEqual('star1')396 expect(res[1][0]).toEqual('star2')111 expect(() => {112 router.match('GET', '/')113 }).toThrowError(UnsupportedPathError)114 })115 })116399 it('get /long/prefix', () => {400 const [res] = router.match('GET', '/long/prefix')401 expect(res.length).toBe(4)402 expect(res[0][0]).toEqual('long-prefix')403 expect(res[1][0]).toEqual('long')404 expect(res[2][0]).toEqual('star1')405 expect(res[3][0]).toEqual('star2')406 })407408 it('get /long/prefix/test', () => {409 const [res] = router.match('GET', '/long/prefix/test')410 expect(res.length).toBe(4)411 expect(res[0][0]).toEqual('long-prefix')412 expect(res[1][0]).toEqual('long')413 expect(res[2][0]).toEqual('star1')414 expect(res[3][0]).toEqual('star2')415 })416 })417418 describe('ALL and GET', () => {419 const router = new RegExpRouter<string>()420421 router.add('ALL', '/long/prefix/*', 'long-prefix')422 router.add('ALL', '/long/*', 'long')423 router.add('GET', '*', 'star1')424 router.add('GET', '*', 'star2')425426 it('get /', () => {427 const [res] = router.match('GET', '/')428 expect(res.length).toBe(2)429 expect(res[0][0]).toEqual('star1')430 expect(res[1][0]).toEqual('star2')431 })432433 it('get /long/prefix', () => {434 const [res] = router.match('GET', '/long/prefix')435 expect(res.length).toBe(4)436 expect(res[0][0]).toEqual('long-prefix')437 expect(res[1][0]).toEqual('long')438 expect(res[2][0]).toEqual('star1')439 expect(res[3][0]).toEqual('star2')440 })441442 it('get /long/prefix/test', () => {443 const [res] = router.match('GET', '/long/prefix/test')444 expect(res.length).toBe(4)445 expect(res[0][0]).toEqual('long-prefix')446 expect(res[1][0]).toEqual('long')447 expect(res[2][0]).toEqual('star1')448 expect(res[3][0]).toEqual('star2')449 })450 })451452 describe('Including slashes', () => {453 const router = new RegExpRouter<string>()454455 router.add('GET', '/js/:filename{[a-z0-9/]+.js}', 'any file')456457 // XXX This route can not be added with `:label` to RegExpRouter. This is ambiguous.458 // router.add('GET', '/js/main.js', 'main.js')459 // it('get /js/main.js', () => {460 // const [res] = router.match('GET', '/js/main.js')461 // expect(res.length).toBe(1)462 // expect(res[0][0]).toEqual('any file', 'main.js')463 // expect(stash?.[res[0][1]['filename'] as number]).toEqual('main.js')464 // })465466 it('get /js/chunk/123.js', () => {467 const [res, stash] = router.match('GET', '/js/chunk/123.js')468 expect(res.length).toBe(1)469 expect(res[0][0]).toEqual('any file')470 expect(stash?.[res[0][1]['filename'] as number]).toEqual('chunk/123.js')471 })472473 it('get /js/chunk/nest/123.js', () => {474 const [res, stash] = router.match('GET', '/js/chunk/nest/123.js')475 expect(res.length).toBe(1)476 expect(res[0][0]).toEqual('any file')477 expect(stash?.[res[0][1]['filename'] as number]).toEqual('chunk/nest/123.js')478 })479 })480481 describe('REST API', () => {482 const router = new RegExpRouter<string>()483484 router.add('GET', '/users/:username{[a-z]+}', 'profile')485 router.add('GET', '/users/:username{[a-z]+}/posts', 'posts')486487 it('get /users/hono', () => {488 const [res] = router.match('GET', '/users/hono')489 expect(res.length).toBe(1)490 expect(res[0][0]).toEqual('profile')491 })492493 it('get /users/hono/posts', () => {494 const [res] = router.match('GET', '/users/hono/posts')495 expect(res.length).toBe(1)496 expect(res[0][0]).toEqual('posts')497 })498 })499})500501describe('static routes of ALL and GET', () => {502 const router = new RegExpRouter<string>()503504 router.add('ALL', '/foo', 'foo')505 router.add('GET', '/bar', 'bar')506507 it('get /foo', () => {508 const [res] = router.match('GET', '/foo')509 expect(res[0][0]).toEqual('foo')510 })511})512513describe('ALL and Star', () => {514 const router = new RegExpRouter<string>()515516 router.add('ALL', '/x', '/x')517 router.add('GET', '*', 'star')518519 it('Should return /x and star', async () => {520 const [res] = router.match('GET', '/x')521 expect(res.length).toBe(2)522 expect(res[0][0]).toEqual('/x')523 expect(res[1][0]).toEqual('star')524 })525})526527describe('GET star, ALL static, GET star...', () => {528 const router = new RegExpRouter<string>()529530 router.add('GET', '*', 'star1')531 router.add('ALL', '/x', '/x')532 router.add('GET', '*', 'star2')533 router.add('GET', '*', 'star3')534535 it('Should return /x and star', async () => {536 const [res] = router.match('GET', '/x')537 expect(res.length).toBe(4)538 expect(res[0][0]).toEqual('star1')539 expect(res[1][0]).toEqual('/x')540 expect(res[2][0]).toEqual('star2')541 expect(res[3][0]).toEqual('star3')542 })543})544545// https://github.com/honojs/hono/issues/699546describe('GET star, GET static, ALL star...', () => {547 const router = new RegExpRouter<string>()548549 router.add('GET', '/y/*', 'star1')550 router.add('GET', '/y/a', 'a')551 router.add('ALL', '/y/b/*', 'star2')552 router.add('GET', '/y/b/bar', 'bar')553554 it('Should return star1, star2, and bar', async () => {555 const [res] = router.match('GET', '/y/b/bar')556 expect(res.length).toBe(3)557 expect(res[0][0]).toEqual('star1')558 expect(res[1][0]).toEqual('star2')559 expect(res[2][0]).toEqual('bar')560 })561})562563describe('ALL star, ALL star, GET static, ALL star...', () => {564 const router = new RegExpRouter<string>()565566 router.add('ALL', '*', 'wildcard')567 router.add('ALL', '/a/*', 'star1')568 router.add('GET', '/a/foo', 'foo')569 router.add('ALL', '/b/*', 'star2')570 router.add('GET', '/b/bar', 'bar')571572 it('Should return wildcard, star2 and bar', async () => {573 const [res] = router.match('GET', '/b/bar')574 expect(res.length).toBe(3)575 expect(res[0][0]).toEqual('wildcard')576 expect(res[1][0]).toEqual('star2')577 expect(res[2][0]).toEqual('bar')578 })579})580581describe('Routing with a hostname', () => {582 const router = new RegExpRouter<string>()583 router.add('get', 'www1.example.com/hello', 'www1')584 router.add('get', 'www2.example.com/hello', 'www2')585 it('GET www1.example.com/hello', () => {586 const [res] = router.match('get', 'www1.example.com/hello')587 expect(res.length).toBe(1)588 expect(res[0][0]).toEqual('www1')589 })590 it('GET www2.example.com/hello', () => {591 const [res] = router.match('get', 'www2.example.com/hello')592 expect(res.length).toBe(1)593 expect(res[0][0]).toEqual('www2')594 })595 it('GET /hello', () => {596 const [res] = router.match('get', '/hello')597 expect(res.length).toBe(0)598 })599})600601describe('Capture Group', () => {602 describe('Simple capturing group', () => {603 const router = new RegExpRouter<string>()604 router.add('get', '/foo/:capture{(?:bar|baz)}', 'ok')605 it('GET /foo/bar', () => {606 const [res, stash] = router.match('get', '/foo/bar')607 expect(res.length).toBe(1)608 expect(res[0][0]).toBe('ok')609 expect(stash?.[res[0][1]['capture'] as number]).toBe('bar')610 })611612 it('GET /foo/baz', () => {613 const [res, stash] = router.match('get', '/foo/baz')614 expect(res.length).toBe(1)615 expect(res[0][0]).toBe('ok')616 expect(stash?.[res[0][1]['capture'] as number]).toBe('baz')617 })618619 it('GET /foo/qux', () => {620 const [res] = router.match('get', '/foo/qux')621 expect(res.length).toBe(0)622 })623 })624625 describe('Non-capturing group', () => {626 const router = new RegExpRouter<string>()627 router.add('get', '/foo/:capture{(?:bar|baz)}', 'ok')628 it('GET /foo/bar', () => {629 const [res, stash] = router.match('get', '/foo/bar')630 expect(res.length).toBe(1)631 expect(res[0][0]).toBe('ok')632 expect(stash?.[res[0][1]['capture'] as number]).toBe('bar')633 })634635 it('GET /foo/baz', () => {636 const [res, stash] = router.match('get', '/foo/baz')637 expect(res.length).toBe(1)638 expect(res[0][0]).toBe('ok')639 expect(stash?.[res[0][1]['capture'] as number]).toBe('baz')640 })641642 it('GET /foo/qux', () => {643 const [res] = router.match('get', '/foo/qux')644 expect(res.length).toBe(0)645 })646 })647648 describe('Non-capturing group with prefix', () => {649 const router = new RegExpRouter<string>()650 router.add('get', '/foo/:capture{ba(?:r|z)}', 'ok')651 it('GET /foo/bar', () => {652 const [res, stash] = router.match('get', '/foo/bar')653 expect(res.length).toBe(1)654 expect(res[0][0]).toBe('ok')655 expect(stash?.[res[0][1]['capture'] as number]).toBe('bar')656 })657658 it('GET /foo/baz', () => {659 const [res, stash] = router.match('get', '/foo/baz')660 expect(res.length).toBe(1)661 expect(res[0][0]).toBe('ok')662 expect(stash?.[res[0][1]['capture'] as number]).toBe('baz')663 })664665 it('GET /foo/qux', () => {666 const [res] = router.match('get', '/foo/qux')667 expect(res.length).toBe(0)668 })669 })670671 describe('Complex capturing group', () => {672 const router = new RegExpRouter<string>()673 router.add('get', '/foo/:capture{ba(r|z)}', 'ok')674 it('GET request', () => {117 it('different regular expression', () => {118 const router = new RegExpRouter<string>()119 router.add('GET', '/:id/:action{create|update}', 'foo')120 router.add('GET', '/:id/:action{delete}', 'bar')121 expect(() => {676 router.match('GET', '/foo/bar')122 router.match('GET', '/')123 }).toThrowError(UnsupportedPathError)124 })679 })680})125682describe('Unknown method', () => {683 const router = new RegExpRouter<string>()684 router.add('GET', '/', 'index')685 router.add('ALL', '/all', 'all')686687 it('UNKNOWN_METHOD /', () => {688 const [res] = router.match('UNKNOWN_METHOD', '/')689 expect(res.length).toBe(0)126 describe('Capture Group', () => {127 describe('Complex capturing group', () => {128 it('GET request', () => {129 const router = new RegExpRouter<string>()130 router.add('GET', '/foo/:capture{ba(r|z)}', 'ok')131 expect(() => {132 router.match('GET', '/foo/bar')133 }).toThrowError(UnsupportedPathError)134 })135 })136 })137 })691692 it('UNKNOWN_METHOD /all', () => {693 const [res] = router.match('UNKNOWN_METHOD', '/all')694 expect(res.length).toBe(1)695 expect(res[0][0]).toBe('all')696 })138})
modified:src/router/smart-router/router.test.ts+6 added-88 removed
1import { runTest } from '../common.case.test'2import { RegExpRouter } from '../reg-exp-router'3import { TrieRouter } from '../trie-router'4import { SmartRouter } from './router'55describe('RegExpRouter', () => {6 describe('Complex', () => {7 let router: SmartRouter<string>8 beforeEach(() => {9 router = new SmartRouter<string>({6describe('SmartRouter', () => {7 runTest({8 newRouter: () =>9 new SmartRouter({10 routers: [new RegExpRouter(), new TrieRouter()],11 })12 })1314 it('Named Param', async () => {15 router.add('GET', '/entry/:id', 'get entry')16 const [res, stash] = router.match('GET', '/entry/123')17 expect(res.length).toBe(1)18 expect(res[0][0]).toEqual('get entry')19 expect(stash?.[res[0][1]['id'] as number] ?? res[0][1]['id']).toBe('123')20 expect(router.activeRouter).toBeInstanceOf(RegExpRouter)21 expect(router.name).toBe('SmartRouter + RegExpRouter')22 })2324 it('Wildcard', async () => {25 router.add('GET', '/wild/*/card', 'get wildcard')26 const [res] = router.match('GET', '/wild/xxx/card')27 expect(res.length).toBe(1)28 expect(res[0][0]).toEqual('get wildcard')29 })3031 it('Default', async () => {32 router.add('GET', '/api/abc', 'get api')33 router.add('GET', '/api/*', 'fallback')34 let [res] = router.match('GET', '/api/abc')35 expect(res.length).toBe(2)36 expect(res[0][0]).toEqual('get api')37 expect(res[1][0]).toEqual('fallback')38 ;[res] = router.match('GET', '/api/def')39 expect(res.length).toBe(1)40 expect(res[0][0]).toEqual('fallback')41 })4243 it('Regexp', async () => {44 router.add('GET', '/post/:date{[0-9]+}/:title{[a-z]+}', 'get post')45 let [res, stash] = router.match('GET', '/post/20210101/hello')46 expect(res.length).toBe(1)47 expect(res[0][0]).toEqual('get post')48 expect(stash?.[res[0][1]['date'] as number] ?? res[0][1]['date']).toBe('20210101')49 expect(stash?.[res[0][1]['title'] as number] ?? res[0][1]['title']).toBe('hello')50 ;[res] = router.match('GET', '/post/onetwothree')51 expect(res.length).toBe(0)52 ;[res] = router.match('GET', '/post/123/123')53 expect(res.length).toBe(0)54 })5556 it('/*', async () => {57 router.add('GET', '/api/*', 'auth middleware')58 router.add('GET', '/api', 'top')59 router.add('GET', '/api/posts', 'posts')60 router.add('GET', '/api/*', 'fallback')6162 let [res] = router.match('GET', '/api')63 expect(res.length).toBe(3)64 expect(res[0][0]).toEqual('auth middleware')65 expect(res[1][0]).toEqual('top')66 expect(res[2][0]).toEqual('fallback')67 ;[res] = router.match('GET', '/api/posts')68 expect(res.length).toBe(3)69 expect(res[0][0]).toEqual('auth middleware')70 expect(res[1][0]).toEqual('posts')71 expect(res[2][0]).toEqual('fallback')72 })11 }),12 })13})7576describe('TrieRouter', () => {77 const router = new SmartRouter<string>({78 routers: [new RegExpRouter(), new TrieRouter()],79 })8081 router.add('GET', '/:user/entries', 'get user entries')82 router.add('GET', '/entry/:name', 'get entry')83 it('GET /entry/entry', async () => {84 const [res] = router.match('GET', '/entry/entries')85 expect(res.length).toBe(2)86 expect(res[0][0]).toEqual('get user entries')87 expect(res[0][1]['user']).toBe('entry')88 expect(res[0][1]['name']).toBe(undefined)89 expect(res[1][0]).toEqual('get entry')90 expect(res[1][1]['user']).toBe(undefined)91 expect(res[1][1]['name']).toBe('entries')92 expect(router.activeRouter).toBeInstanceOf(TrieRouter)93 expect(router.name).toBe('SmartRouter + TrieRouter')94 })95})
modified:src/router/trie-router/router.test.ts+4 added-239 removed
1import { runTest } from '../common.case.test'2import { TrieRouter } from './router'33describe('Basic Usage', () => {4 const router = new TrieRouter<string>()56 router.add('GET', '/hello', 'get hello')7 router.add('POST', '/hello', 'post hello')8 router.add('PURGE', '/hello', 'purge hello')910 it('get, post hello', async () => {11 let [res] = router.match('GET', '/hello')12 expect(res.length).toBe(1)13 expect(res[0][0]).toEqual('get hello')14 ;[res] = router.match('POST', '/hello')15 expect(res.length).toBe(1)16 expect(res[0][0]).toEqual('post hello')17 ;[res] = router.match('PURGE', '/hello')18 expect(res.length).toBe(1)19 expect(res[0][0]).toEqual('purge hello')20 ;[res] = router.match('PUT', '/hello')21 expect(res.length).toBe(0)22 ;[res] = router.match('GET', '/')23 expect(res.length).toBe(0)4describe('TrieRouter', () => {5 runTest({6 newRouter: () => new TrieRouter(),7 })8})2627describe('Complex', () => {28 const router = new TrieRouter<string>()2930 it('Named Param', async () => {31 router.add('GET', '/entry/:id', 'get entry')32 const [res] = router.match('GET', '/entry/123')33 expect(res.length).toBe(1)34 expect(res[0][0]).toEqual('get entry')35 expect(res[0][1]['id']).toBe('123')36 })3738 it('Named param with trailing wildcard', async () => {39 router.add('GET', '/article/:id/*', 'get article with wildcard')40 let [res] = router.match('GET', '/article/123')41 expect(res.length).toBe(1)42 expect(res[0][0]).toEqual('get article with wildcard')43 expect(res[0][1]['id']).toBe('123')44 ;[res] = router.match('GET', '/article/123/action')45 expect(res.length).toBe(1)46 expect(res[0][0]).toEqual('get article with wildcard')47 expect(res[0][1]['id']).toBe('123')48 })4950 it('Wildcard', async () => {51 router.add('GET', '/wild/*/card', 'get wildcard')52 const [res] = router.match('GET', '/wild/xxx/card')53 expect(res.length).toBe(1)54 expect(res[0][0]).toEqual('get wildcard')55 })5657 it('Default', async () => {58 router.add('GET', '/api/*', 'fallback')59 router.add('GET', '/api/abc', 'get api')60 let [res] = router.match('GET', '/api/abc')61 expect(res.length).toBe(2)62 expect(res[0][0]).toEqual('fallback')63 expect(res[1][0]).toEqual('get api')64 ;[res] = router.match('GET', '/api/def')65 expect(res.length).toBe(1)66 expect(res[0][0]).toEqual('fallback')67 })6869 it('Regexp', async () => {70 router.add('GET', '/post/:date{[0-9]+}/:title{[a-z]+}', 'get post')71 let [res] = router.match('GET', '/post/20210101/hello')72 expect(res.length).toBe(1)73 expect(res[0][0]).toEqual('get post')74 expect(res[0][1]['date']).toBe('20210101')75 expect(res[0][1]['title']).toBe('hello')76 ;[res] = router.match('GET', '/post/onetwothree')77 expect(res.length).toBe(0)78 ;[res] = router.match('GET', '/post/123/123')79 expect(res.length).toBe(0)80 })81})8283describe('Multi match', () => {84 const router = new TrieRouter<string>()8586 describe('Blog', () => {87 router.add('ALL', '*', 'middleware a')88 router.add('GET', '*', 'middleware b')89 router.add('GET', '/entry', 'get entries')90 router.add('POST', '/entry/*', 'middleware c')91 router.add('POST', '/entry', 'post entry')92 router.add('GET', '/entry/:id', 'get entry')93 router.add('GET', '/entry/:id/comment/:comment_id', 'get comment')94 it('GET /', async () => {95 const [res] = router.match('GET', '/')96 expect(res.length).toBe(2)97 expect(res[0][0]).toEqual('middleware a')98 expect(res[1][0]).toEqual('middleware b')99 })100 it('GET /entry/123', async () => {101 const [res] = router.match('GET', '/entry/123')102 expect(res.length).toBe(3)103 expect(res[0][0]).toEqual('middleware a')104 expect(res[0][1]['id']).toBe(undefined)105 expect(res[1][0]).toEqual('middleware b')106 expect(res[1][1]['id']).toBe(undefined)107 expect(res[2][0]).toEqual('get entry')108 expect(res[2][1]['id']).toBe('123')109 })110 it('GET /entry/123/comment/456', async () => {111 const [res] = router.match('GET', '/entry/123/comment/456')112 expect(res.length).toBe(3)113 expect(res[0][0]).toEqual('middleware a')114 expect(res[0][1]['id']).toBe(undefined)115 expect(res[0][1]['comment_id']).toBe(undefined)116 expect(res[1][0]).toEqual('middleware b')117 expect(res[1][1]['id']).toBe(undefined)118 expect(res[1][1]['comment_id']).toBe(undefined)119 expect(res[2][0]).toEqual('get comment')120 expect(res[2][1]['id']).toBe('123')121 expect(res[2][1]['comment_id']).toBe('456')122 })123 it('POST /entry', async () => {124 const [res] = router.match('POST', '/entry')125 expect(res.length).toBe(3)126 expect(res[0][0]).toEqual('middleware a')127 expect(res[1][0]).toEqual('middleware c')128 expect(res[2][0]).toEqual('post entry')129 })130 it('DELETE /entry', async () => {131 const [res] = router.match('DELETE', '/entry')132 expect(res.length).toBe(1)133 expect(res[0][0]).toEqual('middleware a')134 })135 })136})137138describe('Fallback', () => {139 const router = new TrieRouter<string>()140 router.add('POST', '/entry', 'post entry')141 router.add('POST', '/entry/*', 'fallback')142 router.add('GET', '/entry/:id', 'get entry')143 it('POST /entry', async () => {144 const [res] = router.match('POST', '/entry')145 expect(res.length).toBe(2)146 expect(res[0][0]).toEqual('post entry')147 expect(res[1][0]).toEqual('fallback')148 })149})150151describe('page', () => {152 const router = new TrieRouter<string>()153 router.add('GET', '/page', 'page')154 router.add('ALL', '*', 'fallback') // or '*'155 it('GET /page', async () => {156 const [res] = router.match('GET', '/page')157 expect(res[0][0]).toEqual('page')158 expect(res[1][0]).toEqual('fallback')159 })160})161162describe('Optional route', () => {163 const router = new TrieRouter<string>()164 router.add('GET', '/api/animals/:type?', 'animals')165 it('GET /api/animals/dog', async () => {166 const [res] = router.match('GET', '/api/animals/dog')167 expect(res[0][0]).toEqual('animals')168 expect(res[0][1]['type']).toBe('dog')169 })170 it('GET /api/animals', async () => {171 const [res] = router.match('GET', '/api/animals')172 expect(res[0][0]).toEqual('animals')173 expect(res[0][1]['type']).toBeUndefined()174 })175 router.add('GET', '/v1/:version?/:platform?', 'result')176 it('GET /v1/123/abc', () => {177 const [res] = router.match('GET', '/v1/123/abc')178 expect(res.length).toBe(1)179 expect(res[0][0]).toEqual('result')180 expect(res[0][1]['version']).toBe('123')181 expect(res[0][1]['platform']).toBe('abc')182 })183 it('GET /v1/123', () => {184 const [res] = router.match('GET', '/v1/123')185 expect(res.length).toBe(1)186 expect(res[0][0]).toEqual('result')187 expect(res[0][1]['version']).toBe('123')188 expect(res[0][1]['platform']).toBeUndefined()189 })190 it('GET /v1', () => {191 const [res] = router.match('GET', '/v1')192 expect(res.length).toBe(1)193 expect(res[0][0]).toEqual('result')194 expect(res[0][1]['version']).toBeUndefined()195 expect(res[0][1]['platform']).toBeUndefined()196 })197})198199describe('routing order with named parameters', () => {200 const router = new TrieRouter<string>()201 router.add('GET', '/book/a', 'no-slug')202 router.add('GET', '/book/:slug', 'slug')203 router.add('GET', '/book/b', 'no-slug-b')204 it('GET /book/a', async () => {205 const [res] = router.match('GET', '/book/a')206 expect(res[0][0]).toEqual('no-slug')207 expect(res[0][1]['slug']).toBe(undefined)208 expect(res[1][0]).toEqual('slug')209 expect(res[1][1]['slug']).toBe('a')210 })211 it('GET /book/foo', async () => {212 const [res] = router.match('GET', '/book/foo')213 expect(res[0][0]).toEqual('slug')214 expect(res[0][1]['slug']).toBe('foo')215 })216 it('GET /book/b', async () => {217 const [res] = router.match('GET', '/book/b')218 expect(res[0][0]).toEqual('slug')219 expect(res[0][1]['slug']).toBe('b')220 expect(res[1][0]).toEqual('no-slug-b')221 expect(res[1][1]['slug']).toBe(undefined)222 })223})224225describe('Routing with a hostname', () => {226 const router = new TrieRouter<string>()227 router.add('get', 'www1.example.com/hello', 'www1')228 router.add('get', 'www2.example.com/hello', 'www2')229 it('GET www1.example.com/hello', () => {230 const [res] = router.match('get', 'www1.example.com/hello')231 expect(res.length).toBe(1)232 expect(res[0][0]).toEqual('www1')233 })234 it('GET www2.example.com/hello', () => {235 const [res] = router.match('get', 'www2.example.com/hello')236 expect(res.length).toBe(1)237 expect(res[0][0]).toEqual('www2')238 })239 it('GET /hello', () => {240 const [res] = router.match('get', '/hello')241 expect(res.length).toBe(0)242 })243})
added:src/router/common.case.test.ts+731 added
1import type { Suite } from 'vitest'2import type { Router, Params, ParamIndexMap } from '../router'34const getSuiteHierarchy = (suite: Suite) => {5 const res: Suite[] = []6 let s: Suite | undefined = suite7 while (s) {8 res.unshift(s)9 s = s.suite10 }11 return res12}1314export const runTest = ({15 skip = [],16 newRouter,17}: {18 skip?: {19 reason: string20 tests: string[]21 }[]22 newRouter: <T>() => Router<T>23}) => {24 describe('Common', () => {25 type Match = (method: string, path: string) => { handler: string; params: Params }[]26 let router: Router<string>27 let match: Match2829 beforeEach(({ task, skip: skipTask }) => {30 const suites = getSuiteHierarchy(task.suite)31 const name = [...suites.slice(3).map((s) => s.name), task.name].join(' > ')32 const isSkip = skip.find((s) => s.tests.includes(name))33 if (isSkip) {34 console.log(`Skip: ${isSkip.reason}`)35 skipTask()36 return37 }3839 router = newRouter()40 match = (method: string, path: string) => {41 const [matchRes, stash] = router.match(method, path)42 const res = matchRes.map((r) =>43 stash44 ? {45 handler: r[0],46 params: Object.keys(r[1]).reduce((acc, key) => {47 acc[key] = stash[(r[1] as ParamIndexMap)[key]]48 return acc49 }, {} as Params),50 }51 : { handler: r[0], params: r[1] as Params }52 )53 return res54 }55 })5657 describe('Basic Usage', () => {58 beforeEach(() => {59 router.add('GET', '/hello', 'get hello')60 router.add('POST', '/hello', 'post hello')61 router.add('PURGE', '/hello', 'purge hello')62 })6364 it('GET, post hello', async () => {65 let res = match('GET', '/hello')66 expect(res.length).toBe(1)67 expect(res[0].handler).toEqual('get hello')68 res = match('POST', '/hello')69 expect(res.length).toBe(1)70 expect(res[0].handler).toEqual('post hello')71 res = match('PURGE', '/hello')72 expect(res.length).toBe(1)73 expect(res[0].handler).toEqual('purge hello')74 res = match('PUT', '/hello')75 expect(res.length).toBe(0)76 res = match('GET', '/')77 expect(res.length).toBe(0)78 })79 })8081 describe('Complex', () => {82 it('Named Param', async () => {83 router.add('GET', '/entry/:id', 'get entry')84 const res = match('GET', '/entry/123')85 expect(res.length).toBe(1)86 expect(res[0].handler).toEqual('get entry')87 expect(res[0].params['id']).toBe('123')88 })8990 it('Wildcard', async () => {91 router.add('GET', '/wild/*/card', 'get wildcard')92 const res = match('GET', '/wild/xxx/card')93 expect(res.length).toBe(1)94 expect(res[0].handler).toEqual('get wildcard')95 })9697 it('Default', async () => {98 router.add('GET', '/api/abc', 'get api')99 router.add('GET', '/api/*', 'fallback')100 let res = match('GET', '/api/abc')101 expect(res.length).toBe(2)102 expect(res[0].handler).toEqual('get api')103 expect(res[1].handler).toEqual('fallback')104 res = match('GET', '/api/def')105 expect(res.length).toBe(1)106 expect(res[0].handler).toEqual('fallback')107 })108109 it('Regexp', async () => {110 router.add('GET', '/post/:date{[0-9]+}/:title{[a-z]+}', 'get post')111 let res = match('GET', '/post/20210101/hello')112 expect(res.length).toBe(1)113 expect(res[0].handler).toEqual('get post')114 expect(res[0].params['date']).toBe('20210101')115 expect(res[0].params['title']).toBe('hello')116 res = match('GET', '/post/onetwothree')117 expect(res.length).toBe(0)118 res = match('GET', '/post/123/123')119 expect(res.length).toBe(0)120 })121122 it('/*', async () => {123 router.add('GET', '/api/*', 'auth middleware')124 router.add('GET', '/api', 'top')125 router.add('GET', '/api/posts', 'posts')126 router.add('GET', '/api/*', 'fallback')127128 let res = match('GET', '/api')129 expect(res.length).toBe(3)130 expect(res[0].handler).toEqual('auth middleware')131 expect(res[1].handler).toEqual('top')132 expect(res[2].handler).toEqual('fallback')133 res = match('GET', '/api/posts')134 expect(res.length).toBe(3)135 expect(res[0].handler).toEqual('auth middleware')136 expect(res[1].handler).toEqual('posts')137 expect(res[2].handler).toEqual('fallback')138 })139 })140141 describe('Registration order', () => {142 it('middleware -> handler', async () => {143 router.add('GET', '*', 'bar')144 router.add('GET', '/:type/:action', 'foo')145 const res = match('GET', '/posts/123')146 expect(res.length).toBe(2)147 expect(res[0].handler).toEqual('bar')148 expect(res[1].handler).toEqual('foo')149 })150151 it('handler -> fallback', async () => {152 router.add('GET', '/:type/:action', 'foo')153 router.add('GET', '*', 'fallback')154 const res = match('GET', '/posts/123')155 expect(res.length).toBe(2)156 expect(res[0].handler).toEqual('foo')157 expect(res[1].handler).toEqual('fallback')158 })159 })160161 describe('Multi match', () => {162 describe('Blog', () => {163 beforeEach(() => {164 router.add('ALL', '*', 'middleware a')165 router.add('GET', '*', 'middleware b')166 router.add('GET', '/entry', 'get entries')167 router.add('POST', '/entry/*', 'middleware c')168 router.add('POST', '/entry', 'post entry')169 router.add('GET', '/entry/:id', 'get entry')170 router.add('GET', '/entry/:id/comment/:comment_id', 'get comment')171 })172173 it('GET /', async () => {174 const res = match('GET', '/')175 expect(res.length).toBe(2)176 expect(res[0].handler).toEqual('middleware a')177 expect(res[1].handler).toEqual('middleware b')178 })179 it('GET /entry/123', async () => {180 const res = match('GET', '/entry/123')181 expect(res.length).toBe(3)182 expect(res[0].handler).toEqual('middleware a')183 expect(res[0].params['id']).toBe(undefined)184 expect(res[1].handler).toEqual('middleware b')185 expect(res[1].params['id']).toBe(undefined)186 expect(res[2].handler).toEqual('get entry')187 expect(res[2].params['id']).toBe('123')188 })189 it('GET /entry/123/comment/456', async () => {190 const res = match('GET', '/entry/123/comment/456')191 expect(res.length).toBe(3)192 expect(res[0].handler).toEqual('middleware a')193 expect(res[0].params['id']).toBe(undefined)194 expect(res[0].params['comment_id']).toBe(undefined)195 expect(res[1].handler).toEqual('middleware b')196 expect(res[1].params['id']).toBe(undefined)197 expect(res[1].params['comment_id']).toBe(undefined)198 expect(res[2].handler).toEqual('get comment')199 expect(res[2].params['id']).toBe('123')200 expect(res[2].params['comment_id']).toBe('456')201 })202 it('POST /entry', async () => {203 const res = match('POST', '/entry')204 expect(res.length).toBe(3)205 expect(res[0].handler).toEqual('middleware a')206 expect(res[1].handler).toEqual('middleware c')207 expect(res[2].handler).toEqual('post entry')208 })209 it('DELETE /entry', async () => {210 const res = match('DELETE', '/entry')211 expect(res.length).toBe(1)212 expect(res[0].handler).toEqual('middleware a')213 })214 })215216 describe('`params` per a handler', () => {217 beforeEach(() => {218 router.add('ALL', '*', 'middleware a')219 router.add('GET', '/entry/:id/*', 'middleware b')220 router.add('GET', '/entry/:id/:action', 'action')221 })222223 it('GET /entry/123/show', async () => {224 const res = match('GET', '/entry/123/show')225 expect(res.length).toBe(3)226 expect(res[0].handler).toEqual('middleware a')227 expect(res[0].params['id']).toBe(undefined)228 expect(res[0].params['action']).toBe(undefined)229 expect(res[1].handler).toEqual('middleware b')230 expect(res[1].params['id']).toBe('123')231 expect(res[1].params['comment_id']).toBe(undefined)232 expect(res[2].handler).toEqual('action')233 expect(res[2].params['id']).toBe('123')234 expect(res[2].params['action']).toBe('show')235 })236 })237238 it('hierarchy', () => {239 router.add('GET', '/posts/:id/comments/:comment_id', 'foo')240 router.add('GET', '/posts/:id', 'bar')241 expect(() => {242 router.match('GET', '/')243 }).not.toThrow()244 })245 })246247 describe('Duplicate param name', () => {248 it('self', () => {249 router.add('GET', '/:id/:id', 'foo')250 const res = match('GET', '/123/456')251 expect(res.length).toBe(1)252 expect(res[0].handler).toEqual('foo')253 expect(res[0].params['id']).toBe('123')254 })255256 it('parent', () => {257 router.add('GET', '/:id/:action', 'foo')258 router.add('GET', '/posts/:id', 'bar')259 const res = match('GET', '/posts/get')260 expect(res.length).toBe(2)261 expect(res[0].handler).toEqual('foo')262 expect(res[0].params['id']).toBe('posts')263 expect(res[0].params['action']).toBe('get')264 expect(res[1].handler).toEqual('bar')265 expect(res[1].params['id']).toBe('get')266 })267268 it('child', () => {269 router.add('GET', '/posts/:id', 'foo')270 router.add('GET', '/:id/:action', 'bar')271 const res = match('GET', '/posts/get')272 expect(res.length).toBe(2)273 expect(res[0].handler).toEqual('foo')274 expect(res[0].params['id']).toBe('get')275 expect(res[1].handler).toEqual('bar')276 expect(res[1].params['id']).toBe('posts')277 expect(res[1].params['action']).toBe('get')278 })279 })280281 describe('page', () => {282 it('GET /page', async () => {283 router.add('GET', '/page', 'page')284 router.add('ALL', '*', 'fallback') // or '*'285286 const res = match('GET', '/page')287 expect(res.length).toBe(2)288 expect(res[0].handler).toEqual('page')289 expect(res[1].handler).toEqual('fallback')290 })291 })292293 describe('star', () => {294 beforeEach(() => {295 router.add('GET', '/', '/')296 router.add('GET', '/*', '/*')297 router.add('GET', '*', '*')298299 router.add('GET', '/x', '/x')300 router.add('GET', '/x/*', '/x/*')301 })302303 it('top', async () => {304 const res = match('GET', '/')305 expect(res.length).toBe(3)306 expect(res[0].handler).toEqual('/')307 expect(res[1].handler).toEqual('/*')308 expect(res[2].handler).toEqual('*')309 })310311 it('Under a certain path', async () => {312 const res = match('GET', '/x')313 expect(res.length).toBe(4)314 expect(res[0].handler).toEqual('/*')315 expect(res[1].handler).toEqual('*')316 expect(res[2].handler).toEqual('/x')317 expect(res[3].handler).toEqual('/x/*')318 })319 })320321 describe('Optional route', () => {322 beforeEach(() => {323 router.add('GET', '/api/animals/:type?', 'animals')324 router.add('GET', '/v1/:version?/:platform?', 'result')325 })326327 it('GET /api/animals/dog', async () => {328 const res = match('GET', '/api/animals/dog')329 expect(res.length).toBe(1)330 expect(res[0].handler).toEqual('animals')331 expect(res[0].params['type']).toBe('dog')332 })333 it('GET /api/animals', async () => {334 const res = match('GET', '/api/animals')335 expect(res.length).toBe(1)336 expect(res[0].handler).toEqual('animals')337 expect(res[0].params['type']).toBeUndefined()338 })339 it('GET /v1/123/abc', () => {340 const res = match('GET', '/v1/123/abc')341 expect(res.length).toBe(1)342 expect(res[0].handler).toEqual('result')343 expect(res[0].params['version']).toBe('123')344 expect(res[0].params['platform']).toBe('abc')345 })346 it('GET /v1/123', () => {347 const res = match('GET', '/v1/123')348 expect(res.length).toBe(1)349 expect(res[0].handler).toEqual('result')350 expect(res[0].params['version']).toBe('123')351 expect(res[0].params['platform']).toBeUndefined()352 })353 it('GET /v1', () => {354 const res = match('GET', '/v1')355 expect(res.length).toBe(1)356 expect(res[0].handler).toEqual('result')357 expect(res[0].params['version']).toBeUndefined()358 expect(res[0].params['platform']).toBeUndefined()359 })360 })361362 describe('All', () => {363 beforeEach(() => {364 router.add('GET', '/hello', 'get hello')365 router.add('ALL', '/all', 'get all')366 })367368 it('GET, all hello', async () => {369 const res = match('GET', '/all')370 expect(res.length).toBe(1)371 })372 })373374 describe('long prefix, then star', () => {375 describe('GET only', () => {376 beforeEach(() => {377 router.add('GET', '/long/prefix/*', 'long-prefix')378 router.add('GET', '/long/*', 'long')379 router.add('GET', '*', 'star1')380 router.add('GET', '*', 'star2')381 })382383 it('GET /', () => {384 const res = match('GET', '/')385 expect(res.length).toBe(2)386 expect(res[0].handler).toEqual('star1')387 expect(res[1].handler).toEqual('star2')388 })389390 it('GET /long/prefix', () => {391 const res = match('GET', '/long/prefix')392 expect(res.length).toBe(4)393 expect(res[0].handler).toEqual('long-prefix')394 expect(res[1].handler).toEqual('long')395 expect(res[2].handler).toEqual('star1')396 expect(res[3].handler).toEqual('star2')397 })398399 it('GET /long/prefix/test', () => {400 const res = match('GET', '/long/prefix/test')401 expect(res.length).toBe(4)402 expect(res[0].handler).toEqual('long-prefix')403 expect(res[1].handler).toEqual('long')404 expect(res[2].handler).toEqual('star1')405 expect(res[3].handler).toEqual('star2')406 })407 })408409 describe('ALL and GET', () => {410 beforeEach(() => {411 router.add('ALL', '/long/prefix/*', 'long-prefix')412 router.add('ALL', '/long/*', 'long')413 router.add('GET', '*', 'star1')414 router.add('GET', '*', 'star2')415 })416417 it('GET /', () => {418 const res = match('GET', '/')419 expect(res.length).toBe(2)420 expect(res[0].handler).toEqual('star1')421 expect(res[1].handler).toEqual('star2')422 })423424 it('GET /long/prefix', () => {425 const res = match('GET', '/long/prefix')426 expect(res.length).toBe(4)427 expect(res[0].handler).toEqual('long-prefix')428 expect(res[1].handler).toEqual('long')429 expect(res[2].handler).toEqual('star1')430 expect(res[3].handler).toEqual('star2')431 })432433 it('GET /long/prefix/test', () => {434 const res = match('GET', '/long/prefix/test')435 expect(res.length).toBe(4)436 expect(res[0].handler).toEqual('long-prefix')437 expect(res[1].handler).toEqual('long')438 expect(res[2].handler).toEqual('star1')439 expect(res[3].handler).toEqual('star2')440 })441 })442 })443444 describe('Including slashes', () => {445 beforeEach(() => {446 router.add('GET', '/js/:filename{[a-z0-9/]+.js}', 'any file')447 })448449 it('GET /js/main.js', () => {450 router.add('GET', '/js/main.js', 'main.js')451452 const res = match('GET', '/js/main.js')453 expect(res.length).toBe(2)454 expect(res[0].handler).toEqual('any file')455 expect(res[0].params['filename']).toEqual('main.js')456 expect(res[1].handler).toEqual('main.js')457 expect(res[1].params['filename']).toEqual(undefined)458 })459460 it('GET /js/chunk/123.js', () => {461 const res = match('GET', '/js/chunk/123.js')462 expect(res.length).toBe(1)463 expect(res[0].handler).toEqual('any file')464 expect(res[0].params['filename']).toEqual('chunk/123.js')465 })466467 it('GET /js/chunk/nest/123.js', () => {468 const res = match('GET', '/js/chunk/nest/123.js')469 expect(res.length).toBe(1)470 expect(res[0].handler).toEqual('any file')471 expect(res[0].params['filename']).toEqual('chunk/nest/123.js')472 })473 })474475 describe('REST API', () => {476 beforeEach(() => {477 router.add('GET', '/users/:username{[a-z]+}', 'profile')478 router.add('GET', '/users/:username{[a-z]+}/posts', 'posts')479 })480481 it('GET /users/hono', () => {482 const res = match('GET', '/users/hono')483 expect(res.length).toBe(1)484 expect(res[0].handler).toEqual('profile')485 })486487 it('GET /users/hono/posts', () => {488 const res = match('GET', '/users/hono/posts')489 expect(res.length).toBe(1)490 expect(res[0].handler).toEqual('posts')491 })492 })493494 describe('Trailing slash', () => {495 beforeEach(() => {496 router.add('GET', '/book', 'GET /book')497 router.add('GET', '/book/:id', 'GET /book/:id')498 })499500 it('GET /book', () => {501 const res = match('GET', '/book')502 expect(res.length).toBe(1)503 expect(res[0].handler).toEqual('GET /book')504 })505 it('GET /book/', () => {506 const res = match('GET', '/book/')507 expect(res.length).toBe(0)508 })509 })510511 describe('Same path', () => {512 beforeEach(() => {513 router.add('GET', '/hey', 'Middleware A')514 router.add('GET', '/hey', 'Middleware B')515 })516517 it('GET /hey', () => {518 const res = match('GET', '/hey')519 expect(res.length).toBe(2)520 expect(res[0].handler).toEqual('Middleware A')521 expect(res[1].handler).toEqual('Middleware B')522 })523 })524525 describe('Routing with a hostname', () => {526 beforeEach(() => {527 router.add('GET', 'www1.example.com/hello', 'www1')528 router.add('GET', 'www2.example.com/hello', 'www2')529 })530 it('GET www1.example.com/hello', () => {531 const res = match('GET', 'www1.example.com/hello')532 expect(res.length).toBe(1)533 expect(res[0].handler).toEqual('www1')534 })535 it('GET www2.example.com/hello', () => {536 const res = match('GET', 'www2.example.com/hello')537 expect(res.length).toBe(1)538 expect(res[0].handler).toEqual('www2')539 })540 it('GET /hello', () => {541 const res = match('GET', '/hello')542 expect(res.length).toBe(0)543 })544 })545546 describe('static routes of ALL and GET', () => {547 beforeEach(() => {548 router.add('ALL', '/foo', 'foo')549 router.add('GET', '/bar', 'bar')550 })551552 it('get /foo', () => {553 const res = match('GET', '/foo')554 expect(res[0].handler).toEqual('foo')555 })556 })557558 describe('ALL and Star', () => {559 beforeEach(() => {560 router.add('ALL', '/x', '/x')561 router.add('GET', '*', 'star')562 })563564 it('Should return /x and star', async () => {565 const res = match('GET', '/x')566 expect(res.length).toBe(2)567 expect(res[0].handler).toEqual('/x')568 expect(res[1].handler).toEqual('star')569 })570 })571572 describe('GET star, ALL static, GET star...', () => {573 beforeEach(() => {574 router.add('GET', '*', 'star1')575 router.add('ALL', '/x', '/x')576 router.add('GET', '*', 'star2')577 router.add('GET', '*', 'star3')578 })579580 it('Should return /x and star', async () => {581 const res = match('GET', '/x')582 expect(res.length).toBe(4)583 expect(res[0].handler).toEqual('star1')584 expect(res[1].handler).toEqual('/x')585 expect(res[2].handler).toEqual('star2')586 expect(res[3].handler).toEqual('star3')587 })588 })589590 // https://github.com/honojs/hono/issues/699591 describe('GET star, GET static, ALL star...', () => {592 beforeEach(() => {593 router.add('GET', '/y/*', 'star1')594 router.add('GET', '/y/a', 'a')595 router.add('ALL', '/y/b/*', 'star2')596 router.add('GET', '/y/b/bar', 'bar')597 })598599 it('Should return star1, star2, and bar', async () => {600 const res = match('GET', '/y/b/bar')601 expect(res.length).toBe(3)602 expect(res[0].handler).toEqual('star1')603 expect(res[1].handler).toEqual('star2')604 expect(res[2].handler).toEqual('bar')605 })606 })607608 describe('ALL star, ALL star, GET static, ALL star...', () => {609 beforeEach(() => {610 router.add('ALL', '*', 'wildcard')611 router.add('ALL', '/a/*', 'star1')612 router.add('GET', '/a/foo', 'foo')613 router.add('ALL', '/b/*', 'star2')614 router.add('GET', '/b/bar', 'bar')615 })616617 it('Should return wildcard, star2 and bar', async () => {618 const res = match('GET', '/b/bar')619 expect(res.length).toBe(3)620 expect(res[0].handler).toEqual('wildcard')621 expect(res[1].handler).toEqual('star2')622 expect(res[2].handler).toEqual('bar')623 })624 })625626 describe('Capture Group', () => {627 describe('Simple capturing group', () => {628 beforeEach(() => {629 router.add('get', '/foo/:capture{(?:bar|baz)}', 'ok')630 })631632 it('GET /foo/bar', () => {633 const res = match('get', '/foo/bar')634 expect(res.length).toBe(1)635 expect(res[0].handler).toBe('ok')636 expect(res[0].params['capture']).toBe('bar')637 })638639 it('GET /foo/baz', () => {640 const res = match('get', '/foo/baz')641 expect(res.length).toBe(1)642 expect(res[0].handler).toBe('ok')643 expect(res[0].params['capture']).toBe('baz')644 })645646 it('GET /foo/qux', () => {647 const res = match('get', '/foo/qux')648 expect(res.length).toBe(0)649 })650 })651652 describe('Non-capturing group', () => {653 beforeEach(() => {654 router.add('get', '/foo/:capture{(?:bar|baz)}', 'ok')655 })656657 it('GET /foo/bar', () => {658 const res = match('get', '/foo/bar')659 expect(res.length).toBe(1)660 expect(res[0].handler).toBe('ok')661 expect(res[0].params['capture']).toBe('bar')662 })663664 it('GET /foo/baz', () => {665 const res = match('get', '/foo/baz')666 expect(res.length).toBe(1)667 expect(res[0].handler).toBe('ok')668 expect(res[0].params['capture']).toBe('baz')669 })670671 it('GET /foo/qux', () => {672 const res = match('get', '/foo/qux')673 expect(res.length).toBe(0)674 })675 })676677 describe('Non-capturing group with prefix', () => {678 beforeEach(() => {679 router.add('get', '/foo/:capture{ba(?:r|z)}', 'ok')680 })681682 it('GET /foo/bar', () => {683 const res = match('get', '/foo/bar')684 expect(res.length).toBe(1)685 expect(res[0].handler).toBe('ok')686 expect(res[0].params['capture']).toBe('bar')687 })688689 it('GET /foo/baz', () => {690 const res = match('get', '/foo/baz')691 expect(res.length).toBe(1)692 expect(res[0].handler).toBe('ok')693 expect(res[0].params['capture']).toBe('baz')694 })695696 it('GET /foo/qux', () => {697 const res = match('get', '/foo/qux')698 expect(res.length).toBe(0)699 })700 })701702 describe('Complex capturing group', () => {703 it('GET request', () => {704 router.add('get', '/foo/:capture{ba(r|z)}', 'ok')705706 const res = match('get', '/foo/bar')707 expect(res.length).toBe(1)708 expect(res[0].handler).toBe('ok')709 })710 })711 })712713 describe('Unknown method', () => {714 beforeEach(() => {715 router.add('GET', '/', 'index')716 router.add('ALL', '/all', 'all')717 })718719 it('UNKNOWN_METHOD /', () => {720 const res = match('UNKNOWN_METHOD', '/')721 expect(res.length).toBe(0)722 })723724 it('UNKNOWN_METHOD /all', () => {725 const res = match('UNKNOWN_METHOD', '/all')726 expect(res.length).toBe(1)727 expect(res[0].handler).toBe('all')728 })729 })730 })731}
modified:vitest.config.ts+1 added-1 removed
3 unmodified lines
4export default defineConfig({5 test: {6 globals: true,7 include: ['**/src/**/(*.)+(spec|test).+(ts|tsx|js)'],8 exclude: [...configDefaults.exclude, '**/sandbox/**'],8 exclude: [...configDefaults.exclude, '**/sandbox/**', '**/*.case.test.+(ts|tsx|js)'],9 setupFiles: ['./src/test-utils/setup-vitest.ts'],10 coverage: {11 provider: 'v8',12 reporter: ['text'],
4 unmodified lines