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

Add fallback for mime.lookup #63

Merged
merged 1 commit into from
Mar 14, 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
5 changes: 4 additions & 1 deletion request.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,8 @@ Request.prototype.init = function (options) {
self.src = src
if (isReadStream(src)) {
if (!self.hasHeader('content-type')) {
self.setHeader('Content-Type', mime.lookup(src.path))
// @note fallback to 'application/octet-stream' if mime.lookup returns `false`
self.setHeader('Content-Type', mime.lookup(src.path) || 'application/octet-stream')
}
} else {
if (src.headers) {
Expand Down Expand Up @@ -897,6 +898,8 @@ Request.prototype.start = function () {
if (Array.isArray(self.blacklistHeaders) && self.blacklistHeaders.length) {
self.blacklistHeaders.forEach(function (header) {
self.req.removeHeader(header)
// also remove from the `self` for the consistency
self.removeHeader(header)
})
}
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions tests/raw.file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
21 changes: 21 additions & 0 deletions tests/test-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var server = require('./server')
var request = require('../index')
var tape = require('tape')
var http = require('http')
var path = require('path')
var fs = require('fs')

var s = server.createServer()

Expand Down Expand Up @@ -127,6 +129,25 @@ addTest('testPutMultipartPostambleCRLF', {
]
})

tape('testBinaryFile', function (t) {
var server = http.createServer()
server.on('request', function (req, res) {
req.pipe(res)
})
server.listen(0, function () {
request({
uri: 'http://localhost:' + this.address().port,
method: 'POST',
body: fs.createReadStream(path.join(__dirname, 'raw.file'))
}, function (err, res, body) {
t.error(err)
// defaults to 'application/octet-stream' content-type
t.equal(res.request.headers['Content-Type'], 'application/octet-stream')
server.close(t.end)
})
})
})

tape('typed array', function (t) {
var server = http.createServer()
server.on('request', function (req, res) {
Expand Down