Skip to content

Commit

Permalink
fix(command-dev): handle missing content-type header (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvipanda authored Aug 12, 2020
1 parent 2d12d4a commit 23d290a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/commands/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ async function startProxy(settings = {}, addonUrls, configPath, projectDir, func

if (match) return serveRedirect(req, res, proxy, match, options)

const ct = req.headers['content-type'] ? contentType.parse(req) : {}
const ct = req.headers['content-type'] ? contentType.parse(req).type : ''
if (
req.method === 'POST' &&
!isInternal(req.url) &&
(ct.type.endsWith('/x-www-form-urlencoded') || ct.type === 'multipart/form-data')
(ct.endsWith('/x-www-form-urlencoded') || ct === 'multipart/form-data')
) {
return proxy.web(req, res, { target: functionsServer })
}
Expand Down Expand Up @@ -338,12 +338,12 @@ async function serveRedirect(req, res, proxy, match, options) {
return handler(req, res, {})
}

const ct = req.headers['content-type'] ? contentType.parse(req) : {}
const ct = req.headers['content-type'] ? contentType.parse(req).type : ''
if (
req.method === 'POST' &&
!isInternal(req.url) &&
!isInternal(destURL) &&
(ct.type.endsWith('/x-www-form-urlencoded') || ct.type === 'multipart/form-data')
(ct.endsWith('/x-www-form-urlencoded') || ct === 'multipart/form-data')
) {
return proxy.web(req, res, { target: options.functionsServer })
}
Expand Down
39 changes: 39 additions & 0 deletions tests/command.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,3 +764,42 @@ test('should redirect requests to an external server', async t => {
server.close()
})
})

test('should redirect POST request if content-type is missing', async t => {
await withSiteBuilder('site-with-post-no-content-type', async builder => {
builder.withNetlifyToml({
config: {
build: { functions: 'functions' },
redirects: [{ from: '/api/*', to: '/other/:splat', status: 200 }],
},
})

await builder.buildAsync()

await withDevServer({ cwd: builder.directory }, async server => {
// we use http.request since fetch automatically sends a content-type header
const http = require('http')
const options = {
host: server.host,
port: server.port,
path: '/api/echo',
method: 'POST',
}
let data = ''
await new Promise(resolve => {
const callback = response => {
response.on('data', chunk => {
data += chunk
})
response.on('end', resolve)
}
const req = http.request(options, callback)
req.write('param=value')
req.end()
})

// we're testing Netlify Dev didn't crash
t.is(data, 'Method Not Allowed')
})
})
})

0 comments on commit 23d290a

Please sign in to comment.