Skip to content

Commit

Permalink
fix(mock-server): Setting content length == 0 causes the HTTP exchang…
Browse files Browse the repository at this point in the history
…e to use chunked encoding #1828
  • Loading branch information
rholshausen committed Oct 23, 2024
1 parent ec066a0 commit 3b25199
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package au.com.dius.pact.consumer.junit5;

import au.com.dius.pact.consumer.MockServer;
import au.com.dius.pact.consumer.dsl.PactBuilder;
import au.com.dius.pact.core.model.V4Pact;
import au.com.dius.pact.core.model.annotations.Pact;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "HeadMethodProvider")
public class HeadMethodTest {
@Pact(consumer = "HeadMethodConsumer")
public V4Pact pact(PactBuilder builder) {
return builder
.expectsToReceiveHttpInteraction("HEAD request",
interaction -> interaction
.withRequest(request -> request.path("/v1/my/path").method("HEAD"))
.willRespondWith(response -> response.status(200)))
.toPact();
}

@Test
void testPact(MockServer mockServer) throws IOException {
ClassicHttpResponse httpResponse = (ClassicHttpResponse) Request.head(mockServer.getUrl() + "/v1/my/path")
.execute()
.returnResponse();
assertThat(httpResponse.getCode(), is(equalTo(200)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ abstract class BaseJdkMockServer(

private fun pactResponseToHttpExchange(response: IResponse, exchange: HttpExchange) {
val headers = response.headers
exchange.responseHeaders.putAll(headers)
if (headers.isNotEmpty()) {
exchange.responseHeaders.putAll(headers)
}
if (config.addCloseHeader) {
exchange.responseHeaders.add("Connection", "close")
}
Expand All @@ -283,7 +285,7 @@ abstract class BaseJdkMockServer(
exchange.sendResponseHeaders(response.status, bytes.size.toLong())
exchange.responseBody.write(bytes)
} else {
exchange.sendResponseHeaders(response.status, 0)
exchange.sendResponseHeaders(response.status, -1)
}
exchange.close()
}
Expand Down

0 comments on commit 3b25199

Please sign in to comment.