Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add partioned
  • Loading branch information
gurgunday committed Nov 8, 2023
commit 0d14fb85fc437e4648c76adef9d4e5bb879b932b
6 changes: 5 additions & 1 deletion cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function parse (str, options) {
const key = str.substring(pos, eqIdx++).trim()

// only assign once
if (undefined === result[key]) {
if (result[key] === undefined) {
const val = (str.charCodeAt(eqIdx) === 0x22)
? str.substring(eqIdx + 1, terminatorPos - 1).trim()
: str.substring(eqIdx, terminatorPos).trim()
Expand Down Expand Up @@ -183,6 +183,10 @@ function serialize (name, val, options) {
str += '; Secure'
}

if (opt.partitioned) {
str += '; Partitioned'
}

if (opt.sameSite) {
const sameSite = typeof opt.sameSite === 'string'
? opt.sameSite.toLowerCase()
Expand Down
37 changes: 37 additions & 0 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,43 @@ test('should set multiple cookies', (t) => {
})
})

test('should set multiple cookies', (t) => {
t.plan(11)
const fastify = Fastify()
fastify.register(plugin)

fastify.get('/', (req, reply) => {
reply
.setCookie('foo', 'foo')
.cookie('bar', 'test')
.setCookie('wee', 'woo', {
partitioned: true,
secure: true
})
.send({ hello: 'world' })
})

fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.same(JSON.parse(res.body), { hello: 'world' })

const cookies = res.cookies
t.equal(cookies.length, 3)
t.equal(cookies[0].name, 'foo')
t.equal(cookies[0].value, 'foo')
t.equal(cookies[1].name, 'bar')
t.equal(cookies[1].value, 'test')
t.equal(cookies[2].name, 'wee')
t.equal(cookies[2].value, 'woo')

t.equal(res.headers['set-cookie'][2], 'wee=woo; Secure; Partitioned')
})
})

test('cookies get set correctly with millisecond dates', (t) => {
t.plan(8)
const fastify = Fastify()
Expand Down
2 changes: 1 addition & 1 deletion types/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ declare namespace fastifyCookie {
httpOnly?: boolean;
/** A `number` in seconds that specifies the `Expires` attribute by adding the specified seconds to the current date. If both `expires` and `maxAge` are set, then `expires` is used. */
maxAge?: number;
partitioned?: boolean;
/** The `Path` attribute. Defaults to `/` (the root path). */
path?: string;
priority?: "low" | "medium" | "high";
/** A `boolean` or one of the `SameSite` string attributes. E.g.: `lax`, `none` or `strict`. */
sameSite?: 'lax' | 'none' | 'strict' | boolean;
/** The `boolean` value of the `Secure` attribute. Set this option to false when communicating over an unencrypted (HTTP) connection. Value can be set to `auto`; in this case the `Secure` attribute will be set to false for HTTP request, in case of HTTPS it will be set to true. Defaults to true. */
Expand Down
1 change: 1 addition & 0 deletions types/plugin.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ const parseOptions: fastifyCookieStar.CookieSerializeOptions = {
sameSite: 'lax',
secure: true,
signed: true,
partitioned: false,
};
expectType<fastifyCookieStar.CookieSerializeOptions>(parseOptions);

Expand Down