0

I am running wiremock standalone using wiremockserver.

The application does not work in PCF because of the header Transfer-Encoding: chunked.

(If this header is present then in PCF I get the error 502 Bad Gateway: Registered endpoint failed to handle the request. and in headers x-cf-routererror endpoint_failure endpoint_failure (net/http: http/1.x transport connection broken: too many transfer encodings: ["chunked" "chunked"]).)

I don't want this header so I tried to disable the chunked encoding in wiremock:

@SpringBootApplication
public class MockApplication implements CommandLineRunner {

    private final int serverPort;

    private final ResourceLoader resourceLoader;

    public MockApplication(@Value("${server.port}") final int serverPort, final ResourceLoader resourceLoader) {
        this.serverPort = serverPort;
        this.resourceLoader = resourceLoader;
    }

    public static void main(final String[] args) {
        SpringApplication.run(MockApplication.class, args); //start springboot application
    }

    @Override
    public void run(final String... args) {
        final WireMockServer wireMockServer = new WireMockServer(options().port(serverPort+2)
                    .useChunkedTransferEncoding(Options.ChunkedEncodingPolicy.NEVER)
                    .extensions(new ResponseTemplateTransformer(false)));
        wireMockServer.start(); //start wiremock server
    }

Spring boot app posts data to wiremock app using restTemplate. sample request:

{
    "request": {
        "url": "/some-wiremock-endpoint",
        "method": "POST",
        "bodyPatterns": [
            {
                "contains": "12345"
            }
        ]
    },
    "response": {
        "status": 200,
        "headers": null,
        "body": "{\"books\":[{\"book_number\":\"12345\",\"book_code\":\"FICTION\"}]}"
    }
}

When the customer uses http://localhost:8080/some-wiremock-endpoint then the springboot app connects to wiremock url http://localhost:8082/some-wiremock-endpoint to retrieve the stub data from wiremock server:

ResponseEntity<String> exchange = null;
        try {
            exchange = restTemplate.exchange(wiremockUrlAndUri, HttpMethod.POST, request, String.class);
//http://localhost:8082/some-wiremock-endpoint is the wiremockUrlAndUri value in this case.

When I see exchange.getHeaders() then I still see Transfer-Encoding: chunked header.

How do I disable this header in Springboot application?

firstpostcommenter
  • 2,328
  • 4
  • 30
  • 59
  • https://wiremock.org/docs/running-standalone/, https://wiremock.org/docs/configuration/#:~:text=Transfer%20encoding,a%20chunked%20encoding%20policy%20e.g.&text=Valid%20values%20are%3A,NEVER%20-%20Never%20use%20chunked%20encoding. – firstpostcommenter May 19 '22 at 20:23
  • Are you directly constructing the WireMockServer as you've shown above, or are you using the Spring Boot WireMock extension to manage it? – Tom May 20 '22 at 09:34
  • Hi, I updated the question with the same request that the springboot app posts to wiremock server and how the springboot app retrieves data from wiremock – firstpostcommenter May 20 '22 at 11:37
  • For now, I am removing the header manually from wiremock response https://stackoverflow.com/questions/32005513/how-do-i-remove-certain-http-headers-added-by-springs-resttemplate – firstpostcommenter May 20 '22 at 11:39
  • Just removing the header is likely to break things. The alternative you need is setting a content-length header. WireMock should do this for you though - I asked about how you're starting WireMock as I suspect that the config parameter isn't being set. – Tom May 20 '22 at 12:56

0 Answers0