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

fix: make mock intercept agnostic to query key ordering #1607

Merged
merged 1 commit into from Aug 18, 2022
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
21 changes: 19 additions & 2 deletions lib/mock/mock-utils.js
Expand Up @@ -85,6 +85,22 @@ function matchHeaders (mockDispatch, headers) {
return true
}

function safeUrl (path) {
if (typeof path !== 'string') {
return path
}

const pathSegments = path.split('?')

if (pathSegments.length !== 2) {
return path
}

const qp = new URLSearchParams(pathSegments.pop())
qp.sort()
return [...pathSegments, qp.toString()].join('?')
}

function matchKey (mockDispatch, { path, method, body, headers }) {
const pathMatch = matchValue(mockDispatch.path, path)
const methodMatch = matchValue(mockDispatch.method, method)
Expand All @@ -104,10 +120,11 @@ function getResponseData (data) {
}

function getMockDispatch (mockDispatches, key) {
const resolvedPath = key.query ? buildURL(key.path, key.query) : key.path
const basePath = key.query ? buildURL(key.path, key.query) : key.path
const resolvedPath = typeof basePath === 'string' ? safeUrl(basePath) : basePath

// Match path
let matchedMockDispatches = mockDispatches.filter(({ consumed }) => !consumed).filter(({ path }) => matchValue(path, resolvedPath))
let matchedMockDispatches = mockDispatches.filter(({ consumed }) => !consumed).filter(({ path }) => matchValue(safeUrl(path), resolvedPath))
if (matchedMockDispatches.length === 0) {
throw new MockNotMatchedError(`Mock dispatch not matched for path '${resolvedPath}'`)
}
Expand Down
48 changes: 48 additions & 0 deletions test/mock-client.js
Expand Up @@ -316,6 +316,54 @@ test('MockClient - should intercept query params with hardcoded path', async (t)
t.same(response, 'hello')
})

test('MockClient - should intercept query params regardless of key ordering', async (t) => {
t.plan(3)

const server = createServer((req, res) => {
res.setHeader('content-type', 'text/plain')
res.end('should not be called')
t.fail('should not be called')
t.end()
})
t.teardown(server.close.bind(server))

await promisify(server.listen.bind(server))(0)

const baseUrl = `http://localhost:${server.address().port}`

const mockAgent = new MockAgent({ connections: 1 })
t.teardown(mockAgent.close.bind(mockAgent))

const mockClient = mockAgent.get(baseUrl)
t.type(mockClient, MockClient)
setGlobalDispatcher(mockClient)

const query = {
pageNum: 1,
limit: 100,
ordering: [false, true]
}

mockClient.intercept({
path: '/foo',
query: {
ordering: query.ordering,
pageNum: query.pageNum,
limit: query.limit
},
method: 'GET'
}).reply(200, 'hello')

const { statusCode, body } = await request(`${baseUrl}/foo`, {
method: 'GET',
query
})
t.equal(statusCode, 200)

const response = await getResponse(body)
t.same(response, 'hello')
})

test('MockClient - should be able to use as a local dispatcher', async (t) => {
t.plan(3)

Expand Down