Skip to content

Commit 070eed4

Browse files
authored
fix(command-dev): call middleware next on invalid content-type (#1949)
1 parent da3595a commit 070eed4

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/utils/detect-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ const serverSettings = async (devConfig, flags, projectDir, log) => {
176176
settings.jwtRolePath = devConfig.jwtRolePath || 'app_metadata.authorization.roles'
177177
settings.functions = devConfig.functions || settings.functions
178178
if (settings.functions) {
179-
settings.functionsPort = await getPort({ port: settings.functionsPort || 0 })
179+
settings.functionsPort = await getPort({ port: devConfig.functionsPort || 0 })
180180
}
181181

182182
return settings

src/utils/serve-functions.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ const createHandler = async function ({ dir, capabilities, warn }) {
249249
}
250250
}
251251

252-
const createFormSubmissionHandler = function ({ siteUrl }) {
252+
const createFormSubmissionHandler = function ({ siteUrl, warn }) {
253253
return async function formSubmissionHandler(req, res, next) {
254254
if (req.url.startsWith('/.netlify/') || req.method !== 'POST') return next()
255255

@@ -305,10 +305,12 @@ const createFormSubmissionHandler = function ({ siteUrl }) {
305305
})
306306
})
307307
} catch (error) {
308-
return console.error(error)
308+
warn(error)
309+
return next()
309310
}
310311
} else {
311-
return console.error('Invalid Content-Type for Netlify Dev forms request')
312+
warn('Invalid Content-Type for Netlify Dev forms request')
313+
return next()
312314
}
313315
const data = JSON.stringify({
314316
payload: {
@@ -370,7 +372,7 @@ const getFunctionsServer = async function ({ dir, siteUrl, capabilities, warn })
370372
}),
371373
)
372374
app.use(bodyParser.raw({ limit: '6mb', type: '*/*' }))
373-
app.use(createFormSubmissionHandler({ siteUrl }))
375+
app.use(createFormSubmissionHandler({ siteUrl, warn }))
374376
app.use(
375377
expressLogging(console, {
376378
blacklist: ['/favicon.ico'],

tests/command.dev.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,27 @@ testMatrix.forEach(({ args }) => {
13741374
})
13751375
})
13761376
})
1377+
1378+
test(testName(`doesn't hang when sending a application/json POST request to function server`, args), async (t) => {
1379+
await withSiteBuilder('site-with-functions', async (builder) => {
1380+
const functionsPort = 6666
1381+
await builder
1382+
.withNetlifyToml({ config: { build: { functions: 'functions' }, dev: { functionsPort } } })
1383+
.buildAsync()
1384+
1385+
await withDevServer({ cwd: builder.directory, args }, async ({ url, port }) => {
1386+
const response = await gotCatch404(`${url.replace(port, functionsPort)}/test`, {
1387+
method: 'POST',
1388+
headers: {
1389+
'Content-Type': 'application/json',
1390+
},
1391+
body: '{}',
1392+
})
1393+
t.is(response.statusCode, 404)
1394+
t.is(response.body, 'Function not found...')
1395+
})
1396+
})
1397+
})
13771398
}
13781399
})
13791400
/* eslint-enable require-await */

0 commit comments

Comments
 (0)