From 7721a3ee9ee22dd7a650aafa66faf129995e8841 Mon Sep 17 00:00:00 2001 From: "jj@jjsweb.site" Date: Mon, 23 Aug 2021 21:31:30 -0500 Subject: [PATCH 1/2] Ensure error is shown correctly for empty headers field --- packages/next/lib/load-custom-routes.ts | 2 ++ .../invalid-custom-routes/test/index.test.js | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/packages/next/lib/load-custom-routes.ts b/packages/next/lib/load-custom-routes.ts index 832f5d6ed1e37e4..350fe08b582d646 100644 --- a/packages/next/lib/load-custom-routes.ts +++ b/packages/next/lib/load-custom-routes.ts @@ -108,6 +108,8 @@ function checkHeader(route: Header): string[] { if (!Array.isArray(route.headers)) { invalidParts.push('`headers` field must be an array') + } else if (route.headers.length === 0) { + invalidParts.push('`headers` field can not be empty') } else { for (const header of route.headers) { if (!header || typeof header !== 'object') { diff --git a/test/integration/invalid-custom-routes/test/index.test.js b/test/integration/invalid-custom-routes/test/index.test.js index 3140373cea433ea..b6730877d9c1908 100644 --- a/test/integration/invalid-custom-routes/test/index.test.js +++ b/test/integration/invalid-custom-routes/test/index.test.js @@ -25,6 +25,23 @@ const writeConfig = async (routes, type = 'redirects') => { let getStderr const runTests = () => { + it('should error when empty headers array is present on header item', async () => { + await writeConfig( + [ + { + source: `/:path*`, + headers: [], + }, + ], + 'headers' + ) + const stderr = await getStderr() + + expect(stderr).toContain( + '`headers` field can not be empty for route {"source":"/:path*"' + ) + }) + it('should error when source and destination length is exceeded', async () => { await writeConfig( [ From a27069775a082a0a5eebfec75c7725bd6821316f Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 24 Aug 2021 15:44:51 -0500 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Steven --- packages/next/lib/load-custom-routes.ts | 2 +- test/integration/invalid-custom-routes/test/index.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/next/lib/load-custom-routes.ts b/packages/next/lib/load-custom-routes.ts index 350fe08b582d646..b2be7f13fb286c5 100644 --- a/packages/next/lib/load-custom-routes.ts +++ b/packages/next/lib/load-custom-routes.ts @@ -109,7 +109,7 @@ function checkHeader(route: Header): string[] { if (!Array.isArray(route.headers)) { invalidParts.push('`headers` field must be an array') } else if (route.headers.length === 0) { - invalidParts.push('`headers` field can not be empty') + invalidParts.push('`headers` field cannot be empty') } else { for (const header of route.headers) { if (!header || typeof header !== 'object') { diff --git a/test/integration/invalid-custom-routes/test/index.test.js b/test/integration/invalid-custom-routes/test/index.test.js index b6730877d9c1908..9a03488580538a2 100644 --- a/test/integration/invalid-custom-routes/test/index.test.js +++ b/test/integration/invalid-custom-routes/test/index.test.js @@ -38,7 +38,7 @@ const runTests = () => { const stderr = await getStderr() expect(stderr).toContain( - '`headers` field can not be empty for route {"source":"/:path*"' + '`headers` field cannot be empty for route {"source":"/:path*"' ) })