DataSourceContractTest: Add tests for resolved vs original URI

PiperOrigin-RevId: 688076205
(cherry picked from commit 74bbd7727d)
This commit is contained in:
ibaker 2024-10-21 04:03:19 -07:00 committed by Iván Budnik
parent 6f42f36c05
commit ab4dff7530
4 changed files with 52 additions and 17 deletions

View file

@ -5,7 +5,7 @@
## 1.5.0-rc01 (2024-11-13)
This release includes the following changes since the
[1.5.0-beta01 release](#150-2024-10-30):
[1.5.0-beta01 release](#150-beta01-2024-10-30):
* Extractors:
* Fix media duration parsing in `mdhd` box of MP4 files to handle `-1`
@ -27,7 +27,7 @@ This release includes the following changes since the
### 1.5.0-beta01 (2024-10-30)
This release includes the following changes since the
[1.5.0-alpha01 release](#150-2024-09-06):
[1.5.0-alpha01 release](#150-alpha01-2024-09-06):
* Common Library:
* Remove `@DoNotInline` annotations from manually out-of-lined inner

View file

@ -51,6 +51,7 @@ public class ResolvingDataSourceContractTest extends DataSourceContractTest {
new TestResource.Builder()
.setName("simple")
.setUri(URI)
.setResolvedUri(RESOLVED_URI)
.setExpectedBytes(simpleData)
.build());
}

View file

@ -540,7 +540,7 @@ public abstract class DataSourceContractTest {
}
@Test
public void getUri_returnsNonNullValueOnlyWhileOpen() throws Exception {
public void getUri_returnsExpectedValueOnlyWhileOpen() throws Exception {
forAllTestResourcesAndDataSources(
(resource, dataSource) -> {
try {
@ -548,7 +548,7 @@ public abstract class DataSourceContractTest {
dataSource.open(new DataSpec(resource.getUri()));
assertThat(dataSource.getUri()).isNotNull();
assertThat(dataSource.getUri()).isEqualTo(resource.getResolvedUri());
} finally {
dataSource.close();
}
@ -740,11 +740,13 @@ public abstract class DataSourceContractTest {
@Nullable private final String name;
private final Uri uri;
private final Uri resolvedUri;
private final byte[] expectedBytes;
private TestResource(@Nullable String name, Uri uri, byte[] expectedBytes) {
private TestResource(@Nullable String name, Uri uri, Uri resolvedUri, byte[] expectedBytes) {
this.name = name;
this.uri = uri;
this.resolvedUri = resolvedUri;
this.expectedBytes = expectedBytes;
}
@ -754,11 +756,19 @@ public abstract class DataSourceContractTest {
return name;
}
/** Returns the URI where the resource is available. */
/** Returns the URI where the resource should be requested from. */
public Uri getUri() {
return uri;
}
/**
* Returns the URI where the resource is served from. This is equal to {@link #getUri()} unless
* redirection occurred when opening the resource.
*/
public Uri getResolvedUri() {
return resolvedUri;
}
/** Returns the expected contents of this resource. */
public byte[] getExpectedBytes() {
return expectedBytes;
@ -768,6 +778,7 @@ public abstract class DataSourceContractTest {
public static final class Builder {
private @MonotonicNonNull String name;
private @MonotonicNonNull Uri uri;
private @MonotonicNonNull Uri resolvedUri;
private byte @MonotonicNonNull [] expectedBytes;
/**
@ -779,19 +790,38 @@ public abstract class DataSourceContractTest {
return this;
}
/** Sets the URI where this resource is located. */
/** Sets the URI where this resource should be requested from. */
@CanIgnoreReturnValue
public Builder setUri(String uri) {
return setUri(Uri.parse(uri));
}
/** Sets the URI where this resource is located. */
/** Sets the URI where this resource should be requested from. */
@CanIgnoreReturnValue
public Builder setUri(Uri uri) {
this.uri = uri;
return this;
}
/**
* Sets the URI where this resource is served from. This only needs to be explicitly set if
* it's different to {@link #setUri(Uri)}. See {@link #getResolvedUri()}.
*/
@CanIgnoreReturnValue
public Builder setResolvedUri(String uri) {
return setResolvedUri(Uri.parse(uri));
}
/**
* Sets the URI where this resource is served from. This only needs to be explicitly set if
* it's different to {@link #setUri(Uri)}. See {@link #getResolvedUri()}.
*/
@CanIgnoreReturnValue
public Builder setResolvedUri(Uri uri) {
this.resolvedUri = uri;
return this;
}
/**
* Sets the expected contents of this resource.
*
@ -805,7 +835,11 @@ public abstract class DataSourceContractTest {
}
public TestResource build() {
return new TestResource(name, checkNotNull(uri), checkNotNull(expectedBytes));
return new TestResource(
name,
checkNotNull(uri),
resolvedUri != null ? resolvedUri : uri,
checkNotNull(expectedBytes));
}
}
}

View file

@ -93,8 +93,13 @@ public class HttpDataSourceTestEnv extends ExternalResource {
"range not supported, length unknown", RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN),
createTestResource("gzip enabled", GZIP_ENABLED),
createTestResource("gzip forced", GZIP_FORCED),
createTestResource(
"302 redirect", REDIRECTS_TO_RANGE_SUPPORTED, /* server= */ redirectionServer));
new DataSourceContractTest.TestResource.Builder()
.setName("302 redirect")
.setUri(
Uri.parse(redirectionServer.url(REDIRECTS_TO_RANGE_SUPPORTED.getPath()).toString()))
.setResolvedUri(originServer.url(RANGE_SUPPORTED.getPath()).toString())
.setExpectedBytes(REDIRECTS_TO_RANGE_SUPPORTED.getData())
.build());
}
public String getNonexistentUrl() {
@ -142,14 +147,9 @@ public class HttpDataSourceTestEnv extends ExternalResource {
private DataSourceContractTest.TestResource createTestResource(
String name, WebServerDispatcher.Resource resource) {
return createTestResource(name, resource, originServer);
}
private static DataSourceContractTest.TestResource createTestResource(
String name, WebServerDispatcher.Resource resource, MockWebServer server) {
return new DataSourceContractTest.TestResource.Builder()
.setName(name)
.setUri(Uri.parse(server.url(resource.getPath()).toString()))
.setUri(Uri.parse(originServer.url(resource.getPath()).toString()))
.setExpectedBytes(resource.getData())
.build();
}