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 includeHeaderValues option #23

Closed
wants to merge 3 commits into from
Closed
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
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,11 @@ and while one may wish to exercise both mechanisms, it is not useful to require
that the actual authentication cookie have a particular value.

Sepia generates filenames based on the presence and absence of header and
cookie _names_. In particular, all the header names are lower-cased and sorted
alphabetically, and this list is used to construct the fixture filename
cookie _names_ by default. In particular, all the header names are lower-cased
and sorted alphabetically, and this list is used to construct the fixture filename
corresponding to a request. The same applies to the cookie names.


If this feature is not desired, it can be disabled by calling
`sepia.configure()`:

Expand All @@ -233,6 +234,15 @@ If this feature is not desired, it can be disabled by calling
includeCookieNames: false
});


If you also want to include header values, set the includeHeaderValues flag in the
configure options:

var sepia = require('sepia');
sepia.configure({
includeHeaderValues: true
});

Additionally, a whitelist can be specified for the headers or for the cookies.
If the whitelist is empty, as is the default, all header names and cookie names
will be used to construct the fixture filename. If either whitelist has any
Expand Down Expand Up @@ -399,4 +409,3 @@ data is retrieved from a file and sent back using a dummy response object.
* [Deepank Gupta](https://github.com/deepankgupta)
* [Priyanka Salvi](https://github.com/salvipriyanka/)
* [Ashima Atul](https://github.com/ashimaatul)

34 changes: 22 additions & 12 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function reset() {

globalOptions.filenameFilters = [];

globalOptions.includeHeaderValues = false;
globalOptions.includeHeaderNames = true;
globalOptions.headerWhitelist = [];

Expand Down Expand Up @@ -60,6 +61,10 @@ function configure(options) {
globalOptions.includeHeaderNames = options.includeHeaderNames;
}

if (options.includeHeaderValues) {
globalOptions.includeHeaderValues = options.includeHeaderValues;
}

if (options.headerWhitelist != null) {
globalOptions.headerWhitelist = options.headerWhitelist.map(
function(item) {
Expand Down Expand Up @@ -267,18 +272,24 @@ function parseCookiesNames(cookieValue) {
return cookies.sort();
}

function parseHeaderNames(headers) {
function parseHeaders(headers) {
headers = removeInternalHeaders(headers);
var headerData = [];
var whitelist = globalOptions.headerWhitelist || [];

var headerNames = [];
for (var name in headers) {
if (headers.hasOwnProperty(name)) {
headerNames.push(name.toLowerCase());
var lowerName = name.toLowerCase();
if (headers.hasOwnProperty(name) && (whitelist.length===0 || whitelist.indexOf(lowerName)>=0)) {
if (globalOptions.includeHeaderValues) {
headerData.push(lowerName + ':' + headers[name]);
}
else {
headerData.push(lowerName);
}
}
}

headerNames = filterByWhitelist(headerNames, globalOptions.headerWhitelist);
return headerNames.sort();
return headerData.sort();
}

function gatherFilenameHashParts(method, reqUrl, reqBody, reqHeaders) {
Expand All @@ -287,9 +298,9 @@ function gatherFilenameHashParts(method, reqUrl, reqBody, reqHeaders) {

var filtered = applyMatchingFilters(reqUrl, reqBody);

var headerNames = [];
if (globalOptions.includeHeaderNames) {
headerNames = parseHeaderNames(reqHeaders);
var headers = [];
if (globalOptions.includeHeaderNames || globalOptions.includeHeaderValues) {
headers = parseHeaders(reqHeaders);
}

var cookieNames = [];
Expand All @@ -305,7 +316,7 @@ function gatherFilenameHashParts(method, reqUrl, reqBody, reqHeaders) {
['method', method],
['url', filtered.filteredUrl],
['body', filtered.filteredBody],
['headerNames', headerNames],
['headerNames', headers],
['cookieNames', cookieNames]
];
}
Expand Down Expand Up @@ -334,7 +345,6 @@ function constructAndCreateFixtureFolder(reqUrl, reqHeaders) {

function constructFilename(method, reqUrl, reqBody, reqHeaders) {
var hashParts = gatherFilenameHashParts(method, reqUrl, reqBody, reqHeaders);

var hash = crypto.createHash('md5');
hash.update(JSON.stringify(hashParts));

Expand Down Expand Up @@ -449,7 +459,7 @@ module.exports.internal.log = log;
module.exports.internal.logFixtureStatus = logFixtureStatus;
module.exports.internal.logFixtureDebugStatus = logFixtureDebugStatus;
module.exports.internal.parseCookiesNames = parseCookiesNames;
module.exports.internal.parseHeaderNames = parseHeaderNames;
module.exports.internal.parseHeaders = parseHeaders;
module.exports.internal.applyMatchingFilters = applyMatchingFilters;
module.exports.internal.gatherFilenameHashParts = gatherFilenameHashParts;
module.exports.internal.constructAndCreateFixtureFolder =
Expand Down
49 changes: 37 additions & 12 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,16 @@ describe('utils.js', function() {
});
});

describe('#parseHeaderNames', function() {
const parseHeaderNames = sepiaUtil.internal.parseHeaderNames;
describe('#parseHeaders', function() {
const parseHeaders = sepiaUtil.internal.parseHeaders;

it('returns an empty list when there are no headers', function() {
parseHeaderNames().should.eql([]);
parseHeaderNames({}).should.eql([]);
parseHeaders().should.eql([]);
parseHeaders({}).should.eql([]);
});

it('parses out all header names when there is no whitelist', function() {
parseHeaderNames({
parseHeaders({
name1: 'value1',
name2: 'value2'
}).should.eql([
Expand All @@ -478,15 +478,15 @@ describe('utils.js', function() {
});

it('alphabetizes the header names', function() {
parseHeaderNames({
parseHeaders({
b: 1,
c: 2,
a: 3
}).should.eql(['a', 'b', 'c']);
});

it('lower cases the header names', function() {
parseHeaderNames({
parseHeaders({
A: 1,
B: 2,
C: 3
Expand All @@ -498,15 +498,40 @@ describe('utils.js', function() {
headerWhitelist: ['a', 'b']
});

parseHeaderNames({
parseHeaders({
b: 1,
c: 2,
a: 3
}).should.eql(['a', 'b']);
});

it('includes header values if requested', function () {
sepiaUtil.configure({
includeHeaderValues: true
});

parseHeaders({
b: 1,
c: 2,
a: 3
}).should.eql(['a:3', 'b:1', 'c:2']);
});

it('includes header values if requested and filters by whitelist case insensitively', function () {
sepiaUtil.configure({
includeHeaderValues: true,
headerWhitelist: ['A', 'B']
});

parseHeaders({
b: 1,
c: 2,
a: 3
}).should.eql(['a:3', 'b:1']);
});

it('filters out sepia headers', function() {
parseHeaderNames({
parseHeaders({
b: 1,
'x-sepia-internal-header': 2,
a: 3
Expand Down Expand Up @@ -754,9 +779,9 @@ describe('utils.js', function() {
});


it('constructs using all the available information', function() {
it('constructs using all the available information using default values', function() {
sepiaUtil.setFixtureDir('/global/fixture/dir');
sepiaUtil.setTestOptions({ testName: 'test/name' });
sepiaUtil.setTestOptions({ testName: 'test/name', });

var filename = constructFilename('get', 'my-url', 'my-body', {
'accept-language': 'en-US',
Expand All @@ -767,6 +792,7 @@ describe('utils.js', function() {
filename.should.equal('/global/fixture/dir/en-US/test/name/' +
'32772f774a3f187d465d47a526b80e6f');
});

});

describe('#urlFromHttpRequestOptions', function() {
Expand Down Expand Up @@ -961,4 +987,3 @@ describe('utils.js', function() {

});
});