import { describe, expect, test } from 'vitest' // set this up for testing, pausing async function fetchThing(path = '/', type: 'text' | 'json' | 'headers') { return await fetch(`${process.env.ONE_SERVER_URL}${path}`).then((res) => { if (type === 'headers') { return res.headers } return res[type]() }) } describe('Middleware', () => { test('middleware can dynamically import module with nested server-only import', async () => { // this test verifies that middleware can dynamically import a module // that imports another module that imports 'server-only' (nested chain) // this is the pattern that fails when server-only is externalized const res = await fetchThing('/?test-server-only-dynamic', 'json') expect(res).toEqual({ auth: { authenticated: true, util: 'server-only-util-value' } }) }) test('root middleware doesnt intercept', async () => { const res = await fetchThing('/', 'text') expect(res).includes(` { const res = await fetchThing('/middleware?test-middleware', 'json') expect(res).toMatchInlineSnapshot(` { "middleware": "works", } `) }) test('parent middleware runs and changes response before sub middleware', async () => { const res = await fetchThing('/middleware?test-middleware&intercept', 'json') expect(res).toMatchInlineSnapshot(` { "middleware": "works", } `) }) test('middleware runs even on not found routes', async () => { const res = await fetchThing( '/testing/that/this-route-doesnt-exist.md?test-middleware', 'json' ) expect(res).toMatchInlineSnapshot(` { "middleware": "works", } `) }) // test('sub middleware intercepts', async () => { // const res = await fetchThing('/middleware?intercept', 'json') // expect(JSON.stringify(devRes)).toBe(JSON.stringify(prodRes)) // expect(devRes).toMatchInlineSnapshot(` // { // "didIntercept": true, // } // `) // }) test('wrapping response with new Response preserves body and adds headers', async () => { const res = await fetch(`${process.env.ONE_SERVER_URL}/middleware?test-wrap-response`) expect(res.headers.get('X-Custom-Wrap')).toBe('wrapped') expect(res.headers.get('Cache-Control')).toBe('no-cache') const text = await res.text() expect(text).toContain('middleware-route') }) test.skip('sub middleware runs and changes headers', async () => { const res = (await fetchThing('/middleware', 'headers')) as Headers // TODO intercept not working // expect(devRes.get('test-header')).toBe(prodRes.get('test-header')) expect(res.get('test-header')).toBe('test-value') }) })