use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method clientConfiguredCustomContentEncoding.
@Test
public void clientConfiguredCustomContentEncoding() throws Exception {
server.enqueue(new MockResponse().setBody("ABCDE").addHeader("Content-Encoding: custom"));
URLConnection connection = urlFactory.open(server.url("/").url());
connection.addRequestProperty("Accept-Encoding", "custom");
assertEquals("ABCDE", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest request = server.takeRequest();
assertEquals("custom", request.getHeader("Accept-Encoding"));
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method authCallsForHeader.
private List<String> authCallsForHeader(String authHeader) throws IOException {
boolean proxy = authHeader.startsWith("Proxy-");
int responseCode = proxy ? 407 : 401;
RecordingAuthenticator authenticator = new RecordingAuthenticator(null);
Authenticator.setDefault(authenticator);
MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(responseCode).addHeader(authHeader).setBody("Please authenticate.");
server.enqueue(pleaseAuthenticate);
if (proxy) {
urlFactory.setClient(urlFactory.client().newBuilder().proxy(server.toProxyAddress()).proxyAuthenticator(new JavaNetAuthenticator()).build());
connection = urlFactory.open(new URL("http://android.com/"));
} else {
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new JavaNetAuthenticator()).build());
connection = urlFactory.open(server.url("/").url());
}
assertEquals(responseCode, connection.getResponseCode());
connection.getErrorStream().close();
return authenticator.calls;
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method emptyResponseHeaderValueIsAllowed.
@Test
public void emptyResponseHeaderValueIsAllowed() throws Exception {
server.enqueue(new MockResponse().addHeader("A:").setBody("body"));
connection = urlFactory.open(server.url("/").url());
assertContent("body", connection);
assertEquals("", connection.getHeaderField("A"));
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method streamedBodyWithClientRequestTimeout.
@Test
public void streamedBodyWithClientRequestTimeout() throws Exception {
enqueueClientRequestTimeoutResponses();
HttpURLConnection connection = urlFactory.open(server.url("/").url());
connection.setRequestMethod("POST");
connection.setChunkedStreamingMode(0);
connection.getOutputStream().write("Hello".getBytes("UTF-8"));
assertEquals(408, connection.getResponseCode());
assertEquals(1, server.getRequestCount());
connection.getErrorStream().close();
}
use of okhttp3.Connection 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();
}
}
Aggregations