Skip to content

Commit 3b25199

Browse files
committedOct 23, 2024·
fix(mock-server): Setting content length == 0 causes the HTTP exchange to use chunked encoding #1828
1 parent ec066a0 commit 3b25199

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package au.com.dius.pact.consumer.junit5;
2+
3+
import au.com.dius.pact.consumer.MockServer;
4+
import au.com.dius.pact.consumer.dsl.PactBuilder;
5+
import au.com.dius.pact.core.model.V4Pact;
6+
import au.com.dius.pact.core.model.annotations.Pact;
7+
import org.apache.hc.client5.http.fluent.Request;
8+
import org.apache.hc.core5.http.ClassicHttpResponse;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
12+
import java.io.IOException;
13+
14+
import static org.hamcrest.MatcherAssert.assertThat;
15+
import static org.hamcrest.Matchers.equalTo;
16+
import static org.hamcrest.Matchers.is;
17+
18+
@ExtendWith(PactConsumerTestExt.class)
19+
@PactTestFor(providerName = "HeadMethodProvider")
20+
public class HeadMethodTest {
21+
@Pact(consumer = "HeadMethodConsumer")
22+
public V4Pact pact(PactBuilder builder) {
23+
return builder
24+
.expectsToReceiveHttpInteraction("HEAD request",
25+
interaction -> interaction
26+
.withRequest(request -> request.path("/v1/my/path").method("HEAD"))
27+
.willRespondWith(response -> response.status(200)))
28+
.toPact();
29+
}
30+
31+
@Test
32+
void testPact(MockServer mockServer) throws IOException {
33+
ClassicHttpResponse httpResponse = (ClassicHttpResponse) Request.head(mockServer.getUrl() + "/v1/my/path")
34+
.execute()
35+
.returnResponse();
36+
assertThat(httpResponse.getCode(), is(equalTo(200)));
37+
}
38+
}

Diff for: ‎consumer/src/main/kotlin/au/com/dius/pact/consumer/MockHttpServer.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ abstract class BaseJdkMockServer(
273273

274274
private fun pactResponseToHttpExchange(response: IResponse, exchange: HttpExchange) {
275275
val headers = response.headers
276-
exchange.responseHeaders.putAll(headers)
276+
if (headers.isNotEmpty()) {
277+
exchange.responseHeaders.putAll(headers)
278+
}
277279
if (config.addCloseHeader) {
278280
exchange.responseHeaders.add("Connection", "close")
279281
}
@@ -283,7 +285,7 @@ abstract class BaseJdkMockServer(
283285
exchange.sendResponseHeaders(response.status, bytes.size.toLong())
284286
exchange.responseBody.write(bytes)
285287
} else {
286-
exchange.sendResponseHeaders(response.status, 0)
288+
exchange.sendResponseHeaders(response.status, -1)
287289
}
288290
exchange.close()
289291
}

0 commit comments

Comments
 (0)
Please sign in to comment.