Search in sources :

Example 1 with OkHttpDataSource

use of com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource in project ExoPlayer by google.

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(com.google.android.exoplayer2.upstream.DataSpec) HttpDataSource(com.google.android.exoplayer2.upstream.HttpDataSource) Test(org.junit.Test)

Example 2 with OkHttpDataSource

use of com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource in project ExoPlayer by google.

the class OkHttpDataSourceTest method factory_setRequestPropertyAfterCreation_setsCorrectHeaders.

@Test
public void factory_setRequestPropertyAfterCreation_setsCorrectHeaders() throws Exception {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.enqueue(new MockResponse());
    DataSpec dataSpec = new DataSpec.Builder().setUri(mockWebServer.url("/test-path").toString()).build();
    OkHttpDataSource.Factory factory = new OkHttpDataSource.Factory(new OkHttpClient());
    OkHttpDataSource dataSource = factory.createDataSource();
    Map<String, String> defaultRequestProperties = new HashMap<>();
    defaultRequestProperties.put("0", "afterCreation");
    factory.setDefaultRequestProperties(defaultRequestProperties);
    dataSource.open(dataSpec);
    Headers headers = mockWebServer.takeRequest(10, SECONDS).getHeaders();
    assertThat(headers.get("0")).isEqualTo("afterCreation");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpClient(okhttp3.OkHttpClient) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Test(org.junit.Test)

Example 3 with OkHttpDataSource

use of com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource in project ExoPlayer by google.

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(com.google.android.exoplayer2.upstream.DataSpec) HttpDataSource(com.google.android.exoplayer2.upstream.HttpDataSource) Test(org.junit.Test)

Aggregations

DataSpec (com.google.android.exoplayer2.upstream.DataSpec)3 OkHttpClient (okhttp3.OkHttpClient)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 MockWebServer (okhttp3.mockwebserver.MockWebServer)3 Test (org.junit.Test)3 HttpDataSource (com.google.android.exoplayer2.upstream.HttpDataSource)2 HashMap (java.util.HashMap)2 Headers (okhttp3.Headers)2