Skip to content

Commit c3e2fe9

Browse files
author
Ronald Holshausen
committed
feat: add option to mock server to disable persistant HTTP/1.1 connections #1383 #342
1 parent 2caa5b9 commit c3e2fe9

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

consumer/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,3 +643,13 @@ newJsonArray {
643643
}
644644
}
645645
```
646+
647+
## Dealing with persistent HTTP/1.1 connections (Keep Alive)
648+
649+
As each test will get a new mock server, connections can not be persisted between tests. HTTP clients can cache
650+
connections with HTTP/1.1, and this can cause subsequent tests to fail. See [#342](https://github.com/pact-foundation/pact-jvm/issues/342)
651+
and [#1383](https://github.com/pact-foundation/pact-jvm/issues/1383).
652+
653+
One option (if the HTTP client supports it, Apache HTTP Client does) is to set the system property `http.keepAlive` to `false` in
654+
the test JVM. The other option is to set `pact.mockserver.addCloseHeader` to `true` to force the mock server to
655+
send a `Connection: close` header with every response (supported with Pact-JVM 4.2.7+).

consumer/junit/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,3 +773,13 @@ You can also just use the key instead of an expression:
773773
```java
774774
.valueFromProviderState('userId', 'userId', 100) // will look value using userId as the key
775775
```
776+
777+
## Dealing with persistent HTTP/1.1 connections (Keep Alive)
778+
779+
As each test will get a new mock server, connections can not be persisted between tests. HTTP clients can cache
780+
connections with HTTP/1.1, and this can cause subsequent tests to fail. See [#342](https://github.com/pact-foundation/pact-jvm/issues/342)
781+
and [#1383](https://github.com/pact-foundation/pact-jvm/issues/1383).
782+
783+
One option (if the HTTP client supports it, Apache HTTP Client does) is to set the system property `http.keepAlive` to `false` in
784+
the test JVM. The other option is to set `pact.mockserver.addCloseHeader` to `true` to force the mock server to
785+
send a `Connection: close` header with every response (supported with Pact-JVM 4.2.7+).

consumer/junit5/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,16 @@ To enable this:
204204

205205
For an example, see [MultiProviderTest](https://github.com/DiUS/pact-jvm/blob/master/consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/MultiProviderTest.groovy).
206206

207+
## Dealing with persistent HTTP/1.1 connections (Keep Alive)
208+
209+
As each test will get a new mock server, connections can not be persisted between tests. HTTP clients can cache
210+
connections with HTTP/1.1, and this can cause subsequent tests to fail. See [#342](https://github.com/pact-foundation/pact-jvm/issues/342)
211+
and [#1383](https://github.com/pact-foundation/pact-jvm/issues/1383).
212+
213+
One option (if the HTTP client supports it, Apache HTTP Client does) is to set the system property `http.keepAlive` to `false` in
214+
the test JVM. The other option is to set `pact.mockserver.addCloseHeader` to `true` to force the mock server to
215+
send a `Connection: close` header with every response (supported with Pact-JVM 4.2.7+).
216+
207217
# Message Pacts
208218
## Consumer test for a message consumer
209219
For testing a consumer of messages from a message queue using JUnit 5 and Pact V4, see [AsyncMessageTest](https://github.com/pact-foundation/pact-jvm/blob/ac6a0eae0b18183f6f453eafddb89b90741ace42/consumer/junit5/src/test/java/au/com/dius/pact/consumer/junit5/AsyncMessageTest.java).

consumer/src/main/kotlin/au/com/dius/pact/consumer/MockHttpServer.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ abstract class BaseJdkMockServer(
246246
private fun pactResponseToHttpExchange(response: IResponse, exchange: HttpExchange) {
247247
val headers = response.headers
248248
exchange.responseHeaders.putAll(headers)
249+
if (config.addCloseHeader) {
250+
exchange.responseHeaders.add("Connection", "close")
251+
}
249252
val body = response.body
250253
if (body.isPresent()) {
251254
val bytes = body.unwrap()

consumer/src/main/kotlin/au/com/dius/pact/consumer/model/MockProviderConfig.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ open class MockProviderConfig @JvmOverloads constructor (
4040
open val port: Int = 0,
4141
open val pactVersion: PactSpecVersion = PactSpecVersion.V3,
4242
open val scheme: String = HTTP,
43-
open val mockServerImplementation: MockServerImplementation = MockServerImplementation.JavaHttpServer
43+
open val mockServerImplementation: MockServerImplementation = MockServerImplementation.JavaHttpServer,
44+
open val addCloseHeader: Boolean = false
4445
) {
4546

4647
fun url() = "$scheme://$hostname:$port"
@@ -52,12 +53,15 @@ open class MockProviderConfig @JvmOverloads constructor (
5253
const val HTTP = "http"
5354

5455
@JvmStatic
56+
@JvmOverloads
5557
fun httpConfig(
5658
hostname: String = LOCALHOST,
5759
port: Int = 0,
5860
pactVersion: PactSpecVersion = PactSpecVersion.V3,
59-
implementation: MockServerImplementation = MockServerImplementation.JavaHttpServer
60-
) = MockProviderConfig(hostname, port, pactVersion, HTTP, implementation.merge(MockServerImplementation.JavaHttpServer))
61+
implementation: MockServerImplementation = MockServerImplementation.JavaHttpServer,
62+
addCloseHeader: Boolean = System.getProperty("pact.mockserver.addCloseHeader") == "true"
63+
) = MockProviderConfig(hostname, port, pactVersion, HTTP,
64+
implementation.merge(MockServerImplementation.JavaHttpServer), addCloseHeader)
6165

6266
@JvmStatic
6367
fun createDefault() = createDefault(LOCALHOST, PactSpecVersion.V3)
@@ -67,6 +71,7 @@ open class MockProviderConfig @JvmOverloads constructor (
6771

6872
@JvmStatic
6973
fun createDefault(host: String, pactVersion: PactSpecVersion) =
70-
MockProviderConfig(hostname = host, pactVersion = pactVersion)
74+
MockProviderConfig(hostname = host, pactVersion = pactVersion,
75+
addCloseHeader = System.getProperty("pact.mockserver.addCloseHeader") == "true")
7176
}
7277
}

0 commit comments

Comments
 (0)