Skip to content

Commit

Permalink
Expose internal APIs for pluggable file systems.
Browse files Browse the repository at this point in the history
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.

square#1459
  • Loading branch information
squarejesse committed Aug 1, 2015
1 parent ad4b328 commit a560f72
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand All @@ -37,13 +38,11 @@
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;
Expand All @@ -59,17 +58,10 @@
* </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 {
Expand Down Expand Up @@ -123,7 +115,7 @@ public CacheResponse get(URI uri, String method, Map<String, List<String>> heade
};
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");
Expand Down Expand Up @@ -238,7 +230,7 @@ public CacheResponse get(URI uri, String method, Map<String, List<String>> heade
};
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.testing.RecordingHostnameVerifier;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -66,7 +67,6 @@
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;
Expand All @@ -90,18 +90,12 @@
* 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;
Expand Down Expand Up @@ -270,7 +264,7 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {

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
Expand All @@ -282,7 +276,7 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {

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());
Expand Down Expand Up @@ -354,7 +348,7 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {
.setBody("DEF"));

client.setSslSocketFactory(sslContext.getSocketFactory());
client.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
client.setHostnameVerifier(hostnameVerifier);

HttpsURLConnection connection1 = (HttpsURLConnection) openConnection(server.getUrl("/"));
assertEquals("ABC", readAscii(connection1));
Expand Down Expand Up @@ -392,7 +386,7 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {
.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));
Expand Down Expand Up @@ -1461,7 +1455,7 @@ private RecordedRequest assertClientSuppliedCondition(MockResponse seed, String
.setBody("B"));

client.setSslSocketFactory(sslContext.getSocketFactory());
client.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
client.setHostnameVerifier(hostnameVerifier);

URL url = server.getUrl("/");
HttpURLConnection connection1 = openConnection(url);
Expand Down Expand Up @@ -1996,13 +1990,13 @@ private InsecureResponseCache(ResponseCache delegate) {

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));
}

Expand Down
5 changes: 5 additions & 0 deletions okhttp-testing-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
14 changes: 7 additions & 7 deletions okhttp-tests/src/test/java/com/squareup/okhttp/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
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;
Expand Down Expand Up @@ -55,7 +57,6 @@
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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -1926,7 +1926,7 @@ public void assertCookies(HttpUrl url, String... expectedCookies) throws Excepti
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);
Expand Down Expand Up @@ -2152,7 +2152,7 @@ private Response get(HttpUrl url) throws IOException {


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();
}
Expand Down
15 changes: 8 additions & 7 deletions okhttp-tests/src/test/java/com/squareup/okhttp/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
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;
Expand Down Expand Up @@ -64,7 +67,6 @@
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;

Expand All @@ -78,13 +80,12 @@
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();
Expand All @@ -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);
}

Expand Down Expand Up @@ -1828,7 +1829,7 @@ public List<SSLSocket> getSocketsCreated() {
* 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

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;
Expand All @@ -28,22 +25,22 @@
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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand All @@ -44,7 +45,6 @@
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;
Expand All @@ -64,21 +64,15 @@

/** 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();

/** Protocol to test, for example {@link com.squareup.okhttp.Protocol#SPDY_3} */
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;
Expand All @@ -91,7 +85,7 @@ protected HttpOverSpdyTest(Protocol protocol){
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);
}

Expand Down
Loading

0 comments on commit a560f72

Please sign in to comment.