Comments (5)
By complexity I mean actually the purpose of the library. I want the Hamcrest matchers be the only source of truth for the end user, while more complex logic should be a part of his own code, this has 2 benefits:
- I don't have to maintain too much of complex code that will satisfy 10% of the users
- More complex matching of responses should be a part of your own code, so you don't need to debug library code to understand what happened wrong.
I think that better approach than prioritizing responses is to provide you with a mechanism of determining response on per-request basis in the following way:
RESTMockServer.whenGET(pathStartsWith("login"))
.thenAnswer((request) -> {
if (not(hasQueryParameters()).matches(request)) {
return new MockResponse().setBody("NOT OK").setResponseCode(401);
} else if (hasQueryParameterNames("user","pass").matches(request)) {
return new MockResponse().setBody("OK").setResponseCode(200);
}
return null;
});
This mechanism is a part of the next release #74
from restmock.
definetely something to consider! will come up with approach and let you know here and will be a part of the next version. Thanks!
from restmock.
I don't really see much complexity in having a priority parameter. In my head, it would not be part of the hamcrest matchers, so without adding priorities everything would work like right now. Priorities would only be considered when there are multiple matching responses (in MatchableCallsRequestDispatcher#dispatch() -> else if (matchedCalls.size() > 1) {
). I imagine something like this:
if (matchedCalls.size() == 1) {
return onOneResponseMatched(recordedRequest, matchedCalls);
} else if (matchedCalls.size() > 1) {
// -> New logic
List<MatchableCall> highestPriorityCalls = takeHighestPriority(matchedCalls)
if (highestPriorityCalls.size() == 1) {
return onOneResponseMatched(recordedRequest, highestPriorityCalls);
} else {
return onTooManyResponsesMatched(recordedRequest, matchedCalls);
}
// <- New logic
} else {
return onNoResponsesMatched(recordedRequest);
}
Where the priority would be an optional attribute of MatchableCall, and not part of its Matcher<RecordedRequest>
. (The code could probably be more clean, it's just to show the idea)
Is this also what you thought? What do you think of that approach? Does it still add too much complexity?
My login request scenario was just one example, we have more scenarios where we need some way to overwrite the same request for particular cases.
Anyways, if you don't feel this is something you'd like to have in the library it's totally OK. We can always fork the code or implement a custom dispatcher for ourselves :)
from restmock.
I've had a thought on this and am rather reluctant on introducing the priority, since it will make matching responses more complex by adding one additional layer which will make it way harder for debug if you specify priorities incorectly. We here are dealing with hamcrest matchers that let us do very complex matching rules themselves. This particular issue you've mentioned could be solved with matchers, like:
/api/login -> allOf(pathEndsWithIgnoringQueryParams("login"), not(hasQueryParameters()))
/api/login?user=me&pass=123 -> allOf(pathEndsWithIgnoringQueryParams("login"), hasQueryParameterNames("user", "pass")))
.
Both of those matchers will be introduced in the upcoming release pretty shortly.
Please share your thoughts
from restmock.
Aha, ok I understand. You're right, priorities would mean introducing a new concept for matchings.
I will try to satisfy all of my requirements with your suggestions.
Thanks a lot @andrzejchm for the new mechanism and for all your work on this library :)
from restmock.
Related Issues (20)
- AndroidX compatability HOT 4
- SSLHandshakeException when using RESTMock HOT 1
- ConcurrentModificationException when resetting the server HOT 4
- Upgrading to OkHttp 4.0.0 HOT 5
- Question: What are your thoughts/plans on @Nullable @NotNullable annotations? HOT 2
- got Problem with relative paths from android gradle plugin 3.5.0 HOT 3
- Issue with Test not passing HOT 1
- Test delayed data streaming HOT 3
- Library not working for apps using current version of OkHttp (4.4.1) HOT 2
- Upgrading to OkHttp 4.5.0 HOT 3
- Crash with proguard HOT 1
- converting android sample project to Kotlin gives ClassNotFoundException HOT 4
- Any chance RESTMock can be moved off of Jitpack?
- Needs Explaination why we need to change base url in application HOT 3
- Regex support for URL matchers? HOT 1
- Version 0.3.2 is not working doing as the sample app HOT 2
- Repository miss a Changelog file HOT 1
- Android P support HOT 1
- Version 0.3.2 with HTTPS throws exception on API Level 28 HOT 3
- useHttps true in RESTMockTestRunner HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from restmock.