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

Adds "ctx.cookie()" response transformer #103

Merged
merged 1 commit into from
Apr 18, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"@open-draft/until": "^1.0.0",
"chalk": "^3.0.0",
"cookie": "^0.4.0",
"graphql": "^14.6.0",
"node-match-path": "^0.3.1",
"ramda": "^0.27.0",
Expand All @@ -40,6 +41,7 @@
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@types/cookie": "^0.3.3",
"@types/jest": "^25.1.2",
"@types/node": "^13.7.7",
"@types/puppeteer": "^2.0.1",
Expand Down
24 changes: 24 additions & 0 deletions src/context/cookie.test.ts
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')
})
})
})
20 changes: 20 additions & 0 deletions src/context/cookie.ts
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
}
}
5 changes: 3 additions & 2 deletions src/context/index.ts
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'
2 changes: 2 additions & 0 deletions src/handlers/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RequestHandler, ResponseResolver } from './requestHandler'
import { Mask } from '../composeMocks'
import { set } from '../context/set'
import { status } from '../context/status'
import { cookie } from '../context/cookie'
import { body } from '../context/body'
import { text } from '../context/text'
import { json } from '../context/json'
Expand All @@ -23,6 +24,7 @@ export enum RESTMethods {
export const restContext = {
set,
status,
cookie,
body,
text,
json,
Expand Down
14 changes: 14 additions & 0 deletions test/rest-api/cookies.mocks.ts
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()
51 changes: 51 additions & 0 deletions test/rest-api/cookies.test.ts
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')
})
})
})
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,11 @@
dependencies:
"@types/node" "*"

"@types/cookie@^0.3.3":
version "0.3.3"
resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.3.3.tgz#85bc74ba782fb7aa3a514d11767832b0e3bc6803"
integrity sha512-LKVP3cgXBT9RYj+t+9FDKwS5tdI+rPBXaNSkma7hvqy35lc7mAokC2zsqWJH0LaqIt3B962nuYI77hsJoT1gow==

"@types/events@*":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"
Expand Down Expand Up @@ -2522,7 +2527,7 @@ cookie-signature@1.0.6:
resolved "https://registry.yarnpkg.com/cookie-signature/-/cookie-signature-1.0.6.tgz#e303a882b342cc3ee8ca513a79999734dab3ae2c"
integrity sha1-4wOogrNCzD7oylE6eZmXNNqzriw=

cookie@0.4.0:
cookie@0.4.0, cookie@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba"
integrity sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==
Expand Down