use of okhttp3 in project okhttp by square.
the class CallTest method leakedResponseBodyLogsStackTrace.
@Test
public void leakedResponseBodyLogsStackTrace() 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 {
// Ignore the response so it gets leaked then GC'd.
client.newCall(request).execute();
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.execute("));
assertTrue(message.contains("okhttp3.CallTest.leakedResponseBodyLogsStackTrace("));
} finally {
logger.setLevel(original);
}
}
use of okhttp3 in project okhttp by square.
the class CallTest method canceledBeforeIOSignalsOnFailure.
/**
* This test puts a request in front of one that is to be canceled, so that it is canceled before
* I/O takes place.
*/
@Test
public void canceledBeforeIOSignalsOnFailure() throws Exception {
// Force requests to be executed serially.
okhttp3.Dispatcher dispatcher = new okhttp3.Dispatcher(client.dispatcher().executorService());
dispatcher.setMaxRequests(1);
client = client.newBuilder().dispatcher(dispatcher).build();
Request requestA = new Request.Builder().url(server.url("/a")).build();
Request requestB = new Request.Builder().url(server.url("/b")).build();
final Call callA = client.newCall(requestA);
final Call callB = client.newCall(requestB);
server.setDispatcher(new Dispatcher() {
char nextResponse = 'A';
@Override
public MockResponse dispatch(RecordedRequest request) {
callB.cancel();
return new MockResponse().setBody(Character.toString(nextResponse++));
}
});
callA.enqueue(callback);
callB.enqueue(callback);
assertEquals("/a", server.takeRequest().getPath());
callback.await(requestA.url()).assertBody("A");
// At this point we know the callback is ready, and that it will receive a cancel failure.
callback.await(requestB.url()).assertFailure("Canceled", "Socket closed");
}
use of okhttp3 in project picasso by square.
the class OkHttp3DownloaderTest method works.
@Test
public void works() throws Exception {
server.enqueue(new MockResponse().setBody("Hi"));
okhttp3.Request request = new Request.Builder().url(server.url("/")).build();
Response response = downloader.load(request);
assertThat(response.body().string()).isEqualTo("Hi");
}
use of okhttp3 in project MovieGuide by esoxjem.
the class NetworkModule method provideOkHttpClient.
@Provides
@Singleton
OkHttpClient provideOkHttpClient() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return new okhttp3.OkHttpClient.Builder().connectTimeout(CONNECT_TIMEOUT_IN_MS, TimeUnit.MILLISECONDS).addInterceptor(loggingInterceptor).build();
}
use of okhttp3 in project feign by OpenFeign.
the class OkHttpClient method execute.
@Override
public feign.Response execute(feign.Request input, feign.Request.Options options) throws IOException {
okhttp3.OkHttpClient requestScoped;
if (delegate.connectTimeoutMillis() != options.connectTimeoutMillis() || delegate.readTimeoutMillis() != options.readTimeoutMillis()) {
requestScoped = delegate.newBuilder().connectTimeout(options.connectTimeoutMillis(), TimeUnit.MILLISECONDS).readTimeout(options.readTimeoutMillis(), TimeUnit.MILLISECONDS).build();
} else {
requestScoped = delegate;
}
Request request = toOkHttpRequest(input);
Response response = requestScoped.newCall(request).execute();
return toFeignResponse(response).toBuilder().request(input).build();
}
Aggregations