Skip to content
Myr

feat: update visibility (#164)

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

authored by Yusuke Wada · committed by GitHub · parent 1a30e37 · Browse files · 12 files+75 added−101 removed
This commit

State

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

Diff stats

12 files+75 added−101 removed

  • tests:+53 added−75 removed
  • other:+22 added−26 removed

12 files+75 added−101 removed

modified:src/middleware/basic-auth/index.test.ts+11 added-11 removed
modified:src/middleware/body-parse/index.test.ts+3 added-3 removed
modified:src/middleware/cookie/index.test.ts+3 added-5 removed
16 unmodified lines
17 it('Parse cookie on c.req.cookie', async () => {18 const req = new Request('http://localhost/cookie')19 const cookieString = 'yummy_cookie=choco; tasty_cookie = strawberry '20 req.headers.set('Cookie', cookieString)21 const res = await app.dispatch(req)21 const res = await app.request(req)2223 expect(res.headers.get('Yummy-Cookie')).toBe('choco')24 expect(res.headers.get('Tasty-Cookie')).toBe('strawberry')25 })
5 unmodified lines
31 return c.text('Give cookie')32 })3334 it('Set cookie on c.cookie', async () => {35 const req = new Request('http://localhost/set-cookie')36 const res = await app.dispatch(req)35 const res = await app.request('http://localhost/set-cookie')36 expect(res.status).toBe(200)37 const header = res.headers.get('Set-Cookie')38 expect(header).toBe('delicious_cookie=macha')39 })
13 unmodified lines
53 return c.text('Give cookie')54 })5556 it('Complex pattern', async () => {58 const req = new Request('http://localhost/set-cookie-complex')59 const res = await app.dispatch(req)57 const res = await app.request('http://localhost/set-cookie-complex')58 expect(res.status).toBe(200)59 const header = res.headers.get('Set-Cookie')60 expect(header).toBe(61 'great_cookie=banana; Max-Age=1000; Domain=example.com; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict'
3 unmodified lines
modified:src/middleware/cors/index.test.ts+3 added-4 removed
23 unmodified lines
24 return c.json({ success: true })25 })2627 it('GET default', async () => {28 const req = new Request('http://localhost/api/abc')29 const res = await app.dispatch(req)28 const res = await app.request('http://localhost/api/abc')2930 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*')31 expect(res.headers.get('Vary')).toBeNull()32 })3334 it('Preflight default', async () => {35 const req = new Request('https://localhost/api/abc', { method: 'OPTIONS' })36 req.headers.append('Access-Control-Request-Headers', 'X-PINGOTHER, Content-Type')38 const res = await app.dispatch(req)37 const res = await app.request(req)3839 expect(res.status).toBe(204)40 expect(res.headers.get('Access-Control-Allow-Methods').split(',')[0]).toBe('GET')41 expect(res.headers.get('Access-Control-Allow-Headers').split(',')).toEqual([
3 unmodified lines
45 })4647 it('Preflight with options', async () => {48 const req = new Request('https://localhost/api2/abc', { method: 'OPTIONS' })50 const res = await app.dispatch(req)49 const res = await app.request(req)5051 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')52 expect(res.headers.get('Vary').split(/\s*,\s*/)).toEqual(expect.arrayContaining(['Origin']))53 expect(res.headers.get('Access-Control-Allow-Headers').split(/\s*,\s*/)).toEqual([
16 unmodified lines
modified:src/middleware/etag/index.test.ts+9 added-18 removed
15 unmodified lines
16 app.get('/etag-weak/abc', (c) => {17 return c.text('Hono is cool')18 })1920 it('should return etag header', async () => {21 let req = new Request('http://localhost/etag/abc')22 let res = await app.dispatch(req)2320 it('Should return etag header', async () => {21 let res = await app.request('http://localhost/etag/abc')22 expect(res.headers.get('ETag')).not.toBeFalsy()23 expect(res.headers.get('ETag')).toBe('"4e32298b1cb4edc595237405e5b696e105c2399a"')2427 req = new Request('http://localhost/etag/def')28 res = await app.dispatch(req)2925 res = await app.request('http://localhost/etag/def')26 expect(res.headers.get('ETag')).not.toBeFalsy()27 expect(res.headers.get('ETag')).toBe('"c1d44ff03aff1372856c281854f454e2e1d15b7c"')28 })2934 it('should return etag header - weak', async () => {35 const req = new Request('http://localhost/etag-weak/abc')36 const res = await app.dispatch(req)3730 it('Should return etag header - weak', async () => {31 const res = await app.request('http://localhost/etag-weak/abc')32 expect(res.headers.get('ETag')).not.toBeFalsy()33 expect(res.headers.get('ETag')).toBe('W/"4e32298b1cb4edc595237405e5b696e105c2399a"')34 })3542 it('should return 304 response', async () => {43 let req = new Request('http://localhost/etag/abc')44 let res = await app.dispatch(req)4536 it('Should return 304 response', async () => {37 let res = await app.request('http://localhost/etag/abc')38 expect(res.status).toBe(200)39 expect(res.headers.get('ETag')).not.toBeFalsy()40 const etag = res.headers.get('Etag')4150 req = new Request('http://localhost/etag/abc', {42 const req = new Request('http://localhost/etag/abc', {43 headers: {44 'If-None-Match': etag,45 },46 })55 res = await app.dispatch(req)5647 res = await app.request(req)48 expect(res.status).toBe(304)49 expect(await res.text()).toBe('')50 })51})
modified:src/middleware/logger/index.test.ts+4 added-9 removed
16 unmodified lines
17 app.get('/long', (c) => c.text(longRandomString))18 app.get('/empty', (c) => c.text(''))1920 it('Log status 200 with empty body', async () => {21 const req = new Request('http://localhost/empty')22 const res = await app.dispatch(req)21 const res = await app.request('http://localhost/empty')22 expect(res).not.toBeNull()23 expect(res.status).toBe(200)24 expect(log.startsWith(' --> GET /empty \x1b[32m200\x1b[0m')).toBe(true)25 expect(log.endsWith(' 0b')).toBe(true)26 })2728 it('Log status 200 with small body', async () => {30 const req = new Request('http://localhost/short')31 const res = await app.dispatch(req)29 const res = await app.request('http://localhost/short')30 expect(res).not.toBeNull()31 expect(res.status).toBe(200)32 expect(log.startsWith(' --> GET /short \x1b[32m200\x1b[0m')).toBe(true)33 expect(log.endsWith(` ${shortRandomString.length}b`)).toBe(true)34 })3536 it('Log status 200 with big body', async () => {39 const req = new Request('http://localhost/long')40 const res = await app.dispatch(req)37 const res = await app.request('http://localhost/long')38 expect(res).not.toBeNull()39 expect(res.status).toBe(200)40 expect(log.startsWith(' --> GET /long \x1b[32m200\x1b[0m')).toBe(true)41 expect(log.endsWith(` ${longRandomString.length / 1024}kB`)).toBe(true)
3 unmodified lines
45 const msg = 'Default 404 Not Found'46 app.all('*', (c) => {47 return c.text(msg, 404)48 })5253 const req = new Request('http://localhost/notfound')49 const res = await app.request('http://localhost/notfound')54 const res = await app.dispatch(req)50 expect(res).not.toBeNull()51 expect(res.status).toBe(404)52 expect(log.startsWith(' --> GET /notfound \x1b[33m404\x1b[0m')).toBe(true)53 expect(log.endsWith(` ${msg.length}b`)).toBe(true)
2 unmodified lines
modified:src/middleware/mustache/index.test.ts+2 added-4 removed
43 unmodified lines
44 return c.render('index')45 })4647 it('Mustache template rendering', async () => {48 const req = new Request('http://localhost/foo')49 const res = await app.dispatch(req)48 const res = await app.request('http://localhost/foo')49 expect(res.status).toBe(200)50 expect(await res.text()).toBe('<html><body>Title: Hono!</body></html>')51 })5253 it('Mustache template rendering with root', async () => {55 const req = new Request('http://localhost/bar')56 const res = await app.dispatch(req)54 const res = await app.request('http://localhost/bar')55 expect(res.status).toBe(200)56 expect(await res.text()).toBe('<h1>With Root</h1>')57 })58})
modified:src/middleware/powered-by/index.test.ts+3 added-4 removed
3 unmodified lines
4describe('Powered by Middleware', () => {5 const app = new Hono()67 app.use('*', poweredBy())8 app.get('/', () => new Response('root'))8 app.get('/', (c) => c.text('root'))910 it('Response headers include X-Powered-By', async () => {11 const req = new Request('http://localhost/')12 const res = await app.dispatch(req)10 it('Should return with X-Powered-By header', async () => {11 const res = await app.request('http://localhost/')12 expect(res).not.toBeNull()13 expect(res.status).toBe(200)14 expect(res.headers.get('X-Powered-By')).toBe('Hono')15 })
1 unmodified line
modified:src/middleware/pretty-json/index.test.ts+2 added-4 removed
8 unmodified lines
9 })1011 it('Should return pretty JSON output', async () => {12 app.use('*', prettyJSON())13 const req = new Request('http://localhost/?pretty')14 const res = await app.dispatch(req)13 const res = await app.request('http://localhost/?pretty')14 expect(res).not.toBeNull()15 expect(res.status).toBe(200)16 expect(await res.text()).toBe(`{17 "message": "Hono!"
1 unmodified line
19 })2021 it('Should return pretty JSON output with 4 spaces', async () => {22 app.use('*', prettyJSON({ space: 4 }))24 const req = new Request('http://localhost/?pretty')25 const res = await app.dispatch(req)23 const res = await app.request('http://localhost/?pretty')24 expect(res).not.toBeNull()25 expect(res.status).toBe(200)26 expect(await res.text()).toBe(`{27 "message": "Hono!"
3 unmodified lines
modified:src/middleware/serve-static/index.test.ts+13 added-13 removed
24 unmodified lines
25})2627describe('ServeStatic Middleware', () => {28 const app = new Hono()2929 app.use('/static/*', serveStatic({ root: './assets' }))30 app.use('/static-no-root/*', serveStatic())3133 it('Serve static files', async () => {34 let req = new Request('http://localhost/static/plain.txt')35 let res = await app.dispatch(req)32 it('Should return plain.txt', async () => {33 const res = await app.request('http://localhost/static/plain.txt')34 expect(res.status).toBe(200)35 expect(await res.text()).toBe('This is plain.txt')36 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')37 expect(res.headers.get('Content-Length')).toBe('17')38 })3941 req = new Request('http://localhost/static/hono.html')42 res = await app.dispatch(req)40 it('Should return hono.html', async () => {41 const res = await app.request('http://localhost/static/hono.html')42 expect(res.status).toBe(200)43 expect(await res.text()).toBe('<h1>Hono!</h1>')44 expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8')45 expect(res.headers.get('Content-Length')).toBe('14')46 })4748 req = new Request('http://localhost/static/not-found.html')49 res = await app.dispatch(req)48 it('Should return 404 response', async () => {49 const res = await app.request('http://localhost/static/not-found.html')50 expect(res.status).toBe(404)51 })5252 req = new Request('http://localhost/static-no-root/plain.txt')53 res = await app.dispatch(req)53 it('Should return plan.txt', async () => {54 const res = await app.request('http://localhost/static-no-root/plain.txt')55 expect(res.status).toBe(200)56 expect(await res.text()).toBe('That is plain.txt')57 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')58 expect(res.headers.get('Content-Length')).toBe('17')59 })6060 it('Serve index.html', async () => {61 const req = new Request('http://localhost/static/top')62 const res = await app.dispatch(req)61 it('Should return index.html', async () => {62 const res = await app.request('http://localhost/static/top')63 expect(res.status).toBe(200)64 expect(await res.text()).toBe('<h1>Top</h1>')65 expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8')66 expect(res.headers.get('Content-Length')).toBe('12')
2 unmodified lines
modified:src/context.ts+13 added-9 removed
23 unmodified lines
24 constructor(25 req: Request<RequestParamKeyType>,26 opts?: { res: Response; env: Env; event: FetchEvent }27 ) {28 this.req = req28 this.req = this.initRequest(req)29 if (opts) {30 this.res = opts.res31 this.env = opts.env32 this.event = opts.event33 }34 this._headers = {}35 }3637 private initRequest<T>(req: Request<T>): Request<T> {38 req.header = (name: string): string => {39 return req.headers.get(name)40 }41 req.query = (key: string): string => {42 const url = new URL(req.url)43 return url.searchParams.get(key)44 }45 return req46 }4748 header(name: string, value: string): void {38 /*39 XXX:40 app.use('*', (c, next) => {41 next()42 c.header('foo', 'bar') // => c.res.headers.set(...)43 })44 */49 if (this.res) {50 this.res.headers.set(name, value)51 }52 this._headers[name] = value
51 unmodified lines
104 }105106 json(object: object, status: StatusCode = this._status, headers: Headers = {}): Response {107 if (typeof object !== 'object') {104 throw new TypeError('json method arg must be a object!')108 throw new TypeError('json method arg must be an object!')109 }110 const body = this._pretty111 ? JSON.stringify(object, null, this._prettySpace)112 : JSON.stringify(object)
29 unmodified lines
modified:src/hono.ts+9 added-17 removed
9 unmodified lines
10 interface Request<ParamKeyType = string> {11 param: (key: ParamKeyType) => string12 query: (key: string) => string13 header: (name: string) => string14 // TODO: do not use `any`14 parsedBody: any15 }16}17
44 unmodified lines
62 'options',63 'patch',64 'all'65) {67 routerClass: { new (): Router<any> } = TrieRouter68 strict: boolean = true // strict routing - default is true69 router: Router<Handler>70 middlewareRouters: Router<MiddlewareHandler>[]71 tempPath: string66 readonly routerClass: { new (): Router<any> } = TrieRouter67 readonly strict: boolean = true // strict routing - default is true68 private router: Router<Handler>69 private middlewareRouters: Router<MiddlewareHandler>[]70 private tempPath: string7172 constructor(init: Partial<Pick<Hono, 'routerClass' | 'strict'>> = {}) {73 super()74
60 unmodified lines
135 private async matchRoute(method: string, path: string): Promise<Result<Handler>> {136 return this.router.match(method, path)137 }138140 async dispatch(request: Request, env?: Env, event?: FetchEvent): Promise<Response> {139 private async dispatch(request: Request, env?: Env, event?: FetchEvent): Promise<Response> {140 const path = getPathFromURL(request.url, { strict: this.strict })141 const method = request.method142143 const result = await this.matchRoute(method, path)
1 unmodified line
145 // Methods for Request object146 request.param = (key: string): string => {147 if (result) return result.params[key]148 }150 request.header = (name: string): string => {151 return request.headers.get(name)152 }153 request.query = (key: string): string => {154 const url = new URL(c.req.url)155 return url.searchParams.get(key)156 }149150 const handler = result ? result.handler : this.notFoundHandler151152 const middleware = []
30 unmodified lines
183 async fetch(request: Request, env?: Env, event?: FetchEvent): Promise<Response> {184 return this.dispatch(request, env, event)185 }186195 request(input: RequestInfo, requestInit?: RequestInit) {196 const req = new Request(input, requestInit)187 request(input: RequestInfo, requestInit?: RequestInit): Promise<Response> {188 const req = input instanceof Request ? input : new Request(input, requestInit)189 return this.dispatch(req)190 }191200 fire() {192 fire(): void {193 addEventListener('fetch', (event: FetchEvent): void => {194 event.respondWith(this.handleEvent(event))195 })196 }
1 unmodified line