use of androidx.media3.datasource.HttpDataSource in project media by androidx.
the class CronetDataSourceTest method factorySetFallbackHttpDataSourceFactory_cronetNotAvailable_usesFallbackFactory.
// Tests deprecated fallback functionality.
@SuppressWarnings("deprecation")
@Test
public void factorySetFallbackHttpDataSourceFactory_cronetNotAvailable_usesFallbackFactory() throws HttpDataSourceException, InterruptedException {
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse());
CronetEngineWrapper cronetEngineWrapper = new CronetEngineWrapper((CronetEngine) null);
DefaultHttpDataSource.Factory fallbackFactory = new DefaultHttpDataSource.Factory().setUserAgent("customFallbackFactoryUserAgent");
HttpDataSource dataSourceUnderTest = new CronetDataSource.Factory(cronetEngineWrapper, executorService).setFallbackFactory(fallbackFactory).createDataSource();
dataSourceUnderTest.open(new DataSpec.Builder().setUri(mockWebServer.url("/test-path").toString()).build());
Headers headers = mockWebServer.takeRequest(10, SECONDS).getHeaders();
assertThat(headers.get("user-agent")).isEqualTo("customFallbackFactoryUserAgent");
}
use of androidx.media3.datasource.HttpDataSource in project media by androidx.
the class OkHttpDataSourceTest method open_setsCorrectHeaders.
/**
* This test will set HTTP default request parameters (1) in the OkHttpDataSource, (2) via
* OkHttpDataSource.setRequestProperty() and (3) in the DataSpec instance according to the table
* below. Values wrapped in '*' are the ones that should be set in the connection request.
*
* <pre>{@code
* +---------------+-----+-----+-----+-----+-----+-----+-----+
* | | Header Key |
* +---------------+-----+-----+-----+-----+-----+-----+-----+
* | Location | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
* +---------------+-----+-----+-----+-----+-----+-----+-----+
* | Constructor | *Y* | Y | Y | | Y | | |
* | Setter | | *Y* | Y | Y | | *Y* | |
* | DataSpec | | | *Y* | *Y* | *Y* | | *Y* |
* +---------------+-----+-----+-----+-----+-----+-----+-----+
* }</pre>
*/
@Test
public void open_setsCorrectHeaders() throws Exception {
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse());
String propertyFromFactory = "fromFactory";
Map<String, String> defaultRequestProperties = new HashMap<>();
defaultRequestProperties.put("0", propertyFromFactory);
defaultRequestProperties.put("1", propertyFromFactory);
defaultRequestProperties.put("2", propertyFromFactory);
defaultRequestProperties.put("4", propertyFromFactory);
HttpDataSource dataSource = new OkHttpDataSource.Factory(new OkHttpClient()).setDefaultRequestProperties(defaultRequestProperties).createDataSource();
String propertyFromSetter = "fromSetter";
dataSource.setRequestProperty("1", propertyFromSetter);
dataSource.setRequestProperty("2", propertyFromSetter);
dataSource.setRequestProperty("3", propertyFromSetter);
dataSource.setRequestProperty("5", propertyFromSetter);
String propertyFromDataSpec = "fromDataSpec";
Map<String, String> dataSpecRequestProperties = new HashMap<>();
dataSpecRequestProperties.put("2", propertyFromDataSpec);
dataSpecRequestProperties.put("3", propertyFromDataSpec);
dataSpecRequestProperties.put("4", propertyFromDataSpec);
dataSpecRequestProperties.put("6", propertyFromDataSpec);
DataSpec dataSpec = new DataSpec.Builder().setUri(mockWebServer.url("/test-path").toString()).setHttpRequestHeaders(dataSpecRequestProperties).build();
dataSource.open(dataSpec);
Headers headers = mockWebServer.takeRequest(10, SECONDS).getHeaders();
assertThat(headers.get("0")).isEqualTo(propertyFromFactory);
assertThat(headers.get("1")).isEqualTo(propertyFromSetter);
assertThat(headers.get("2")).isEqualTo(propertyFromDataSpec);
assertThat(headers.get("3")).isEqualTo(propertyFromDataSpec);
assertThat(headers.get("4")).isEqualTo(propertyFromDataSpec);
assertThat(headers.get("5")).isEqualTo(propertyFromSetter);
assertThat(headers.get("6")).isEqualTo(propertyFromDataSpec);
}
use of androidx.media3.datasource.HttpDataSource in project media by androidx.
the class OkHttpDataSourceTest method open_invalidResponseCode.
@Test
public void open_invalidResponseCode() {
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody("failure msg"));
HttpDataSource okHttpDataSource = new OkHttpDataSource.Factory(new OkHttpClient()).createDataSource();
DataSpec dataSpec = new DataSpec.Builder().setUri(mockWebServer.url("/test-path").toString()).build();
HttpDataSource.InvalidResponseCodeException exception = assertThrows(HttpDataSource.InvalidResponseCodeException.class, () -> okHttpDataSource.open(dataSpec));
assertThat(exception.responseCode).isEqualTo(404);
assertThat(exception.responseBody).isEqualTo("failure msg".getBytes(Charsets.UTF_8));
}
use of androidx.media3.datasource.HttpDataSource in project media by androidx.
the class CronetDataSourceTest method factory_noFallbackFactoryCronetNotAvailable_delegateDefaultRequestPropertiesToInternalFallbackFactoryAfterCreation.
// Tests deprecated fallback functionality.
@SuppressWarnings("deprecation")
@Test
public void factory_noFallbackFactoryCronetNotAvailable_delegateDefaultRequestPropertiesToInternalFallbackFactoryAfterCreation() throws HttpDataSourceException, InterruptedException {
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse());
CronetEngineWrapper cronetEngineWrapper = new CronetEngineWrapper((CronetEngine) null);
Map<String, String> defaultRequestProperties = new HashMap<>();
defaultRequestProperties.put("0", "defaultRequestProperty0");
CronetDataSource.Factory factory = new CronetDataSource.Factory(cronetEngineWrapper, executorService);
HttpDataSource dataSourceUnderTest = factory.setDefaultRequestProperties(defaultRequestProperties).createDataSource();
defaultRequestProperties.clear();
defaultRequestProperties.put("1", "defaultRequestPropertyAfterCreation");
factory.setDefaultRequestProperties(defaultRequestProperties);
dataSourceUnderTest.open(new DataSpec.Builder().setUri(mockWebServer.url("/test-path").toString()).build());
Headers headers = mockWebServer.takeRequest(10, SECONDS).getHeaders();
assertThat(headers.get("0")).isNull();
assertThat(headers.get("1")).isEqualTo("defaultRequestPropertyAfterCreation");
assertThat(dataSourceUnderTest).isInstanceOf(DefaultHttpDataSource.class);
}
use of androidx.media3.datasource.HttpDataSource in project media by androidx.
the class CronetDataSourceTest method factory_noFallbackFactoryCronetNotAvailable_delegateDefaultRequestPropertiesToInternalFallbackFactory.
// Tests deprecated fallback functionality.
@SuppressWarnings("deprecation")
@Test
public void factory_noFallbackFactoryCronetNotAvailable_delegateDefaultRequestPropertiesToInternalFallbackFactory() throws HttpDataSourceException, InterruptedException {
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse());
CronetEngineWrapper cronetEngineWrapper = new CronetEngineWrapper((CronetEngine) null);
Map<String, String> defaultRequestProperties = new HashMap<>();
defaultRequestProperties.put("0", "defaultRequestProperty0");
HttpDataSource dataSourceUnderTest = new CronetDataSource.Factory(cronetEngineWrapper, executorService).setDefaultRequestProperties(defaultRequestProperties).createDataSource();
dataSourceUnderTest.open(new DataSpec.Builder().setUri(mockWebServer.url("/test-path").toString()).build());
Headers headers = mockWebServer.takeRequest(10, SECONDS).getHeaders();
assertThat(headers.get("0")).isEqualTo("defaultRequestProperty0");
assertThat(dataSourceUnderTest).isInstanceOf(DefaultHttpDataSource.class);
}
Aggregations