use of okhttp3.Connection in project okhttp by square.
the class HttpLoggingInterceptorTest method headersPostNoContentType.
@Test
public void headersPostNoContentType() throws IOException {
setLevel(Level.HEADERS);
server.enqueue(new MockResponse());
Request request = request().post(RequestBody.create(null, "Hi?")).build();
Response response = client.newCall(request).execute();
response.body().close();
applicationLogs.assertLogEqual("--> POST " + url + " http/1.1").assertLogEqual("Content-Length: 3").assertLogEqual("--> END POST").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP").assertNoMoreLogs();
networkLogs.assertLogEqual("--> POST " + url + " http/1.1").assertLogEqual("Content-Length: 3").assertLogEqual("Host: " + host).assertLogEqual("Connection: Keep-Alive").assertLogEqual("Accept-Encoding: gzip").assertLogMatch("User-Agent: okhttp/.+").assertLogEqual("--> END POST").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP").assertNoMoreLogs();
}
use of okhttp3.Connection in project okhttp by square.
the class HttpLoggingInterceptorTest method bodyResponseNotIdentityEncoded.
@Test
public void bodyResponseNotIdentityEncoded() throws IOException {
setLevel(Level.BODY);
server.enqueue(new MockResponse().setHeader("Content-Encoding", "gzip").setHeader("Content-Type", PLAIN).setBody(new Buffer().write(ByteString.decodeBase64("H4sIAAAAAAAAAPNIzcnJ11HwQKIAdyO+9hMAAAA="))));
Response response = client.newCall(request().build()).execute();
response.body().close();
networkLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogEqual("Host: " + host).assertLogEqual("Connection: Keep-Alive").assertLogEqual("Accept-Encoding: gzip").assertLogMatch("User-Agent: okhttp/.+").assertLogEqual("--> END GET").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Encoding: gzip").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogMatch("Content-Length: \\d+").assertLogEqual("<-- END HTTP (encoded body omitted)").assertNoMoreLogs();
applicationLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogEqual("--> END GET").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogEqual("").assertLogEqual("Hello, Hello, Hello").assertLogEqual("<-- END HTTP (19-byte body)").assertNoMoreLogs();
}
use of okhttp3.Connection in project okhttp by square.
the class SocksProxyTest method proxy.
@Test
public void proxy() throws Exception {
server.enqueue(new MockResponse().setBody("abc"));
server.enqueue(new MockResponse().setBody("def"));
OkHttpClient client = defaultClient().newBuilder().proxy(socksProxy.proxy()).build();
Request request1 = new Request.Builder().url(server.url("/")).build();
Response response1 = client.newCall(request1).execute();
assertEquals("abc", response1.body().string());
Request request2 = new Request.Builder().url(server.url("/")).build();
Response response2 = client.newCall(request2).execute();
assertEquals("def", response2.body().string());
// The HTTP calls should share a single connection.
assertEquals(1, socksProxy.connectionCount());
}
use of okhttp3.Connection in project okhttp by square.
the class InterceptorTest method networkInterceptorReturnsConnectionOnEmptyBody.
@Test
public void networkInterceptorReturnsConnectionOnEmptyBody() throws Exception {
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_END).addHeader("Connection", "Close"));
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
assertNotNull(chain.connection());
return response;
}
};
client = client.newBuilder().addNetworkInterceptor(interceptor).build();
Request request = new Request.Builder().url(server.url("/")).build();
Response response = client.newCall(request).execute();
response.body().close();
}
use of okhttp3.Connection in project okhttp by square.
the class CallTest method reusedSinksGetIndependentTimeoutInstances.
@Test
public void reusedSinksGetIndependentTimeoutInstances() throws Exception {
server.enqueue(new MockResponse());
server.enqueue(new MockResponse());
// Call 1: set a deadline on the request body.
RequestBody requestBody1 = new RequestBody() {
@Override
public MediaType contentType() {
return MediaType.parse("text/plain");
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.writeUtf8("abc");
sink.timeout().deadline(5, TimeUnit.SECONDS);
}
};
Request request1 = new Request.Builder().url(server.url("/")).method("POST", requestBody1).build();
Response response1 = client.newCall(request1).execute();
assertEquals(200, response1.code());
// Call 2: check for the absence of a deadline on the request body.
RequestBody requestBody2 = new RequestBody() {
@Override
public MediaType contentType() {
return MediaType.parse("text/plain");
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
assertFalse(sink.timeout().hasDeadline());
sink.writeUtf8("def");
}
};
Request request2 = new Request.Builder().url(server.url("/")).method("POST", requestBody2).build();
Response response2 = client.newCall(request2).execute();
assertEquals(200, response2.code());
// Use sequence numbers to confirm the connection was pooled.
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(1, server.takeRequest().getSequenceNumber());
}
Aggregations