-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(routing): trailingSlash on endpoints (#9597)
* fix(routing): applies trailingSlash on endpoints * add changeset * add test * Update .changeset/funny-lobsters-promise.md Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com> --------- Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
- Loading branch information
Showing
3 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"astro": patch | ||
--- | ||
|
||
Fixes an issue where configuring trailingSlash had no effect on API routes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |