@Test public void meshProxyClientChangesTargetAndInjectHostHeader() throws Exception { server.enqueue(new MockResponse().setBody("foo")); String serviceUrl = "http://foo.com/"; ClientConfiguration proxiedConfig = ClientConfiguration.builder() .from(createTestConfig(serviceUrl)) .meshProxy(HostAndPort.fromParts("localhost", server.getPort())) .maxNumRetries(0) .build(); OkHttpClient client = OkHttpClients.create(proxiedConfig, AGENT, hostEventsSink, OkHttpClientsTest.class); assertThat(client.newCall(new Request.Builder().url(serviceUrl).build()).execute().body().string()) .isEqualTo("foo"); assertThat(server.takeRequest().getHeader(HttpHeaders.HOST)).isEqualTo("foo.com"); }
@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(timeout = 10_000) public void randomizesUrls() throws IOException { boolean server2WasHit = false; server.shutdown(); server2.enqueue(new MockResponse().setResponseCode(200).setBody("foo")); while (!server2WasHit) { OkHttpClient client = OkHttpClients.create( ClientConfiguration.builder() .from(createTestConfig(url, url2)) .maxNumRetries(0) .build(), AGENT, hostEventsSink, OkHttpClientsTest.class); Call call = client.newCall(new Request.Builder().url(url).build()); String response = null; try { response = call.execute().body().string(); } catch (Exception e) { System.out.println("Failed to talk to shutdown server (this is expected with p=1/2)"); assertThat(e.getCause().toString()).contains(Integer.toString(server.getPort())); } if (response != null) { assertThat(response).isEqualTo("foo"); server2WasHit = true; } } }
@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); } }