Add 302 redirect test to DefaultHttpDataSourceContractTest
PiperOrigin-RevId: 348760170
This commit is contained in:
parent
f44e5bd292
commit
653d180d4c
@ -21,7 +21,12 @@ import com.google.android.exoplayer2.testutil.DataSourceContractTest;
|
|||||||
import com.google.android.exoplayer2.testutil.TestUtil;
|
import com.google.android.exoplayer2.testutil.TestUtil;
|
||||||
import com.google.android.exoplayer2.testutil.WebServerDispatcher;
|
import com.google.android.exoplayer2.testutil.WebServerDispatcher;
|
||||||
import com.google.common.collect.ImmutableList;
|
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.MockWebServer;
|
||||||
|
import okhttp3.mockwebserver.RecordedRequest;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -61,23 +66,43 @@ public class DefaultHttpDataSourceContractTest extends DataSourceContractTest {
|
|||||||
.resolvesToUnknownLength(true)
|
.resolvesToUnknownLength(true)
|
||||||
.build();
|
.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
|
@Before
|
||||||
public void startServer() throws Exception {
|
public void startServers() throws Exception {
|
||||||
mockWebServer.start();
|
originServer.start();
|
||||||
mockWebServer.setDispatcher(
|
originServer.setDispatcher(
|
||||||
WebServerDispatcher.forResources(
|
WebServerDispatcher.forResources(
|
||||||
ImmutableList.of(
|
ImmutableList.of(
|
||||||
RANGE_SUPPORTED,
|
RANGE_SUPPORTED,
|
||||||
RANGE_SUPPORTED_LENGTH_UNKNOWN,
|
RANGE_SUPPORTED_LENGTH_UNKNOWN,
|
||||||
RANGE_NOT_SUPPORTED,
|
RANGE_NOT_SUPPORTED,
|
||||||
RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN)));
|
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
|
@After
|
||||||
public void shutdownServer() throws Exception {
|
public void shutdownServers() throws Exception {
|
||||||
mockWebServer.shutdown();
|
originServer.shutdown();
|
||||||
|
redirectionServer.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -88,21 +113,27 @@ public class DefaultHttpDataSourceContractTest extends DataSourceContractTest {
|
|||||||
@Override
|
@Override
|
||||||
protected ImmutableList<TestResource> getTestResources() {
|
protected ImmutableList<TestResource> getTestResources() {
|
||||||
return ImmutableList.of(
|
return ImmutableList.of(
|
||||||
toTestResource("range supported", RANGE_SUPPORTED),
|
toTestResource("range supported", RANGE_SUPPORTED, originServer::url),
|
||||||
toTestResource("range supported, length unknown", RANGE_SUPPORTED_LENGTH_UNKNOWN),
|
toTestResource(
|
||||||
toTestResource("range not supported", RANGE_NOT_SUPPORTED),
|
"range supported, length unknown", RANGE_SUPPORTED_LENGTH_UNKNOWN, originServer::url),
|
||||||
toTestResource("range not supported, length unknown", RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN));
|
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
|
@Override
|
||||||
protected Uri getNotFoundUri() {
|
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()
|
return new TestResource.Builder()
|
||||||
.setName(name)
|
.setName(name)
|
||||||
.setUri(Uri.parse(mockWebServer.url(resource.getPath()).toString()))
|
.setUri(Uri.parse(urlResolver.apply(resource.getPath()).toString()))
|
||||||
.setExpectedBytes(resource.getData())
|
.setExpectedBytes(resource.getData())
|
||||||
.setResolvesToUnknownLength(resource.resolvesToUnknownLength())
|
.setResolvesToUnknownLength(resource.resolvesToUnknownLength())
|
||||||
.setEndOfInputExpected(!resource.resolvesToUnknownLength())
|
.setEndOfInputExpected(!resource.resolvesToUnknownLength())
|
||||||
|
@ -48,6 +48,16 @@ public class WebServerDispatcher extends Dispatcher {
|
|||||||
private boolean supportsRangeRequests;
|
private boolean supportsRangeRequests;
|
||||||
private boolean resolvesToUnknownLength;
|
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.
|
* Sets the path this data should be served at. This is required.
|
||||||
*
|
*
|
||||||
@ -132,6 +142,11 @@ public class WebServerDispatcher extends Dispatcher {
|
|||||||
public boolean resolvesToUnknownLength() {
|
public boolean resolvesToUnknownLength() {
|
||||||
return 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;
|
private final ImmutableMap<String, Resource> resourcesByPath;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user