-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
180 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict' | ||
|
||
/* | ||
* adonis-session | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
const SessionClient = require('../Session/Client') | ||
|
||
module.exports = function (Request, Config) { | ||
/** | ||
* A request getter to set session client on the | ||
* request. | ||
*/ | ||
Request.getter('_session', function () { | ||
return new SessionClient(Config) | ||
}, true) | ||
|
||
/** | ||
* A request macro to set session when making | ||
* requests | ||
*/ | ||
Request.macro('session', function (key, value) { | ||
this._session.put(key, value) | ||
return this | ||
}) | ||
|
||
/** | ||
* Adding session as cookie before request | ||
* is executed | ||
*/ | ||
Request.before((requestInstance) => { | ||
requestInstance._session.toJSON().forEach((item) => { | ||
requestInstance.cookie(item.key, item.value) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict' | ||
|
||
/* | ||
* adonis-session | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
const test = require('japa') | ||
const { Config } = require('@adonisjs/sink') | ||
const SessionClient = require('../src/Session/Client') | ||
const VowRequest = require('../src/VowBindings/Request') | ||
|
||
class Request { | ||
constructor () { | ||
this._getters = [] | ||
this._macros = [] | ||
this._hooks = [] | ||
} | ||
|
||
getter (key, value, singleton) { | ||
this._getters.push({ key, value, singleton }) | ||
} | ||
|
||
macro (key, value) { | ||
this._macros.push({ key, value }) | ||
} | ||
|
||
before (fn) { | ||
this._hooks.push({ fn }) | ||
} | ||
} | ||
|
||
test.group('Vow request', () => { | ||
test('add session getter to the request', (assert) => { | ||
const req = new Request() | ||
VowRequest(req, new Config()) | ||
assert.lengthOf(req._getters, 1) | ||
assert.equal(req._getters[0].key, '_session') | ||
assert.instanceOf(req._getters[0].value(), SessionClient) | ||
}) | ||
|
||
test('add macro to the request', (assert) => { | ||
const req = new Request() | ||
VowRequest(req, new Config()) | ||
assert.lengthOf(req._macros, 1) | ||
assert.equal(req._macros[0].key, 'session') | ||
}) | ||
|
||
test('add before hook', (assert) => { | ||
const req = new Request() | ||
VowRequest(req, new Config()) | ||
assert.lengthOf(req._hooks, 1) | ||
assert.isFunction(req._hooks[0].fn) | ||
}) | ||
}) |