forked from Archives/Athou_commafeed
restore the connect timeout feature
This commit is contained in:
@@ -101,10 +101,11 @@ public class HttpGetter {
|
|||||||
return new HttpResult(content, contentType, lastModifiedHeader, eTagHeader, duration, urlAfterRedirect);
|
return new HttpResult(content, contentType, lastModifiedHeader, eTagHeader, duration, urlAfterRedirect);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HttpClient newClient() {
|
private HttpClient newClient() {
|
||||||
SSLFactory sslFactory = SSLFactory.builder().withUnsafeTrustMaterial().withUnsafeHostnameVerifier().build();
|
SSLFactory sslFactory = SSLFactory.builder().withUnsafeTrustMaterial().withUnsafeHostnameVerifier().build();
|
||||||
return HttpClient.newBuilder()
|
return HttpClient.newBuilder()
|
||||||
.version(Version.HTTP_1_1)
|
.version(Version.HTTP_1_1)
|
||||||
|
.connectTimeout(Duration.ofSeconds(5))
|
||||||
.followRedirects(Redirect.ALWAYS)
|
.followRedirects(Redirect.ALWAYS)
|
||||||
.sslContext(sslFactory.getSslContext())
|
.sslContext(sslFactory.getSslContext())
|
||||||
.sslParameters(sslFactory.getSslParameters())
|
.sslParameters(sslFactory.getSslParameters())
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.commafeed.backend;
|
package com.commafeed.backend;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.http.HttpConnectTimeoutException;
|
||||||
import java.net.http.HttpTimeoutException;
|
import java.net.http.HttpTimeoutException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
@@ -102,7 +103,17 @@ class HttpGetterTest {
|
|||||||
this.mockServerClient.when(HttpRequest.request().withMethod("GET"))
|
this.mockServerClient.when(HttpRequest.request().withMethod("GET"))
|
||||||
.respond(HttpResponse.response().withDelay(Delay.milliseconds(smallTimeout * 2)));
|
.respond(HttpResponse.response().withDelay(Delay.milliseconds(smallTimeout * 2)));
|
||||||
|
|
||||||
Assertions.assertThrows(HttpTimeoutException.class, () -> getter.getBinary(this.feedUrl, smallTimeout));
|
HttpTimeoutException e = Assertions.assertThrows(HttpTimeoutException.class, () -> getter.getBinary(this.feedUrl, smallTimeout));
|
||||||
|
Assertions.assertEquals("request timed out", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void connectTimeout() {
|
||||||
|
// try to connect to a non-routable address
|
||||||
|
// https://stackoverflow.com/a/904609/1885506
|
||||||
|
HttpConnectTimeoutException e = Assertions.assertThrows(HttpConnectTimeoutException.class,
|
||||||
|
() -> getter.getBinary("http://10.255.255.1", 10000));
|
||||||
|
Assertions.assertEquals("HTTP connect timed out", e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user