|
| 1 | +import { createNext, FileRef } from 'e2e-utils' |
| 2 | +import { NextInstance } from 'test/lib/next-modes/base' |
| 3 | +import { check, fetchViaHTTP } from 'next-test-utils' |
| 4 | +import { join } from 'path' |
| 5 | +import webdriver from 'next-webdriver' |
| 6 | + |
| 7 | +describe('skip-trailing-slash-redirect', () => { |
| 8 | + let next: NextInstance |
| 9 | + |
| 10 | + beforeAll(async () => { |
| 11 | + next = await createNext({ |
| 12 | + files: new FileRef(join(__dirname, 'app')), |
| 13 | + dependencies: {}, |
| 14 | + }) |
| 15 | + }) |
| 16 | + afterAll(() => next.destroy()) |
| 17 | + |
| 18 | + it('should allow rewriting invalid buildId correctly', async () => { |
| 19 | + const res = await fetchViaHTTP( |
| 20 | + next.url, |
| 21 | + '/_next/data/missing-id/hello.json', |
| 22 | + undefined, |
| 23 | + { |
| 24 | + headers: { |
| 25 | + 'x-nextjs-data': '1', |
| 26 | + }, |
| 27 | + } |
| 28 | + ) |
| 29 | + expect(res.status).toBe(200) |
| 30 | + expect(await res.text()).toContain('Example Domain') |
| 31 | + |
| 32 | + if (!(global as any).isNextDeploy) { |
| 33 | + await check(() => next.cliOutput, /missing-id rewrite/) |
| 34 | + expect(next.cliOutput).toContain('/_next/data/missing-id/hello.json') |
| 35 | + } |
| 36 | + }) |
| 37 | + |
| 38 | + it('should allow response body from middleware with flag', async () => { |
| 39 | + const res = await fetchViaHTTP(next.url, '/middleware-response-body') |
| 40 | + expect(res.status).toBe(200) |
| 41 | + expect(res.headers.get('x-from-middleware')).toBe('true') |
| 42 | + expect(await res.text()).toBe('hello from middleware') |
| 43 | + }) |
| 44 | + |
| 45 | + it('should merge cookies from middleware and API routes correctly', async () => { |
| 46 | + const res = await fetchViaHTTP(next.url, '/api/test-cookie', undefined, { |
| 47 | + redirect: 'manual', |
| 48 | + }) |
| 49 | + expect(res.status).toBe(200) |
| 50 | + expect(res.headers.get('set-cookie')).toEqual( |
| 51 | + 'from-middleware=1; Path=/, hello=From API' |
| 52 | + ) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should merge cookies from middleware and edge API routes correctly', async () => { |
| 56 | + const res = await fetchViaHTTP( |
| 57 | + next.url, |
| 58 | + '/api/test-cookie-edge', |
| 59 | + undefined, |
| 60 | + { |
| 61 | + redirect: 'manual', |
| 62 | + } |
| 63 | + ) |
| 64 | + expect(res.status).toBe(200) |
| 65 | + expect(res.headers.get('set-cookie')).toEqual( |
| 66 | + 'from-middleware=1; Path=/, hello=From%20API; Path=/' |
| 67 | + ) |
| 68 | + }) |
| 69 | + |
| 70 | + if ((global as any).isNextStart) { |
| 71 | + it('should not have trailing slash redirects in manifest', async () => { |
| 72 | + const routesManifest = JSON.parse( |
| 73 | + await next.readFile('.next/routes-manifest.json') |
| 74 | + ) |
| 75 | + |
| 76 | + expect( |
| 77 | + routesManifest.redirects.some((redirect) => { |
| 78 | + return ( |
| 79 | + redirect.statusCode === 308 && |
| 80 | + (redirect.destination === '/:path+' || |
| 81 | + redirect.destination === '/:path+/') |
| 82 | + ) |
| 83 | + }) |
| 84 | + ).toBe(false) |
| 85 | + }) |
| 86 | + } |
| 87 | + |
| 88 | + it('should correct skip URL normalizing in middleware', async () => { |
| 89 | + let res = await fetchViaHTTP( |
| 90 | + next.url, |
| 91 | + '/middleware-rewrite-with-slash', |
| 92 | + undefined, |
| 93 | + { redirect: 'manual', headers: { 'x-nextjs-data': '1' } } |
| 94 | + ) |
| 95 | + expect(res.headers.get('x-nextjs-rewrite').endsWith('/another/')).toBe(true) |
| 96 | + |
| 97 | + res = await fetchViaHTTP( |
| 98 | + next.url, |
| 99 | + '/middleware-rewrite-without-slash', |
| 100 | + undefined, |
| 101 | + { redirect: 'manual', headers: { 'x-nextjs-data': '1' } } |
| 102 | + ) |
| 103 | + expect(res.headers.get('x-nextjs-rewrite').endsWith('/another')).toBe(true) |
| 104 | + |
| 105 | + res = await fetchViaHTTP( |
| 106 | + next.url, |
| 107 | + '/middleware-redirect-external-with', |
| 108 | + undefined, |
| 109 | + { redirect: 'manual' } |
| 110 | + ) |
| 111 | + expect(res.status).toBe(307) |
| 112 | + expect(res.headers.get('Location')).toBe( |
| 113 | + 'https://example.vercel.sh/somewhere/' |
| 114 | + ) |
| 115 | + |
| 116 | + res = await fetchViaHTTP( |
| 117 | + next.url, |
| 118 | + '/middleware-redirect-external-without', |
| 119 | + undefined, |
| 120 | + { redirect: 'manual' } |
| 121 | + ) |
| 122 | + expect(res.status).toBe(307) |
| 123 | + expect(res.headers.get('Location')).toBe( |
| 124 | + 'https://example.vercel.sh/somewhere' |
| 125 | + ) |
| 126 | + }) |
| 127 | + |
| 128 | + it('should apply config redirect correctly', async () => { |
| 129 | + const res = await fetchViaHTTP(next.url, '/redirect-me', undefined, { |
| 130 | + redirect: 'manual', |
| 131 | + }) |
| 132 | + expect(res.status).toBe(307) |
| 133 | + expect(new URL(res.headers.get('location'), 'http://n').pathname).toBe( |
| 134 | + '/another' |
| 135 | + ) |
| 136 | + }) |
| 137 | + |
| 138 | + it('should apply config rewrites correctly', async () => { |
| 139 | + const res = await fetchViaHTTP(next.url, '/rewrite-me', undefined, { |
| 140 | + redirect: 'manual', |
| 141 | + }) |
| 142 | + expect(res.status).toBe(200) |
| 143 | + expect(await res.text()).toContain('another page') |
| 144 | + }) |
| 145 | + |
| 146 | + it('should not apply trailing slash redirect (with slash)', async () => { |
| 147 | + const res = await fetchViaHTTP(next.url, '/another/', undefined, { |
| 148 | + redirect: 'manual', |
| 149 | + }) |
| 150 | + expect(res.status).toBe(200) |
| 151 | + expect(await res.text()).toContain('another page') |
| 152 | + }) |
| 153 | + |
| 154 | + it('should not apply trailing slash redirect (without slash)', async () => { |
| 155 | + const res = await fetchViaHTTP(next.url, '/another', undefined, { |
| 156 | + redirect: 'manual', |
| 157 | + }) |
| 158 | + expect(res.status).toBe(200) |
| 159 | + expect(await res.text()).toContain('another page') |
| 160 | + }) |
| 161 | + |
| 162 | + it('should respond to index correctly', async () => { |
| 163 | + const res = await fetchViaHTTP(next.url, '/', undefined, { |
| 164 | + redirect: 'manual', |
| 165 | + }) |
| 166 | + expect(res.status).toBe(200) |
| 167 | + expect(await res.text()).toContain('index page') |
| 168 | + }) |
| 169 | + |
| 170 | + it('should respond to dynamic route correctly', async () => { |
| 171 | + const res = await fetchViaHTTP(next.url, '/blog/first', undefined, { |
| 172 | + redirect: 'manual', |
| 173 | + }) |
| 174 | + expect(res.status).toBe(200) |
| 175 | + expect(await res.text()).toContain('blog page') |
| 176 | + }) |
| 177 | + |
| 178 | + it('should navigate client side correctly', async () => { |
| 179 | + const browser = await webdriver(next.url, '/') |
| 180 | + |
| 181 | + expect(await browser.eval('location.pathname')).toBe('/') |
| 182 | + |
| 183 | + await browser.elementByCss('#to-another').click() |
| 184 | + await browser.waitForElementByCss('#another') |
| 185 | + |
| 186 | + expect(await browser.eval('location.pathname')).toBe('/another') |
| 187 | + await browser.back() |
| 188 | + await browser.waitForElementByCss('#index') |
| 189 | + |
| 190 | + expect(await browser.eval('location.pathname')).toBe('/') |
| 191 | + |
| 192 | + await browser.elementByCss('#to-blog-first').click() |
| 193 | + await browser.waitForElementByCss('#blog') |
| 194 | + |
| 195 | + expect(await browser.eval('location.pathname')).toBe('/blog/first') |
| 196 | + }) |
| 197 | +}) |
0 commit comments