Skip to content

Commit

Permalink
Test cookie addition to RequestContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
cskr committed Jan 10, 2011
1 parent f191094 commit e2a63e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
18 changes: 17 additions & 1 deletion grasshopper/lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ RequestContext.prototype.getAuth = function() {
}
};

RequestContext.prototype.addCookie = function(cookie) {
var cookieLine = cookie.name + '=' + encodeURIComponent(cookie.value);
cookieLine += cookie.path ? '; path=' + cookie.path : '';
cookieLine += cookie.expires ? '; expires=' + cookie.expires : '';
cookieLine += cookie.domain ? '; domain=' + cookie.domain : '';
cookieLine += cookie.secure ? '; secure' : '';
cookieLine += cookie.httpOnly ? '; HttpOnly' : '';

if(this.headers['set-cookie']) {
this.headers['set-cookie'] += '\r\nset-cookie: ' + cookieLine;
} else {
this.headers['set-cookie'] = cookieLine;
}
};

RequestContext.prototype.renderText = function(text) {
this._writeHead();
if(this.request.method != 'HEAD') {
Expand All @@ -131,7 +146,8 @@ RequestContext.prototype.renderError = function(status, error, cb) {
if(!err && stats.isFile()) {
try {
self._writeHead();
ghp.fill(viewFile, self.response, {error: error}, self.encoding, viewsDir, self.extn, this.locale);
ghp.fill(viewFile, self.response, {error: error},
self.encoding, viewsDir, self.extn, this.locale);
cb && cb();
} catch(e) {
self._handleError(e);
Expand Down
18 changes: 18 additions & 0 deletions test/simple/context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var assert = require('assert'),

var context = require('../../grasshopper/lib/context'),
base64 = require('../../grasshopper/lib/base64'),
Cookie = require('../../grasshopper/lib/cookie').api.Cookie,
RequestContext = context.RequestContext;

var suite = {name: 'Request Context Tests'};
Expand Down Expand Up @@ -130,6 +131,23 @@ suite.tests = {
assert.equal(res.statusCode, 500);
next();
});
},

'Add Cookie.': function(next) {
var req = new MockRequest('GET', '/test.txt', {
cookie: 'name=Chandru; city=Bangalore'
});
var res = new MockResponse();

var ctx = new RequestContext(req, res);
ctx.addCookie(new Cookie('language', 'JS'));
assert.equal(ctx.headers['set-cookie'],
'language=JS; path=/; HttpOnly');
ctx.addCookie(new Cookie('vm', 'v8'));
assert.equal(ctx.headers['set-cookie'],
'language=JS; path=/; HttpOnly'
+ '\r\nset-cookie: vm=v8; path=/; HttpOnly');
next();
}
};

Expand Down

0 comments on commit e2a63e7

Please sign in to comment.