From 706547e0c3cdfcaf0e7c744aba9406642918aeb7 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 24 Aug 2021 19:26:33 -0500 Subject: [PATCH] Ensure error is shown correctly for empty headers field (#28430) * Ensure error is shown correctly for empty headers field * Apply suggestions from code review Co-authored-by: Steven Co-authored-by: Steven --- 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..b2be7f13fb286c5 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 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 3140373cea433ea..9a03488580538a2 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 cannot be empty for route {"source":"/:path*"' + ) + }) + it('should error when source and destination length is exceeded', async () => { await writeConfig( [