Permalink
Browse files

Support flash-like in get

  • Loading branch information...
1 parent 6988f6d commit a19de2d29a7250f7e6512e3fdcfe902b2748f09c @hueniverse hueniverse committed Apr 2, 2013
Showing with 72 additions and 3 deletions.
  1. +1 −1 README.md
  2. +7 −2 lib/index.js
  3. +64 −0 test/index.js
View
@@ -66,7 +66,7 @@ server.plugin.allow({ ext: true }).require('yar', options, function (err) { });
- `reset()` - clears the session and assigns a new session id.
- `set(key, value)` - assigns a value (string, object, etc) to a given key which will persist across requests.
- `set(keysObject)` - assigns values to multiple keys using each 'keysObject' top-level property.
-- `get(key)` - retreive value using a key.
+- `get(key, clear)` - retreive value using a key. If 'clear' is 'true', key is cleared on return.
- `clear(key)` - clears key.
- `touch()` - Manually notify the session of changes (when using `get()` and changing the content of the returned reference directly without calling `set()`).
- `flash(type, message, isOverride)` - stores volatile data - data that should be deleted once read. When given no arguments, it will return all of the flash messages and delete the originals. When given only a type, it will return all of the flash messages of that type and delete the originals. When given a type and a message, it will set or append that message to the given type. 'isOverride' used to indicate that the message provided should replace any existing value instead of being appended to it (defaults to false).
View
@@ -105,9 +105,14 @@ exports.register = function (pack, options, next) {
cache.drop(request.session.id, function (err) { });
};
- request.session.get = function (key) {
+ request.session.get = function (key, clear) {
- return request.session._store[key];
+ var value = request.session._store[key];
+ if (clear) {
+ request.session.clear(key);
+ }
+
+ return value;
};
request.session.set = function (key, value) {
View
@@ -243,6 +243,70 @@ describe('Yar', function () {
});
});
+ it('sets session value then gets it back (clear)', function (done) {
+
+ var options = {
+ maxCookieSize: 0,
+ cookieOptions: {
+ password: 'password',
+ isSecure: false
+ }
+ };
+
+ var server = new Hapi.Server(0);
+
+ server.route([
+ {
+ method: 'GET', path: '/1', handler: function () {
+
+ this.session.set('some', '2');
+ return this.reply('1');
+ }
+ },
+ {
+ method: 'GET', path: '/2', handler: function () {
+
+ var some = this.session.get('some', true);
+ return this.reply(some);
+ }
+ },
+ {
+ method: 'GET', path: '/3', handler: function () {
+
+ var some = this.session.get('some');
+ return this.reply(some || '3');
+ }
+ }
+ ]);
+
+ server.plugin.allow({ ext: true }).require('../', options, function (err) {
+
+ expect(err).to.not.exist;
+ server.start(function () {
+
+ server.inject({ method: 'GET', url: '/1' }, function (res) {
+
+ expect(res.result).to.equal('1');
+ var header = res.headers['set-cookie'];
+ var cookie = header[0].match(/(session=[^\x00-\x20\"\,\;\\\x7F]*)/);
+
+ server.inject({ method: 'GET', url: '/2', headers: { cookie: cookie[1] } }, function (res) {
+
+ expect(res.result).to.equal('2');
+ var header = res.headers['set-cookie'];
+ var cookie = header[0].match(/(session=[^\x00-\x20\"\,\;\\\x7F]*)/);
+
+ server.inject({ method: 'GET', url: '/3', headers: { cookie: cookie[1] } }, function (res) {
+
+ expect(res.result).to.equal('3');
+ done();
+ });
+ });
+ });
+ });
+ });
+ });
+
it('fails to set cookie in invalid cache', function (done) {
var options = {

0 comments on commit a19de2d

Please sign in to comment.