From f3986df57e31f40407fa80b88aa4116c7dbcf2c4 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Sun, 24 Sep 2023 21:17:35 +0000 Subject: [PATCH] add tests --- .../redirects/src/pages/more/[dynamic].astro | 8 +++++ .../src/pages/more/[dynamic]/[route].astro | 8 +++++ .../src/pages/more/new/[...spread].astro | 8 +++++ packages/astro/test/redirects.test.js | 30 ++++++++++++++++++- 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 packages/astro/test/fixtures/redirects/src/pages/more/[dynamic].astro create mode 100644 packages/astro/test/fixtures/redirects/src/pages/more/[dynamic]/[route].astro create mode 100644 packages/astro/test/fixtures/redirects/src/pages/more/new/[...spread].astro diff --git a/packages/astro/test/fixtures/redirects/src/pages/more/[dynamic].astro b/packages/astro/test/fixtures/redirects/src/pages/more/[dynamic].astro new file mode 100644 index 000000000000..f980c25baa68 --- /dev/null +++ b/packages/astro/test/fixtures/redirects/src/pages/more/[dynamic].astro @@ -0,0 +1,8 @@ +--- +export function getStaticPaths() { + return [{ params: { dynamic:'hello' } }] +} +--- +{JSON.stringify(Astro.params)} + +home \ No newline at end of file diff --git a/packages/astro/test/fixtures/redirects/src/pages/more/[dynamic]/[route].astro b/packages/astro/test/fixtures/redirects/src/pages/more/[dynamic]/[route].astro new file mode 100644 index 000000000000..94ccb545ff9b --- /dev/null +++ b/packages/astro/test/fixtures/redirects/src/pages/more/[dynamic]/[route].astro @@ -0,0 +1,8 @@ +--- +export function getStaticPaths() { + return [{ params: { dynamic:'hello', route:'world' } }] +} +--- +{JSON.stringify(Astro.params)} + +home diff --git a/packages/astro/test/fixtures/redirects/src/pages/more/new/[...spread].astro b/packages/astro/test/fixtures/redirects/src/pages/more/new/[...spread].astro new file mode 100644 index 000000000000..f34bb28f64a8 --- /dev/null +++ b/packages/astro/test/fixtures/redirects/src/pages/more/new/[...spread].astro @@ -0,0 +1,8 @@ +--- +export function getStaticPaths() { + return [{ params: {spread:'welcome/world'} }] +} +--- +{JSON.stringify(Astro.params)} + +home \ No newline at end of file diff --git a/packages/astro/test/redirects.test.js b/packages/astro/test/redirects.test.js index e8e598b33014..a5a06530935e 100644 --- a/packages/astro/test/redirects.test.js +++ b/packages/astro/test/redirects.test.js @@ -78,6 +78,9 @@ describe('Astro.redirect', () => { status: 302, destination: '/test', }, + '/more/old/[dynamic]': '/more/[dynamic]', + '/more/old/[dynamic]/[route]': '/more/[dynamic]/[route]', + '/more/old/[...spread]': '/more/new/[...spread]', }, }); await fixture.build(); @@ -149,6 +152,12 @@ describe('Astro.redirect', () => { expect(html).to.include('http-equiv="refresh'); expect(html).to.include('url=/test'); }); + + it('falls back to spread rule when dynamic rules should not match', async () => { + const html = await fixture.readFile('/more/old/welcome/world/index.html'); + expect(html).to.include('http-equiv="refresh'); + expect(html).to.include('url=/more/new/welcome/world'); + }); }); describe('dev', () => { @@ -161,6 +170,9 @@ describe('Astro.redirect', () => { output: 'static', redirects: { '/one': '/', + '/more/old/[dynamic]': '/more/[dynamic]', + '/more/old/[dynamic]/[route]': '/more/[dynamic]/[route]', + '/more/old/[...spread]': '/more/new/[...spread]', }, }); devServer = await fixture.startDevServer(); @@ -170,11 +182,27 @@ describe('Astro.redirect', () => { await devServer.stop(); }); - it('Returns 301', async () => { + it('performs simple redirects', async () => { let res = await fixture.fetch('/one', { redirect: 'manual', }); expect(res.status).to.equal(301); + expect(res.headers.get('Location')).to.equal('/'); + }); + + it('performs dynamic redirects', async () => { + const response = await fixture.fetch('/more/old/hello', { redirect: 'manual' }); + expect(response.headers.get('Location')).to.equal('/more/hello'); + }); + + it('performs dynamic redirects with multiple params', async () => { + const response = await fixture.fetch('/more/old/hello/world', { redirect: 'manual' }); + expect(response.headers.get('Location')).to.equal('/more/hello/world'); + }); + + it.skip('falls back to spread rule when dynamic rules should not match', async () => { + const response = await fixture.fetch('/more/old/welcome/world', { redirect: 'manual' }); + expect(response.headers.get('Location')).to.equal('/more/new/welcome/world'); }); }); });