fix(helper/cookie): make default `Path=/` for `setCookie()`/`setSignedCookie()` (#1742)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(helper/cookie): make default `path` as `/` for `setCookie()`/`setSignedCookie()` * denoify * fixed test
authored by Yusuke Wada · committed by GitHub · parent c3275ee · Browse files · 4 files·+42 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+42 added−11 removed
- tests:+38 added−7 removed
- other:+4 added−4 removed
Provenance
- Commit
7dc2155- Tree
78964fb- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
c3275ee
4 files changed+42 added−11 removed
modified:deno_dist/helper/cookie/index.ts+2 added-2 removed
37 unmodified lines383940414242434445462 unmodified lines495051525353545556572 unmodified lines37 unmodified lines return obj as any}
export const setCookie = (c: Context, name: string, value: string, opt?: CookieOptions): void => { const cookie = serialize(name, value, opt) const cookie = serialize(name, value, { path: '/', ...opt }) c.header('set-cookie', cookie, { append: true })}
export const setSignedCookie = async (2 unmodified lines value: string, secret: string | BufferSource, opt?: CookieOptions): Promise<void> => { const cookie = await serializeSigned(name, value, secret, opt) const cookie = await serializeSigned(name, value, secret, { path: '/', ...opt }) c.header('set-cookie', cookie, { append: true })}
export const deleteCookie = (c: Context, name: string, opt?: CookieOptions): void => {2 unmodified linesmodified:runtime_tests/lambda/index.test.ts+2 added-2 removed
93 unmodified lines94 const testCookie1 = {95 key: 'id',96 value: crypto.randomUUID(),97 get serialized() {98- return `${this.key}=${this.value}`98+ return `${this.key}=${this.value}; Path=/`99 },100 }101 const testCookie2 = {102 key: 'secret',103 value: crypto.randomUUID(),104 get serialized() {105- return `${this.key}=${this.value}`105+ return `${this.key}=${this.value}; Path=/`106 },107 }108 109 app.post('/cookie', (c) => {440 unmodified linesmodified:src/helper/cookie/index.test.ts+36 added-5 removed
146 unmodified lines147 it('Set cookie with setCookie()', async () => {148 const res = await app.request('http://localhost/set-cookie')149 expect(res.status).toBe(200)150 const header = res.headers.get('Set-Cookie')151- expect(header).toBe('delicious_cookie=macha')151+ expect(header).toBe('delicious_cookie=macha; Path=/')152 })153 154+ app.get('/a/set-cookie-path', (c) => {155+ setCookie(c, 'delicious_cookie', 'macha', { path: '/a' })156+ return c.text('Give cookie')157+ })158+159+ it('Set cookie with setCookie() and path option', async () => {160+ const res = await app.request('http://localhost/a/set-cookie-path')161+ expect(res.status).toBe(200)162+ const header = res.headers.get('Set-Cookie')163+ expect(header).toBe('delicious_cookie=macha; Path=/a')164+ })165+166 app.get('/set-signed-cookie', async (c) => {167 const secret = 'secret chocolate chips'168 await setSignedCookie(c, 'delicious_cookie', 'macha', secret)169 return c.text('Give signed cookie')2 unmodified lines172 it('Set signed cookie with setSignedCookie()', async () => {173 const res = await app.request('http://localhost/set-signed-cookie')174 expect(res.status).toBe(200)175 const header = res.headers.get('Set-Cookie')164- expect(header).toBe('delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D')176+ expect(header).toBe(177+ 'delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D; Path=/'178+ )179 })180 181+ app.get('/a/set-signed-cookie-path', async (c) => {182+ const secret = 'secret chocolate chips'183+ await setSignedCookie(c, 'delicious_cookie', 'macha', secret, { path: '/a' })184+ return c.text('Give signed cookie')185+ })186+187+ it('Set signed cookie with setSignedCookie() and path option', async () => {188+ const res = await app.request('http://localhost/a/set-signed-cookie-path')189+ expect(res.status).toBe(200)190+ const header = res.headers.get('Set-Cookie')191+ expect(header).toBe(192+ 'delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D; Path=/a'193+ )194+ })195+196 app.get('/set-cookie-complex', (c) => {197 setCookie(c, 'great_cookie', 'banana', {198 path: '/',199 secure: true,47 unmodified lines247 it('Multiple values', async () => {248 const res = await app.request('http://localhost/set-cookie-multiple')249 expect(res.status).toBe(200)250 const header = res.headers.get('Set-Cookie')222- expect(header).toBe('delicious_cookie=macha, delicious_cookie=choco')251+ expect(header).toBe('delicious_cookie=macha; Path=/, delicious_cookie=choco; Path=/')252 })253 })254 255 describe('Delete cookie', () => {7 unmodified lines263 it('Delete cookie', async () => {264 const res2 = await app.request('http://localhost/delete-cookie')265 expect(res2.status).toBe(200)266 const header2 = res2.headers.get('Set-Cookie')238- expect(header2).toBe('delicious_cookie=; Max-Age=0')267+ expect(header2).toBe('delicious_cookie=; Max-Age=0; Path=/')268 })269 270 app.get('/delete-cookie-multiple', (c) => {271 deleteCookie(c, 'delicious_cookie')4 unmodified lines276 it('Delete multiple cookies', async () => {277 const res2 = await app.request('http://localhost/delete-cookie-multiple')278 expect(res2.status).toBe(200)279 const header2 = res2.headers.get('Set-Cookie')251- expect(header2).toBe('delicious_cookie=; Max-Age=0, delicious_cookie2=; Max-Age=0')280+ expect(header2).toBe(281+ 'delicious_cookie=; Max-Age=0; Path=/, delicious_cookie2=; Max-Age=0; Path=/'282+ )283 })284 285 app.get('/delete-cookie-with-options', (c) => {286 deleteCookie(c, 'delicious_cookie', {15 unmodified linesmodified:src/helper/cookie/index.ts+2 added-2 removed
37 unmodified lines383940414242434445462 unmodified lines495051525353545556572 unmodified lines37 unmodified lines return obj as any}
export const setCookie = (c: Context, name: string, value: string, opt?: CookieOptions): void => { const cookie = serialize(name, value, opt) const cookie = serialize(name, value, { path: '/', ...opt }) c.header('set-cookie', cookie, { append: true })}
export const setSignedCookie = async (2 unmodified lines value: string, secret: string | BufferSource, opt?: CookieOptions): Promise<void> => { const cookie = await serializeSigned(name, value, secret, opt) const cookie = await serializeSigned(name, value, secret, { path: '/', ...opt }) c.header('set-cookie', cookie, { append: true })}
export const deleteCookie = (c: Context, name: string, opt?: CookieOptions): void => {2 unmodified lines