-
-
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
8 changed files
with
200 additions
and
7 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,20 @@ | ||
'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 NE = require('node-exceptions') | ||
|
||
class InvalidArgumentException extends NE.InvalidArgumentException { | ||
static invalidSessionDriver (name) { | ||
return new this(`${name} is not a valid session provider`, 500, 'E_INVALID_SESSION_DRIVER') | ||
} | ||
} | ||
|
||
module.exports = { InvalidArgumentException } |
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,61 @@ | ||
'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 path = require('path') | ||
const { ioc } = require('@adonisjs/fold') | ||
const { Config, Helpers } = require('@adonisjs/sink') | ||
|
||
const manager = require('../src/Session/Manager') | ||
const drivers = require('../src/Session/Drivers') | ||
|
||
test.group('Session Manager', (group) => { | ||
group.before(() => { | ||
ioc.singleton('Adonis/Src/Config', () => { | ||
return new Config() | ||
}) | ||
|
||
ioc.singleton('Adonis/Src/Helpers', () => { | ||
return new Helpers(path.join(__dirname)) | ||
}) | ||
|
||
ioc.singleton('Adonis/Addons/RedisFactory', () => { | ||
return class RedisFactory {} | ||
}) | ||
}) | ||
|
||
test('add a new driver', (assert) => { | ||
class Mongo {} | ||
manager.extend('mongo', Mongo) | ||
assert.deepEqual(manager._drivers, { mongo: Mongo }) | ||
}) | ||
|
||
test('get cookie driver instance', (assert) => { | ||
assert.instanceOf(manager.makeDriverInstance('cookie'), drivers.cookie) | ||
}) | ||
|
||
test('get redis driver instance', (assert) => { | ||
assert.instanceOf(manager.makeDriverInstance('redis'), drivers.redis) | ||
}) | ||
|
||
test('get file driver instance', (assert) => { | ||
assert.instanceOf(manager.makeDriverInstance('file'), drivers.file) | ||
}) | ||
|
||
test('get custom driver instance', (assert) => { | ||
assert.isDefined(manager.makeDriverInstance('mongo')) | ||
}) | ||
|
||
test('throw exception when unable to find driver', (assert) => { | ||
const fn = () => manager.makeDriverInstance('foo') | ||
assert.throw(fn, 'E_INVALID_SESSION_DRIVER: foo is not a valid session provider') | ||
}) | ||
}) |
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,103 @@ | ||
'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 http = require('http') | ||
const { ioc } = require('@adonisjs/fold') | ||
const supertest = require('supertest') | ||
const { Config } = require('@adonisjs/sink') | ||
|
||
const Session = require('../src/Session') | ||
const SessionManager = require('../src/Session/Manager') | ||
const Middleware = require('../src/Session/Middleware') | ||
const { redis: Redis } = require('../src/Session/Drivers') | ||
|
||
test.group('Middleware', (group) => { | ||
group.beforeEach(() => { | ||
ioc.singleton('Adonis/Src/Config', () => { | ||
return new Config() | ||
}) | ||
|
||
ioc.singleton('Adonis/Addons/RedisFactory', () => { | ||
return class RedisFactory { | ||
expire () {} | ||
} | ||
}) | ||
}) | ||
|
||
test('attach session instance on the http context', async (assert) => { | ||
const server = http.createServer((req, res) => { | ||
const request = { | ||
cookie: () => {} | ||
} | ||
|
||
const response = { | ||
cookie: () => {} | ||
} | ||
|
||
const context = { request, response } | ||
const middleware = new Middleware(ioc.use('Adonis/Src/Config'), SessionManager) | ||
middleware | ||
.handle(context, async function () { | ||
return true | ||
}) | ||
.then(() => { | ||
assert.isDefined(context.session) | ||
assert.instanceOf(context.session, Session) | ||
res.end() | ||
}).catch(({ message }) => { | ||
console.log(message) | ||
res.writeHead(500) | ||
res.write(message) | ||
res.end() | ||
}) | ||
}) | ||
|
||
await supertest(server).get('/').expect(200) | ||
}) | ||
|
||
test('do not call setRequest when driver has not implemented the method', async (assert) => { | ||
ioc.singleton('Adonis/Src/Config', () => { | ||
const config = new Config() | ||
config.set('session.driver', 'redis') | ||
return config | ||
}) | ||
|
||
const server = http.createServer((req, res) => { | ||
const request = { | ||
cookie: () => {} | ||
} | ||
|
||
const response = { | ||
cookie: () => {} | ||
} | ||
|
||
const context = { request, response } | ||
const middleware = new Middleware(ioc.use('Adonis/Src/Config'), SessionManager) | ||
middleware | ||
.handle(context, async function () { | ||
return true | ||
}) | ||
.then(() => { | ||
assert.isDefined(context.session) | ||
assert.instanceOf(context.session._driverInstance, Redis) | ||
res.end() | ||
}).catch(({ message }) => { | ||
console.log(message) | ||
res.writeHead(500) | ||
res.write(message) | ||
res.end() | ||
}) | ||
}) | ||
|
||
await supertest(server).get('/').expect(200) | ||
}) | ||
}) |