Skip to content
Myr

node.test.ts

This file

Path

test/node.test.ts

177 lines · 5.4 KiB · TypeScript

Last changed by

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

History of this file

Take it

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 1–6why, one step back
1import { Node } from '../src/node'
23describe('Root Node', () => {4 const node = new Node()5 node.insert('get', '/', 'get root')6 it('get /', () => {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 7why, one step back
7 const res = node.search('get', '/')

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 8–64why, one step back
8 expect(res).not.toBeNull()
9 expect(res.handler).toBe('get root')10 expect(node.search('get', '/hello')).toBeNull()11 })12})1314describe('Root Node is not defined', () => {15 const node = new Node()16 node.insert('get', '/hello', 'get hello')17 it('get /', () => {18 expect(node.search('get', '/')).toBeNull()19 })20})2122describe('All with *', () => {23 const node = new Node()24 node.insert('get', '*', 'get all')25 it('get /', () => {26 expect(node.search('get', '/')).not.toBeNull()27 expect(node.search('get', '/hello')).not.toBeNull()28 })29})3031describe('Basic Usage', () => {32 const node = new Node()33 node.insert('get', '/hello', 'get hello')34 node.insert('post', '/hello', 'post hello')35 node.insert('get', '/hello/foo', 'get hello foo')3637 it('get, post /hello', () => {38 expect(node.search('get', '/')).toBeNull()39 expect(node.search('post', '/')).toBeNull()4041 expect(node.search('get', '/hello').handler).toBe('get hello')42 expect(node.search('post', '/hello').handler).toBe('post hello')43 expect(node.search('put', '/hello')).toBeNull()44 })45 it('get /nothing', () => {46 expect(node.search('get', '/nothing')).toBeNull()47 })48 it('/hello/foo, /hello/bar', () => {49 expect(node.search('get', '/hello/foo').handler).toBe('get hello foo')50 expect(node.search('post', '/hello/foo')).toBeNull()51 expect(node.search('get', '/hello/bar')).toBeNull()52 })53 it('/hello/foo/bar', () => {54 expect(node.search('get', '/hello/foo/bar')).toBeNull()55 })56})5758describe('Name path', () => {59 const node = new Node()60 node.insert('get', '/entry/:id', 'get entry')61 node.insert('get', '/entry/:id/comment/:comment_id', 'get comment')62 node.insert('get', '/map/:location/events', 'get events')6364 it('get /entry/123', () => {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 65why, one step back
65 const res = node.search('get', '/entry/123')

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 66–73why, one step back
66 expect(res).not.toBeNull()
67 expect(res.handler).toBe('get entry')68 expect(res.params).not.toBeNull()69 expect(res.params['id']).toBe('123')70 expect(res.params['id']).not.toBe('1234')71 })7273 it('get /entry/456/comment', () => {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 74why, one step back
74 const res = node.search('get', '/entry/456/comment')

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 75–78why, one step back
75 expect(res).toBeNull()
76 })7778 it('get /entry/789/comment/123', () => {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 79why, one step back
79 const res = node.search('get', '/entry/789/comment/123')

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 80–86why, one step back
80 expect(res).not.toBeNull()
81 expect(res.handler).toBe('get comment')82 expect(res.params['id']).toBe('789')83 expect(res.params['comment_id']).toBe('123')84 })8586 it('get /map/:location/events', () => {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 87why, one step back
87 const res = node.search('get', '/map/yokohama/events')

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 88–97why, one step back
88 expect(res).not.toBeNull()
89 expect(res.handler).toBe('get events')90 expect(res.params['location']).toBe('yokohama')91 })92})9394describe('Wildcard', () => {95 const node = new Node()96 node.insert('get', '/wildcard-abc/*/wildcard-efg', 'wildcard')97 it('/wildcard-abc/xxxxxx/wildcard-efg', () => {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 98why, one step back
98 const res = node.search('get', '/wildcard-abc/xxxxxx/wildcard-efg')

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 99–103why, one step back
99 expect(res).not.toBeNull()
100 expect(res.handler).toBe('wildcard')101 })102 node.insert('get', '/wildcard-abc/*/wildcard-efg/hijk', 'wildcard')103 it('/wildcard-abc/xxxxxx/wildcard-efg/hijk', () => {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 104why, one step back
104 const res = node.search('get', '/wildcard-abc/xxxxxx/wildcard-efg/hijk')

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 105–147why, one step back
105 expect(res).not.toBeNull()
106 expect(res.handler).toBe('wildcard')107 })108})109110describe('Regexp', () => {111 const node = new Node()112 node.insert('get', '/regex-abc/:id{[0-9]+}/comment/:comment_id{[a-z]+}', 'regexp')113 it('/regexp-abc/123/comment/abc', () => {114 const res = node.search('get', '/regex-abc/123/comment/abc')115 expect(res).not.toBeNull()116 expect(res.handler).toBe('regexp')117 expect(res.params['id']).toBe('123')118 expect(res.params['comment_id']).toBe('abc')119 })120 it('/regexp-abc/abc', () => {121 const res = node.search('get', '/regex-abc/abc')122 expect(res).toBeNull()123 })124 it('/regexp-abc/123/comment/123', () => {125 const res = node.search('get', '/regex-abc/123/comment/123')126 expect(res).toBeNull()127 })128})129130describe('All', () => {131 const node = new Node()132 node.insert('ALL', '/all-methods', 'all methods') // ALL133 it('/all-methods', () => {134 let res = node.search('get', '/all-methods')135 expect(res).not.toBeNull()136 expect(res.handler).toBe('all methods')137 res = node.search('put', '/all-methods')138 expect(res).not.toBeNull()139 expect(res.handler).toBe('all methods')140 })141})142143describe('Special Wildcard', () => {144 const node = new Node()145 node.insert('ALL', '*', 'match all')146147 it('/foo', () => {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 148why, one step back
148 const res = node.search('get', '/foo')

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 149–152why, one step back
149 expect(res).not.toBeNull()
150 expect(res.handler).toBe('match all')151 })152 it('/hello', () => {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 153why, one step back
153 const res = node.search('get', '/hello')

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 154–157why, one step back
154 expect(res).not.toBeNull()
155 expect(res.handler).toBe('match all')156 })157 it('/hello/foo', () => {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 158why, one step back
158 const res = node.search('get', '/hello/foo')

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 159–167why, one step back
159 expect(res).not.toBeNull()
160 expect(res.handler).toBe('match all')161 })162})163164describe('Special Wildcard deeply', () => {165 const node = new Node()166 node.insert('ALL', '/hello/*', 'match hello')167 it('/hello', () => {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 168why, one step back
168 const res = node.search('get', '/hello')

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 169–172why, one step back
169 expect(res).not.toBeNull()
170 expect(res.handler).toBe('match hello')171 })172 it('/hello/foo', () => {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 173why, one step back
173 const res = node.search('get', '/hello/foo')

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 174–177why, one step back
174 expect(res).not.toBeNull()
175 expect(res.handler).toBe('match hello')176 })177})