Skip to content

Commit

Permalink
State autoValue option
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Mar 28, 2013
1 parent 94ee73f commit a0e1b48
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/response/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ internals.Base.prototype.state = function (name, value, options) { // o
};

if (options) {
Utils.assert(!options.autoValue, 'Cannot set autoValue directly in a response');
state.options = Utils.clone(options);
}

Expand Down
25 changes: 19 additions & 6 deletions lib/response/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.cache = function (response, request) {
if (request._route.cache.isMode('client')) {
if (ttl === null ||
ttl === undefined) {

ttl = request._route.cache.ttl();
}
}
Expand All @@ -49,15 +49,15 @@ exports.cors = function (response, request) {
if (!request.server.settings.cors) {
return;
}

if (request.server.settings.cors.origin &&
request.server.settings.cors.origin.length) {

var allowOrigin = request.server.settings.cors.origin;
var origin = request.raw.req.headers.origin;
if (origin &&
(allowOrigin.indexOf(origin) !== -1 || allowOrigin.indexOf('*') !== -1)) {

allowOrigin = origin;
}
else {
Expand All @@ -66,7 +66,7 @@ exports.cors = function (response, request) {

response.header('Access-Control-Allow-Origin', allowOrigin);
}

response.header('Access-Control-Max-Age', request.server.settings.cors.maxAge);
response.header('Access-Control-Allow-Methods', request.server.settings.cors._methods);
response.header('Access-Control-Allow-Headers', request.server.settings.cors._headers);
Expand Down Expand Up @@ -99,19 +99,32 @@ exports.state = function (response, request, callback) {

// Merge response cookies with request cookies (set while response wasn't ready)

var names = {};
var states = [];
Object.keys(response._states).forEach(function (name) {

names[name] = true;
states.push(response._states[name]);
});

Object.keys(request._states).forEach(function (name) {

if (!response._states[name]) {
if (!names[name]) {
names[name] = true;
states.push(request._states[name]);
}
});

Object.keys(request.server._stateDefinitions).forEach(function (name) {

if (request.server._stateDefinitions[name].autoValue &&
!names[name]) {

names[name] = true;
states.push({ name: name, value: request.server._stateDefinitions[name].autoValue });
}
});

if (!states.length) {
return callback();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hapi",
"description": "HTTP Server framework",
"homepage": "http://hapijs.com",
"version": "0.15.6",
"version": "0.15.7",
"author": {
"name": "Eran Hammer",
"email": "eran@hueniverse.com",
Expand Down
3 changes: 2 additions & 1 deletion test/integration/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('Response', function () {
server.route({ method: 'GET', path: '/', config: { handler: handler, cache: { mode: 'client', expiresIn: 9999 } } });
server.route({ method: 'GET', path: '/bound', config: { handler: handlerBound } });
server.state('sid', { encoding: 'base64' });
server.state('always', { autoValue: 'present' });
server.ext('onPostHandler', function (request, next) {

request.setState('test', '123');
Expand All @@ -64,7 +65,7 @@ describe('Response', function () {
expect(res.headers['cache-control']).to.equal('max-age=1, must-revalidate');
expect(res.headers['access-control-allow-origin']).to.equal('test.example.com www.example.com');
expect(res.headers['access-control-allow-credentials']).to.not.exist;
expect(res.headers['set-cookie']).to.deep.equal(['sid=YWJjZGVmZzEyMzQ1Ng==', 'other=something; Secure', 'x=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT', "test=123", "empty=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT"]);
expect(res.headers['set-cookie']).to.deep.equal(['sid=YWJjZGVmZzEyMzQ1Ng==', 'other=something; Secure', 'x=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT', 'test=123', 'empty=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT', 'always=present']);

server.inject({ method: 'GET', url: '/bound', headers: { origin: 'www.example.com' } }, function (res) {

Expand Down

0 comments on commit a0e1b48

Please sign in to comment.