Skip to content
Browse files

Test cookie addition to RequestContext.

  • Loading branch information...
1 parent f191094 commit e2a63e7654a3160e5093c26102d507f6a629b423 @tuxychandru committed
Showing with 35 additions and 1 deletion.
  1. +17 −1 grasshopper/lib/context.js
  2. +18 −0 test/simple/context-test.js
View
18 grasshopper/lib/context.js
@@ -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') {
@@ -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);
View
18 test/simple/context-test.js
@@ -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'};
@@ -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();
}
};

0 comments on commit e2a63e7

Please sign in to comment.
Something went wrong with that request. Please try again.