-
-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds "ctx.cookie()" response transformer
- Loading branch information
1 parent
e498f8f
commit ca18b75
Showing
8 changed files
with
122 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import cookieUtils from 'cookie' | ||
import { cookie } from './cookie' | ||
import { response } from '../response' | ||
|
||
describe('cookie', () => { | ||
describe('given I set a cookie', () => { | ||
let result: ReturnType<typeof response> | ||
|
||
beforeAll(() => { | ||
result = response(cookie('my-cookie', 'arbitrary-value')) | ||
}) | ||
|
||
it('should be set on the response headers', () => { | ||
expect(result.headers.get('set-cookie')).toEqual( | ||
'my-cookie=arbitrary-value', | ||
) | ||
}) | ||
|
||
it('should set the cookie on "document.cookie"', () => { | ||
const allCookies = cookieUtils.parse(document.cookie) | ||
expect(allCookies).toHaveProperty('my-cookie', 'arbitrary-value') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import cookieUtils, { CookieSerializeOptions } from 'cookie' | ||
import { ResponseTransformer } from '../response' | ||
|
||
/** | ||
* Sets a given cookie on the response. | ||
* @example | ||
* res(cookie('name', 'value')) | ||
*/ | ||
export const cookie = ( | ||
name: string, | ||
value: string, | ||
options?: CookieSerializeOptions, | ||
): ResponseTransformer => { | ||
return (res) => { | ||
const serializedCookie = cookieUtils.serialize(name, value, options) | ||
res.headers.set('Set-Cookie', serializedCookie) | ||
document.cookie = serializedCookie | ||
return res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
export { status } from './status' | ||
export { set } from './set' | ||
export { cookie } from './cookie' | ||
export { body } from './body' | ||
export { data } from './data' | ||
export { delay } from './delay' | ||
export { errors } from './errors' | ||
export { fetch } from './fetch' | ||
export { json } from './json' | ||
export { set } from './set' | ||
export { status } from './status' | ||
export { text } from './text' | ||
export { xml } from './xml' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { composeMocks, rest } from 'msw' | ||
|
||
const { start } = composeMocks( | ||
rest.get('/user', (req, res, ctx) => { | ||
return res( | ||
ctx.cookie('my-cookie', 'value'), | ||
ctx.json({ | ||
mocked: true, | ||
}), | ||
) | ||
}), | ||
) | ||
|
||
start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as path from 'path' | ||
import * as cookieUtils from 'cookie' | ||
import { Response } from 'puppeteer' | ||
import { TestAPI, runBrowserWith } from '../support/runBrowserWith' | ||
|
||
describe('REST: Cookies', () => { | ||
let test: TestAPI | ||
|
||
beforeAll(async () => { | ||
test = await runBrowserWith(path.resolve(__dirname, 'cookies.mocks.ts')) | ||
}) | ||
|
||
afterAll(() => { | ||
return test.cleanup() | ||
}) | ||
|
||
describe('given I set cookies on the mocked response', () => { | ||
let res: Response | ||
|
||
beforeAll(async () => { | ||
res = await test.request({ | ||
url: `${test.origin}/user`, | ||
}) | ||
}) | ||
|
||
it('should return the mocked response', async () => { | ||
const headers = res.headers() | ||
const body = await res.json() | ||
|
||
expect(headers).toHaveProperty('x-powered-by', 'msw') | ||
expect(body).toEqual({ | ||
mocked: true, | ||
}) | ||
}) | ||
|
||
it('should not have "Set-Cookie" header on the mocked response', () => { | ||
const headers = res.headers() | ||
|
||
expect(headers).not.toHaveProperty('set-cookie') | ||
}) | ||
|
||
it('should be able to access response cookies via "document.cookie"', async () => { | ||
const cookieString = await test.page.evaluate(() => { | ||
return document.cookie | ||
}) | ||
const allCookies = cookieUtils.parse(cookieString) | ||
|
||
expect(allCookies).toHaveProperty('my-cookie', 'value') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters