index.test.ts
This file
Path
runtime-tests/fastly/index.test.ts
109 lines · 3.2 KiB · TypeScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–109why, one step back1import { createHash } from 'crypto'2import { getRuntimeKey } from '../../src/helper/adapter'3import { Hono } from '../../src/index'4import { basicAuth } from '../../src/middleware/basic-auth'5import { jwt } from '../../src/middleware/jwt'67declare global {8 var __fastlyComputeNodeDefaultCrypto: boolean | undefined9}1011beforeAll(() => {12 vi.stubGlobal('fastly', true)13 vi.stubGlobal('navigator', undefined)14})1516afterAll(() => {17 vi.unstubAllGlobals()18})1920const app = new Hono()2122describe('Hello World', () => {23 app.get('/', (c) => c.text('Hello! Compute!'))24 app.get('/runtime-name', (c) => {25 return c.text(getRuntimeKey())26 })2728 it('Should return 200', async () => {29 const res = await app.request('http://localhost/')30 expect(res.status).toBe(200)31 expect(await res.text()).toBe('Hello! Compute!')32 })3334 it('Should return the correct runtime name', async () => {35 const res = await app.request('http://localhost/runtime-name')36 expect(res.status).toBe(200)37 expect(await res.text()).toBe('fastly')38 })39})4041describe('Basic Auth Middleware without `hashFunction`', () => {42 const app = new Hono()4344 const username = 'hono-user-a'45 const password = 'hono-password-a'46 app.use(47 '/auth/*',48 basicAuth({49 username,50 password,51 })52 )5354 app.get('/auth/*', () => new Response('auth'))5556 it('Should not authorize, return 401 Response', async () => {57 const req = new Request('http://localhost/auth/a')58 const res = await app.request(req)59 expect(res.status).toBe(401)60 expect(await res.text()).toBe('Unauthorized')61 })62})6364describe('Basic Auth Middleware with `hashFunction`', () => {65 const app = new Hono()6667 const username = 'hono-user-a'68 const password = 'hono-password-a'69 app.use(70 '/auth/*',71 basicAuth({72 username,73 password,74 hashFunction: (m: string) => createHash('sha256').update(m).digest('hex'),75 })76 )7778 app.get('/auth/*', () => new Response('auth'))7980 it('Should not authorize, return 401 Response', async () => {81 const req = new Request('http://localhost/auth/a')82 const res = await app.request(req)83 expect(res.status).toBe(401)84 expect(await res.text()).toBe('Unauthorized')85 })8687 it('Should authorize, return 200 Response', async () => {88 const credential = 'aG9uby11c2VyLWE6aG9uby1wYXNzd29yZC1h'89 const req = new Request('http://localhost/auth/a')90 req.headers.set('Authorization', `Basic ${credential}`)91 const res = await app.request(req)92 expect(res.status).toBe(200)93 expect(await res.text()).toBe('auth')94 })95})9697describe('JWT Auth Middleware does not work', () => {98 const app = new Hono()99100 // Since nodejs 20 or later, global WebCrypto object becomes stable (experimental on nodejs 18)101 // but WebCrypto does not have compatibility with Fastly Compute runtime (lacking some objects/methods in Fastly)102 // so following test should run only be polyfill-ed via vite-plugin-fastly-js-compute plugin.103 // To confirm polyfill-ed or not, check __fastlyComputeNodeDefaultCrypto field is true.104 it.runIf(!globalThis.__fastlyComputeNodeDefaultCrypto)('Should throw error', () => {105 expect(() => {106 app.use('/jwt/*', jwt({ alg: 'HS256', secret: 'secret' }))107 }).toThrow(/`crypto.subtle.importKey` is undefined/)108 })109})