Skip to content
This repository has been archived by the owner on Jan 7, 2018. It is now read-only.

Commit

Permalink
Merge pull request #7 from darylrobbins/dynamic-header
Browse files Browse the repository at this point in the history
Support For Dynamic Headers
  • Loading branch information
GUI committed Feb 3, 2015
2 parents 8fd2c80 + 9df5224 commit e962160
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/gatekeeper/middleware/api_matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ _.extend(ApiMatcher.prototype, {
}

this.configCacheSettings(api.settings);
this.configHeaderSettings(api.settings);

if(api.sub_settings) {
for(j = 0; j < api.sub_settings.length; j++) {
Expand All @@ -58,6 +59,7 @@ _.extend(ApiMatcher.prototype, {
}

this.configCacheSettings(subSettings.settings);
this.configHeaderSettings(subSettings.settings);
}
}

Expand All @@ -77,6 +79,15 @@ _.extend(ApiMatcher.prototype, {
}
},

configHeaderSettings: function(settings) {
if(settings.headers) {
for(var i = 0, len = settings.headers.length; i < len; i++) {
var header = settings.headers[i];
header.template = handlebars.compile(header.value);
}
}
},

configCacheSettings: function(settings) {
settings.cache = {};

Expand Down
7 changes: 6 additions & 1 deletion lib/gatekeeper/middleware/rewrite_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ _.extend(RewriteRequest.prototype, {
if(headers) {
for(var i = 0, len = headers.length; i < len; i++) {
var header = headers[i];
request.headers[header.key] = header.value;
var potentialValue = header.template(request);

// Only set header if it evaluates to a value
if (potentialValue && potentialValue.length > 0) {
request.headers[header.key] = header.template(request);
}
}
}
},
Expand Down
89 changes: 89 additions & 0 deletions test/server/request_rewriting.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ describe('request rewriting', function() {
done();
});
});

});

describe('sub-url match', function() {
Expand All @@ -612,6 +613,94 @@ describe('request rewriting', function() {
});
});


describe('setting dynamic headers', function() {
shared.runServer({
apis: [
{
frontend_host: 'localhost',
backend_host: 'example.com',
url_matches: [
{
frontend_prefix: '/',
backend_prefix: '/',
}
],
settings: {
headers: [
{ key: 'X-Dynamic', value: '({{headers.x-dynamic-source}}-{{headers.x-dynamic-source}})' },
{ key: 'X-Dynamic-Missing', value: '{{headers.x-missing}}' },
{ key: 'X-Dynamic-Default-Absent', value: '{{#if headers.x-missing}}{{headers.x-missing}}{{else}}default{{/if}}' },
{ key: 'X-Dynamic-Default-Present', value: '{{#if headers.x-dynamic-source}}{{headers.x-dynamic-source}}{{else}}static{{/if}}' },

],
},
sub_settings: [
{
http_method: 'any',
regex: '^/info/sub',
settings: {
headers: [
{ key: 'X-Dynamic-Sub', value: '{{headers.x-dynamic-source}}' },
],
},
},
],
},
],
});

function stripStandardHeaders(headers) {
return _.omit(headers, 'host', 'connection', 'x-api-umbrella-backend-scheme', 'x-api-umbrella-backend-id', 'x-api-key', 'x-api-user-id');
}

describe('default', function() {

it('evaluates dynamic headers', function(done) {
var options = {
headers: {
'x-dynamic-source': 'dynamic'
},
};

request.get('http://localhost:9333/info/?api_key=' + this.apiKey, options, function(error, response, body) {
var data = JSON.parse(body);
stripStandardHeaders(data.headers).should.eql({
'x-dynamic': '(dynamic-dynamic)',
'x-dynamic-source': 'dynamic',
// x-dynamic-missing is not set
'x-dynamic-default-absent': 'default',
'x-dynamic-default-present': 'dynamic',
});

done();
});
});

});

describe('sub-url match', function() {
it('evaluates sub-url dynamic headers', function(done) {
var options = {
headers: {
'x-dynamic-source': 'dynamic'
},
};

request.get('http://localhost:9333/info/sub/?api_key=' + this.apiKey, options, function(error, response, body) {
var data = JSON.parse(body);
stripStandardHeaders(data.headers).should.eql({
'x-dynamic-sub': 'dynamic',
'x-dynamic-source': 'dynamic'
});

done();
});
});
});
});


describe('http basic auth', function() {
shared.runServer({
apis: [
Expand Down

0 comments on commit e962160

Please sign in to comment.