Skip to content

Commit 295b2b6

Browse files
committed
fix: add more test cases and include additional check for base path fetch
1 parent 8d450d9 commit 295b2b6

File tree

9 files changed

+101
-15
lines changed

9 files changed

+101
-15
lines changed

packages/kit/src/runtime/server/fetch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ export function create_fetch({ event, options, manifest, state, get_cookie_heade
5656
}
5757

5858
const decoded = decodeURIComponent(url.pathname);
59+
5960
if (
6061
url.origin !== event.url.origin ||
61-
(paths.base && !decoded.startsWith(`${paths.base}/`))
62+
(paths.base && decoded !== paths.base && !decoded.startsWith(`${paths.base}/`))
6263
) {
6364
// Allow cookie passthrough for "credentials: same-origin" and "credentials: include"
6465
// if SvelteKit is serving my.domain.com:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function GET() {
2+
return new Response('root');
3+
}

packages/kit/test/apps/options/source/pages/routing/link-outside-base/+page.server.js renamed to packages/kit/test/apps/options/source/pages/fetch/link-outside-base/+page.server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export async function load({ fetch }) {
22
const response = await fetch('/not-base-path/');
33
return {
4-
fetchUrl: response.url
4+
fetchUrl: response.url,
5+
fetchResponse: await response.text()
56
};
67
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
/** @type {import('./$types').PageProps} */
3+
const { data } = $props();
4+
</script>
5+
6+
<p data-testid="fetch-url">{data.fetchUrl}</p>
7+
<p data-testid="fetch-response">{data.fetchResponse}</p>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export async function load({ fetch }) {
2+
// fetch to root with trailing slash
3+
const response1 = await fetch('/');
4+
// fetch to root without trailing slash
5+
const response2 = await fetch('');
6+
// fetch to root with custom base path with trailing slash
7+
const response3 = await fetch('/path-base/');
8+
// fetch to root with custom base path without trailing slash
9+
const response4 = await fetch('/path-base');
10+
11+
return {
12+
fetchUrl1: response1.url,
13+
fetchUrl2: response2.url,
14+
fetchUrl3: response3.url,
15+
fetchUrl4: response4.url,
16+
fetchResponse1: await response1.text(),
17+
fetchResponse2: await response2.text(),
18+
fetchResponse3: await response3.text(),
19+
fetchRedirect4: response4.headers.get('location')
20+
};
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<script>
2+
/** @type {import('./$types').PageProps} */
3+
const { data } = $props();
4+
</script>
5+
6+
<h2>Fetch URLs</h2>
7+
8+
<dl>
9+
<dt>fetch1-url</dt>
10+
<dd data-testid="fetch1-url">{data.fetchUrl1}</dd>
11+
<dt>fetch2-url</dt>
12+
<dd data-testid="fetch2-url">{data.fetchUrl2}</dd>
13+
<dt>fetch3-url</dt>
14+
<dd data-testid="fetch3-url">{data.fetchUrl3}</dd>
15+
<dt>fetch4-url</dt>
16+
<dd data-testid="fetch4-url">{data.fetchUrl4}</dd>
17+
</dl>
18+
19+
<h2>Fetch Responses</h2>
20+
<dl>
21+
<dt>fetch1-response</dt>
22+
<dd data-testid="fetch1-response">{data.fetchResponse1}</dd>
23+
<dt>fetch2-response</dt>
24+
<dd data-testid="fetch2-response">{data.fetchResponse2}</dd>
25+
<dt>fetch3-response</dt>
26+
<dd data-testid="fetch3-response">{data.fetchResponse3}</dd>
27+
<dt>fetch4-redirect</dt>
28+
<dd data-testid="fetch4-redirect">{data.fetchRedirect4}</dd>
29+
</dl>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function GET() {
2+
return new Response('relative');
3+
}

packages/kit/test/apps/options/source/pages/routing/link-outside-base/+page.svelte

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/kit/test/apps/options/test/test.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,6 @@ test.describe('Routing', () => {
236236
await page.click('[href="/path-base/routing/link-outside-app-target/target/"]');
237237
await expect(page.locator('h2')).toHaveText('target: 0');
238238
});
239-
240-
test('fetch outside base path succeeds', async ({ page, baseURL }) => {
241-
await page.goto('/path-base/routing/link-outside-base/');
242-
expect(await page.locator('p[data-testid="fetch-url"]').textContent()).toContain(
243-
`${baseURL}/not-base-path/`
244-
);
245-
});
246239
});
247240

248241
test.describe('Async', () => {
@@ -265,4 +258,38 @@ test.describe('Async', () => {
265258
await expect(page.locator('h1', { hasText: 'Page B' })).toBeVisible();
266259
expect(logs).toEqual(['mounted', 'navigated']);
267260
});
261+
262+
test.describe('Fetch', () => {
263+
test('fetch outside base path succeeds', async ({ page, baseURL }) => {
264+
await page.goto('/path-base/fetch/link-outside-base/');
265+
expect(await page.locator('[data-testid="fetch-url"]').textContent()).toContain(
266+
`${baseURL}/not-base-path/`
267+
);
268+
expect(await page.locator('[data-testid="fetch-response"]').textContent()).toContain(
269+
'did you mean to visit'
270+
);
271+
});
272+
273+
test('fetch to root succeeds', async ({ page, baseURL }) => {
274+
await page.goto('/path-base/fetch/link-root/');
275+
// fetch to root with trailing slash
276+
expect(await page.locator('[data-testid="fetch1-url"]').textContent()).toContain(
277+
`${baseURL}/`
278+
);
279+
expect(await page.locator('[data-testid="fetch1-response"]').textContent()).toContain('root');
280+
// fetch to root without trailing slash should be relative
281+
expect(await page.locator('[data-testid="fetch2-url"]').textContent()).toBeFalsy();
282+
expect(await page.locator('[data-testid="fetch2-response"]').textContent()).toBe('relative');
283+
284+
// fetch to root with custom base path with trailing slash
285+
expect(await page.locator('[data-testid="fetch3-url"]').textContent()).toBeFalsy();
286+
expect(await page.locator('[data-testid="fetch3-response"]').textContent()).toBe('root');
287+
288+
// fetch to root with custom base path without trailing slash
289+
expect(await page.locator('[data-testid="fetch4-url"]').textContent()).toBeFalsy();
290+
expect(await page.locator('[data-testid="fetch4-redirect"]').textContent()).toBe(
291+
'/path-base/'
292+
);
293+
});
294+
});
268295
});

0 commit comments

Comments
 (0)