[ci] format

This commit is contained in:
matthewp 2022-11-11 16:30:25 +00:00 committed by fredkbot
parent 9eee0f0166
commit 8aea2dea61
5 changed files with 129 additions and 98 deletions

View file

@ -74,10 +74,7 @@ export const AstroConfigSchema = z.object({
.url()
.optional()
.transform((val) => (val ? appendForwardSlash(val) : val)),
base: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.base),
base: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.base),
trailingSlash: z
.union([z.literal('always'), z.literal('never'), z.literal('ignore')])
.optional()
@ -326,11 +323,11 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
config.build.client = new URL('./dist/client/', config.outDir);
}
const trimmedBase = trimSlashes(config.base);
if(trimmedBase.length && config.trailingSlash === 'never') {
config.base = prependForwardSlash(trimmedBase);
} else {
config.base = prependForwardSlash(appendForwardSlash(trimmedBase));
}
if (trimmedBase.length && config.trailingSlash === 'never') {
config.base = prependForwardSlash(trimmedBase);
} else {
config.base = prependForwardSlash(appendForwardSlash(trimmedBase));
}
return config;
});

View file

@ -61,7 +61,11 @@ function getParts(part: string, file: string) {
return result;
}
function getPattern(segments: RoutePart[][], base: string, addTrailingSlash: AstroConfig['trailingSlash']) {
function getPattern(
segments: RoutePart[][],
base: string,
addTrailingSlash: AstroConfig['trailingSlash']
) {
const pathname = segments
.map((segment) => {
if (segment.length === 1 && segment[0].spread) {
@ -94,7 +98,7 @@ function getPattern(segments: RoutePart[][], base: string, addTrailingSlash: Ast
const trailing =
addTrailingSlash && segments.length ? getTrailingSlashPattern(addTrailingSlash) : '$';
let initial = '\\/';
if(addTrailingSlash === 'never' && base !== '/') {
if (addTrailingSlash === 'never' && base !== '/') {
initial = '';
}
return new RegExp(`^${pathname || initial}${trailing}`);

View file

@ -27,7 +27,7 @@ export async function handleRequest(
const url = new URL(origin + req.url);
let pathname: string;
if(config.trailingSlash === 'never' && !req.url) {
if (config.trailingSlash === 'never' && !req.url) {
pathname = '';
} else {
pathname = decodeURI(url.pathname);

View file

@ -9,97 +9,121 @@ describe('base configuration', () => {
describe('with trailingSlash: "never"', () => {
describe('index route', () => {
it('Requests that include a trailing slash 404', async () => {
const fs = createFs({
'/src/pages/index.astro': `<h1>testing</h1>`,
}, root);
await runInContainer({
fs,
root,
userConfig: {
base: '/docs',
trailingSlash: 'never',
const fs = createFs(
{
'/src/pages/index.astro': `<h1>testing</h1>`,
},
}, async (container) => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/docs/',
});
container.handle(req, res);
await done;
expect(res.statusCode).to.equal(404);
});
root
);
await runInContainer(
{
fs,
root,
userConfig: {
base: '/docs',
trailingSlash: 'never',
},
},
async (container) => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/docs/',
});
container.handle(req, res);
await done;
expect(res.statusCode).to.equal(404);
}
);
});
it('Requests that exclude a trailing slash 200', async () => {
const fs = createFs({
'/src/pages/index.astro': `<h1>testing</h1>`,
}, root);
await runInContainer({
fs,
root,
userConfig: {
base: '/docs',
trailingSlash: 'never',
const fs = createFs(
{
'/src/pages/index.astro': `<h1>testing</h1>`,
},
}, async (container) => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/docs',
});
container.handle(req, res);
await done;
expect(res.statusCode).to.equal(200);
});
root
);
await runInContainer(
{
fs,
root,
userConfig: {
base: '/docs',
trailingSlash: 'never',
},
},
async (container) => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/docs',
});
container.handle(req, res);
await done;
expect(res.statusCode).to.equal(200);
}
);
});
});
describe('sub route', () => {
it('Requests that include a trailing slash 404', async () => {
const fs = createFs({
'/src/pages/sub/index.astro': `<h1>testing</h1>`,
}, root);
await runInContainer({
fs,
root,
userConfig: {
base: '/docs',
trailingSlash: 'never',
const fs = createFs(
{
'/src/pages/sub/index.astro': `<h1>testing</h1>`,
},
}, async (container) => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/docs/sub/',
});
container.handle(req, res);
await done;
expect(res.statusCode).to.equal(404);
});
root
);
await runInContainer(
{
fs,
root,
userConfig: {
base: '/docs',
trailingSlash: 'never',
},
},
async (container) => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/docs/sub/',
});
container.handle(req, res);
await done;
expect(res.statusCode).to.equal(404);
}
);
});
it('Requests that exclude a trailing slash 200', async () => {
const fs = createFs({
'/src/pages/sub/index.astro': `<h1>testing</h1>`,
}, root);
await runInContainer({
fs,
root,
userConfig: {
base: '/docs',
trailingSlash: 'never',
const fs = createFs(
{
'/src/pages/sub/index.astro': `<h1>testing</h1>`,
},
}, async (container) => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/docs/sub',
});
container.handle(req, res);
await done;
expect(res.statusCode).to.equal(200);
});
root
);
await runInContainer(
{
fs,
root,
userConfig: {
base: '/docs',
trailingSlash: 'never',
},
},
async (container) => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/docs/sub',
});
container.handle(req, res);
await done;
expect(res.statusCode).to.equal(200);
}
);
});
});
});

View file

@ -9,17 +9,23 @@ const root = new URL('../../fixtures/alias/', import.meta.url);
describe('routing - createRouteManifest', () => {
it('using trailingSlash: "never" does not match the index route when it contains a trailing slash', async () => {
const fs = createFs({
'/src/pages/index.astro': `<h1>test</h1>`,
}, root);
const settings = await createDefaultDevSettings({
base: '/search',
trailingSlash: 'never'
}, root);
const fs = createFs(
{
'/src/pages/index.astro': `<h1>test</h1>`,
},
root
);
const settings = await createDefaultDevSettings(
{
base: '/search',
trailingSlash: 'never',
},
root
);
const manifest = createRouteManifest({
cwd: fileURLToPath(root),
settings,
fsMod: fs
fsMod: fs,
});
const [{ pattern }] = manifest.routes;
expect(pattern.test('')).to.equal(true);