cjstehno / ersatz

🤖 A simulated HTTP server for testing client code with configurable responses.
https://cjstehno.github.io/ersatz
Apache License 2.0
47 stars 5 forks source link

Add generic expectation definition methods #160

Closed cjstehno closed 2 years ago

cjstehno commented 2 years ago

As an alternative to the method-specific expectation definition methods, add a set of generic ones along the lines of:

Request request(HttpMethod method, String path);

Request request(HttpMethod method, Matcher<String> matcher);

Request request(HttpMethod method, String path, Consumer<Request> config);

Request request(HttpMethod method, Matcher<String> matcher, Consumer<Request> config);

Request request(HttpMethod method, PathMatcher pathMatcher);

Request request(HttpMethod method, PathMatcher pathMatcher, Consumer<Request> config);

These would probably just be added to the Expectations interface.

Could add a helper set of methods that work with RequestWithContent so that you can avoid casting when using the API (use method name requestWithContent).

This would allow an expectation like:

server.expectations(expect -> {
    expect.request(GET, "/some/path", req -> {
        req.responds().code(200);
    });
});

The main benefit here is that you could easily write tests for the same matchers and responses across multiple HTTP methods using parameterized tests with the HTTP method as one of the parameters.

cjstehno commented 2 years ago

Implemented in 3.2