Skip to content

Commit

Permalink
Allow '0' as a valid header value, for content-length
Browse files Browse the repository at this point in the history
Also add test to prevent regressions on this bug

Fixes #39
  • Loading branch information
arekinath committed Sep 21, 2015
1 parent e7d96a1 commit b1d848a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ module.exports = {

if (h !== 'request-line') {
var value = request.getHeader(h);
if (!value) {
if (value === undefined || value === '') {
throw new MissingHeaderError(h + ' was not in the request');
}
stringToSign += h + ': ' + value;
Expand Down
99 changes: 99 additions & 0 deletions test/header.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2015 Joyent, Inc. All rights reserved.

var crypto = require('crypto');
var fs = require('fs');
var http = require('http');

var test = require('tap').test;
var uuid = require('node-uuid');

var httpSignature = require('../lib/index');



///--- Globals

var hmacKey = null;
var httpOptions = null;
var rsaPrivate = null;
var signOptions = null;
var server = null;
var socket = null;



///--- Tests


test('setup', function(t) {
rsaPrivate = fs.readFileSync(__dirname + '/rsa_private.pem', 'ascii');
t.ok(rsaPrivate);

socket = '/tmp/.' + uuid();

server = http.createServer(function(req, res) {
res.writeHead(200);
res.end();
});

server.listen(socket, function() {
hmacKey = uuid();
httpOptions = {
socketPath: socket,
path: '/',
method: 'HEAD',
headers: {
'content-length': '0',
'x-foo': 'false'
}
};

signOptions = {
key: rsaPrivate,
keyId: 'unitTest',
};

t.end();
});
});



test('header with 0 value', function(t) {
var req = http.request(httpOptions, function(res) {
t.end();
});
var opts = {
keyId: 'unit',
key: rsaPrivate,
headers: ['date', 'request-line', 'content-length']
};

t.ok(httpSignature.sign(req, opts));
t.ok(req.getHeader('Authorization'));
console.log('> ' + req.getHeader('Authorization'));
req.end();
});

test('header with boolean-mungable value', function(t) {
var req = http.request(httpOptions, function(res) {
t.end();
});
var opts = {
keyId: 'unit',
key: rsaPrivate,
headers: ['date', 'x-foo']
};

t.ok(httpSignature.sign(req, opts));
t.ok(req.getHeader('Authorization'));
console.log('> ' + req.getHeader('Authorization'));
req.end();
});

test('tear down', function(t) {
server.on('close', function() {
t.end();
});
server.close();
});

0 comments on commit b1d848a

Please sign in to comment.