Skip to content

Commit

Permalink
Merge 7627347 into 5e41b47
Browse files Browse the repository at this point in the history
  • Loading branch information
sodawy committed Jan 6, 2018
2 parents 5e41b47 + 7627347 commit 0b6f90f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class ContextSession {
} catch (err) {
// backwards compatibility:
// create a new session if parsing fails.
// new Buffer(string, 'base64') does not seem to crash
// Buffer.alloc(size, string, 'base64') does not seem to crash
// when `string` is not base64-encoded.
// but `JSON.parse(string)` will crash.
debug('decode %j error: %s', cookie, err);
Expand Down
2 changes: 1 addition & 1 deletion lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Session {
}

/**
* Return how many values there are in the session object.
* Return how many keys there are in the session object.
* Used to see if it's "populated".
*
* @return {Number}
Expand Down
4 changes: 2 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
*/

decode(string) {
const body = new Buffer(string, 'base64').toString('utf8');
const body = Buffer.alloc(Buffer.byteLength(string, 'base64'), string, 'base64').toString('utf8');
const json = JSON.parse(body);
return json;
},
Expand All @@ -28,7 +28,7 @@ module.exports = {

encode(body) {
body = JSON.stringify(body);
return new Buffer(body).toString('base64');
return Buffer.alloc(Buffer.byteLength(body), body).toString('base64');
},

hash(sess) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"license": "MIT",
"dependencies": {
"crc": "^3.4.4",
"debug": "^2.2.0",
"debug": "^2.6.9",
"is-type-of": "^1.0.0",
"uid-safe": "^2.1.3"
},
Expand Down
31 changes: 30 additions & 1 deletion test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ describe('Koa Session Cookie', () => {
});

describe('.length', () => {
it('should return session length', done => {
it('should return session`s keys length', done => {
const app = App();

app.use(async function(ctx) {
Expand Down Expand Up @@ -464,6 +464,35 @@ describe('Koa Session Cookie', () => {
.expect(500, done);
});
});

describe('contains multibyte character', () => {
it('should still work', done => {
const app = App();
const MULTIBYTE_CHAR = '€';

app.use(async function(ctx) {
if (ctx.method === 'POST') {
ctx.session.string = MULTIBYTE_CHAR;
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 cookie = res.headers['set-cookie'];
request(server)
.get('/')
.set('Cookie', cookie.join(';'))
.expect(MULTIBYTE_CHAR, done);
});
});
});
});

describe('when an error is thrown downstream and caught upstream', () => {
Expand Down

0 comments on commit 0b6f90f

Please sign in to comment.