Skip to content

Commit

Permalink
Add query key ordering fix (#1607)
Browse files Browse the repository at this point in the history
  • Loading branch information
James1x0 authored Aug 18, 2022
1 parent d83c1e2 commit e46d8bf
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit e46d8bf

Please sign in to comment.