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
Provenance
- Commit
884b287- Tree
a6868bc- Author
Tom Lienard <tom.lienrd@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
4f772dd
6 files changed+349 added−4 removed
modified:.github/workflows/ci.yml+12 added
42 unmodified lines4344454647484950515253545556575842 unmodified lines node-version: 16.x - run: curl -fsSL https://bun.sh/install | bash - run: echo "${HOME}/.bun/bin" >> $GITHUB_PATH - run: npm run test:bun
lagon: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: 18.x - run: yarn install --frozen-lockfile - run: npm run build - run: npm i -g @lagon/cli@latest esbuild@latest - run: npm run test:lagonmodified:deno_dist/context.ts+5 added-1 removed
5 unmodified lines6 import type { Schema, SchemaToProp } from './validator/schema.ts'7 8 type HeaderField = [string, string]9 type Headers = Record<string, string | string[]>10-type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'other'10+type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'lagon' | 'other'11 export type Data = string | ArrayBuffer | ReadableStream12 13 export class Context<14 P extends string = string,215 unmodified lines230 if (global?.process?.release?.name === 'node') {231 return 'node'232 }233 234+ if (global?.__lagon__ !== undefined) {235+ return 'lagon'236+ }237+238 return 'other'239 }240 }added:lagon_test/index.test.ts+200 added
1+describe('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+ })9+10+ 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+ })15+16+ 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+ })21+22+ 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+ })27+28+ 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+ })33+34+ 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+ })40+41+ 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+ })46+47+ 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+ })55+56+ 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+ })63+64+ 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+ })70+71+ 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+ })81+82+ 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+ })88+89+ 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+ })99+100+ 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+ })106+107+ 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+ })112+113+ 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+ })122+123+ 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+ })133+134+ 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+ })141+142+ 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+ })147+148+ 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+ })163+164+ 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: `-----------------------------9051914041544843365972754266172+Content-Disposition: form-data; name="hello"173+174+world!175+-----------------------------9051914041544843365972754266176+Content-Disposition: form-data; name="description"177+178+this 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+ })188+189+ 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+ })194+195+ 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
1+import { Hono } from '../src'2+import { basicAuth } from '../src/middleware/basic-auth'3+import { bearerAuth } from '../src/middleware/bearer-auth'4+import { etag } from '../src/middleware/etag'5+import { poweredBy } from '../src/middleware/powered-by'6+import { prettyJSON } from '../src/middleware/pretty-json'7+8+const app = new Hono()9+10+// Mount Builtin Middleware11+app.use('*', poweredBy())12+// app.use('*', logger())13+app.use(14+ '/basic-auth/*',15+ basicAuth({16+ username: 'hono',17+ password: 'acoolproject',18+ })19+)20+app.use(21+ '/bearer-auth/*',22+ bearerAuth({23+ token: 'secrettoken',24+ })25+)26+app.use('/etag/*', etag())27+28+// Custom Middleware29+// Add Custom Header30+app.use('/hello/*', async (c, next) => {31+ await next()32+ c.header('X-message', 'This is addHeader middleware!')33+})34+35+// Add X-Response-Time header36+app.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+})42+43+// Custom Not Found Message44+app.notFound((c) => {45+ return c.text('Custom 404 Not Found', 404)46+})47+48+// Error handling49+app.onError((err, c) => {50+ console.error(`${err}`)51+ return c.text('Custom Error Message', 500)52+})53+54+app.head('/', (c) => c.text('OK'))55+56+// Routing57+app.get('/', (c) => c.text('Hono!!'))58+// Use Response object directly59+app.get('/hello', () => new Response('This is /hello'))60+61+app.get('/runtime', (c) => new Response(c.runtime))62+63+// Named parameter64+app.get('/entry/:id', (c) => {65+ const id = c.req.param('id')66+ return c.text(`Your ID is ${id}`)67+})68+69+// Nested route70+const book = new Hono()71+book.get('/', (c) => c.text('List Books'))72+book.get('/:id', (c) => {73+ const id = c.req.param('id')74+ return c.text('Get Book: ' + id)75+})76+book.post('/', (c) => c.text('Create Book'))77+app.route('/book', book)78+79+// Redirect80+app.get('/redirect', (c) => c.redirect('/'))81+// Authentication required82+app.get('/basic-auth/*', (c) => c.text('You are authorized'))83+app.get('/bearer-auth/*', (c) => c.text('You are authorized'))84+// ETag85+app.get('/etag/cached', (c) => c.text('Is this cached?'))86+87+// Async88+app.get('/fetch-url', async (c) => {89+ const response = await fetch('https://example.com/')90+ return c.text(`https://example.com/ is ${response.status}`)91+})92+93+// Request headers94+app.get('/user-agent', (c) => {95+ const userAgent = c.req.header('User-Agent')96+ return c.text(`Your UserAgent is ${userAgent}`)97+})98+99+// JSON100+app.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 code110+app.post('/api/posts', (c) => c.json({ message: 'Created!' }, 201))111+// default route112+app.get('/api/*', (c) => c.text('API endpoint is not found', 404))113+114+app.post('/form', async (c) => {115+ const body = await c.req.parseBody()116+ return c.json(body)117+})118+119+// Throw Error120+app.get('/error', () => {121+ throw Error('Error has occurred')122+})123+124+export const handler = app.fetchmodified:src/context.ts+5 added-1 removed
5 unmodified lines6 import type { Schema, SchemaToProp } from './validator/schema'7 8 type HeaderField = [string, string]9 type Headers = Record<string, string | string[]>10-type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'other'10+type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'lagon' | 'other'11 export type Data = string | ArrayBuffer | ReadableStream12 13 export class Context<14 P extends string = string,215 unmodified lines230 if (global?.process?.release?.name === 'node') {231 return 'node'232 }233 234+ if (global?.__lagon__ !== undefined) {235+ return 'lagon'236+ }237+238 return 'other'239 }240 }modified:package.json+3 added-2 removed
11 unmodified lines12 "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 lines282 },283 "engines": {284 "node": ">=16.0.0"285 }285-}286+}