use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.
the class WebServerDispatcherTest method rangeRequestsSupported_lengthUnknown_rejectsSuffixRange.
@Test
public void rangeRequestsSupported_lengthUnknown_rejectsSuffixRange() throws Exception {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH)).addHeader("Range", "bytes=-5").build();
try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(416);
}
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.
the class WebServerDispatcherTest method rangeRequestsSupported_truncatesBoundedRangeToLength.
@Test
public void rangeRequestsSupported_truncatesBoundedRangeToLength() throws Exception {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_PATH)).addHeader("Range", "bytes=5-25").build();
try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(206);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes");
assertThat(response.header("Content-Length")).isEqualTo("15");
assertThat(response.header("Content-Range")).isEqualTo("bytes 5-19/20");
assertThat(response.body().bytes()).isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_DATA, 5, 20));
}
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.
the class WebServerDispatcherTest method gzipDisabled_acceptEncodingHeaderAllowsAnyCoding_identityResponse.
@Test
public void gzipDisabled_acceptEncodingHeaderAllowsAnyCoding_identityResponse() throws Exception {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_PATH)).addHeader("Accept-Encoding", "*").build();
try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Content-Encoding")).isEqualTo("identity");
assertThat(response.header("Content-Length")).isEqualTo(String.valueOf(RANGE_SUPPORTED_DATA.length));
assertThat(response.body().bytes()).isEqualTo(RANGE_SUPPORTED_DATA);
}
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.
the class WebServerDispatcherTest method gzipEnabled_acceptEncodingHeaderPrefersIdentity_responseNotGzipped.
@Test
public void gzipEnabled_acceptEncodingHeaderPrefersIdentity_responseNotGzipped() throws Exception {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(mockWebServer.url(GZIP_ENABLED_PATH)).addHeader("Accept-Encoding", "identity;q=0.8, gzip;q=0.2").build();
try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Content-Encoding")).isEqualTo("identity");
assertThat(response.header("Content-Length")).isEqualTo(String.valueOf(GZIP_ENABLED_DATA.length));
assertThat(response.body().bytes()).isEqualTo(GZIP_ENABLED_DATA);
}
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.
the class WebServerDispatcherTest method rangeRequestsSupported_lengthUnknown_startOnly.
@Test
public void rangeRequestsSupported_lengthUnknown_startOnly() throws Exception {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH)).addHeader("Range", "bytes=5-").build();
try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(206);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes");
assertThat(response.header("Content-Length")).isNull();
assertThat(response.header("Content-Range")).isEqualTo("bytes 5-19/*");
assertThat(response.body().contentLength()).isEqualTo(-1);
// Calling ResponseBody#bytes() times out because Content-Length isn't set, so instead we
// read exactly the number of bytes we expect.
byte[] actualBytes = new byte[15];
response.body().byteStream().read(actualBytes);
assertThat(actualBytes).isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_LENGTH_UNKNOWN_DATA, 5, 20));
}
}
Aggregations