Skip to content
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
14 changes: 13 additions & 1 deletion packages/dynamock/test/bin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('bin integration tests', () => {

for (let i = 0; i < testData.length; i++) {
const { action, expectation } = testData[i]
const { path, method, headers, body, bodyJSON, query } = action
const { path, method, headers, cookies, body, bodyJSON, query } = action
const fetchOptions: {
method: string
headers: { [key: string]: string }
Expand All @@ -53,6 +53,18 @@ describe('bin integration tests', () => {
url.searchParams.set(queryKey, query[queryKey])
}

// nodejs fetch method "patch" is working only in upper case
// CF: https://github.com/nodejs/node/issues/51336
if (fetchOptions.method === 'patch') {
fetchOptions.method = 'PATCH'
}

if (cookies) {
fetchOptions.headers.cookie = Object.entries(cookies)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('; ')
}

const result = await fetch(url.toString(), fetchOptions)

if (expectation) {
Expand Down
14 changes: 8 additions & 6 deletions packages/dynamock/test/config/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ const isPortUsed = async (port: number): Promise<boolean> => {
return new Promise((resolve) => {
const server = createServer()
server.once('error', (err: Error) => {
server.close()
resolve('code' in err && err.code === 'EADDRINUSE')
server.close(() => {
resolve('code' in err && err.code === 'EADDRINUSE')
})
})
server.once('listening', () => {
server.close()
resolve(false)
server.close(() => {
resolve(false)
})
})
server.listen(port)
})
}

export function waitPortUsed(port: number, reverse = false, retry = 50) {
export function waitPortUsed(port: number, retry = 50, reverse = false) {
return new Promise((resolve) => {
const loop = async () => {
const isUsed = await isPortUsed(port)
Expand All @@ -35,5 +37,5 @@ export function waitPortUsed(port: number, reverse = false, retry = 50) {
}

export function waitPortFree(port: number, retry?: number) {
return waitPortUsed(port, true, retry)
return waitPortUsed(port, retry, true)
}
187 changes: 0 additions & 187 deletions packages/dynamock/test/integrations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,193 +167,6 @@ describe('integrations.js', () => {
})
})

describe('matching method', () => {
test.each([
['get', null, 'get', true],
['post', null, 'post', true],
['*', null, 'get', true],
['*', null, 'post', true],
['/(get|post)/', { allowRegex: true }, 'get', true],
['/(get|post)/', { allowRegex: true }, 'post', true],
])(
'matching method match="%s" options="%o" test="%s" result=%s',
async (matchMethod, options, method, shouldMatch) => {
const path = '/test'

await request
.post('/___fixtures')
.send({
request: {
path,
method: matchMethod,
...(options
? {
options: {
method: options,
},
}
: {}),
},
response: {
body: '',
},
})
.expect(201)

await request[method as Method](path).expect(shouldMatch ? 200 : 404)
},
)
})

describe('matching headers', () => {
test.each([
[null, { a: 'a' }, null, { a: 'a' }, true],
[null, { a: 'a' }, null, { A: 'a' }, true],
[null, { A: 'a' }, null, { a: 'a' }, true],
[null, { a: 1 }, null, { a: '1' }, true],
[null, { a: [] }, null, { a: '[]' }, true],
[null, { a: { b: 'b' } }, null, { a: '{"b":"b"}' }, true],
[null, { a: null }, null, { a: 'null' }, true],
[null, { a: 'a' }, { allowRegex: true }, { a: 'a' }, true],
[null, { a: '/a/' }, { allowRegex: true }, { a: 'a' }, true],
[null, { a: '/A/' }, { allowRegex: true }, { a: 'a' }, false],
[null, { a: '/A/ig' }, { allowRegex: true }, { a: 'a' }, true],
[null, { a: 'a' }, null, { a: 'a', b: 'b' }, true],
[null, { a: '/a/' }, { allowRegex: true }, { a: 'a', b: 'b' }, true],
[null, { a: 'a', b: 'b' }, null, { a: 'a' }, false],
[null, { a: '/a/', b: 'b' }, { allowRegex: true }, { a: 'a' }, false],
[{ custom: { a: 'a' } }, ['custom'], null, {}, false],
[{ custom: { a: '/a/' } }, ['custom'], { allowRegex: true }, {}, false],
[{ custom: { a: 'a' } }, ['custom'], null, { a: 'a' }, true],
[{ custom: { a: '/a/' } }, ['custom'], { allowRegex: true }, { a: 'a' }, true],
[{ custom: { a: 'a', b: 'b' } }, ['custom'], null, { a: 'a' }, false],
[{ custom: { a: '/a/', b: '/b/' } }, ['custom'], { allowRegex: true }, { a: 'a' }, false],
[{ custom: { a: 'a', b: 'b' } }, ['custom'], null, { a: 'a', b: 'b' }, true],
[{ a: { a: 'a' }, b: { b: 'b' } }, ['a', 'b'], null, { a: 'a' }, false],
[{ a: { a: 'a' }, b: { b: 'b' } }, ['a', 'b'], null, { a: 'a', b: 'b' }, true],
])(
'match headers config="%o" match="%o" options="%o" result=%s',
async (configuration, matchValues, options, values, shouldMatch) => {
const path = '/test'
const method = 'get'

if (configuration) {
await request
.put('/___config')
.send({
headers: configuration,
})
.expect(200)
}

await request
.post('/___fixtures')
.send({
request: {
path,
method,
headers: matchValues,
...(options
? {
options: {
headers: options,
},
}
: {}),
},
response: {
body: '',
},
})
.expect(201)

await request[method](path)
.set(values)
.expect(shouldMatch ? 200 : 404)
},
)
})

describe('matching cookies', () => {
test.each([
[null, { x: 'x' }, { x: 'x' }, null, true],
[null, { x: 1 }, { x: '1' }, null, true],
[null, { x: [] }, { x: '[]' }, null, true],
[null, { x: { y: 'y' } }, { x: '{"y":"y"}' }, null, true],
[null, { x: 'x' }, { x: 'x' }, { strict: false }, true],
[null, { x: 'x' }, { x: 'x' }, { allowRegex: true }, true],
[null, { x: '/x/' }, { x: 'x' }, { allowRegex: true }, true],
[null, { x: 'x' }, { x: 'x' }, { strict: true }, true],
[null, { x: 'x' }, { x: 'x', y: 'y' }, { strict: true }, false],
[null, { x: 'x', y: 'y' }, { x: 'x' }, { strict: true }, false],
[null, { x: 'x' }, { x: 'x', other: 'other' }, null, true],
[null, { x: '/x/' }, { x: 'x', other: 'other' }, { allowRegex: true }, true],
[null, { x: 'x' }, { x: 'x', other: 'other' }, { strict: true }, false],
[null, { x: 'x', other: 'other' }, { x: 'x' }, null, false],
[null, { x: '/x/', other: 'other' }, { x: 'x' }, { allowRegex: true }, false],
[{ xOnly: { x: 'x' } }, ['xOnly'], {}, null, false],
[{ xOnly: { x: '/x/' } }, ['xOnly'], {}, { allowRegex: true }, false],
[{ xOnly: { x: 'x' } }, ['xOnly'], { x: 'x' }, null, true],
[{ xOnly: { x: '/X/i' } }, ['xOnly'], { x: 'x' }, { allowRegex: true }, true],
[{ xOnly: { x: 'x' } }, ['xOnly'], { x: 'x' }, { strict: true }, true],
[{ xAndY: { x: 'x', y: 'y' } }, ['xAndY'], { x: 'x' }, null, false],
[{ xAndY: { x: 'x', y: 'y' } }, ['xAndY'], { x: 'x', y: 'y' }, null, true],
[{ xAndY: { x: '/X/ig', y: '/Y/ig' } }, ['xAndY'], { x: 'x', y: 'y' }, { allowRegex: true }, true],
[{ xAndY: { x: 'x', y: 'y' } }, ['xAndY'], { x: 'x', y: 'y' }, { strict: true }, true],
[{ xOnly: { x: 'x' }, yOnly: { y: 'y' } }, ['xOnly', 'yOnly'], { x: 'x' }, null, false],
[{ xOnly: { x: 'x' }, yOnly: { y: 'y' } }, ['xOnly', 'yOnly'], { x: 'x', y: 'y' }, null, true],
[{ xOnly: { x: 'x' } }, ['xOnly', { y: 'y' }], { x: 'x', y: 'y' }, null, true],
])(
'match cookies config=%o match=%o request=%o result=%s',
async (configuration, matchValues, values, options, shouldMatch) => {
const path = '/test'
const method = 'get'

if (configuration) {
await request
.put('/___config')
.send({
cookies: configuration,
})
.expect(200)
}

await request
.post('/___fixtures')
.send({
request: {
path,
method,
cookies: matchValues,
...(options
? {
options: {
cookies: options,
},
}
: {}),
},
response: {
body: '',
},
})
.expect(201)

const cookies = Object.entries(values)
.reduce((acc, [key, value]) => {
// @ts-ignore
acc.push(`${key}=${value}`)
return acc
}, [])
.join(';')

await request[method](path)
.set('Cookie', cookies)
.expect(shouldMatch ? 200 : 404)
},
)
})

describe('matching query', () => {
test.each([
[null, '/test', { x: '1' }, '/test', null, false],
Expand Down
15 changes: 13 additions & 2 deletions packages/puppeteer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,24 @@ function mapToCoreRequest(request: HTTPRequest): CoreRequest {

const parsedUrl = new URL(request.url())

const { cookie: cookieHeader, ...headersWithoutCookie } = headers

const cookies = cookieHeader
? Object.fromEntries(
cookieHeader.split('; ').map((cookie) => {
const [key, value] = cookie.split('=')
return [decodeURIComponent(key), decodeURIComponent(value)]
}),
)
: {}

return {
origin: parsedUrl.origin,
path: parsedUrl.pathname,
method: request.method(),
body,
headers: request.headers(),
cookies: {},
headers: headersWithoutCookie,
cookies,
query: Object.entries(parsedUrl.searchParams).reduce<{ [key in string]: string }>((acc, [key, value]) => {
if (value) {
if (typeof value === 'string') {
Expand Down
1 change: 1 addition & 0 deletions packages/puppeteer/test/config/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ beforeEach(async () => {
})

afterEach(async () => {
await page.deleteCookie(...(await page.cookies()))
await page.close()
})

Expand Down
9 changes: 7 additions & 2 deletions packages/puppeteer/test/puppeteer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ActionEnum, getTestFiles, wrapError } from '@dynamock/test-cases'
import { getPuppeteerTestCases } from './config/getTestCases.js'

describe('puppeteer integration tests', () => {
const allTests = getTestFiles().filter(([filePath]) => filePath.endsWith('.yml'))
const allTests = getTestFiles() //.filter(([filePath]) => filePath.endsWith('create-and-delete-bulk.yml'))

beforeEach(() => page.goto('http://127.0.0.1:3000/index.html'))

Expand Down Expand Up @@ -43,10 +43,14 @@ describe('puppeteer integration tests', () => {
break
}
case ActionEnum.test_fixture: {
const { path, method, body, bodyJSON, headers, query } = action.data
const { path, method, body, bodyJSON, headers, cookies, query } = action.data
const safeHeaders = headers ?? {}
const safeQuery = query ?? {}

if (cookies) {
await page.setCookie(...Object.entries(cookies ?? {}).map(([name, value]) => ({ name, value })))
}

const result = await page.evaluate(
async (_path, _method, _body, _bodyJSON, _headers, _query) => {
const fetchOptions: {
Expand Down Expand Up @@ -75,6 +79,7 @@ describe('puppeteer integration tests', () => {
url.searchParams.set(queryKey, _query[queryKey])
}

console.log('pup test fetch', url.toString(), fetchOptions)
const result = await fetch(url.toString(), fetchOptions)

const bodyText = await result.text()
Expand Down
9 changes: 1 addition & 8 deletions packages/test-cases/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type ApiTest = {
path: string
method: string
headers?: { [key: string]: string }
cookies?: { [key: string]: string }
query?: { [key: string]: string }
body?: null | string
bodyJSON?: unknown
Expand Down Expand Up @@ -121,11 +122,3 @@ export async function wrapError(testIndex: number, task: () => unknown) {
throw error
}
}

export function fetchHeadersToObject(headers: HeadersInit) {
return Object.entries(headers).reduce<{ [key: string]: string }>((acc, [key, value]) => {
acc[key] = value

return acc
}, {})
}
Loading
Loading