@Test public void handlesRetryOther_redirectsToOtherUrl() throws Exception { OkHttpClient client = OkHttpClients.withStableUris( ClientConfiguration.builder().from(createTestConfig(url, url2)).build(), AGENT, hostEventsSink, OkHttpClientsTest.class); server.enqueue(new MockResponse().setResponseCode(308).addHeader(HttpHeaders.LOCATION, url2)); server2.enqueue(new MockResponse().setResponseCode(200).setBody("foo")); Call call = client.newCall(new Request.Builder().url(url + "/foo?bar").build()); assertThat(call.execute().body().string()).isEqualTo("foo"); assertThat(server.takeRequest().getPath()).isEqualTo("/foo?bar"); assertThat(server2.takeRequest().getPath()).isEqualTo("/foo?bar"); }
@Test public void sync_call_to_a_slow_endpoint_should_not_time_out_if_read_timeout_is_zero() throws Exception { server.enqueue(new MockResponse() .setBodyDelay(Duration.ofSeconds(11).toMillis(), TimeUnit.MILLISECONDS) .setBody("Hello, world!")); OkHttpClient client = OkHttpClients.withStableUris( ClientConfigurations.of( ServiceConfiguration.builder() .addUris(url) // ClientConfigurations has a connectTimeout default of 10 seconds .readTimeout(Duration.ZERO) // unlimited pls .writeTimeout(Duration.ZERO) // unlimited pls .security(SslConfiguration.of(Paths.get("src", "test", "resources", "trustStore.jks"))) .build() ), AGENT, hostEventsSink, OkHttpClientsTest.class); Request request = new Request.Builder() .url(url) .build(); Response synchronousCall = client.newCall(request).execute(); assertThat(synchronousCall.body().string()).isEqualTo("Hello, world!"); }
@Test public void timeouts_set_to_all_zero_should_be_treated_as_infinity() throws Exception { server.enqueue(new MockResponse() .setBodyDelay(Duration.ofSeconds(11).toMillis(), TimeUnit.MILLISECONDS) .setBody("Hello, world!")); OkHttpClient client = OkHttpClients.withStableUris( ClientConfiguration.builder() .from(createTestConfig(url)) .connectTimeout(Duration.ZERO) .readTimeout(Duration.ZERO) .writeTimeout(Duration.ZERO) // want to allow unlimited time for uploads .maxNumRetries(0) .backoffSlotSize(Duration.ofMillis(10)) .build(), AGENT, hostEventsSink, OkHttpClientsTest.class); Request request = new Request.Builder() .url(url) .build(); Response synchronousCall = client.newCall(request).execute(); assertThat(synchronousCall.body().string()).isEqualTo("Hello, world!"); }
private OkHttpClient createRetryingClient(int maxNumRetries, Duration backoffSlotSize, String... urls) { return OkHttpClients.withStableUris( ClientConfiguration.builder() .from(createTestConfig(urls)) .maxNumRetries(maxNumRetries) .backoffSlotSize(backoffSlotSize) .build(), AGENT, hostEventsSink, OkHttpClientsTest.class); } }