use of okhttp3.Callback in project zipkin by openzipkin.
the class HttpCallTest method executionException_conversionException.
@Test
public void executionException_conversionException() throws Exception {
mws.enqueue(new MockResponse());
http.newCall(request, b -> {
throw new IllegalArgumentException("eeek");
}).submit(callback);
try {
callback.get();
failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
} catch (IllegalArgumentException expected) {
assertThat(expected).isInstanceOf(IllegalArgumentException.class);
}
}
use of okhttp3.Callback in project zipkin by openzipkin.
the class HttpBulkSpanIndexerTest method doesntWriteSpanId.
@Test
public void doesntWriteSpanId() throws Exception {
es.enqueue(new MockResponse());
indexer.add("test_zipkin_http-2016-10-01", TestObjects.LOTS_OF_SPANS[0], (Long) null);
indexer.execute(callback);
callback.get();
RecordedRequest request = es.takeRequest();
assertThat(request.getBody().readByteString().utf8()).doesNotContain("\"_id\"");
}
use of okhttp3.Callback in project okhttp by square.
the class InterceptorTest method asyncInterceptors.
private void asyncInterceptors(boolean network) throws Exception {
server.enqueue(new MockResponse());
addInterceptor(network, new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().addHeader("OkHttp-Intercepted", "yep").build();
}
});
Request request = new Request.Builder().url(server.url("/")).build();
client.newCall(request).enqueue(callback);
callback.await(request.url()).assertCode(200).assertHeader("OkHttp-Intercepted", "yep");
}
use of okhttp3.Callback in project okhttp by square.
the class RealWebSocket method connect.
public void connect(OkHttpClient client) {
client = client.newBuilder().protocols(ONLY_HTTP1).build();
final int pingIntervalMillis = client.pingIntervalMillis();
final Request request = originalRequest.newBuilder().header("Upgrade", "websocket").header("Connection", "Upgrade").header("Sec-WebSocket-Key", key).header("Sec-WebSocket-Version", "13").build();
call = Internal.instance.newWebSocketCall(client, request);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
try {
checkResponse(response);
} catch (ProtocolException e) {
failWebSocket(e, response);
closeQuietly(response);
return;
}
// Promote the HTTP streams into web socket streams.
StreamAllocation streamAllocation = Internal.instance.streamAllocation(call);
// Prevent connection pooling!
streamAllocation.noNewStreams();
Streams streams = streamAllocation.connection().newWebSocketStreams(streamAllocation);
// Process all web socket messages.
try {
listener.onOpen(RealWebSocket.this, response);
String name = "OkHttp WebSocket " + request.url().redact();
initReaderAndWriter(name, pingIntervalMillis, streams);
streamAllocation.connection().socket().setSoTimeout(0);
loopReader();
} catch (Exception e) {
failWebSocket(e, null);
}
}
@Override
public void onFailure(Call call, IOException e) {
failWebSocket(e, null);
}
});
}
use of okhttp3.Callback in project okhttp by square.
the class AsynchronousGet method run.
public void run() throws Exception {
Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(responseBody.string());
}
}
});
}
Aggregations