|
| 1 | +import { nextTestSetup } from 'e2e-utils' |
| 2 | +import { fetchViaHTTP } from 'next-test-utils' |
| 3 | + |
| 4 | +describe('client-max-body-size', () => { |
| 5 | + describe('default 10MB limit', () => { |
| 6 | + const { next, skipped } = nextTestSetup({ |
| 7 | + files: __dirname, |
| 8 | + // Deployed environment has it's own configured limits. |
| 9 | + skipDeployment: true, |
| 10 | + }) |
| 11 | + |
| 12 | + if (skipped) return |
| 13 | + |
| 14 | + it('should reject request body over 10MB by default', async () => { |
| 15 | + const bodySize = 11 * 1024 * 1024 // 11MB |
| 16 | + const body = 'x'.repeat(bodySize) |
| 17 | + |
| 18 | + const res = await fetchViaHTTP( |
| 19 | + next.url, |
| 20 | + '/api/echo', |
| 21 | + {}, |
| 22 | + { |
| 23 | + body, |
| 24 | + method: 'POST', |
| 25 | + } |
| 26 | + ) |
| 27 | + |
| 28 | + expect(res.status).toBe(400) |
| 29 | + expect(next.cliOutput).toContain('Request body exceeded 10MB') |
| 30 | + }) |
| 31 | + |
| 32 | + it('should accept request body at exactly 10MB', async () => { |
| 33 | + const bodySize = 10 * 1024 * 1024 // 10MB |
| 34 | + const body = 'y'.repeat(bodySize) |
| 35 | + |
| 36 | + const res = await fetchViaHTTP( |
| 37 | + next.url, |
| 38 | + '/api/echo', |
| 39 | + {}, |
| 40 | + { |
| 41 | + body, |
| 42 | + method: 'POST', |
| 43 | + } |
| 44 | + ) |
| 45 | + |
| 46 | + expect(res.status).toBe(200) |
| 47 | + const responseBody = await res.text() |
| 48 | + expect(responseBody).toBe('Hello World') |
| 49 | + }) |
| 50 | + |
| 51 | + it('should accept request body under 10MB', async () => { |
| 52 | + const bodySize = 5 * 1024 * 1024 // 5MB |
| 53 | + const body = 'z'.repeat(bodySize) |
| 54 | + |
| 55 | + const res = await fetchViaHTTP( |
| 56 | + next.url, |
| 57 | + '/api/echo', |
| 58 | + {}, |
| 59 | + { |
| 60 | + body, |
| 61 | + method: 'POST', |
| 62 | + } |
| 63 | + ) |
| 64 | + |
| 65 | + expect(res.status).toBe(200) |
| 66 | + const responseBody = await res.text() |
| 67 | + expect(responseBody).toBe('Hello World') |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + describe('custom limit with string format', () => { |
| 72 | + const { next, skipped } = nextTestSetup({ |
| 73 | + files: __dirname, |
| 74 | + skipDeployment: true, |
| 75 | + nextConfig: { |
| 76 | + experimental: { |
| 77 | + middlewareClientMaxBodySize: '5mb', |
| 78 | + }, |
| 79 | + }, |
| 80 | + }) |
| 81 | + |
| 82 | + if (skipped) return |
| 83 | + |
| 84 | + it('should reject request body over custom 5MB limit', async () => { |
| 85 | + const bodySize = 6 * 1024 * 1024 // 6MB |
| 86 | + const body = 'a'.repeat(bodySize) |
| 87 | + |
| 88 | + const res = await fetchViaHTTP( |
| 89 | + next.url, |
| 90 | + '/api/echo', |
| 91 | + {}, |
| 92 | + { |
| 93 | + body, |
| 94 | + method: 'POST', |
| 95 | + } |
| 96 | + ) |
| 97 | + |
| 98 | + expect(res.status).toBe(400) |
| 99 | + expect(next.cliOutput).toContain('Request body exceeded 5MB') |
| 100 | + }) |
| 101 | + |
| 102 | + it('should accept request body under custom 5MB limit', async () => { |
| 103 | + const bodySize = 4 * 1024 * 1024 // 4MB |
| 104 | + const body = 'b'.repeat(bodySize) |
| 105 | + |
| 106 | + const res = await fetchViaHTTP( |
| 107 | + next.url, |
| 108 | + '/api/echo', |
| 109 | + {}, |
| 110 | + { |
| 111 | + body, |
| 112 | + method: 'POST', |
| 113 | + } |
| 114 | + ) |
| 115 | + |
| 116 | + expect(res.status).toBe(200) |
| 117 | + const responseBody = await res.text() |
| 118 | + expect(responseBody).toBe('Hello World') |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + describe('custom limit with number format', () => { |
| 123 | + const { next, skipped } = nextTestSetup({ |
| 124 | + files: __dirname, |
| 125 | + skipDeployment: true, |
| 126 | + nextConfig: { |
| 127 | + experimental: { |
| 128 | + middlewareClientMaxBodySize: 2 * 1024 * 1024, // 2MB in bytes |
| 129 | + }, |
| 130 | + }, |
| 131 | + }) |
| 132 | + |
| 133 | + if (skipped) return |
| 134 | + |
| 135 | + it('should reject request body over custom 2MB limit', async () => { |
| 136 | + const bodySize = 3 * 1024 * 1024 // 3MB |
| 137 | + const body = 'c'.repeat(bodySize) |
| 138 | + |
| 139 | + const res = await fetchViaHTTP( |
| 140 | + next.url, |
| 141 | + '/api/echo', |
| 142 | + {}, |
| 143 | + { |
| 144 | + body, |
| 145 | + method: 'POST', |
| 146 | + } |
| 147 | + ) |
| 148 | + |
| 149 | + expect(res.status).toBe(400) |
| 150 | + expect(next.cliOutput).toContain('Request body exceeded 2MB') |
| 151 | + }) |
| 152 | + |
| 153 | + it('should accept request body under custom 2MB limit', async () => { |
| 154 | + const bodySize = 1 * 1024 * 1024 // 1MB |
| 155 | + const body = 'd'.repeat(bodySize) |
| 156 | + |
| 157 | + const res = await fetchViaHTTP( |
| 158 | + next.url, |
| 159 | + '/api/echo', |
| 160 | + {}, |
| 161 | + { |
| 162 | + body, |
| 163 | + method: 'POST', |
| 164 | + } |
| 165 | + ) |
| 166 | + |
| 167 | + expect(res.status).toBe(200) |
| 168 | + const responseBody = await res.text() |
| 169 | + expect(responseBody).toBe('Hello World') |
| 170 | + }) |
| 171 | + }) |
| 172 | + |
| 173 | + describe('large custom limit', () => { |
| 174 | + const { next, skipped } = nextTestSetup({ |
| 175 | + files: __dirname, |
| 176 | + skipDeployment: true, |
| 177 | + nextConfig: { |
| 178 | + experimental: { |
| 179 | + middlewareClientMaxBodySize: '50mb', |
| 180 | + }, |
| 181 | + }, |
| 182 | + }) |
| 183 | + |
| 184 | + if (skipped) return |
| 185 | + |
| 186 | + it('should accept request body up to 50MB with custom limit', async () => { |
| 187 | + const bodySize = 20 * 1024 * 1024 // 20MB |
| 188 | + const body = 'e'.repeat(bodySize) |
| 189 | + |
| 190 | + const res = await fetchViaHTTP( |
| 191 | + next.url, |
| 192 | + '/api/echo', |
| 193 | + {}, |
| 194 | + { |
| 195 | + body, |
| 196 | + method: 'POST', |
| 197 | + } |
| 198 | + ) |
| 199 | + |
| 200 | + expect(res.status).toBe(200) |
| 201 | + const responseBody = await res.text() |
| 202 | + expect(responseBody).toBe('Hello World') |
| 203 | + }) |
| 204 | + |
| 205 | + it('should reject request body over custom 50MB limit', async () => { |
| 206 | + const bodySize = 51 * 1024 * 1024 // 51MB |
| 207 | + const body = 'f'.repeat(bodySize) |
| 208 | + |
| 209 | + const res = await fetchViaHTTP( |
| 210 | + next.url, |
| 211 | + '/api/echo', |
| 212 | + {}, |
| 213 | + { |
| 214 | + body, |
| 215 | + method: 'POST', |
| 216 | + } |
| 217 | + ) |
| 218 | + |
| 219 | + expect(res.status).toBe(400) |
| 220 | + expect(next.cliOutput).toContain('Request body exceeded 50MB') |
| 221 | + }) |
| 222 | + }) |
| 223 | +}) |
0 commit comments