Giter Club home page Giter Club logo

Comments (5)

andrzejchm avatar andrzejchm commented on August 21, 2024 3

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.

andrzejchm avatar andrzejchm commented on August 21, 2024 2

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.

Sloy avatar Sloy commented on August 21, 2024 1

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.

andrzejchm avatar andrzejchm commented on August 21, 2024

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.

Sloy avatar Sloy commented on August 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.