Skip to content

Commit

Permalink
Merge pull request #63 from postmanlabs/feature/mime-lookup
Browse files Browse the repository at this point in the history
Add fallback for mime.lookup
  • Loading branch information
codenirvana committed Mar 14, 2020
2 parents 712965b + c0433b8 commit edc91e9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
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

0 comments on commit edc91e9

Please sign in to comment.