mirror of
https://github.com/square/okhttp.git
synced 2025-12-25 00:01:02 +03:00
Expose internal APIs for pluggable file systems.
We aren't yet ready to make FileSystem a public type, but I don't mind making it _almost_ available via an internal API for those brave enough to try that. Also migrate all of our tests to use the in memory file system. It's simpler. https://github.com/square/okhttp/issues/1459
This commit is contained in:
@@ -22,6 +22,7 @@ import com.squareup.okhttp.internal.Internal;
|
||||
import com.squareup.okhttp.internal.SslContextBuilder;
|
||||
import com.squareup.okhttp.mockwebserver.MockResponse;
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.testing.RecordingHostnameVerifier;
|
||||
import java.io.IOException;
|
||||
import java.net.CacheRequest;
|
||||
import java.net.CacheResponse;
|
||||
@@ -37,13 +38,11 @@ import java.util.Map;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import okio.Buffer;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import okio.Buffer;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -59,17 +58,10 @@ import static org.junit.Assert.assertTrue;
|
||||
* </ul>
|
||||
*/
|
||||
public class CacheAdapterTest {
|
||||
private static final SSLContext sslContext = SslContextBuilder.localhost();
|
||||
private static final HostnameVerifier NULL_HOSTNAME_VERIFIER = new HostnameVerifier() {
|
||||
public boolean verify(String hostname, SSLSession session) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
private SSLContext sslContext = SslContextBuilder.localhost();
|
||||
private HostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
|
||||
private MockWebServer server;
|
||||
|
||||
private OkHttpClient client;
|
||||
|
||||
private HttpURLConnection connection;
|
||||
|
||||
@Before public void setUp() throws Exception {
|
||||
@@ -123,7 +115,7 @@ public class CacheAdapterTest {
|
||||
};
|
||||
Internal.instance.setCache(client, new CacheAdapter(responseCache));
|
||||
client.setSslSocketFactory(sslContext.getSocketFactory());
|
||||
client.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||
client.setHostnameVerifier(hostnameVerifier);
|
||||
|
||||
connection = new OkUrlFactory(client).open(serverUrl);
|
||||
connection.setRequestProperty("key1", "value1");
|
||||
@@ -238,7 +230,7 @@ public class CacheAdapterTest {
|
||||
};
|
||||
Internal.instance.setCache(client, new CacheAdapter(responseCache));
|
||||
client.setSslSocketFactory(sslContext.getSocketFactory());
|
||||
client.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||
client.setHostnameVerifier(hostnameVerifier);
|
||||
|
||||
connection = new OkUrlFactory(client).open(serverUrl);
|
||||
executeGet(connection);
|
||||
|
||||
@@ -27,6 +27,7 @@ import com.squareup.okhttp.internal.SslContextBuilder;
|
||||
import com.squareup.okhttp.mockwebserver.MockResponse;
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.mockwebserver.RecordedRequest;
|
||||
import com.squareup.okhttp.testing.RecordingHostnameVerifier;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
@@ -66,7 +67,6 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import okio.Buffer;
|
||||
import okio.BufferedSink;
|
||||
import okio.GzipSink;
|
||||
@@ -90,18 +90,12 @@ import static org.junit.Assert.fail;
|
||||
* Based on com.squareup.okhttp.CacheTest with changes for ResponseCache and HttpURLConnection.
|
||||
*/
|
||||
public final class ResponseCacheTest {
|
||||
private static final HostnameVerifier NULL_HOSTNAME_VERIFIER = new HostnameVerifier() {
|
||||
@Override public boolean verify(String s, SSLSession sslSession) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
private static final SSLContext sslContext = SslContextBuilder.localhost();
|
||||
|
||||
@Rule public TemporaryFolder cacheRule = new TemporaryFolder();
|
||||
@Rule public MockWebServer server = new MockWebServer();
|
||||
@Rule public MockWebServer server2 = new MockWebServer();
|
||||
|
||||
private HostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
|
||||
private SSLContext sslContext = SslContextBuilder.localhost();
|
||||
private OkHttpClient client;
|
||||
private ResponseCache cache;
|
||||
private CookieManager cookieManager;
|
||||
@@ -270,7 +264,7 @@ public final class ResponseCacheTest {
|
||||
|
||||
HttpsURLConnection c1 = (HttpsURLConnection) openConnection(server.getUrl("/"));
|
||||
c1.setSSLSocketFactory(sslContext.getSocketFactory());
|
||||
c1.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||
c1.setHostnameVerifier(hostnameVerifier);
|
||||
assertEquals("ABC", readAscii(c1));
|
||||
|
||||
// OpenJDK 6 fails on this line, complaining that the connection isn't open yet
|
||||
@@ -282,7 +276,7 @@ public final class ResponseCacheTest {
|
||||
|
||||
HttpsURLConnection c2 = (HttpsURLConnection) openConnection(server.getUrl("/")); // cached!
|
||||
c2.setSSLSocketFactory(sslContext.getSocketFactory());
|
||||
c2.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||
c2.setHostnameVerifier(hostnameVerifier);
|
||||
assertEquals("ABC", readAscii(c2));
|
||||
|
||||
assertEquals(suite, c2.getCipherSuite());
|
||||
@@ -354,7 +348,7 @@ public final class ResponseCacheTest {
|
||||
.setBody("DEF"));
|
||||
|
||||
client.setSslSocketFactory(sslContext.getSocketFactory());
|
||||
client.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||
client.setHostnameVerifier(hostnameVerifier);
|
||||
|
||||
HttpsURLConnection connection1 = (HttpsURLConnection) openConnection(server.getUrl("/"));
|
||||
assertEquals("ABC", readAscii(connection1));
|
||||
@@ -392,7 +386,7 @@ public final class ResponseCacheTest {
|
||||
.addHeader("Location: " + server2.getUrl("/")));
|
||||
|
||||
client.setSslSocketFactory(sslContext.getSocketFactory());
|
||||
client.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||
client.setHostnameVerifier(hostnameVerifier);
|
||||
|
||||
HttpURLConnection connection1 = openConnection(server.getUrl("/"));
|
||||
assertEquals("ABC", readAscii(connection1));
|
||||
@@ -1461,7 +1455,7 @@ public final class ResponseCacheTest {
|
||||
.setBody("B"));
|
||||
|
||||
client.setSslSocketFactory(sslContext.getSocketFactory());
|
||||
client.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||
client.setHostnameVerifier(hostnameVerifier);
|
||||
|
||||
URL url = server.getUrl("/");
|
||||
HttpURLConnection connection1 = openConnection(url);
|
||||
@@ -1996,13 +1990,13 @@ public final class ResponseCacheTest {
|
||||
|
||||
HttpsURLConnection connection1 = (HttpsURLConnection) openConnection(server.getUrl("/"));
|
||||
connection1.setSSLSocketFactory(sslContext.getSocketFactory());
|
||||
connection1.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||
connection1.setHostnameVerifier(hostnameVerifier);
|
||||
assertEquals("ABC", readAscii(connection1));
|
||||
|
||||
// Not cached!
|
||||
HttpsURLConnection connection2 = (HttpsURLConnection) openConnection(server.getUrl("/"));
|
||||
connection2.setSSLSocketFactory(sslContext.getSocketFactory());
|
||||
connection2.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||
connection2.setHostnameVerifier(hostnameVerifier);
|
||||
assertEquals("DEF", readAscii(connection2));
|
||||
}
|
||||
|
||||
|
||||
@@ -18,5 +18,10 @@
|
||||
<artifactId>junit</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -19,6 +19,8 @@ package com.squareup.okhttp;
|
||||
import com.squareup.okhttp.internal.Internal;
|
||||
import com.squareup.okhttp.internal.SslContextBuilder;
|
||||
import com.squareup.okhttp.internal.Util;
|
||||
import com.squareup.okhttp.internal.io.FileSystem;
|
||||
import com.squareup.okhttp.internal.io.InMemoryFileSystem;
|
||||
import com.squareup.okhttp.mockwebserver.MockResponse;
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.mockwebserver.RecordedRequest;
|
||||
@@ -55,7 +57,6 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import static com.squareup.okhttp.mockwebserver.SocketPolicy.DISCONNECT_AT_END;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -73,19 +74,18 @@ public final class CacheTest {
|
||||
}
|
||||
};
|
||||
|
||||
private static final SSLContext sslContext = SslContextBuilder.localhost();
|
||||
|
||||
@Rule public TemporaryFolder cacheRule = new TemporaryFolder();
|
||||
@Rule public MockWebServer server = new MockWebServer();
|
||||
@Rule public MockWebServer server2 = new MockWebServer();
|
||||
|
||||
private final SSLContext sslContext = SslContextBuilder.localhost();
|
||||
private final FileSystem fileSystem = new InMemoryFileSystem();
|
||||
private final OkHttpClient client = new OkHttpClient();
|
||||
private Cache cache;
|
||||
private final CookieManager cookieManager = new CookieManager();
|
||||
|
||||
@Before public void setUp() throws Exception {
|
||||
server.setProtocolNegotiationEnabled(false);
|
||||
cache = new Cache(cacheRule.getRoot(), Integer.MAX_VALUE);
|
||||
cache = new Cache(new File("/cache/"), Integer.MAX_VALUE, fileSystem);
|
||||
client.setCache(cache);
|
||||
CookieHandler.setDefault(cookieManager);
|
||||
}
|
||||
@@ -1926,7 +1926,7 @@ public final class CacheTest {
|
||||
writeFile(cache.getDirectory(), urlKey + ".0", entryMetadata);
|
||||
writeFile(cache.getDirectory(), urlKey + ".1", entryBody);
|
||||
writeFile(cache.getDirectory(), "journal", journalBody);
|
||||
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE);
|
||||
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE, fileSystem);
|
||||
client.setCache(cache);
|
||||
|
||||
Response response = get(url);
|
||||
@@ -2152,7 +2152,7 @@ public final class CacheTest {
|
||||
|
||||
|
||||
private void writeFile(File directory, String file, String content) throws IOException {
|
||||
BufferedSink sink = Okio.buffer(Okio.sink(new File(directory, file)));
|
||||
BufferedSink sink = Okio.buffer(fileSystem.sink(new File(directory, file)));
|
||||
sink.writeUtf8(content);
|
||||
sink.close();
|
||||
}
|
||||
|
||||
@@ -21,12 +21,15 @@ import com.squareup.okhttp.internal.RecordingOkAuthenticator;
|
||||
import com.squareup.okhttp.internal.SingleInetAddressNetwork;
|
||||
import com.squareup.okhttp.internal.SslContextBuilder;
|
||||
import com.squareup.okhttp.internal.Version;
|
||||
import com.squareup.okhttp.internal.io.FileSystem;
|
||||
import com.squareup.okhttp.internal.io.InMemoryFileSystem;
|
||||
import com.squareup.okhttp.mockwebserver.Dispatcher;
|
||||
import com.squareup.okhttp.mockwebserver.MockResponse;
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.mockwebserver.RecordedRequest;
|
||||
import com.squareup.okhttp.mockwebserver.SocketPolicy;
|
||||
import com.squareup.okhttp.testing.RecordingHostnameVerifier;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InterruptedIOException;
|
||||
@@ -64,7 +67,6 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.rules.Timeout;
|
||||
|
||||
@@ -78,13 +80,12 @@ import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public final class CallTest {
|
||||
private static final SSLContext sslContext = SslContextBuilder.localhost();
|
||||
|
||||
@Rule public final TemporaryFolder tempDir = new TemporaryFolder();
|
||||
@Rule public final TestRule timeout = new Timeout(30_000);
|
||||
|
||||
@Rule public final MockWebServer server = new MockWebServer();
|
||||
@Rule public final MockWebServer server2 = new MockWebServer();
|
||||
|
||||
private SSLContext sslContext = SslContextBuilder.localhost();
|
||||
private FileSystem fileSystem = new InMemoryFileSystem();
|
||||
private OkHttpClient client = new OkHttpClient();
|
||||
private RecordingCallback callback = new RecordingCallback();
|
||||
private TestLogHandler logHandler = new TestLogHandler();
|
||||
@@ -95,7 +96,7 @@ public final class CallTest {
|
||||
callback = new RecordingCallback();
|
||||
logHandler = new TestLogHandler();
|
||||
|
||||
cache = new Cache(tempDir.getRoot(), Integer.MAX_VALUE);
|
||||
cache = new Cache(new File("/cache/"), Integer.MAX_VALUE, fileSystem);
|
||||
logger.addHandler(logHandler);
|
||||
}
|
||||
|
||||
@@ -1828,7 +1829,7 @@ public final class CallTest {
|
||||
* TLS_FALLBACK_SCSV cipher on fallback connections. See
|
||||
* {@link com.squareup.okhttp.FallbackTestClientSocketFactory} for details.
|
||||
*/
|
||||
private static void suppressTlsFallbackScsv(OkHttpClient client) {
|
||||
private void suppressTlsFallbackScsv(OkHttpClient client) {
|
||||
FallbackTestClientSocketFactory clientSocketFactory =
|
||||
new FallbackTestClientSocketFactory(sslContext.getSocketFactory());
|
||||
client.setSslSocketFactory(clientSocketFactory);
|
||||
|
||||
@@ -16,12 +16,12 @@
|
||||
package com.squareup.okhttp;
|
||||
|
||||
import com.squareup.okhttp.internal.Internal;
|
||||
import com.squareup.okhttp.testing.RecordingHostnameVerifier;
|
||||
import com.squareup.okhttp.internal.SslContextBuilder;
|
||||
import com.squareup.okhttp.internal.Util;
|
||||
import com.squareup.okhttp.internal.http.AuthenticatorAdapter;
|
||||
import com.squareup.okhttp.internal.http.RecordingProxySelector;
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.testing.RecordingHostnameVerifier;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
@@ -52,8 +52,8 @@ public final class ConnectionPoolTest {
|
||||
ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT);
|
||||
|
||||
private static final int KEEP_ALIVE_DURATION_MS = 5000;
|
||||
private static final SSLContext sslContext = SslContextBuilder.localhost();
|
||||
|
||||
private SSLContext sslContext = SslContextBuilder.localhost();
|
||||
private MockWebServer spdyServer;
|
||||
private InetSocketAddress spdySocketAddress;
|
||||
private Address spdyAddress;
|
||||
|
||||
@@ -17,9 +17,6 @@ package com.squareup.okhttp.internal;
|
||||
|
||||
import com.squareup.okhttp.ConnectionSpec;
|
||||
import com.squareup.okhttp.TlsVersion;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.util.Arrays;
|
||||
@@ -28,22 +25,22 @@ import java.util.Set;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ConnectionSpecSelectorTest {
|
||||
|
||||
static {
|
||||
Internal.initializeInstanceForTests();
|
||||
}
|
||||
|
||||
private static final SSLContext sslContext = SslContextBuilder.localhost();
|
||||
|
||||
public static final SSLHandshakeException RETRYABLE_EXCEPTION = new SSLHandshakeException(
|
||||
"Simulated handshake exception");
|
||||
|
||||
private SSLContext sslContext = SslContextBuilder.localhost();
|
||||
|
||||
@Test
|
||||
public void nonRetryableIOException() throws Exception {
|
||||
ConnectionSpecSelector connectionSpecSelector =
|
||||
|
||||
@@ -27,6 +27,7 @@ import com.squareup.okhttp.mockwebserver.MockResponse;
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.mockwebserver.RecordedRequest;
|
||||
import com.squareup.okhttp.mockwebserver.SocketPolicy;
|
||||
import com.squareup.okhttp.testing.RecordingHostnameVerifier;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.Authenticator;
|
||||
@@ -44,7 +45,6 @@ import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import okio.Buffer;
|
||||
import okio.BufferedSink;
|
||||
import okio.GzipSink;
|
||||
@@ -64,14 +64,6 @@ import static org.junit.Assert.fail;
|
||||
|
||||
/** Test how SPDY interacts with HTTP features. */
|
||||
public abstract class HttpOverSpdyTest {
|
||||
private static final SSLContext sslContext = SslContextBuilder.localhost();
|
||||
|
||||
private static final HostnameVerifier NULL_HOSTNAME_VERIFIER = new HostnameVerifier() {
|
||||
public boolean verify(String hostname, SSLSession session) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
@Rule public final TemporaryFolder tempDir = new TemporaryFolder();
|
||||
@Rule public final MockWebServer server = new MockWebServer();
|
||||
|
||||
@@ -79,6 +71,8 @@ public abstract class HttpOverSpdyTest {
|
||||
private final Protocol protocol;
|
||||
protected String hostHeader = ":host";
|
||||
|
||||
protected SSLContext sslContext = SslContextBuilder.localhost();
|
||||
protected HostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
|
||||
protected final OkUrlFactory client = new OkUrlFactory(new OkHttpClient());
|
||||
protected HttpURLConnection connection;
|
||||
protected Cache cache;
|
||||
@@ -91,7 +85,7 @@ public abstract class HttpOverSpdyTest {
|
||||
server.useHttps(sslContext.getSocketFactory(), false);
|
||||
client.client().setProtocols(Arrays.asList(protocol, Protocol.HTTP_1_1));
|
||||
client.client().setSslSocketFactory(sslContext.getSocketFactory());
|
||||
client.client().setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||
client.client().setHostnameVerifier(hostnameVerifier);
|
||||
cache = new Cache(tempDir.getRoot(), Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
@@ -113,12 +113,11 @@ import static org.junit.Assert.fail;
|
||||
|
||||
/** Android's URLConnectionTest. */
|
||||
public final class URLConnectionTest {
|
||||
private static final SSLContext sslContext = SslContextBuilder.localhost();
|
||||
|
||||
@Rule public final MockWebServer server = new MockWebServer();
|
||||
@Rule public final MockWebServer server2 = new MockWebServer();
|
||||
@Rule public final TemporaryFolder tempDir = new TemporaryFolder();
|
||||
|
||||
private SSLContext sslContext = SslContextBuilder.localhost();
|
||||
private OkUrlFactory client;
|
||||
private HttpURLConnection connection;
|
||||
private Cache cache;
|
||||
@@ -3312,7 +3311,7 @@ public final class URLConnectionTest {
|
||||
* TLS_FALLBACK_SCSV cipher on fallback connections. See
|
||||
* {@link com.squareup.okhttp.FallbackTestClientSocketFactory} for details.
|
||||
*/
|
||||
private static void suppressTlsFallbackScsv(OkHttpClient client) {
|
||||
private void suppressTlsFallbackScsv(OkHttpClient client) {
|
||||
FallbackTestClientSocketFactory clientSocketFactory =
|
||||
new FallbackTestClientSocketFactory(sslContext.getSocketFactory());
|
||||
client.setSslSocketFactory(clientSocketFactory);
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.squareup.okhttp;
|
||||
|
||||
import com.squareup.okhttp.internal.Platform;
|
||||
import com.squareup.okhttp.internal.io.FileSystem;
|
||||
import com.squareup.okhttp.internal.io.InMemoryFileSystem;
|
||||
import com.squareup.okhttp.mockwebserver.MockResponse;
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.text.DateFormat;
|
||||
@@ -14,7 +17,6 @@ import java.util.concurrent.TimeUnit;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.US_ASCII;
|
||||
import static okio.Okio.buffer;
|
||||
@@ -24,13 +26,13 @@ import static org.junit.Assert.fail;
|
||||
|
||||
public class OkUrlFactoryTest {
|
||||
@Rule public MockWebServer server = new MockWebServer();
|
||||
@Rule public TemporaryFolder cacheFolder = new TemporaryFolder();
|
||||
|
||||
private FileSystem fileSystem = new InMemoryFileSystem();
|
||||
private OkUrlFactory factory;
|
||||
|
||||
@Before public void setUp() throws IOException {
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
client.setCache(new Cache(cacheFolder.getRoot(), 10 * 1024 * 1024));
|
||||
client.setCache(new Cache(new File("/cache/"), 10 * 1024 * 1024, fileSystem));
|
||||
factory = new OkUrlFactory(client);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,23 +19,11 @@ package com.squareup.okhttp;
|
||||
import com.squareup.okhttp.internal.Internal;
|
||||
import com.squareup.okhttp.internal.SslContextBuilder;
|
||||
import com.squareup.okhttp.internal.Util;
|
||||
import com.squareup.okhttp.internal.io.FileSystem;
|
||||
import com.squareup.okhttp.internal.io.InMemoryFileSystem;
|
||||
import com.squareup.okhttp.mockwebserver.MockResponse;
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.mockwebserver.RecordedRequest;
|
||||
import okio.Buffer;
|
||||
import okio.BufferedSink;
|
||||
import okio.GzipSink;
|
||||
import okio.Okio;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
@@ -61,6 +49,18 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import okio.Buffer;
|
||||
import okio.BufferedSink;
|
||||
import okio.GzipSink;
|
||||
import okio.Okio;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.squareup.okhttp.mockwebserver.SocketPolicy.DISCONNECT_AT_END;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -79,19 +79,18 @@ public final class UrlConnectionCacheTest {
|
||||
}
|
||||
};
|
||||
|
||||
private static final SSLContext sslContext = SslContextBuilder.localhost();
|
||||
|
||||
@Rule public TemporaryFolder cacheRule = new TemporaryFolder();
|
||||
@Rule public MockWebServer server = new MockWebServer();
|
||||
@Rule public MockWebServer server2 = new MockWebServer();
|
||||
|
||||
private final SSLContext sslContext = SslContextBuilder.localhost();
|
||||
private final FileSystem fileSystem = new InMemoryFileSystem();
|
||||
private final OkUrlFactory client = new OkUrlFactory(new OkHttpClient());
|
||||
private Cache cache;
|
||||
private final CookieManager cookieManager = new CookieManager();
|
||||
|
||||
@Before public void setUp() throws Exception {
|
||||
server.setProtocolNegotiationEnabled(false);
|
||||
cache = new Cache(cacheRule.getRoot(), Integer.MAX_VALUE);
|
||||
cache = new Cache(new File("/cache/"), Integer.MAX_VALUE, fileSystem);
|
||||
client.client().setCache(cache);
|
||||
CookieHandler.setDefault(cookieManager);
|
||||
}
|
||||
@@ -1640,7 +1639,7 @@ public final class UrlConnectionCacheTest {
|
||||
writeFile(cache.getDirectory(), urlKey + ".0", entryMetadata);
|
||||
writeFile(cache.getDirectory(), urlKey + ".1", entryBody);
|
||||
writeFile(cache.getDirectory(), "journal", journalBody);
|
||||
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE);
|
||||
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE, fileSystem);
|
||||
client.client().setCache(cache);
|
||||
|
||||
HttpURLConnection connection = client.open(url);
|
||||
@@ -1650,7 +1649,7 @@ public final class UrlConnectionCacheTest {
|
||||
}
|
||||
|
||||
private void writeFile(File directory, String file, String content) throws IOException {
|
||||
BufferedSink sink = Okio.buffer(Okio.sink(new File(directory, file)));
|
||||
BufferedSink sink = Okio.buffer(fileSystem.sink(new File(directory, file)));
|
||||
sink.writeUtf8(content);
|
||||
sink.close();
|
||||
}
|
||||
|
||||
@@ -39,9 +39,9 @@ import org.junit.Test;
|
||||
import static com.squareup.okhttp.ws.WebSocket.PayloadType.TEXT;
|
||||
|
||||
public final class WebSocketCallTest {
|
||||
private static final SSLContext sslContext = SslContextBuilder.localhost();
|
||||
@Rule public final MockWebServer server = new MockWebServer();
|
||||
|
||||
private final SSLContext sslContext = SslContextBuilder.localhost();
|
||||
private final WebSocketRecorder listener = new WebSocketRecorder();
|
||||
private final OkHttpClient client = new OkHttpClient();
|
||||
private final Random random = new Random(0);
|
||||
|
||||
@@ -165,7 +165,11 @@ public final class Cache {
|
||||
private int requestCount;
|
||||
|
||||
public Cache(File directory, long maxSize) {
|
||||
cache = DiskLruCache.create(FileSystem.SYSTEM, directory, VERSION, ENTRY_COUNT, maxSize);
|
||||
this(directory, maxSize, FileSystem.SYSTEM);
|
||||
}
|
||||
|
||||
Cache(File directory, long maxSize, FileSystem fileSystem) {
|
||||
this.cache = DiskLruCache.create(fileSystem, directory, VERSION, ENTRY_COUNT, maxSize);
|
||||
}
|
||||
|
||||
private static String urlToKey(Request request) {
|
||||
|
||||
Reference in New Issue
Block a user