Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #460 from sveltejs/cookies
Browse files Browse the repository at this point in the history
more robust cookies
  • Loading branch information
Rich-Harris authored Oct 2, 2018
2 parents 9773781 + 709c999 commit 954bcba
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
28 changes: 15 additions & 13 deletions templates/src/server/middleware/get_page_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,21 @@ export function get_page_handler(
if (include_cookies) {
if (!opts.headers) opts.headers = {};

const str = []
.concat(
cookie.parse(req.headers.cookie || ''),
cookie.parse(opts.headers.cookie || ''),
cookie.parse(res.getHeader('Set-Cookie') || '')
)
.map(cookie => {
return Object.keys(cookie)
.map(name => `${name}=${encodeURIComponent(cookie[name])}`)
.join('; ');
})
.filter(Boolean)
.join(', ');
const cookies = Object.assign(
{},
cookie.parse(req.headers.cookie || ''),
cookie.parse(opts.headers.cookie || '')
);

const set_cookie = res.getHeader('Set-Cookie');
(Array.isArray(set_cookie) ? set_cookie : [set_cookie]).forEach(str => {
const match = /([^=]+)=([^;]+)/.exec(<string>str);
if (match) cookies[match[1]] = match[2];
});

const str = Object.keys(cookies)
.map(key => `${key}=${cookies[key]}`)
.join('; ');

opts.headers.cookie = str;
}
Expand Down
15 changes: 5 additions & 10 deletions test/app/src/routes/credentials/test.json.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import cookie from 'cookie';

export function get(req, res) {
const cookies = req.headers.cookie
? req.headers.cookie.split(/,\s+/).reduce((cookies, cookie) => {
const [pair] = cookie.split('; ');
const [name, value] = pair.split('=');
cookies[name] = value;
return cookies;
}, {})
: {};
if (req.headers.cookie) {
const cookies = cookie.parse(req.headers.cookie);

if (cookies.test) {
res.writeHead(200, {
'Content-Type': 'application/json'
});

res.end(JSON.stringify({
message: cookies.test
message: `a: ${cookies.a}, b: ${cookies.b}, max-age: ${cookies['max-age']}`
}));
} else {
res.writeHead(403, {
Expand Down
2 changes: 1 addition & 1 deletion test/app/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const middlewares = [

// set test cookie
(req, res, next) => {
res.setHeader('Set-Cookie', 'test=woohoo!; Max-Age=3600');
res.setHeader('Set-Cookie', ['a=1; Path=/', 'b=2; Path=/']);
next();
},

Expand Down
4 changes: 2 additions & 2 deletions test/common/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ function run({ mode, basepath = '' }) {
return nightmare.goto(`${base}/credentials?creds=include`)
.page.title()
.then(title => {
assert.equal(title, 'woohoo!');
assert.equal(title, 'a: 1, b: 2, max-age: undefined');
});
});

Expand All @@ -641,7 +641,7 @@ function run({ mode, basepath = '' }) {
.wait(100)
.page.title()
.then(title => {
assert.equal(title, 'woohoo!');
assert.equal(title, 'a: 1, b: 2, max-age: undefined');
});
});

Expand Down

0 comments on commit 954bcba

Please sign in to comment.