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

Delete request headers with undefined value. #1525

Merged
merged 1 commit into from Apr 5, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions request.js
Expand Up @@ -323,6 +323,14 @@ Request.prototype.init = function (options) {
}
self.headers = self.headers ? copy(self.headers) : {}

// Delete headers with value undefined since they break
// ClientRequest.OutgoingMessage.setHeader in node 0.12
for (var headerName in self.headers) {
if (typeof self.headers[headerName] === 'undefined') {
delete self.headers[headerName]
}
}

caseless.httpify(self, self.headers)

if (!self.method) {
Expand Down
18 changes: 18 additions & 0 deletions tests/test-defaults.js
Expand Up @@ -23,6 +23,14 @@ tape('setup', function(t) {
resp.end()
})

s.on('/foo-no-test', function(req, resp) {
assert.equal(req.headers.foo, 'bar')
assert.equal('test' in req.headers, false)
assert.equal(req.method, 'GET')
resp.writeHead(200, {'Content-Type': 'text/plain'})
resp.end('TESTING!')
})

s.on('/post', function (req, resp) {
assert.equal(req.headers.foo, 'bar')
assert.equal(req.headers['content-type'], null)
Expand Down Expand Up @@ -136,6 +144,16 @@ tape('merge headers', function(t) {
})
})

tape('default undefined header', function(t) {
request.defaults({
headers: { foo: 'bar', test: undefined }
})(s.url + '/foo-no-test', function(e, r, b) {
t.equal(e, null)
t.equal(b, 'TESTING!')
t.end()
})
})

tape('post(string, object, function)', function(t) {
request.defaults({
headers: { foo: 'bar' }
Expand Down
24 changes: 24 additions & 0 deletions tests/test-headers.js
Expand Up @@ -18,6 +18,14 @@ s.on('/redirect/to', function(req, res) {
res.end('ok')
})

s.on('/headers.json', function(req, res) {
res.writeHead(200, {
'Content-Type': 'application/json'
})

res.end(JSON.stringify(req.headers))
})

tape('setup', function(t) {
s.listen(s.port, function() {
t.end()
Expand Down Expand Up @@ -138,6 +146,22 @@ tape('upper-case Host header and redirect', function(t) {
})
})

tape('undefined headers', function(t) {
request({
url: s.url + '/headers.json',
headers: {
'X-TEST-1': 'test1',
'X-TEST-2': undefined
},
json: true
}, function(err, res, body) {
t.equal(err, null)
t.equal(body['x-test-1'], 'test1')
t.equal(typeof body['x-test-2'], 'undefined')
t.end()
})
})

tape('cleanup', function(t) {
s.close(function() {
t.end()
Expand Down