From 9e9018ef151e3564bfdb34197ead89e05b15e0de Mon Sep 17 00:00:00 2001 From: "n.a.golofaev" Date: Wed, 19 Feb 2020 16:02:10 +0300 Subject: [PATCH] fix: fixed compare URL with query string --- packages/xhr-mock/src/createMockFunction.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/xhr-mock/src/createMockFunction.ts b/packages/xhr-mock/src/createMockFunction.ts index 279af08..5dd6d2e 100644 --- a/packages/xhr-mock/src/createMockFunction.ts +++ b/packages/xhr-mock/src/createMockFunction.ts @@ -4,6 +4,10 @@ import MockRequest from './MockRequest'; import MockResponse from './MockResponse'; import {createResponseFromObject} from './createResponseFromObject'; +const URL_SLIPT_QUERY_REGEX = /^([^\?]+)?/g; + +const LAST_SLASH_URL_REGEX = /\/$/; + export default function( method: string, url: string | RegExp, @@ -22,7 +26,17 @@ export default function( return url.test(requestURL); } - return requestURL === url; //TODO: should we use .startsWith()??? + const matchRequestUrl = requestURL.match(URL_SLIPT_QUERY_REGEX); + + if (matchRequestUrl) { + const [splitUrl] = matchRequestUrl; + return ( + splitUrl.replace(LAST_SLASH_URL_REGEX, '') === + url.replace(LAST_SLASH_URL_REGEX, '') + ); + } + + return false; }; return (req, res) => {