Skip to content

Commit 90d6cc4

Browse files
authoredSep 5, 2023
Merge pull request #1713 from monochromata/retry-any-http-method
feat: Retry all http methods
2 parents 2b2055f + ef22eb3 commit 90d6cc4

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed
 

‎core/support/src/main/kotlin/au/com/dius/pact/core/support/HttpClient.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import org.apache.hc.client5.http.socket.ConnectionSocketFactory
1818
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory
1919
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder
2020
import org.apache.hc.client5.http.ssl.TrustSelfSignedStrategy
21+
import org.apache.hc.core5.http.HttpRequest
2122
import org.apache.hc.core5.http.config.RegistryBuilder
2223
import org.apache.hc.core5.http.message.BasicHeader
2324
import org.apache.hc.core5.ssl.SSLContexts
@@ -70,6 +71,13 @@ sealed class Auth {
7071
}
7172
}
7273

74+
private class RetryAnyMethod(
75+
maxRetries: Int,
76+
defaultRetryInterval: TimeValue
77+
): DefaultHttpRequestRetryStrategy(maxRetries, defaultRetryInterval) {
78+
override fun handleAsIdempotent(request: HttpRequest) = true
79+
}
80+
7381
/**
7482
* HTTP client support functions
7583
*/
@@ -86,7 +94,7 @@ object HttpClient : KLogging() {
8694
insecureTLS: Boolean = false
8795
): Pair<CloseableHttpClient, CredentialsProvider?> {
8896
val builder = HttpClients.custom().useSystemProperties()
89-
.setRetryStrategy(DefaultHttpRequestRetryStrategy(maxPublishRetries,
97+
.setRetryStrategy(RetryAnyMethod(maxPublishRetries,
9098
TimeValue.ofMilliseconds(publishRetryInterval.toLong())))
9199

92100
val defaultHeaders = mutableMapOf<String, String>()

‎core/support/src/test/groovy/au/com/dius/pact/core/support/HttpClientSpec.groovy

+25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package au.com.dius.pact.core.support
22

3+
import org.apache.hc.client5.http.impl.classic.HttpRequestRetryExec
34
import org.apache.hc.client5.http.impl.classic.MainClientExec
45
import org.apache.hc.client5.http.protocol.RequestDefaultHeaders
6+
import org.apache.hc.core5.http.HttpRequest
7+
import org.apache.hc.core5.http.Method
58
import spock.lang.Specification
69

710
class HttpClientSpec extends Specification {
@@ -30,4 +33,26 @@ class HttpClientSpec extends Specification {
3033
defaultHeaders[0].name == 'Authorization'
3134
defaultHeaders[0].value == 'Bearer 1234abcd'
3235
}
36+
37+
def 'http client should retry any requests for any method'(Method method) {
38+
def uri = new URI('http://localhost')
39+
def request = Mock(HttpRequest)
40+
request.method >> method
41+
def client = HttpClient.INSTANCE.newHttpClient(null, uri, 1, 1, false).component1()
42+
def retryStrategy = null
43+
def execChain = client.execChain
44+
while (retryStrategy == null && execChain != null) {
45+
if (execChain.handler instanceof HttpRequestRetryExec) {
46+
retryStrategy = execChain.handler.retryStrategy
47+
} else {
48+
execChain = execChain.next
49+
}
50+
}
51+
52+
expect:
53+
retryStrategy.handleAsIdempotent(request) == true
54+
55+
where:
56+
method << Method.values()
57+
}
3358
}

0 commit comments

Comments
 (0)
Please sign in to comment.