mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.59k stars 1.07k forks source link

No option to suppress content-length header on the response of forwarded request #870

Closed annayyagari closed 2 years ago

annayyagari commented 4 years ago

Describe the issue No option to suppress content-length header on the response of forwarded request

What you are trying to do Response of forwarded request is always setting Content-length and Connection headers, this is causing issues on our application

MockServer version The version you are using 5.11.1 jar

To Reproduce Steps to reproduce the issue:

  1. Run mock server as a java proxy from command line 5.11.1 jar
  2. Do not define any expectations, such that request gets forwarded to proxyRemoteHost
  3. Verify Response

Expected behaviour Adding Content-length and Connection headers

There are ways to suppress these headers if mocked response is defined in the expectation but not on the forwarded request.

Please suggest if there is any work around for this. Thanks

jamesdbloom commented 2 years ago

I agree that makes sense.

This feature request has been accepted, new feature requests are prioritised on Trello, so closing issue in GitHub, only bugs or unaccepted feature requests will stay in GitHub, so that new feature backlog can be more effectively managed, see: https://trello.com/c/1BVqm032/179-allow-suppression-of-content-length-header-on-the-response-of-forwarded-request

However when this feature is implemented this issue will be checked so please continue any conversation related to this issue here.

jamesdbloom commented 2 years ago

I've now implemented a fix so Content-Length is always removed (see #914), however, I now release there was always a way to suppress these headers, which was included in the documented examples, as follows:

        new MockServerClient("localhost", 1080)
            .when(
                request()
                    .withPath("/some/path")
            )
            .forward(
                new ExpectationForwardCallback() {
                    @Override
                    public HttpRequest handle(HttpRequest httpRequest) throws Exception {
                        return request()
                            .withPath(httpRequest.getPath())
                            .withMethod("POST")
                            .withHeaders(
                                header("x-callback", "test_callback_header"),
                                header("Content-Length", "a_callback_request".getBytes(UTF_8).length),
                                header("Connection", "keep-alive")
                            )
                            .withBody("a_callback_request");
                    }
                },
                new ExpectationForwardAndResponseCallback() {
                    @Override
                    public HttpResponse handle(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception {
                        return httpResponse
                            .withHeader("x-response-test", "x-response-test")
                            .removeHeader(CONTENT_LENGTH.toString())
                            .withBody("some_overridden_response_body");
                    }
                }
            );