Skip to content

Commit

Permalink
Add 302 redirect test to DefaultHttpDataSourceContractTest
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 348760170
  • Loading branch information
icbaker authored and ojw28 committed Dec 23, 2020
1 parent f44e5bd commit 653d180
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
import com.google.android.exoplayer2.testutil.TestUtil;
import com.google.android.exoplayer2.testutil.WebServerDispatcher;
import com.google.common.collect.ImmutableList;
import java.util.function.Function;
import okhttp3.HttpUrl;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -61,23 +66,43 @@ public class DefaultHttpDataSourceContractTest extends DataSourceContractTest {
.resolvesToUnknownLength(true)
.build();

private final MockWebServer mockWebServer = new MockWebServer();
private static final WebServerDispatcher.Resource REDIRECTS_TO_RANGE_SUPPORTED =
RANGE_SUPPORTED.buildUpon().setPath("/redirects/to/range/supported").build();

private final MockWebServer originServer = new MockWebServer();
private final MockWebServer redirectionServer = new MockWebServer();

@Before
public void startServer() throws Exception {
mockWebServer.start();
mockWebServer.setDispatcher(
public void startServers() throws Exception {
originServer.start();
originServer.setDispatcher(
WebServerDispatcher.forResources(
ImmutableList.of(
RANGE_SUPPORTED,
RANGE_SUPPORTED_LENGTH_UNKNOWN,
RANGE_NOT_SUPPORTED,
RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN)));

redirectionServer.start();
redirectionServer.setDispatcher(
new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
if (request.getPath().equals(REDIRECTS_TO_RANGE_SUPPORTED.getPath())) {
return new MockResponse()
.setResponseCode(302)
.setHeader("Location", originServer.url(RANGE_SUPPORTED.getPath()).toString());
} else {
return new MockResponse().setResponseCode(404);
}
}
});
}

@After
public void shutdownServer() throws Exception {
mockWebServer.shutdown();
public void shutdownServers() throws Exception {
originServer.shutdown();
redirectionServer.shutdown();
}

@Override
Expand All @@ -88,21 +113,27 @@ protected DataSource createDataSource() {
@Override
protected ImmutableList<TestResource> getTestResources() {
return ImmutableList.of(
toTestResource("range supported", RANGE_SUPPORTED),
toTestResource("range supported, length unknown", RANGE_SUPPORTED_LENGTH_UNKNOWN),
toTestResource("range not supported", RANGE_NOT_SUPPORTED),
toTestResource("range not supported, length unknown", RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN));
toTestResource("range supported", RANGE_SUPPORTED, originServer::url),
toTestResource(
"range supported, length unknown", RANGE_SUPPORTED_LENGTH_UNKNOWN, originServer::url),
toTestResource("range not supported", RANGE_NOT_SUPPORTED, originServer::url),
toTestResource(
"range not supported, length unknown",
RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN,
originServer::url),
toTestResource("302 redirect", REDIRECTS_TO_RANGE_SUPPORTED, redirectionServer::url));
}

@Override
protected Uri getNotFoundUri() {
return Uri.parse(mockWebServer.url("/not/a/real/path").toString());
return Uri.parse(originServer.url("/not/a/real/path").toString());
}

private TestResource toTestResource(String name, WebServerDispatcher.Resource resource) {
private static TestResource toTestResource(
String name, WebServerDispatcher.Resource resource, Function<String, HttpUrl> urlResolver) {
return new TestResource.Builder()
.setName(name)
.setUri(Uri.parse(mockWebServer.url(resource.getPath()).toString()))
.setUri(Uri.parse(urlResolver.apply(resource.getPath()).toString()))
.setExpectedBytes(resource.getData())
.setResolvesToUnknownLength(resource.resolvesToUnknownLength())
.setEndOfInputExpected(!resource.resolvesToUnknownLength())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public static class Builder {
private boolean supportsRangeRequests;
private boolean resolvesToUnknownLength;

/** Constructs an instance. */
public Builder() {}

private Builder(Resource resource) {
this.path = resource.getPath();
this.data = resource.getData();
this.supportsRangeRequests = resource.supportsRangeRequests();
this.resolvesToUnknownLength = resource.resolvesToUnknownLength();
}

/**
* Sets the path this data should be served at. This is required.
*
Expand Down Expand Up @@ -132,6 +142,11 @@ public boolean supportsRangeRequests() {
public boolean resolvesToUnknownLength() {
return resolvesToUnknownLength;
}

/** Returns a new {@link Builder} initialized with the values from this instance. */
public Builder buildUpon() {
return new Builder(this);
}
}

private final ImmutableMap<String, Resource> resourcesByPath;
Expand Down

0 comments on commit 653d180

Please sign in to comment.