use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.
the class WebServerDispatcherTest method rangeRequestsSupported_lengthUnknown_truncatesBoundedRangeToLength.
@Test
public void rangeRequestsSupported_lengthUnknown_truncatesBoundedRangeToLength() throws Exception {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_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/*");
assertThat(response.body().bytes()).isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_LENGTH_UNKNOWN_DATA, 5, 20));
}
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.
the class WebServerDispatcherTest method rangeRequestsSupported_lengthUnknown_handlesConventionalRequest.
@Test
public void rangeRequestsSupported_lengthUnknown_handlesConventionalRequest() throws Exception {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH)).build();
try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes");
assertThat(response.header("Content-Length")).isNull();
assertThat(response.header("Content-Range")).isNull();
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[20];
response.body().byteStream().read(actualBytes);
assertThat(actualBytes).isEqualTo(RANGE_SUPPORTED_LENGTH_UNKNOWN_DATA);
}
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project media by androidx.
the class WebServerDispatcherTest method gzipForced_acceptEncodingHeaderExcludesGzip_responseNotGzipped.
@Test
public void gzipForced_acceptEncodingHeaderExcludesGzip_responseNotGzipped() throws Exception {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(mockWebServer.url(GZIP_FORCED_PATH)).addHeader("Accept-Encoding", "identity").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_FORCED_DATA.length));
assertThat(response.body().bytes()).isEqualTo(GZIP_FORCED_DATA);
}
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project dash-wallet by dashevo.
the class DynamicFeeLoader method fetchDynamicFees.
private static void fetchDynamicFees(final HttpUrl url, final File tempFile, final File targetFile, final String userAgent) {
final Stopwatch watch = Stopwatch.createStarted();
final Request.Builder requestBuilder = new Request.Builder().url(url).header("User-Agent", userAgent);
if (targetFile.exists())
requestBuilder.header("If-Modified-Since", HttpDate.format(new Date(targetFile.lastModified())));
final OkHttpClient httpClient = Constants.HTTP_CLIENT.newBuilder().connectTimeout(5, TimeUnit.SECONDS).writeTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS).build();
final Call call = httpClient.newCall(requestBuilder.build());
try {
final Response response = call.execute();
final int status = response.code();
if (status == HttpURLConnection.HTTP_NOT_MODIFIED) {
log.info("Dynamic fees not modified at {}, took {}", url, watch);
} else if (status == HttpURLConnection.HTTP_OK) {
final ResponseBody body = response.body();
final FileOutputStream os = new FileOutputStream(tempFile);
Io.copy(body.byteStream(), os);
os.close();
final Date lastModified = response.headers().getDate("Last-Modified");
if (lastModified != null)
tempFile.setLastModified(lastModified.getTime());
body.close();
if (!tempFile.renameTo(targetFile))
throw new IllegalStateException("Cannot rename " + tempFile + " to " + targetFile);
watch.stop();
log.info("Dynamic fees fetched from {}, took {}", url, watch);
} else {
log.warn("HTTP status {} when fetching dynamic fees from {}", response.code(), url);
}
} catch (final Exception x) {
log.warn("Problem when fetching dynamic fees rates from " + url, x);
}
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project java-sdk by dapr.
the class DaprHttpClientTest method setUp.
@Before
public void setUp() throws Exception {
mockInterceptor = new MockInterceptor(Behavior.UNORDERED);
okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build();
}
Aggregations