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

add match function as a matcher to request; make url/verb/match all optionals; at least one must exist #220

Merged
merged 2 commits into from
Nov 1, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 163 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"raw-loader": "^0.5.1",
"script-loader": "^0.7.0",
"style-loader": "^0.14.1",
"ts-loader": "^5.3.0",
"ts-loader": "^8.2.0",
"typescript": "^4.4.4",
"webpack": "^4.44.2",
"webpack-cli": "^3.1.2",
Expand Down
26 changes: 26 additions & 0 deletions src/leonardo/configuration.srv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/// <reference path="leonardo.d.ts" />
import MockRequest from "xhr-mock/lib/MockRequest";

declare let Object: any;
export function leoConfiguration() {
let _states = [],
Expand Down Expand Up @@ -31,6 +33,7 @@ export function leoConfiguration() {
addSavedState: addSavedState,
addOrUpdateSavedState: addOrUpdateSavedState,
fetchStatesByUrlAndMethod: fetchStatesByUrlAndMethod,
fetchStatesByRequest: fetchStatesByRequest,
removeState: removeState,
removeOption: removeOption,
onStateChange: onSetStates,
Expand Down Expand Up @@ -171,6 +174,29 @@ export function leoConfiguration() {
})[0];
}

function fetchStatesByRequest(request: MockRequest) {
return fetchStates()
.filter((state) => state.url || state.verb || typeof state.match === 'function')
.filter((state) => {
if (state.url) {
return isMatchUrl(state.url, request.url().toString());
}
return true;
})
.filter((state) => {
if (state.verb) {
return request.method().toLocaleLowerCase() === state.verb.toLowerCase();
}
return true;
})
.filter((state) => {
if (typeof state.match === 'function') {
return !!this.state.match.call(this.state, request);
}
return true;
})[0];
}

function isMatchUrl(stateUrlPattern, url) {
const urlRegexp = new RegExp(stateUrlPattern);
const decodedUrl = decodeURIComponent(url);
Expand Down
4 changes: 2 additions & 2 deletions src/leonardo/xhr-mock.srv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class XhrMock {
private init() {
xhrMock.setup();
xhrMock.use(async (request: MockRequest, response: MockResponse) => {
const state = Leonardo.fetchStatesByUrlAndMethod(request.url().toString(), request.method());
const state = Leonardo.fetchStatesByRequest(request);
if (state && state.active) {
const activeOption = Leonardo.getActiveStateOption(state.name);
if (!!activeOption) {
Expand Down Expand Up @@ -42,7 +42,7 @@ export class XhrMock {
const delay = activeOption.delay || 0;
const res = response.status(resStatus).headers(resHeaders).body(resData);
this.log(request, res);
return new Promise<MockResponse>((resolve, reject) => {
return new Promise<MockResponse>((resolve) => {
setTimeout(() => resolve(res), delay);
});
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2019",
"module": "commonjs",
"jsx": "react",
"types" : [],
Expand Down