Search in sources :

Example 1 with HttpDataSource

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");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Headers(okhttp3.Headers) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataSpec(androidx.media3.datasource.DataSpec) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpDataSource(androidx.media3.datasource.HttpDataSource) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) Test(org.junit.Test)

Example 2 with HttpDataSource

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);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpClient(okhttp3.OkHttpClient) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataSpec(androidx.media3.datasource.DataSpec) HttpDataSource(androidx.media3.datasource.HttpDataSource) Test(org.junit.Test)

Example 3 with HttpDataSource

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));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpClient(okhttp3.OkHttpClient) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataSpec(androidx.media3.datasource.DataSpec) HttpDataSource(androidx.media3.datasource.HttpDataSource) Test(org.junit.Test)

Example 4 with HttpDataSource

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);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HashMap(java.util.HashMap) Headers(okhttp3.Headers) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataSpec(androidx.media3.datasource.DataSpec) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpDataSource(androidx.media3.datasource.HttpDataSource) Test(org.junit.Test)

Example 5 with HttpDataSource

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);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataSpec(androidx.media3.datasource.DataSpec) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpDataSource(androidx.media3.datasource.HttpDataSource) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

DataSpec (androidx.media3.datasource.DataSpec)6 HttpDataSource (androidx.media3.datasource.HttpDataSource)6 MockResponse (okhttp3.mockwebserver.MockResponse)6 MockWebServer (okhttp3.mockwebserver.MockWebServer)6 Test (org.junit.Test)6 DefaultHttpDataSource (androidx.media3.datasource.DefaultHttpDataSource)4 Headers (okhttp3.Headers)4 HashMap (java.util.HashMap)3 OkHttpClient (okhttp3.OkHttpClient)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Nullable (androidx.annotation.Nullable)1 Format (androidx.media3.common.Format)1 Representation (androidx.media3.exoplayer.dash.manifest.Representation)1