Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
Include URL in key for better debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
suprememoocow committed Mar 17, 2015
1 parent 0509a37 commit a2a22f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
18 changes: 8 additions & 10 deletions lib/key-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

var crypto = require('crypto');

function generateKeyHash(url, headers, varyHeader) {
if (!varyHeader) return { url: url };
function generateKeyHash(headers, varyHeader) {
if (!varyHeader) return { };

var varySplit = varyHeader.toLowerCase().split(/,\s*/);
if (!varySplit.length) return { url: url };
if (!varySplit.length) return { };

var lcHeaders;
if (headers) {
Expand All @@ -21,11 +21,9 @@ function generateKeyHash(url, headers, varyHeader) {
varySplit.sort(); // Always keep the list in alphabetical order

return varySplit.reduce(function(memo, vary) {
memo['h.' + vary] = lcHeaders[vary] || '';
return memo;
}, {
url: url
});
memo[vary] = lcHeaders[vary] || '';
return memo;
}, {});
}

function hashKeyString(s) {
Expand All @@ -35,6 +33,6 @@ function hashKeyString(s) {
}

module.exports = function(url, headers, varyHeader) {
var hash = generateKeyHash(url, headers, varyHeader);
return hashKeyString(JSON.stringify(hash));
var hash = generateKeyHash(headers, varyHeader);
return "" + hashKeyString(JSON.stringify(hash)) + ":" + url;
};
9 changes: 9 additions & 0 deletions test/key-generator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('key-generator', function() {
it('should handle case insensitivity of request headers ', function() {
var k1 = keyGenerator('https://api.github.com/v1', { accePT: 'application/json', authorization: 'x' }, 'aCCept,AuthorizaTIon');
var k2 = keyGenerator('https://api.github.com/v1', { accept: 'application/json', AUthorizatioN: 'x' }, 'AccePT,authorizatioN');

assert(k1);
assert(k2);
assert.strictEqual(k1, k2);
Expand All @@ -84,6 +85,14 @@ describe('key-generator', function() {
assert.strictEqual(k1, k2);
});

it('should generate the same key for different Vary field order', function() {
var k1 = keyGenerator('https://api.github.com/v1', { accept: 'application/json' }, 'AuthorizaTIon, aCCept');
var k2 = keyGenerator('https://api.github.com/v1', { accept: 'application/json' }, 'AccePT,authorizatioN');
assert(k1);
assert(k2);
assert.strictEqual(k1, k2);
});

it('should treat missing and null header fields equally', function() {
var k1 = keyGenerator('https://api.github.com/v1', null, 'aCCept,AuthorizaTIon');
var k2 = keyGenerator('https://api.github.com/v1', { accept: '' }, 'AccePT,authorizatioN');
Expand Down

0 comments on commit a2a22f6

Please sign in to comment.