use of okhttp3.Callback in project okhttp by square.
the class CallTest method post_Async.
@Test
public void post_Async() throws Exception {
server.enqueue(new MockResponse().setBody("abc"));
Request request = new Request.Builder().url(server.url("/")).post(RequestBody.create(MediaType.parse("text/plain"), "def")).build();
client.newCall(request).enqueue(callback);
callback.await(request.url()).assertCode(200).assertBody("abc");
RecordedRequest recordedRequest = server.takeRequest();
assertEquals("def", recordedRequest.getBody().readUtf8());
assertEquals("3", recordedRequest.getHeader("Content-Length"));
assertEquals("text/plain; charset=utf-8", recordedRequest.getHeader("Content-Type"));
}
use of okhttp3.Callback in project okhttp by square.
the class CallTest method get_Async.
@Test
public void get_Async() throws Exception {
server.enqueue(new MockResponse().setBody("abc").addHeader("Content-Type: text/plain"));
Request request = new Request.Builder().url(server.url("/")).header("User-Agent", "AsyncApiTest").build();
client.newCall(request).enqueue(callback);
callback.await(request.url()).assertCode(200).assertHeader("Content-Type", "text/plain").assertBody("abc");
assertEquals("AsyncApiTest", server.takeRequest().getHeader("User-Agent"));
}
use of okhttp3.Callback in project okhttp by square.
the class CallTest method recoverFromTlsHandshakeFailure_Async.
@Test
public void recoverFromTlsHandshakeFailure_Async() throws Exception {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
server.enqueue(new MockResponse().setBody("abc"));
client = client.newBuilder().hostnameVerifier(new RecordingHostnameVerifier()).connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS)).sslSocketFactory(suppressTlsFallbackClientSocketFactory(), sslClient.trustManager).build();
Request request = new Request.Builder().url(server.url("/")).build();
client.newCall(request).enqueue(callback);
callback.await(request.url()).assertBody("abc");
}
use of okhttp3.Callback in project okhttp by square.
the class CallTest method connectionReuseWhenResponseBodyConsumed_Async.
@Test
public void connectionReuseWhenResponseBodyConsumed_Async() throws Exception {
server.enqueue(new MockResponse().setBody("abc"));
server.enqueue(new MockResponse().setBody("def"));
Request request = new Request.Builder().url(server.url("/a")).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
throw new AssertionError();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream bytes = response.body().byteStream();
assertEquals('a', bytes.read());
assertEquals('b', bytes.read());
assertEquals('c', bytes.read());
// This request will share a connection with 'A' cause it's all done.
client.newCall(new Request.Builder().url(server.url("/b")).build()).enqueue(callback);
}
});
callback.await(server.url("/b")).assertCode(200).assertBody("def");
// New connection.
assertEquals(0, server.takeRequest().getSequenceNumber());
// Connection reuse!
assertEquals(1, server.takeRequest().getSequenceNumber());
}
use of okhttp3.Callback in project okhttp by square.
the class CallTest method conditionalCacheMiss_Async.
@Test
public void conditionalCacheMiss_Async() throws Exception {
server.enqueue(new MockResponse().setBody("A").addHeader("ETag: v1"));
server.enqueue(new MockResponse().setBody("B"));
client = client.newBuilder().cache(cache).build();
Request request1 = new Request.Builder().url(server.url("/")).build();
client.newCall(request1).enqueue(callback);
callback.await(request1.url()).assertCode(200).assertBody("A");
assertNull(server.takeRequest().getHeader("If-None-Match"));
Request request2 = new Request.Builder().url(server.url("/")).build();
client.newCall(request2).enqueue(callback);
callback.await(request2.url()).assertCode(200).assertBody("B");
assertEquals("v1", server.takeRequest().getHeader("If-None-Match"));
}
Aggregations