Skip to content

Commit f41bd3f

Browse files
committed
feat: ignore URL params on matching
1 parent 9e7fcd2 commit f41bd3f

File tree

4 files changed

+28
-27
lines changed

4 files changed

+28
-27
lines changed

src/utils/array.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/utils/faker.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
getResponseHeaderMap,
99
defaultResponseHeaders,
1010
} from './headers';
11-
import { arrayEquals } from './array';
1211
import { getNormalizedUrl } from './url';
1312

1413
let global =
@@ -34,9 +33,9 @@ export class Faker {
3433

3534
getRequests = () => Object.values(this.requestMap);
3635

37-
getKey = (url = '', searchParamKeys = [], method = 'GET') =>
36+
getKey = (url = '', method = 'GET') =>
3837
url && method
39-
? [url, ...searchParamKeys, method.toLowerCase()].join('_')
38+
? [url, method.toLowerCase()].join('_')
4039
: '';
4140

4241
makeInitialRequestMap = (requests) => {
@@ -51,12 +50,9 @@ export class Faker {
5150
};
5251

5352
add = (request) => {
54-
const { path, searchParamKeys } = getNormalizedUrl(request.url);
55-
const key = this.getKey(path, searchParamKeys, request.method);
53+
const key = this.getKey(request.url, request.method);
5654
this.requestMap[key] = {
5755
...request,
58-
path,
59-
searchParamKeys,
6056
method: request.method || 'GET',
6157
status: request.status || 200,
6258
delay: request.delay || 0,
@@ -66,8 +62,7 @@ export class Faker {
6662

6763
update = (item, fieldKey, value) => {
6864
const { url, method } = item;
69-
const { path, searchParamKeys } = getNormalizedUrl(url);
70-
const itemKey = this.getKey(path, searchParamKeys, method);
65+
const itemKey = this.getKey(url, method);
7166

7267
if (
7368
// eslint-disable-next-line no-prototype-builtins
@@ -80,17 +75,17 @@ export class Faker {
8075
};
8176

8277
matchMock = (url, method = 'GET') => {
83-
const { path, searchParamKeys } = getNormalizedUrl(url);
78+
const { fullUrl } = getNormalizedUrl(url);
8479

8580
for (let key in this.requestMap) {
8681
const { url: requestUrl, method: requestMethod } =
8782
this.requestMap[key];
88-
const { path: requestPath, searchParamKeys: requestSearchKeys } =
83+
const { fullUrlEscaped } =
8984
getNormalizedUrl(requestUrl);
85+
9086
if (
91-
match(requestPath)(path) &&
87+
match(fullUrlEscaped)(fullUrl) &&
9288
method == requestMethod &&
93-
arrayEquals(searchParamKeys, requestSearchKeys) &&
9489
!this.requestMap[key].skip
9590
) {
9691
return this.requestMap[key];

src/utils/faker.test.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@ describe('Faker', () => {
88
const actual = faker.getKey('', [], '');
99
expect(actual).toEqual('');
1010
});
11-
it('should return a string binding url and method with underscore if searchParamKeys is empty', () => {
12-
const actual = faker.getKey('google.com', [], 'GET');
11+
it('should return a string binding url and method with underscore', () => {
12+
const actual = faker.getKey('google.com', 'GET');
1313
expect(actual).toEqual('google.com_get');
1414
});
15-
it('should return a string binding url, search params keys, and method with underscore', () => {
16-
const actual = faker.getKey('google.com', ['all', 'only'], 'GET');
17-
expect(actual).toEqual('google.com_all_only_get');
18-
});
1915
});
2016
describe('makeInitialRequestMap', () => {
2117
const faker = new Faker();
@@ -69,6 +65,13 @@ describe('Faker', () => {
6965
response: {},
7066
delay: 0,
7167
},
68+
{
69+
url: 'http://request.com?a=1&b=2',
70+
method: 'GET',
71+
status: 200,
72+
response: {},
73+
delay: 0,
74+
},
7275
];
7376
faker.makeInitialRequestMap(requests);
7477

@@ -88,6 +91,12 @@ describe('Faker', () => {
8891
expect(actual.method).toEqual(requests[2].method);
8992
expect(actual.skip).toEqual(false);
9093
});
94+
it('should return request if url matches with the query parameters', () => {
95+
const actual = faker.matchMock('http://request.com?a=1&b=2', 'GET');
96+
expect(actual.url).toEqual(requests[3].url);
97+
expect(actual.method).toEqual(requests[3].method);
98+
expect(actual.skip).toEqual(false);
99+
});
91100
});
92101

93102
describe('restore', () => {

src/utils/url.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ export const getNormalizedUrl = (rawUrl = '') => {
1414
searchParamKeys.push(key);
1515
}
1616
}
17+
18+
const searchEscaped = url.search ? '\\' + url.search : '';
19+
1720
return {
1821
path: url.host + url.pathname,
1922
searchParamKeys,
23+
fullUrl: url.host + url.pathname + url.search,
24+
fullUrlEscaped: url.host + url.pathname + searchEscaped,
2025
};
2126
};

0 commit comments

Comments
 (0)