use of okhttp3 in project keywhiz by square.
the class StatusResourceIntegrationTest method isHealthy.
private boolean isHealthy() throws Exception {
Request get = new Request.Builder().get().url(testUrl("/_status")).build();
okhttp3.Response statusResponse = httpsClient.newCall(get).execute();
return statusResponse.code() == 200;
}
use of okhttp3 in project retrofit by square.
the class RetrofitTest method cloneSharesStatefulInstances.
@Test
public void cloneSharesStatefulInstances() {
CallAdapter.Factory callAdapter = mock(CallAdapter.Factory.class);
Converter.Factory converter = mock(Converter.Factory.class);
HttpUrl baseUrl = server.url("/");
Executor executor = mock(Executor.class);
okhttp3.Call.Factory callFactory = mock(okhttp3.Call.Factory.class);
Retrofit one = new Retrofit.Builder().addCallAdapterFactory(callAdapter).addConverterFactory(converter).baseUrl(baseUrl).callbackExecutor(executor).callFactory(callFactory).build();
CallAdapter.Factory callAdapter2 = mock(CallAdapter.Factory.class);
Converter.Factory converter2 = mock(Converter.Factory.class);
Retrofit two = one.newBuilder().addCallAdapterFactory(callAdapter2).addConverterFactory(converter2).build();
assertEquals(one.callAdapterFactories().size() + 1, two.callAdapterFactories().size());
assertThat(two.callAdapterFactories()).contains(callAdapter, callAdapter2);
assertEquals(one.converterFactories().size() + 1, two.converterFactories().size());
assertThat(two.converterFactories()).contains(converter, converter2);
assertSame(baseUrl, two.baseUrl());
assertSame(executor, two.callbackExecutor());
assertSame(callFactory, two.callFactory());
}
use of okhttp3 in project retrofit by square.
the class RequestBuilderTest method headerParamToString.
@Test
public void headerParamToString() {
class Example {
//
@GET("/foo/bar/")
Call<ResponseBody> method(@Header("kit") BigInteger kit) {
return null;
}
}
Request request = buildRequest(Example.class, new BigInteger("1234"));
assertThat(request.method()).isEqualTo("GET");
okhttp3.Headers headers = request.headers();
assertThat(headers.size()).isEqualTo(1);
assertThat(headers.get("kit")).isEqualTo("1234");
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
assertThat(request.body()).isNull();
}
use of okhttp3 in project okhttp by square.
the class URLConnectionTest method disconnectDuringConnect_cookieJar.
@Test
public void disconnectDuringConnect_cookieJar() throws Exception {
final AtomicReference<HttpURLConnection> connectionHolder = new AtomicReference<>();
class DisconnectingCookieJar implements CookieJar {
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
connectionHolder.get().disconnect();
return Collections.emptyList();
}
}
OkHttpClient client = new okhttp3.OkHttpClient.Builder().cookieJar(new DisconnectingCookieJar()).build();
URL url = server.url("path that should never be accessed").url();
HttpURLConnection connection = new OkHttpURLConnection(url, client);
connectionHolder.set(connection);
try {
connection.getInputStream();
fail("Connection should not be established");
} catch (IOException expected) {
assertEquals("Canceled", expected.getMessage());
} finally {
connection.disconnect();
}
}
use of okhttp3 in project okhttp by square.
the class CallTest method asyncLeakedResponseBodyLogsStackTrace.
@Test
public void asyncLeakedResponseBodyLogsStackTrace() throws Exception {
server.enqueue(new MockResponse().setBody("This gets leaked."));
client = defaultClient().newBuilder().connectionPool(new ConnectionPool(0, 10, TimeUnit.MILLISECONDS)).build();
Request request = new Request.Builder().url(server.url("/")).build();
Level original = logger.getLevel();
logger.setLevel(Level.FINE);
logHandler.setFormatter(new SimpleFormatter());
try {
final CountDownLatch latch = new CountDownLatch(1);
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
fail();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// Ignore the response so it gets leaked then GC'd.
latch.countDown();
}
});
latch.await();
// There's some flakiness when triggering a GC for objects in a separate thread. Adding a
// small delay appears to ensure the objects will get GC'd.
Thread.sleep(200);
awaitGarbageCollection();
String message = logHandler.take();
assertTrue(message.contains("A connection to " + server.url("/") + " was leaked." + " Did you forget to close a response body?"));
assertTrue(message.contains("okhttp3.RealCall.enqueue("));
assertTrue(message.contains("okhttp3.CallTest.asyncLeakedResponseBodyLogsStackTrace("));
} finally {
logger.setLevel(original);
}
}
Aggregations