Skip to content
Myr

feat: Lagon support with runtime detection & tests (#742)

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

* feat: Lagon support with runtime detection & tests * ci: build before running test:lagon * refactor: remove mentions of Lagon since it's still in dev * test: enable ETag test

authored by Tom Lienard · committed by GitHub · parent 4f772dd · Browse files · 6 files+349 added−4 removed
This commit

State

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

Diff stats

6 files+349 added−4 removed

  • tests:+200 added−0 removed
  • other:+149 added−4 removed

6 files+349 added−4 removed

modified:.github/workflows/ci.yml+12 added
modified:deno_dist/context.ts+5 added-1 removed
5 unmodified lines
6import type { Schema, SchemaToProp } from './validator/schema.ts'78type HeaderField = [string, string]9type Headers = Record<string, string | string[]>10type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'other'10type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'lagon' | 'other'11export type Data = string | ArrayBuffer | ReadableStream1213export class Context<14 P extends string = string,
215 unmodified lines
230 if (global?.process?.release?.name === 'node') {231 return 'node'232 }233234 if (global?.__lagon__ !== undefined) {235 return 'lagon'236 }237238 return 'other'239 }240}
added:lagon_test/index.test.ts+200 added
1describe('Example', () => {2 test('GET /', async () => {3 const res = await fetch('http://127.0.0.1:1234')4 expect(res.status).toBe(200)5 expect(await res.text()).toBe('Hono!!')6 expect(res.headers.get('x-powered-by')).toBe('Hono')7 expect(res.headers.get('x-response-time')).toContain('ms')8 })910 test('GET /hello/anything', async () => {11 const res = await fetch('http://127.0.0.1:1234/hello/anything')12 expect(res.status).toBe(404)13 expect(res.headers.get('x-message')).toBe('This is addHeader middleware!')14 })1516 test('GET /hello', async () => {17 const res = await fetch('http://127.0.0.1:1234/hello')18 expect(res.status).toBe(200)19 expect(await res.text()).toBe('This is /hello')20 })2122 test('GET /runtime', async () => {23 const res = await fetch('http://127.0.0.1:1234/runtime')24 expect(res.status).toBe(200)25 expect(await res.text()).toBe('lagon')26 })2728 test('GET /entry/:id', async () => {29 const res = await fetch('http://127.0.0.1:1234/entry/42')30 expect(res.status).toBe(200)31 expect(await res.text()).toBe('Your ID is 42')32 })3334 describe('/book', () => {35 test('GET /book', async () => {36 const res = await fetch('http://127.0.0.1:1234/book')37 expect(res.status).toBe(200)38 expect(await res.text()).toBe('List Books')39 })4041 test('GET /book/:id', async () => {42 const res = await fetch('http://127.0.0.1:1234/book/3')43 expect(res.status).toBe(200)44 expect(await res.text()).toBe('Get Book: 3')45 })4647 test('POST /book', async () => {48 const res = await fetch('http://127.0.0.1:1234/book', {49 method: 'POST',50 })51 expect(res.status).toBe(200)52 expect(await res.text()).toBe('Create Book')53 })54 })5556 test('GET /redirect', async () => {57 const res = await fetch('http://127.0.0.1:1234/redirect', {58 redirect: 'manual',59 })60 expect(res.status).toBe(302)61 expect(res.headers.get('location')).toBe('/')62 })6364 describe('/basic-auth', () => {65 test('GET /basic-auth/anyroute unauthorized', async () => {66 const res = await fetch('http://127.0.0.1:1234/basic-auth/anyroute')67 expect(res.status).toBe(401)68 expect(await res.text()).toBe('Unauthorized')69 })7071 test('GET /basic-auth/anyroute', async () => {72 const res = await fetch('http://127.0.0.1:1234/basic-auth/anyroute', {73 headers: {74 authorization: `Basic ${btoa('hono:acoolproject')}`,75 },76 })77 expect(res.status).toBe(200)78 expect(await res.text()).toBe('You are authorized')79 })80 })8182 describe('/bearer-auth', () => {83 test('GET /bearer-auth/anyroute unauthorized', async () => {84 const res = await fetch('http://127.0.0.1:1234/bearer-auth/anyroute')85 expect(res.status).toBe(401)86 expect(await res.text()).toBe('Unauthorized')87 })8889 test('GET /bearer-auth/anyroute', async () => {90 const res = await fetch('http://127.0.0.1:1234/bearer-auth/anyroute', {91 headers: {92 authorization: 'Bearer secrettoken',93 },94 })95 expect(res.status).toBe(200)96 expect(await res.text()).toBe('You are authorized')97 })98 })99100 test('GET /etag/cached', async () => {101 const res = await fetch('http://127.0.0.1:1234/etag/cached')102 expect(res.status).toBe(200)103 expect(res.headers.get('etag')).toBe('"90ea638841fff3c326fc22cbd156f1146ac0ac02"')104 expect(await res.text()).toBe('Is this cached?')105 })106107 test('GET /fetch-url', async () => {108 const res = await fetch('http://127.0.0.1:1234/fetch-url')109 expect(res.status).toBe(200)110 expect(await res.text()).toBe('https://example.com/ is 200')111 })112113 test('GET /user-agent', async () => {114 const res = await fetch('http://127.0.0.1:1234/user-agent', {115 headers: {116 'user-agent': 'Lagon',117 },118 })119 expect(res.status).toBe(200)120 expect(await res.text()).toBe('Your UserAgent is Lagon')121 })122123 test('GET /api/posts', async () => {124 const res = await fetch('http://127.0.0.1:1234/api/posts')125 expect(res.status).toBe(200)126 expect(await res.json()).toEqual([127 { id: 1, title: 'Good Morning' },128 { id: 2, title: 'Good Aternoon' },129 { id: 3, title: 'Good Evening' },130 { id: 4, title: 'Good Night' },131 ])132 })133134 test('POST /api/posts', async () => {135 const res = await fetch('http://127.0.0.1:1234/api/posts', {136 method: 'POST',137 })138 expect(res.status).toBe(201)139 expect(await res.json()).toEqual({ message: 'Created!' })140 })141142 test('GET /api/default-route', async () => {143 const res = await fetch('http://127.0.0.1:1234/api/default-route')144 expect(res.status).toBe(404)145 expect(await res.text()).toBe('API endpoint is not found')146 })147148 describe('/form', () => {149 test('POST /form x-www-form-urlencoded', async () => {150 const res = await fetch('http://127.0.0.1:1234/form', {151 method: 'POST',152 headers: {153 'content-type': 'application/x-www-form-urlencoded',154 },155 body: 'username=john&password=Pa%24%24w0rd',156 })157 expect(res.status).toBe(200)158 expect(await res.json()).toEqual({159 password: 'Pa$$w0rd',160 username: 'john',161 })162 })163164 test('POST /form', async () => {165 const res = await fetch('http://127.0.0.1:1234/form', {166 method: 'POST',167 headers: {168 'content-type':169 'multipart/form-data; boundary=---------------------------9051914041544843365972754266',170 },171 body: `-----------------------------9051914041544843365972754266172Content-Disposition: form-data; name="hello"173174world!175-----------------------------9051914041544843365972754266176Content-Disposition: form-data; name="description"177178this is another field179-----------------------------9051914041544843365972754266--`,180 })181 expect(res.status).toBe(200)182 expect(await res.json()).toEqual({183 description: 'this is another field',184 hello: 'world!',185 })186 })187 })188189 test('GET /error', async () => {190 const res = await fetch('http://127.0.0.1:1234/error')191 expect(res.status).toBe(500)192 expect(await res.text()).toBe('Custom Error Message')193 })194195 test('GET /api/default-route', async () => {196 const res = await fetch('http://127.0.0.1:1234/api/default-route')197 expect(res.status).toBe(404)198 expect(await res.text()).toBe('API endpoint is not found')199 })200})
added:lagon_test/index.ts+124 added
1import { Hono } from '../src'2import { basicAuth } from '../src/middleware/basic-auth'3import { bearerAuth } from '../src/middleware/bearer-auth'4import { etag } from '../src/middleware/etag'5import { poweredBy } from '../src/middleware/powered-by'6import { prettyJSON } from '../src/middleware/pretty-json'78const app = new Hono()910// Mount Builtin Middleware11app.use('*', poweredBy())12// app.use('*', logger())13app.use(14 '/basic-auth/*',15 basicAuth({16 username: 'hono',17 password: 'acoolproject',18 })19)20app.use(21 '/bearer-auth/*',22 bearerAuth({23 token: 'secrettoken',24 })25)26app.use('/etag/*', etag())2728// Custom Middleware29// Add Custom Header30app.use('/hello/*', async (c, next) => {31 await next()32 c.header('X-message', 'This is addHeader middleware!')33})3435// Add X-Response-Time header36app.use('*', async (c, next) => {37 const start = Date.now()38 await next()39 const ms = Date.now() - start40 c.header('X-Response-Time', `${ms}ms`)41})4243// Custom Not Found Message44app.notFound((c) => {45 return c.text('Custom 404 Not Found', 404)46})4748// Error handling49app.onError((err, c) => {50 console.error(`${err}`)51 return c.text('Custom Error Message', 500)52})5354app.head('/', (c) => c.text('OK'))5556// Routing57app.get('/', (c) => c.text('Hono!!'))58// Use Response object directly59app.get('/hello', () => new Response('This is /hello'))6061app.get('/runtime', (c) => new Response(c.runtime))6263// Named parameter64app.get('/entry/:id', (c) => {65 const id = c.req.param('id')66 return c.text(`Your ID is ${id}`)67})6869// Nested route70const book = new Hono()71book.get('/', (c) => c.text('List Books'))72book.get('/:id', (c) => {73 const id = c.req.param('id')74 return c.text('Get Book: ' + id)75})76book.post('/', (c) => c.text('Create Book'))77app.route('/book', book)7879// Redirect80app.get('/redirect', (c) => c.redirect('/'))81// Authentication required82app.get('/basic-auth/*', (c) => c.text('You are authorized'))83app.get('/bearer-auth/*', (c) => c.text('You are authorized'))84// ETag85app.get('/etag/cached', (c) => c.text('Is this cached?'))8687// Async88app.get('/fetch-url', async (c) => {89 const response = await fetch('https://example.com/')90 return c.text(`https://example.com/ is ${response.status}`)91})9293// Request headers94app.get('/user-agent', (c) => {95 const userAgent = c.req.header('User-Agent')96 return c.text(`Your UserAgent is ${userAgent}`)97})9899// JSON100app.get('/api/posts', prettyJSON(), (c) => {101 const posts = [102 { id: 1, title: 'Good Morning' },103 { id: 2, title: 'Good Aternoon' },104 { id: 3, title: 'Good Evening' },105 { id: 4, title: 'Good Night' },106 ]107 return c.json(posts)108})109// status code110app.post('/api/posts', (c) => c.json({ message: 'Created!' }, 201))111// default route112app.get('/api/*', (c) => c.text('API endpoint is not found', 404))113114app.post('/form', async (c) => {115 const body = await c.req.parseBody()116 return c.json(body)117})118119// Throw Error120app.get('/error', () => {121 throw Error('Error has occurred')122})123124export const handler = app.fetch
modified:src/context.ts+5 added-1 removed
5 unmodified lines
6import type { Schema, SchemaToProp } from './validator/schema'78type HeaderField = [string, string]9type Headers = Record<string, string | string[]>10type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'other'10type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'lagon' | 'other'11export type Data = string | ArrayBuffer | ReadableStream1213export class Context<14 P extends string = string,
215 unmodified lines
230 if (global?.process?.release?.name === 'node') {231 return 'node'232 }233234 if (global?.__lagon__ !== undefined) {235 return 'lagon'236 }237238 return 'other'239 }240}
modified:package.json+3 added-2 removed
11 unmodified lines
12 "scripts": {13 "test": "jest",14 "test:deno": "deno test --allow-read deno_test",15 "test:bun": "bun wiptest --jsx-import-source ../src/middleware/jsx/jsx-dev-runtime bun_test/index.test.tsx",16 "test:lagon": "npx start-server-and-test \"lagon dev lagon_test/index.ts\" http://127.0.0.1:1234 \"yarn jest lagon_test/index.test.ts --testMatch '**/*.test.ts'\"",16 "test:all": "yarn test && yarn test:deno && yarn test:bun",17 "test:all": "yarn test && yarn test:deno && yarn test:bun && yarn test:lagon",18 "lint": "eslint --ext js,ts src .eslintrc.cjs",19 "lint:fix": "eslint --ext js,ts src .eslintrc.cjs --fix",20 "denoify": "rimraf deno_dist && denoify && rimraf 'deno_dist/**/*.test.ts'",21 "copy:package.cjs.json": "cp ./package.cjs.json ./dist/cjs/package.json",
260 unmodified lines
282 },283 "engines": {284 "node": ">=16.0.0"285 }285}286}