Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Request Cookies Type to Add undefined #190

Merged
merged 3 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ declare module 'fastify' {
/**
* Request cookies
*/
cookies: { [cookieName: string]: string };
cookies: { [cookieName: string]: string | undefined };

/**
* Unsigns the specified cookie using the secret provided.
Expand Down
55 changes: 55 additions & 0 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,61 @@ test('parses incoming cookies', (t) => {
})
})

test('defined and undefined cookies', (t) => {
t.plan(23)
const fastify = Fastify()
fastify.register(plugin)

// check that it parses the cookies in the onRequest hook
for (const hook of ['preValidation', 'preHandler']) {
fastify.addHook(hook, (req, reply, done) => {
t.ok(req.cookies)

t.ok(req.cookies.bar)
t.notOk(req.cookies.baz)

t.equal(req.cookies.bar, 'bar')
t.equal(req.cookies.baz, undefined)
done()
})
}

fastify.addHook('preParsing', (req, reply, payload, done) => {
t.ok(req.cookies)

t.ok(req.cookies.bar)
t.notOk(req.cookies.baz)

t.equal(req.cookies.bar, 'bar')
t.equal(req.cookies.baz, undefined)
done()
})

fastify.get('/test2', (req, reply) => {
t.ok(req.cookies)

t.ok(req.cookies.bar)
t.notOk(req.cookies.baz)

t.equal(req.cookies.bar, 'bar')
t.equal(req.cookies.baz, undefined)

reply.send({ hello: 'world' })
})

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

test('does not modify supplied cookie options object', (t) => {
t.plan(3)
const expireDate = Date.now() + 1000
Expand Down
27 changes: 14 additions & 13 deletions test/plugin.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ server.after((_err) => {

server.get('/', (request, reply) => {
const test = request.cookies.test;
expectType<string | undefined>(test);

expectType<setCookieWrapper>(reply.cookie);
expectType<setCookieWrapper>(reply.setCookie);

expectType<FastifyReply>(
reply
.setCookie('test', test, { domain: 'example.com', path: '/' })
.setCookie('test', test!, { domain: 'example.com', path: '/' })
.clearCookie('foo')
.send({ hello: 'world' })
);
Expand All @@ -63,7 +64,7 @@ serverWithHttp2.after(() => {
serverWithHttp2.get('/', (request, reply) => {
const test = request.cookies.test;
reply
.setCookie('test', test, { domain: 'example.com', path: '/' })
.setCookie('test', test!, { domain: 'example.com', path: '/' })
.clearCookie('foo')
.send({ hello: 'world' });
});
Expand All @@ -75,26 +76,26 @@ testSamesiteOptionsApp.register(cookie);
testSamesiteOptionsApp.after(() => {
server.get('/test-samesite-option-true', (request, reply) => {
const test = request.cookies.test;
reply.setCookie('test', test, { sameSite: true }).send({ hello: 'world' });
reply.setCookie('test', test!, { sameSite: true }).send({ hello: 'world' });
});
server.get('/test-samesite-option-false', (request, reply) => {
const test = request.cookies.test;
reply.setCookie('test', test, { sameSite: false }).send({ hello: 'world' });
reply.setCookie('test', test!, { sameSite: false }).send({ hello: 'world' });
});
server.get('/test-samesite-option-lax', (request, reply) => {
const test = request.cookies.test;
reply.setCookie('test', test, { sameSite: 'lax' }).send({ hello: 'world' });
reply.setCookie('test', test!, { sameSite: 'lax' }).send({ hello: 'world' });
});
server.get('/test-samesite-option-strict', (request, reply) => {
const test = request.cookies.test;
reply
.setCookie('test', test, { sameSite: 'strict' })
.setCookie('test', test!, { sameSite: 'strict' })
.send({ hello: 'world' });
});
server.get('/test-samesite-option-none', (request, reply) => {
const test = request.cookies.test;
reply
.setCookie('test', test, { sameSite: 'none' })
.setCookie('test', test!, { sameSite: 'none' })
.send({ hello: 'world' });
});
});
Expand All @@ -106,13 +107,13 @@ appWithImplicitHttpSigned.register(cookie, {
});
appWithImplicitHttpSigned.after(() => {
server.get('/', (request, reply) => {
appWithImplicitHttpSigned.unsignCookie(request.cookies.test);
appWithImplicitHttpSigned.unsignCookie(request.cookies.test!);
appWithImplicitHttpSigned.unsignCookie('test');

reply.unsignCookie(request.cookies.test);
reply.unsignCookie(request.cookies.test!);
reply.unsignCookie('test');

request.unsignCookie(request.cookies.anotherTest);
request.unsignCookie(request.cookies.anotherTest!);
request.unsignCookie('anotherTest');

reply.send({ hello: 'world' });
Expand All @@ -126,7 +127,7 @@ appWithRotationSecret.register(cookie, {
});
appWithRotationSecret.after(() => {
server.get('/', (request, reply) => {
reply.unsignCookie(request.cookies.test);
reply.unsignCookie(request.cookies.test!);
const { valid, renew, value } = reply.unsignCookie('test');

expectType<boolean>(valid);
Expand Down Expand Up @@ -158,7 +159,7 @@ appWithParseOptions.register(cookie, {
});
appWithParseOptions.after(() => {
server.get('/', (request, reply) => {
const { valid, renew, value } = reply.unsignCookie(request.cookies.test);
const { valid, renew, value } = reply.unsignCookie(request.cookies.test!);

expectType<boolean>(valid);
expectType<boolean>(renew);
Expand All @@ -179,7 +180,7 @@ appWithCustomSigner.register(cookie, {
})
appWithCustomSigner.after(() => {
server.get('/', (request, reply) => {
reply.unsignCookie(request.cookies.test)
reply.unsignCookie(request.cookies.test!)
const { valid, renew, value } = reply.unsignCookie('test')

expectType<boolean>(valid)
Expand Down