Skip to content

Commit

Permalink
Merge branch 'release/1.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 22, 2017
2 parents 6f2ee95 + 009ba71 commit 218598e
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<a name="1.0.4"></a>
## [1.0.4](https://github.com/adonisjs/adonis-session/compare/v1.0.3...v1.0.4) (2017-07-22)



<a name="1.0.3"></a>
## [1.0.3](https://github.com/adonisjs/adonis-session/compare/v1.0.2...v1.0.3) (2017-07-22)

Expand Down
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adonisjs/session",
"version": "1.0.3",
"version": "1.0.4",
"description": "This repo is the official session provider for Adonisjs apps. It supports multiple drivers to store session data.",
"main": "providers/SessionProvider",
"directories": {
Expand Down Expand Up @@ -42,6 +42,7 @@
"fs-extra": "^4.0.0",
"lodash": "^4.17.4",
"ms": "^2.0.0",
"node-exceptions": "^2.0.2",
"type-of-is": "^3.5.1",
"uuid": "^3.1.0"
},
Expand Down
20 changes: 20 additions & 0 deletions src/Exceptions/index.js
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 }
5 changes: 3 additions & 2 deletions src/Session/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
* file that was distributed with this source code.
*/

const drivers = require('./Drivers')
const { ioc } = require('@adonisjs/fold')
const drivers = require('./Drivers')
const CE = require('../Exceptions')

/**
* Session manager class is used by ioc container
Expand Down Expand Up @@ -54,7 +55,7 @@ class SessionManager {
makeDriverInstance (name) {
const driver = drivers[name] || this._drivers[name]
if (!driver) {
throw new Error(`${name} is not valid session driver`)
throw CE.InvalidArgumentException.invalidSessionDriver(name)
}
return ioc.make(driver)
}
Expand Down
5 changes: 4 additions & 1 deletion src/Session/Middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ class SessionMiddleware {
debug('using %s session driver', driver)

const driverInstance = this.SessionManager.makeDriverInstance(driver)
driverInstance.setRequest(ctx.request, ctx.response)

if (typeof (driverInstance.setRequest) === 'function') {
driverInstance.setRequest(ctx.request, ctx.response)
}

ctx.session = new Session(ctx.request, ctx.response, driverInstance, this.Config)

Expand Down
61 changes: 61 additions & 0 deletions test/session-manager.spec.js
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')
})
})
103 changes: 103 additions & 0 deletions test/session-middleware.spec.js
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)
})
})

0 comments on commit 218598e

Please sign in to comment.