Skip to content

Commit

Permalink
fix error with array query params
Browse files Browse the repository at this point in the history
  • Loading branch information
yenh-cs committed Apr 21, 2023
1 parent 428fb25 commit d8bc887
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lib/smoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,32 @@ function matchMock(mock, method, set, query) {
);
}

function extractParamsFromUrl(url) {
const queryString = url.split('?')[1];
if (!queryString) {
return {};
}
const params = {};
queryString.split('&').forEach((paramString) => {
const [key, value] = paramString.split('=');
const decodedKey = decodeURIComponent(key);
const decodedValue = decodeURIComponent(value);
if (decodedKey.endsWith('[]')) {
const keyWithoutBrackets = decodedKey.slice(0, -2);
params[keyWithoutBrackets] = params[keyWithoutBrackets]
? `${params[keyWithoutBrackets]},${decodedValue}`
: decodedValue;
} else {
params[decodedKey] = decodedValue;
}
});
return params;
}

function processRequest(options) {
return async (req, res, next) => {
const {query, headers, body, files} = req;
let {query, headers, body, files} = req;
query = extractParamsFromUrl(req.url);
const reqPath = req.path.slice(1);
const method = req.method.toLowerCase();
const data = {method, query, params: {}, headers, body, files};
Expand All @@ -117,7 +140,6 @@ function processRequest(options) {

if (match) {
const accept = req.accepts(mock.type);

if (accept && matchMock(mock, method, options.set, query)) {
const score =
(mock.methods ? 1 : 0) + (mock.set ? 2 : 0) + (mock.params ? 4 : 0) + (mock.data === undefined ? 0.5 : 0);
Expand Down

0 comments on commit d8bc887

Please sign in to comment.