Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle contentType not being set in netlify dev #1083

Merged
merged 2 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
})
})
})