8000 HttpResponderBuilder.setResponder has no use · Issue #17 · Confluex/confluex-mock-http · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

HttpResponderBuilder.setResponder has no use #17

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

Open
huksley opened this issue May 19, 2016 · 4 comments
Open

HttpResponderBuilder.setResponder has no use #17

huksley opened this issue May 19, 2016 · 4 comments

Comments

@huksley
Copy link
huksley commented May 19, 2016

field HttpResponderBuilder.responder is practically immutable, because in
MockHttpServer.respondTo it is used directly without ability to override later.

@huksley
Copy link
Author
huksley commented May 19, 2016

There is no way to implement advanced scenarious which need modifiying response based on actual request body.

@huksley
Copy link
Author
huksley commented May 19, 2016

Here is possible implementation:

class OverridableHttpResponderBuilder extends HttpResponderBuilder {
    HttpRequestMatcher matcher;
    MockHttpServer server;

    public OverridableHttpResponderBuilder(HttpRequestMatcher matcher, MockHttpServer server) {
        this.matcher = matcher;
        this.server = server;
    }

    public OverridableHttpResponderBuilder responder(HttpResponder r) {
        super.setResponder(r);
        server.getHandler().getResponders().put(matcher, getResponder());
        return this;
    }
}

class OverridableMockHttpServer extends MockHttpServer {
    OverridableHttpResponderBuilder respondTo2(HttpRequestMatcher m) {
        OverridableHttpResponderBuilder b = new OverridableHttpResponderBuilder(m, this);
        getHandler().getMatchers().add(0, m);
        getHandler().getResponders().put(m, b.getResponder());
        return b;
    }
}

and invoked with:

server.respondTo2(anyRequest()).responder(new HttpResponder() {
            @Override
            public void render(ClientRequest request, HttpServletResponse response) {
                super.render(request, response);
            }
        }).withStatus(404).withBody("<error>Not found</error>");

@rhoegg
Copy link
Contributor
rhoegg commented May 19, 2016

I prefer the responder field to be immutable in the Builder, since each method call mutates its state.

The current way to use request information in the response is by providing a bodyClosure through HttpResponderBuilder.withBody(Closure). If you want to control something other than the response body, I could see adding some overloads that take Closures to withHeader or withStatus.

The spirit of the library is to provide a simple way to express the behavior of a web service in the context of a single test method. Often, a dynamic stubbed response can be replaced with several test methods, each with a static response.

@rhoegg
Copy link
Contributor
rhoegg commented May 19, 2016

Also, ongoing development has been happening on the sham-http fork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0