diff options
author | 2016-07-27 20:29:48 +0000 | |
---|---|---|
committer | 2016-07-28 18:36:30 -0400 | |
commit | 0e75edb5a314fd2b8b977432d534ebe4027da548 (patch) | |
tree | d600e1fae276e44858798e430187d7de3f19945b /src/test/java/com/google | |
parent | 05c0bd286b3802ad3d7faf48bc19e86929a9cf6d (diff) |
Add file:// support to bazel's new_http_archive rule.
This modifies HttpConnection.java to support other URLConnections besides
simply HttpURLConnection. Supporting at least file://, and possibly ftp:// in
theory.
In the absence of AAR android support, this allows me to import a Google Play Services AAR library using a combination of new_http_archive from file://$ANDROID_SDK/... and a custom BUILD file that repackages it via android_library.
I added a check against zero-sized files, but I'm not 100% sure it's required.
--
MOS_MIGRATED_REVID=128621789
Diffstat (limited to 'src/test/java/com/google')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpConnectionTest.java | 62 |
1 files changed, 53 insertions, 9 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpConnectionTest.java b/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpConnectionTest.java index 9f84ea806a..93d9f61ddd 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpConnectionTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpConnectionTest.java @@ -15,22 +15,24 @@ package com.google.devtools.build.lib.bazel.repository.downloader; import static com.google.common.truth.Truth.assertThat; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.mockito.Mockito.when; +import com.google.common.collect.ImmutableMap; +import com.google.common.io.ByteStreams; import com.google.common.net.MediaType; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; -import org.mockito.Mockito; - +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.util.Map; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.mockito.Mockito; /** * Tests for @{link HttpConnection}. @@ -70,7 +72,7 @@ public class HttpConnectionTest { HttpURLConnection connection = Mockito.mock(HttpURLConnection.class); when(connection.getContentType()).thenReturn(MediaType.HTML_UTF_8.toString()); Charset charset = HttpConnection.getEncoding(connection); - assertEquals(StandardCharsets.UTF_8, charset); + assertEquals(UTF_8, charset); } @Test @@ -89,6 +91,48 @@ public class HttpConnectionTest { public void testNoEncodingNorContentType() throws Exception { HttpURLConnection connection = Mockito.mock(HttpURLConnection.class); Charset charset = HttpConnection.getEncoding(connection); - assertEquals(StandardCharsets.UTF_8, charset); + assertEquals(UTF_8, charset); + } + + /** + * Creates a temporary file with the specified {@code fileContents}. The file will be + * automatically deleted when the JVM exits. + * + * @param fileContents the contents of the file + * @return the {@link File} object representing the temporary file + */ + private static File createTempFile(byte[] fileContents) throws IOException { + File temp = File.createTempFile("httpConnectionTest", ".tmp"); + temp.deleteOnExit(); + try (FileOutputStream outputStream = new FileOutputStream(temp)) { + outputStream.write(fileContents); + } + return temp; + } + + @Test + public void testLocalFileDownload() throws Exception { + byte[] fileContents = "this is a test".getBytes(UTF_8); + File temp = createTempFile(fileContents); + HttpConnection httpConnection = + HttpConnection.createAndConnect(temp.toURI().toURL(), ImmutableMap.<String, String>of()); + + assertThat(httpConnection.getContentLength()).isEqualTo(fileContents.length); + + byte[] readContents = ByteStreams.toByteArray(httpConnection.getInputStream()); + assertThat(readContents).isEqualTo(fileContents); + } + + @Test + public void testLocalEmptyFileDownload() throws Exception { + byte[] fileContents = new byte[0]; + // create a temp file + File temp = createTempFile(fileContents); + try { + HttpConnection.createAndConnect(temp.toURI().toURL(), ImmutableMap.<String, String>of()); + fail("Expected exception"); + } catch (IOException ex) { + // expected + } } } |