From e0b4079637675d838e179b547099c6c1063fed8d Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:09:36 +0000 Subject: [PATCH] add test --- .../test/units/routing/trailing-slash.test.js | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 packages/astro/test/units/routing/trailing-slash.test.js diff --git a/packages/astro/test/units/routing/trailing-slash.test.js b/packages/astro/test/units/routing/trailing-slash.test.js new file mode 100644 index 000000000000..c707c7215a5d --- /dev/null +++ b/packages/astro/test/units/routing/trailing-slash.test.js @@ -0,0 +1,59 @@ +import { + createBasicSettings, + createFs, + createRequestAndResponse, + defaultLogger, +} from '../test-utils.js'; +import { fileURLToPath } from 'node:url'; +import { expect } from 'chai'; +import { createContainer } from '../../../dist/core/dev/container.js'; +import testAdapter from '../../test-adapter.js'; + +const root = new URL('../../fixtures/api-routes/', import.meta.url); +const fileSystem = { + '/src/pages/api.ts': `export const GET = () => Response.json({ success: true })`, +}; + +describe('trailingSlash', () => { + let container; + let settings; + + before(async () => { + const fs = createFs(fileSystem, root); + settings = await createBasicSettings({ + root: fileURLToPath(root), + trailingSlash: 'always', + output: 'server', + adapter: testAdapter(), + }); + container = await createContainer({ + fs, + settings, + logger: defaultLogger, + }); + }); + + after(async () => { + await container.close(); + }); + + it('should match the API route when request has a trailing slash', async () => { + const { req, res, text } = createRequestAndResponse({ + method: 'GET', + url: '/api/', + }); + container.handle(req, res); + const json = await text(); + expect(json).to.equal('{"success":true}'); + }); + + it('should NOT match the API route when request lacks a trailing slash', async () => { + const { req, res, text } = createRequestAndResponse({ + method: 'GET', + url: '/api', + }); + container.handle(req, res); + expect(await text()).to.equal(''); + expect(res.statusCode).to.equal(404); + }); +});