-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
4ea5a63
commit 81906f7
Showing
4 changed files
with
83 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
const Koa = require('koa'); | ||
const request = require('supertest'); | ||
const assert = require('assert'); | ||
const session = require('..'); | ||
const store = require('./store'); | ||
const TOKEN_KEY = 'User-Token'; | ||
describe('Koa Session External Key', () => { | ||
describe('when the external key set/get is invalid', () => { | ||
it('should throw a error', () => { | ||
try { | ||
new App({ | ||
externalKey: {}, | ||
}); | ||
} catch (err) { | ||
assert.equal(err.message, 'externalKey.get must be function'); | ||
} | ||
}); | ||
}); | ||
describe('custom get/set external key', () => { | ||
it('should still work', done => { | ||
const app = App(); | ||
app.use(async function(ctx) { | ||
if (ctx.method === 'POST') { | ||
ctx.session.string = ';'; | ||
ctx.status = 204; | ||
} else { | ||
ctx.body = ctx.session.string; | ||
} | ||
}); | ||
const server = app.listen(); | ||
request(server) | ||
.post('/') | ||
.expect(204, (err, res) => { | ||
if (err) return done(err); | ||
const token = res.get(TOKEN_KEY); | ||
request(server) | ||
.get('/') | ||
.set(TOKEN_KEY, token) | ||
.expect(';', done); | ||
}); | ||
}); | ||
}); | ||
}); | ||
function App(options) { | ||
const app = new Koa(); | ||
app.keys = [ 'a', 'b' ]; | ||
options = options || {}; | ||
options.store = store; | ||
options.externalKey = options.externalKey || { | ||
get: ctx => ctx.get(TOKEN_KEY), | ||
set: (ctx, value) => ctx.set(TOKEN_KEY, value), | ||
}; | ||
app.use(session(options, app)); | ||
return app; | ||
} |