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
46 changes: 0 additions & 46 deletions packages/dynamock/test/integrations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@ describe('integrations.js', () => {
server.close(done)
})

describe('manipulate configuration', () => {
test.each([
// invalid
[[], false],
])('validate config="%o" response="%o" options="%o" isValid=%s', (config, isValid) => {
return request
.put('/___config')
.send(config)
.expect(isValid ? 200 : 400)
})
})

describe('create and delete fixtures', () => {
// Special case:
// We need the same supertest/server port in order to reproduce the sha1 of a fixture
Expand Down Expand Up @@ -169,24 +157,6 @@ describe('integrations.js', () => {

describe('matching query', () => {
test.each([
[null, '/test', { x: '1' }, '/test', null, false],
[null, '/test', { x: '1' }, '/test', { strict: false }, false],
[null, '/test', { x: '1' }, '/test', { allowRegex: true }, false],
[null, '/test', { x: '1' }, '/test', { strict: true }, false],
[null, '/test', { x: '1' }, '/test?x=1', null, true],
[null, '/test', { x: ' 1' }, '/test?x=%201', null, true],
[null, '/test', { x: ' 1' }, '/test?x= 1', null, true],
[null, '/test', { x: '%201' }, '/test?x=%201', null, false],
[null, '/test', { x: 1 }, '/test?x=1', null, true],
[null, '/test', { x: [] }, '/test?x=[]', null, true],
[null, '/test', { x: { y: 'y' } }, '/test?x={"y":"y"}', null, true],
[null, '/test', { x: '1' }, '/test?x=1', { strict: true }, true],
[null, '/test', { x: '1' }, '/test?x=1', { allowRegex: true }, true],
[null, '/test', { x: '/\\d+/' }, '/test?x=1', { allowRegex: true }, true],
[null, '/test', { x: '/\\d+/' }, '/test?x=1', null, false],
[null, '/test', { x: '1' }, '/test?x=2', null, false],
[null, '/test', { x: '1' }, '/test?x=2', { allowRegex: true }, false],
[null, '/test', { x: '1' }, '/test?x=2', { strict: true }, false],
[null, '/test?x=1', {}, '/test?x=1', null, true],
[null, '/test?x=1', {}, '/test?x=1', { allowRegex: true }, true],
[null, '/test?x=1', {}, '/test?x=1', { strict: true }, true],
Expand All @@ -201,22 +171,6 @@ describe('integrations.js', () => {
[null, '/test', { x: '1', y: '2' }, '/test?y=2&x=1', null, true],
[null, '/test', { x: '1' }, '/test?x=1&y=2', null, true],
[null, '/test', { x: '1' }, '/test?x=1&y=2', { strict: true }, false],
[{ xOnly: { x: '1' } }, '/test', ['xOnly'], '/test?x=1', null, true],
[{ xOnly: { x: '1' } }, '/test', ['xOnly'], '/test?x=1', { allowRegex: true }, true],
[{ xOnly: { x: '/\\d+/' } }, '/test', ['xOnly'], '/test?x=1', { allowRegex: true }, true],
[{ xOnly: { x: '1' } }, '/test', ['xOnly'], '/test?x=1', { strict: true }, true],
[{ xOnly: { x: '1' } }, '/test', ['xOnly'], '/test?x=2', null, false],
[{ xAndY: { x: '1', y: '2' } }, '/test', ['xAndY'], '/test?x=1&y=2', null, true],
[{ xOnly: { x: '1' }, yOnly: { y: '2' } }, '/test', ['xOnly', 'yOnly'], '/test?x=1&y=2', null, true],
[
{ xOnly: { x: '/\\d+/' }, yOnly: { y: '/\\d+/' } },
'/test',
['xOnly', 'yOnly'],
'/test?x=1&y=2',
{ allowRegex: true },
true,
],
[{ xOnly: { x: '1' } }, '/test', ['xOnly', { y: '2' }], '/test?x=1&y=2', null, true],
])(
'match query config="%o" matchPath="%s" matchValues="%o" path="%s" options="%o" result=%s',
async (configuration, matchPath, matchValues, path, options, shouldMatch) => {
Expand Down
10 changes: 1 addition & 9 deletions packages/puppeteer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,7 @@ function mapToCoreRequest(request: HTTPRequest): CoreRequest {
body,
headers: headersWithoutCookie,
cookies,
query: Object.entries(parsedUrl.searchParams).reduce<{ [key in string]: string }>((acc, [key, value]) => {
if (value) {
if (typeof value === 'string') {
acc[key] = value
}
}

return acc
}, {}),
query: Object.fromEntries(parsedUrl.searchParams.entries()),
}
}

Expand Down
65 changes: 28 additions & 37 deletions packages/puppeteer/test/puppeteer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,35 @@ describe('puppeteer integration tests', () => {
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: {
method: string
headers: { [key: string]: string }
body: null | string
} = {
method: _method,
headers: _headers,
body: null,
}
const fetchOptions: {
method: string
headers: { [key: string]: string }
body: null | string
} = {
method: method,
headers: safeHeaders,
body: null,
}

if (_bodyJSON !== undefined) {
fetchOptions.body = JSON.stringify(_bodyJSON)
fetchOptions.headers = {
...fetchOptions.headers,
'Content-Type': 'application/json',
}
} else if (_body !== undefined) {
fetchOptions.body = String(_body)
}
if (bodyJSON !== undefined) {
fetchOptions.body = JSON.stringify(bodyJSON)
fetchOptions.headers = {
...fetchOptions.headers,
'Content-Type': 'application/json',
}
} else if (body !== undefined) {
fetchOptions.body = String(body)
}

const url = new URL(`http://127.0.0.1:3000${_path}`)
const url = new URL(`http://127.0.0.1:3000${path}`)

for (const queryKey in _query) {
url.searchParams.set(queryKey, _query[queryKey])
}
for (const queryKey in safeQuery) {
url.searchParams.set(queryKey, safeQuery[queryKey])
}

console.log('pup test fetch', url.toString(), fetchOptions)
const result = await fetch(url.toString(), fetchOptions)
const result = await page.evaluate(
async (_url, _fetchOptions) => {
const result = await fetch(_url, _fetchOptions)

const bodyText = await result.text()

Expand All @@ -91,21 +90,13 @@ describe('puppeteer integration tests', () => {

return {
status: result.status,
headers: Object.entries(_headers).reduce<{ [key: string]: string }>((acc, [key, value]) => {
acc[key] = value

return acc
}, {}),
headers: Object.fromEntries(result.headers.entries()),
bodyText,
bodyJSON,
}
},
path,
method,
body,
bodyJSON,
safeHeaders,
safeQuery,
url.toString(),
fetchOptions,
)

if (expectation.status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@
headers: null
expectation:
status: 400
- action:
name: put_config
data: []
expectation:
status: 400
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
- action:
name: post_fixture
data:
request:
path: /exact-multiple
method: GET
query:
a: a
b: b
response:
status: 200
expectation:
status: 201
- action:
name: test_fixture
data:
path: /exact-multiple
method: GET
query:
a: a
b: b
expectation:
status: 200
- action:
name: put_config
data:
query:
custom:
a: a
b: b
customA:
a: a
customB:
b: b
expectation:
status: 200
- action:
name: post_fixture
data:
request:
path: /exact-multiple-configuration
method: GET
query:
- custom
response:
status: 200
expectation:
status: 201
- action:
name: test_fixture
data:
path: /exact-multiple-configuration
method: GET
query:
a: a
b: b
expectation:
status: 200
- action:
name: post_fixture
data:
request:
path: /exact-multiple-configuration-2
method: GET
query:
- customA
- customB
response:
status: 200
expectation:
status: 201
- action:
name: test_fixture
data:
path: /exact-multiple-configuration-2
method: GET
query:
a: a
b: b
expectation:
status: 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
- action:
name: post_fixture
data:
request:
path: /exact
method: GET
query:
a: a
response:
status: 200
expectation:
status: 201
- action:
name: test_fixture
data:
path: /exact
method: GET
query:
a: a
expectation:
status: 200
- action:
name: put_config
data:
query:
custom:
a: a
expectation:
status: 200
- action:
name: post_fixture
data:
request:
path: /exact-configuration
method: GET
query:
- custom
response:
status: 200
expectation:
status: 201
- action:
name: test_fixture
data:
path: /exact-configuration
method: GET
query:
a: a
expectation:
status: 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
- action:
name: post_fixture
data:
request:
path: /partial
method: GET
query:
a: a
response:
status: 200
expectation:
status: 201
- action:
name: test_fixture
data:
path: /partial
method: GET
query:
a: a
b: b
expectation:
status: 200
- action:
name: put_config
data:
query:
custom:
a: a
expectation:
status: 200
- action:
name: post_fixture
data:
request:
path: /partial-configuration
method: GET
query:
- custom
response:
status: 200
expectation:
status: 201
- action:
name: test_fixture
data:
path: /partial-configuration
method: GET
query:
a: a
b: b
expectation:
status: 200
Loading
Loading