use of okhttp3.OkUrlFactory in project okhttp by square.
the class URLConnectionTest method streamedBodyIsNotRetried.
@Test
public void streamedBodyIsNotRetried() throws Exception {
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AFTER_REQUEST));
urlFactory = new OkUrlFactory(defaultClient().newBuilder().dns(new DoubleInetAddressDns()).build());
HttpURLConnection connection = urlFactory.open(server.url("/").url());
connection.setDoOutput(true);
connection.setChunkedStreamingMode(100);
OutputStream os = connection.getOutputStream();
os.write("OutputStream is no fun.".getBytes("UTF-8"));
os.close();
try {
connection.getResponseCode();
fail();
} catch (IOException expected) {
}
assertEquals(1, server.getRequestCount());
}
use of okhttp3.OkUrlFactory in project okhttp by square.
the class URLEncodingTest method backdoorUrlToUri.
private URI backdoorUrlToUri(URL url) throws Exception {
final AtomicReference<URI> uriReference = new AtomicReference<>();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
Internal.instance.setCache(builder, new InternalCache() {
@Override
public Response get(Request request) throws IOException {
uriReference.set(request.url().uri());
throw new UnsupportedOperationException();
}
@Override
public CacheRequest put(Response response) throws IOException {
return null;
}
@Override
public void remove(Request request) throws IOException {
}
@Override
public void update(Response cached, Response network) {
}
@Override
public void trackConditionalCacheHit() {
}
@Override
public void trackResponse(CacheStrategy cacheStrategy) {
}
});
try {
HttpURLConnection connection = new OkUrlFactory(builder.build()).open(url);
connection.getResponseCode();
} catch (Exception expected) {
if (expected.getCause() instanceof URISyntaxException) {
expected.printStackTrace();
}
}
return uriReference.get();
}
use of okhttp3.OkUrlFactory in project okhttp by square.
the class HttpResponseCacheTest method setUp.
@Before
public void setUp() throws Exception {
cacheDir = cacheRule.getRoot();
urlFactory = new OkUrlFactory(new OkHttpClient());
}
use of okhttp3.OkUrlFactory in project okhttp by square.
the class CacheAdapterTest method put_httpPost.
@Test
public void put_httpPost() throws Exception {
final String statusLine = "HTTP/1.1 200 Fantastic";
final URL serverUrl = configureServer(new MockResponse().setStatus(statusLine).addHeader("A", "c"));
ResponseCache responseCache = new AbstractResponseCache() {
@Override
public CacheRequest put(URI uri, URLConnection connection) throws IOException {
try {
assertTrue(connection instanceof HttpURLConnection);
assertFalse(connection instanceof HttpsURLConnection);
assertEquals(0, connection.getContentLength());
HttpURLConnection httpUrlConnection = (HttpURLConnection) connection;
assertEquals("POST", httpUrlConnection.getRequestMethod());
assertTrue(httpUrlConnection.getDoInput());
assertTrue(httpUrlConnection.getDoOutput());
assertEquals("Fantastic", httpUrlConnection.getResponseMessage());
assertEquals(toUri(serverUrl), uri);
assertEquals(serverUrl, connection.getURL());
assertEquals("value", connection.getRequestProperty("key"));
// Check retrieval by string key.
assertEquals(statusLine, httpUrlConnection.getHeaderField(null));
assertEquals("c", httpUrlConnection.getHeaderField("A"));
// The RI and OkHttp supports case-insensitive matching for this method.
assertEquals("c", httpUrlConnection.getHeaderField("a"));
return null;
} catch (Throwable t) {
throw new IOException("unexpected cache failure", t);
}
}
};
setInternalCache(new CacheAdapter(responseCache));
connection = new OkUrlFactory(client).open(serverUrl);
executePost(connection);
}
use of okhttp3.OkUrlFactory in project okhttp by square.
the class CacheAdapterTest method put_httpGet.
@Test
public void put_httpGet() throws Exception {
final String statusLine = "HTTP/1.1 200 Fantastic";
final byte[] response = "ResponseString".getBytes(StandardCharsets.UTF_8);
final URL serverUrl = configureServer(new MockResponse().setStatus(statusLine).addHeader("A", "c").setBody(new Buffer().write(response)));
ResponseCache responseCache = new AbstractResponseCache() {
@Override
public CacheRequest put(URI uri, URLConnection connection) throws IOException {
try {
assertTrue(connection instanceof HttpURLConnection);
assertFalse(connection instanceof HttpsURLConnection);
assertEquals(response.length, connection.getContentLength());
HttpURLConnection httpUrlConnection = (HttpURLConnection) connection;
assertEquals("GET", httpUrlConnection.getRequestMethod());
assertTrue(httpUrlConnection.getDoInput());
assertFalse(httpUrlConnection.getDoOutput());
assertEquals("Fantastic", httpUrlConnection.getResponseMessage());
assertEquals(toUri(serverUrl), uri);
assertEquals(serverUrl, connection.getURL());
assertEquals("value", connection.getRequestProperty("key"));
// Check retrieval by string key.
assertEquals(statusLine, httpUrlConnection.getHeaderField(null));
assertEquals("c", httpUrlConnection.getHeaderField("A"));
// The RI and OkHttp supports case-insensitive matching for this method.
assertEquals("c", httpUrlConnection.getHeaderField("a"));
return null;
} catch (Throwable t) {
throw new IOException("unexpected cache failure", t);
}
}
};
setInternalCache(new CacheAdapter(responseCache));
connection = new OkUrlFactory(client).open(serverUrl);
connection.setRequestProperty("key", "value");
executeGet(connection);
}
Aggregations