|
| 1 | +import { join as posixJoin } from 'node:path/posix' |
| 2 | +import 'urlpattern-polyfill' |
| 3 | +import { v4 } from 'uuid' |
| 4 | +import { beforeEach, expect, test, vi } from 'vitest' |
| 5 | +import { SERVER_HANDLER_NAME } from '../../src/build/plugin-context.js' |
| 6 | +import { type FixtureTestContext } from '../utils/contexts.js' |
| 7 | +import { createFixture, runPlugin } from '../utils/fixture.js' |
| 8 | +import { generateRandomObjectID, startMockBlobStore } from '../utils/helpers.js' |
| 9 | + |
| 10 | +const ssrRoutes = [ |
| 11 | + ['/static', 'pages router, static rendering, static routing'], |
| 12 | + ['/prerendered', 'pages router, prerendering, static routing'], |
| 13 | + ['/posts/prerendered/1', 'pages router, prerendering, dynamic routing'], |
| 14 | + ['/dynamic', 'pages router, dynamic rendering, static routing'], |
| 15 | + ['/posts/dynamic/1', 'pages router, dynamic rendering, dynamic routing'], |
| 16 | + ['/api/okay', 'pages router, api route, static routing'], |
| 17 | + ['/api/posts/1', 'pages router, api route, dynamic routing'], |
| 18 | + ['/static-fetch-1', 'app router, prerendering, static routing'], |
| 19 | + ['/static-fetch/1', 'app router, prerendering, dynamic routing'], |
| 20 | + ['/static-fetch-dynamic-1', 'app router, dynamic rendering, static routing'], |
| 21 | + ['/static-fetch-dynamic/1', 'app router, dynamic rendering, dynamic routing'], |
| 22 | + ['/api/revalidate-handler', 'app router, route handler, static routing'], |
| 23 | + ['/api/static/1', 'app router, route handler, dynamic routing'], |
| 24 | +] |
| 25 | + |
| 26 | +const notFoundRoutes = [ |
| 27 | + ['/non-existing', 'default'], |
| 28 | + ['/posts/prerendered/3', 'pages router, prerendering, dynamic routing'], |
| 29 | + ['/api/non-existing', 'pages router, api route, static routing'], |
| 30 | +] |
| 31 | + |
| 32 | +const baseURL = 'http://localhost' |
| 33 | + |
| 34 | +beforeEach<FixtureTestContext>(async (ctx) => { |
| 35 | + // set for each test a new deployID and siteID |
| 36 | + ctx.deployID = generateRandomObjectID() |
| 37 | + ctx.siteID = v4() |
| 38 | + vi.stubEnv('DEPLOY_ID', ctx.deployID) |
| 39 | + |
| 40 | + await startMockBlobStore(ctx) |
| 41 | +}) |
| 42 | + |
| 43 | +test<FixtureTestContext>('that the SSR handler routing works correctly', async (ctx) => { |
| 44 | + await createFixture('server-components', ctx) |
| 45 | + await runPlugin(ctx) |
| 46 | + |
| 47 | + const handler = await import( |
| 48 | + posixJoin( |
| 49 | + ctx.cwd, |
| 50 | + '.netlify/functions-internal', |
| 51 | + SERVER_HANDLER_NAME, |
| 52 | + `${SERVER_HANDLER_NAME}.mjs`, |
| 53 | + ) |
| 54 | + ) |
| 55 | + |
| 56 | + const matcher = (path: string) => |
| 57 | + handler.config.path.some((pattern) => |
| 58 | + new URLPattern({ pathname: pattern, baseURL }).test(posixJoin(baseURL, path)), |
| 59 | + ) |
| 60 | + |
| 61 | + // check ssr routes are satisfied by the url patterns |
| 62 | + for (const [path, description] of ssrRoutes) { |
| 63 | + expect(path, `expected 200 response for ${description}`).toSatisfy(matcher) |
| 64 | + } |
| 65 | + |
| 66 | + // check not found routes are not satisfied by the url patterns |
| 67 | + for (const [path, description] of notFoundRoutes) { |
| 68 | + expect(path, `expected 404 response for ${description}`).not.toSatisfy(matcher) |
| 69 | + } |
| 70 | +}) |
0 commit comments