-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add leading and trailing slash sanitization for params (#8276)
* add leading and trailing slash sanitization for params * chore: fix lint * refactor: use shared trimSlashes util --------- Co-authored-by: Nate Moore <[email protected]> Co-authored-by: Nate Moore <[email protected]>
- Loading branch information
1 parent
84427f3
commit d3a6f9f
Showing
3 changed files
with
75 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 | ||
--- | ||
|
||
Sanitize route params for leading and trailing slashes |
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
66 changes: 66 additions & 0 deletions
66
packages/astro/test/units/routing/route-sanitization.test.js
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,66 @@ | ||
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 * as cheerio from 'cheerio'; | ||
import testAdapter from '../../test-adapter.js'; | ||
|
||
const root = new URL('../../fixtures/alias/', import.meta.url); | ||
const fileSystem = { | ||
'/src/pages/[...testSlashTrim].astro': ` | ||
--- | ||
export function getStaticPaths() { | ||
return [ | ||
{ | ||
params: { | ||
testSlashTrim: "/a-route-param-with-leading-trailing-slash/", | ||
}, | ||
}, | ||
]; | ||
} | ||
--- | ||
<p>Success!</p> | ||
`, | ||
}; | ||
|
||
describe('Route sanitization', () => { | ||
let container; | ||
let settings; | ||
|
||
before(async () => { | ||
const fs = createFs(fileSystem, root); | ||
settings = await createBasicSettings({ | ||
root: fileURLToPath(root), | ||
trailingSlash: 'never', | ||
output: 'hybrid', | ||
adapter: testAdapter(), | ||
}); | ||
container = await createContainer({ | ||
fs, | ||
settings, | ||
logger: defaultLogger, | ||
}); | ||
}); | ||
|
||
after(async () => { | ||
await container.close(); | ||
}); | ||
|
||
describe('Request', () => { | ||
it('should correctly match a route param with a trailing slash', async () => { | ||
const { req, res, text } = createRequestAndResponse({ | ||
method: 'GET', | ||
url: '/a-route-param-with-leading-trailing-slash', | ||
}); | ||
container.handle(req, res); | ||
const html = await text(); | ||
const $ = cheerio.load(html); | ||
expect($('p').text()).to.equal('Success!'); | ||
}); | ||
}); | ||
}); |