Use Guava's HttpHeaders consistently in HTTP testing machinery

PiperOrigin-RevId: 688576776
(cherry picked from commit cabc541a6fbdd90286bb425abbf147bf94d10b73)
This commit is contained in:
ibaker 2024-10-22 09:18:44 -07:00 committed by Iván Budnik
parent e6849e082c
commit 70c7ee2e0c
3 changed files with 94 additions and 87 deletions

View File

@ -144,7 +144,8 @@ public class HttpDataSourceTestEnv extends ExternalResource {
if (getRequestPath(request).equals(REDIRECTS_TO_RANGE_SUPPORTED.getPath())) { if (getRequestPath(request).equals(REDIRECTS_TO_RANGE_SUPPORTED.getPath())) {
return new MockResponse() return new MockResponse()
.setResponseCode(302) .setResponseCode(302)
.setHeader("Location", originServer.url(RANGE_SUPPORTED.getPath()).toString()); .setHeader(
HttpHeaders.LOCATION, originServer.url(RANGE_SUPPORTED.getPath()).toString());
} else { } else {
return new MockResponse().setResponseCode(404); return new MockResponse().setResponseCode(404);
} }

View File

@ -36,6 +36,7 @@ import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.net.HttpHeaders;
import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
@ -301,7 +302,7 @@ public class WebServerDispatcher extends Dispatcher {
} }
byte[] resourceData = resource.getData(); byte[] resourceData = resource.getData();
if (resource.supportsRangeRequests()) { if (resource.supportsRangeRequests()) {
response.setHeader("Accept-Ranges", "bytes"); response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
} }
@Nullable ImmutableMap<String, Float> acceptEncodingHeader = getAcceptEncodingHeader(request); @Nullable ImmutableMap<String, Float> acceptEncodingHeader = getAcceptEncodingHeader(request);
@Nullable String preferredContentCoding; @Nullable String preferredContentCoding;
@ -320,17 +321,17 @@ public class WebServerDispatcher extends Dispatcher {
return response.setResponseCode(406); return response.setResponseCode(406);
} }
@Nullable String rangeHeader = request.getHeader("Range"); @Nullable String rangeHeader = request.getHeader(HttpHeaders.RANGE);
if (!resource.supportsRangeRequests() || rangeHeader == null) { if (!resource.supportsRangeRequests() || rangeHeader == null) {
switch (preferredContentCoding) { switch (preferredContentCoding) {
case "gzip": case "gzip":
setResponseBody( setResponseBody(
response, Util.gzip(resourceData), /* chunked= */ resource.resolvesToUnknownLength); response, Util.gzip(resourceData), /* chunked= */ resource.resolvesToUnknownLength);
response.setHeader("Content-Encoding", "gzip"); response.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
break; break;
case "identity": case "identity":
setResponseBody(response, resourceData, /* chunked= */ resource.resolvesToUnknownLength); setResponseBody(response, resourceData, /* chunked= */ resource.resolvesToUnknownLength);
response.setHeader("Content-Encoding", "identity"); response.setHeader(HttpHeaders.CONTENT_ENCODING, "identity");
break; break;
default: default:
throw new IllegalStateException("Unexpected content coding: " + preferredContentCoding); throw new IllegalStateException("Unexpected content coding: " + preferredContentCoding);
@ -344,7 +345,7 @@ public class WebServerDispatcher extends Dispatcher {
if (range == null || (range.first != null && range.first >= resourceData.length)) { if (range == null || (range.first != null && range.first >= resourceData.length)) {
return response return response
.setResponseCode(416) .setResponseCode(416)
.setHeader("Content-Range", "bytes */" + resourceData.length); .setHeader(HttpHeaders.CONTENT_RANGE, "bytes */" + resourceData.length);
} }
if (range.first == null || range.second == null) { if (range.first == null || range.second == null) {
@ -355,7 +356,7 @@ public class WebServerDispatcher extends Dispatcher {
// Can't return the suffix of an unknown-length resource. // Can't return the suffix of an unknown-length resource.
return response return response
.setResponseCode(416) .setResponseCode(416)
.setHeader("Content-Range", "bytes */" + resourceData.length); .setHeader(HttpHeaders.CONTENT_RANGE, "bytes */" + resourceData.length);
} }
start = max(0, resourceData.length - checkNotNull(range.second)); start = max(0, resourceData.length - checkNotNull(range.second));
} else { } else {
@ -365,7 +366,7 @@ public class WebServerDispatcher extends Dispatcher {
response response
.setResponseCode(206) .setResponseCode(206)
.setHeader( .setHeader(
"Content-Range", HttpHeaders.CONTENT_RANGE,
"bytes " "bytes "
+ start + start
+ "-" + "-"
@ -384,14 +385,14 @@ public class WebServerDispatcher extends Dispatcher {
if (range.second < range.first) { if (range.second < range.first) {
return response return response
.setResponseCode(416) .setResponseCode(416)
.setHeader("Content-Range", "bytes */" + resourceData.length); .setHeader(HttpHeaders.CONTENT_RANGE, "bytes */" + resourceData.length);
} }
int end = min(range.second + 1, resourceData.length); int end = min(range.second + 1, resourceData.length);
response response
.setResponseCode(206) .setResponseCode(206)
.setHeader( .setHeader(
"Content-Range", HttpHeaders.CONTENT_RANGE,
"bytes " "bytes "
+ range.first + range.first
+ "-" + "-"
@ -428,7 +429,8 @@ public class WebServerDispatcher extends Dispatcher {
*/ */
@Nullable @Nullable
private static ImmutableMap<String, Float> getAcceptEncodingHeader(RecordedRequest request) { private static ImmutableMap<String, Float> getAcceptEncodingHeader(RecordedRequest request) {
@Nullable List<String> headers = request.getHeaders().toMultimap().get("Accept-Encoding"); @Nullable
List<String> headers = request.getHeaders().toMultimap().get(HttpHeaders.ACCEPT_ENCODING);
if (headers == null) { if (headers == null) {
return null; return null;
} }

View File

@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import androidx.media3.common.util.Util; import androidx.media3.common.util.Util;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.net.HttpHeaders;
import java.util.Arrays; import java.util.Arrays;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
@ -123,9 +124,9 @@ public class WebServerDispatcherTest {
Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_PATH)).build(); Request request = new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_PATH)).build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(200); assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes"); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(response.header("Content-Length")).isEqualTo("20"); assertThat(response.header(HttpHeaders.CONTENT_LENGTH)).isEqualTo("20");
assertThat(response.header("Content-Range")).isNull(); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isNull();
assertThat(response.body().bytes()).isEqualTo(RANGE_SUPPORTED_DATA); assertThat(response.body().bytes()).isEqualTo(RANGE_SUPPORTED_DATA);
} }
} }
@ -136,13 +137,13 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Range", "bytes=5-10") .addHeader(HttpHeaders.RANGE, "bytes=5-10")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(206); assertThat(response.code()).isEqualTo(206);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes"); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(response.header("Content-Length")).isEqualTo("6"); assertThat(response.header(HttpHeaders.CONTENT_LENGTH)).isEqualTo("6");
assertThat(response.header("Content-Range")).isEqualTo("bytes 5-10/20"); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes 5-10/20");
assertThat(response.body().bytes()) assertThat(response.body().bytes())
.isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_DATA, 5, 11)); .isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_DATA, 5, 11));
} }
@ -154,13 +155,13 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Range", "bytes=5-") .addHeader(HttpHeaders.RANGE, "bytes=5-")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(206); assertThat(response.code()).isEqualTo(206);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes"); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(response.header("Content-Length")).isEqualTo("15"); assertThat(response.header(HttpHeaders.CONTENT_LENGTH)).isEqualTo("15");
assertThat(response.header("Content-Range")).isEqualTo("bytes 5-19/20"); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes 5-19/20");
assertThat(response.body().bytes()) assertThat(response.body().bytes())
.isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_DATA, 5, 20)); .isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_DATA, 5, 20));
} }
@ -172,13 +173,13 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Range", "bytes=-5") .addHeader(HttpHeaders.RANGE, "bytes=-5")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(206); assertThat(response.code()).isEqualTo(206);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes"); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(response.header("Content-Length")).isEqualTo("5"); assertThat(response.header(HttpHeaders.CONTENT_LENGTH)).isEqualTo("5");
assertThat(response.header("Content-Range")).isEqualTo("bytes 15-19/20"); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes 15-19/20");
assertThat(response.body().bytes()) assertThat(response.body().bytes())
.isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_DATA, 15, 20)); .isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_DATA, 15, 20));
} }
@ -190,13 +191,13 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Range", "bytes=5-25") .addHeader(HttpHeaders.RANGE, "bytes=5-25")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(206); assertThat(response.code()).isEqualTo(206);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes"); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(response.header("Content-Length")).isEqualTo("15"); assertThat(response.header(HttpHeaders.CONTENT_LENGTH)).isEqualTo("15");
assertThat(response.header("Content-Range")).isEqualTo("bytes 5-19/20"); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes 5-19/20");
assertThat(response.body().bytes()) assertThat(response.body().bytes())
.isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_DATA, 5, 20)); .isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_DATA, 5, 20));
} }
@ -208,13 +209,13 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Range", "bytes=-25") .addHeader(HttpHeaders.RANGE, "bytes=-25")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(206); assertThat(response.code()).isEqualTo(206);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes"); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(response.header("Content-Length")).isEqualTo("20"); assertThat(response.header(HttpHeaders.CONTENT_LENGTH)).isEqualTo("20");
assertThat(response.header("Content-Range")).isEqualTo("bytes 0-19/20"); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes 0-19/20");
assertThat(response.body().bytes()).isEqualTo(RANGE_SUPPORTED_DATA); assertThat(response.body().bytes()).isEqualTo(RANGE_SUPPORTED_DATA);
} }
} }
@ -225,12 +226,12 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Range", "bytes=20-25") .addHeader(HttpHeaders.RANGE, "bytes=20-25")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(416); assertThat(response.code()).isEqualTo(416);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes"); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(response.header("Content-Range")).isEqualTo("bytes */20"); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes */20");
} }
} }
@ -240,12 +241,12 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Range", "bytes=25-30") .addHeader(HttpHeaders.RANGE, "bytes=25-30")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(416); assertThat(response.code()).isEqualTo(416);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes"); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(response.header("Content-Range")).isEqualTo("bytes */20"); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes */20");
} }
} }
@ -256,9 +257,9 @@ public class WebServerDispatcherTest {
new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH)).build(); new Request.Builder().url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH)).build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(200); assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes"); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(response.header("Content-Length")).isNull(); assertThat(response.header(HttpHeaders.CONTENT_LENGTH)).isNull();
assertThat(response.header("Content-Range")).isNull(); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isNull();
assertThat(response.body().contentLength()).isEqualTo(-1); assertThat(response.body().contentLength()).isEqualTo(-1);
// Calling ResponseBody#bytes() times out because Content-Length isn't set, so instead we // Calling ResponseBody#bytes() times out because Content-Length isn't set, so instead we
@ -275,13 +276,13 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH))
.addHeader("Range", "bytes=5-10") .addHeader(HttpHeaders.RANGE, "bytes=5-10")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(206); assertThat(response.code()).isEqualTo(206);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes"); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(response.header("Content-Length")).isEqualTo("6"); assertThat(response.header(HttpHeaders.CONTENT_LENGTH)).isEqualTo("6");
assertThat(response.header("Content-Range")).isEqualTo("bytes 5-10/*"); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes 5-10/*");
assertThat(response.body().bytes()) assertThat(response.body().bytes())
.isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_LENGTH_UNKNOWN_DATA, 5, 11)); .isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_LENGTH_UNKNOWN_DATA, 5, 11));
} }
@ -293,13 +294,13 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH))
.addHeader("Range", "bytes=5-") .addHeader(HttpHeaders.RANGE, "bytes=5-")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(206); assertThat(response.code()).isEqualTo(206);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes"); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(response.header("Content-Length")).isNull(); assertThat(response.header(HttpHeaders.CONTENT_LENGTH)).isNull();
assertThat(response.header("Content-Range")).isEqualTo("bytes 5-19/*"); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes 5-19/*");
assertThat(response.body().contentLength()).isEqualTo(-1); assertThat(response.body().contentLength()).isEqualTo(-1);
// Calling ResponseBody#bytes() times out because Content-Length isn't set, so instead we // Calling ResponseBody#bytes() times out because Content-Length isn't set, so instead we
@ -318,13 +319,13 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH))
.addHeader("Range", "bytes=5-25") .addHeader(HttpHeaders.RANGE, "bytes=5-25")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(206); assertThat(response.code()).isEqualTo(206);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes"); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(response.header("Content-Length")).isEqualTo("15"); assertThat(response.header(HttpHeaders.CONTENT_LENGTH)).isEqualTo("15");
assertThat(response.header("Content-Range")).isEqualTo("bytes 5-19/*"); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes 5-19/*");
assertThat(response.body().bytes()) assertThat(response.body().bytes())
.isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_LENGTH_UNKNOWN_DATA, 5, 20)); .isEqualTo(Arrays.copyOfRange(RANGE_SUPPORTED_LENGTH_UNKNOWN_DATA, 5, 20));
} }
@ -336,7 +337,7 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_LENGTH_UNKNOWN_PATH))
.addHeader("Range", "bytes=-5") .addHeader(HttpHeaders.RANGE, "bytes=-5")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(416); assertThat(response.code()).isEqualTo(416);
@ -349,12 +350,12 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Range", "bytes=15-10") .addHeader(HttpHeaders.RANGE, "bytes=15-10")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(416); assertThat(response.code()).isEqualTo(416);
assertThat(response.header("Accept-Ranges")).isEqualTo("bytes"); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(response.header("Content-Range")).isEqualTo("bytes */20"); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes */20");
} }
} }
@ -364,7 +365,7 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Range", "seconds=5-10") .addHeader(HttpHeaders.RANGE, "seconds=5-10")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(416); assertThat(response.code()).isEqualTo(416);
@ -377,7 +378,7 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Range", "bytes=2-6,10-12") .addHeader(HttpHeaders.RANGE, "bytes=2-6,10-12")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(416); assertThat(response.code()).isEqualTo(416);
@ -390,7 +391,7 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Range", "bytes=foo") .addHeader(HttpHeaders.RANGE, "bytes=foo")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(416); assertThat(response.code()).isEqualTo(416);
@ -404,9 +405,9 @@ public class WebServerDispatcherTest {
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(200); assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Accept-Ranges")).isNull(); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isNull();
assertThat(response.header("Content-Length")).isEqualTo("20"); assertThat(response.header(HttpHeaders.CONTENT_LENGTH)).isEqualTo("20");
assertThat(response.header("Content-Range")).isNull(); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isNull();
assertThat(response.body().bytes()).isEqualTo(RANGE_UNSUPPORTED_DATA); assertThat(response.body().bytes()).isEqualTo(RANGE_UNSUPPORTED_DATA);
} }
} }
@ -417,13 +418,13 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_UNSUPPORTED_PATH)) .url(mockWebServer.url(RANGE_UNSUPPORTED_PATH))
.addHeader("Range", "bytes=5-10") .addHeader(HttpHeaders.RANGE, "bytes=5-10")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(200); assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Accept-Ranges")).isNull(); assertThat(response.header(HttpHeaders.ACCEPT_RANGES)).isNull();
assertThat(response.header("Content-Length")).isEqualTo("20"); assertThat(response.header(HttpHeaders.CONTENT_LENGTH)).isEqualTo("20");
assertThat(response.header("Content-Range")).isNull(); assertThat(response.header(HttpHeaders.CONTENT_RANGE)).isNull();
assertThat(response.body().bytes()).isEqualTo(RANGE_UNSUPPORTED_DATA); assertThat(response.body().bytes()).isEqualTo(RANGE_UNSUPPORTED_DATA);
} }
} }
@ -434,12 +435,12 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Accept-Encoding", "*") .addHeader(HttpHeaders.ACCEPT_ENCODING, "*")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(200); assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Content-Encoding")).isEqualTo("identity"); assertThat(response.header("Content-Encoding")).isEqualTo("identity");
assertThat(response.header("Content-Length")) assertThat(response.header(HttpHeaders.CONTENT_LENGTH))
.isEqualTo(String.valueOf(RANGE_SUPPORTED_DATA.length)); .isEqualTo(String.valueOf(RANGE_SUPPORTED_DATA.length));
assertThat(response.body().bytes()).isEqualTo(RANGE_SUPPORTED_DATA); assertThat(response.body().bytes()).isEqualTo(RANGE_SUPPORTED_DATA);
} }
@ -451,8 +452,8 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Accept-Encoding", "gzip;q=1.0") .addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip;q=1.0")
.addHeader("Accept-Encoding", "identity;q=0") .addHeader(HttpHeaders.ACCEPT_ENCODING, "identity;q=0")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(406); assertThat(response.code()).isEqualTo(406);
@ -466,8 +467,8 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(RANGE_SUPPORTED_PATH)) .url(mockWebServer.url(RANGE_SUPPORTED_PATH))
.addHeader("Accept-Encoding", "gzip;q=1.0") .addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip;q=1.0")
.addHeader("Accept-Encoding", "*;q=0") .addHeader(HttpHeaders.ACCEPT_ENCODING, "*;q=0")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(406); assertThat(response.code()).isEqualTo(406);
@ -480,13 +481,14 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(GZIP_ENABLED_PATH)) .url(mockWebServer.url(GZIP_ENABLED_PATH))
.addHeader("Accept-Encoding", "gzip") .addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
byte[] expectedData = Util.gzip(GZIP_ENABLED_DATA); byte[] expectedData = Util.gzip(GZIP_ENABLED_DATA);
assertThat(response.code()).isEqualTo(200); assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Content-Encoding")).isEqualTo("gzip"); assertThat(response.header("Content-Encoding")).isEqualTo("gzip");
assertThat(response.header("Content-Length")).isEqualTo(String.valueOf(expectedData.length)); assertThat(response.header(HttpHeaders.CONTENT_LENGTH))
.isEqualTo(String.valueOf(expectedData.length));
assertThat(response.body().bytes()).isEqualTo(expectedData); assertThat(response.body().bytes()).isEqualTo(expectedData);
} }
} }
@ -497,14 +499,15 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(GZIP_ENABLED_PATH)) .url(mockWebServer.url(GZIP_ENABLED_PATH))
.addHeader("Accept-Encoding", "*") .addHeader(HttpHeaders.ACCEPT_ENCODING, "*")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
byte[] expectedData = Util.gzip(GZIP_ENABLED_DATA); byte[] expectedData = Util.gzip(GZIP_ENABLED_DATA);
assertThat(response.code()).isEqualTo(200); assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Content-Encoding")).isEqualTo("gzip"); assertThat(response.header("Content-Encoding")).isEqualTo("gzip");
assertThat(response.header("Content-Length")).isEqualTo(String.valueOf(expectedData.length)); assertThat(response.header(HttpHeaders.CONTENT_LENGTH))
.isEqualTo(String.valueOf(expectedData.length));
assertThat(response.body().bytes()).isEqualTo(expectedData); assertThat(response.body().bytes()).isEqualTo(expectedData);
} }
} }
@ -516,12 +519,12 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(GZIP_ENABLED_PATH)) .url(mockWebServer.url(GZIP_ENABLED_PATH))
.addHeader("Accept-Encoding", "identity;q=0.8, gzip;q=0.2") .addHeader(HttpHeaders.ACCEPT_ENCODING, "identity;q=0.8, gzip;q=0.2")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(200); assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Content-Encoding")).isEqualTo("identity"); assertThat(response.header("Content-Encoding")).isEqualTo("identity");
assertThat(response.header("Content-Length")) assertThat(response.header(HttpHeaders.CONTENT_LENGTH))
.isEqualTo(String.valueOf(GZIP_ENABLED_DATA.length)); .isEqualTo(String.valueOf(GZIP_ENABLED_DATA.length));
assertThat(response.body().bytes()).isEqualTo(GZIP_ENABLED_DATA); assertThat(response.body().bytes()).isEqualTo(GZIP_ENABLED_DATA);
} }
@ -533,12 +536,12 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(GZIP_ENABLED_PATH)) .url(mockWebServer.url(GZIP_ENABLED_PATH))
.addHeader("Accept-Encoding", "identity") .addHeader(HttpHeaders.ACCEPT_ENCODING, "identity")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(200); assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Content-Encoding")).isEqualTo("identity"); assertThat(response.header("Content-Encoding")).isEqualTo("identity");
assertThat(response.header("Content-Length")) assertThat(response.header(HttpHeaders.CONTENT_LENGTH))
.isEqualTo(String.valueOf(GZIP_ENABLED_DATA.length)); .isEqualTo(String.valueOf(GZIP_ENABLED_DATA.length));
assertThat(response.body().bytes()).isEqualTo(GZIP_ENABLED_DATA); assertThat(response.body().bytes()).isEqualTo(GZIP_ENABLED_DATA);
} }
@ -550,14 +553,15 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(GZIP_FORCED_PATH)) .url(mockWebServer.url(GZIP_FORCED_PATH))
.addHeader("Accept-Encoding", "gzip") .addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
byte[] expectedData = Util.gzip(GZIP_FORCED_DATA); byte[] expectedData = Util.gzip(GZIP_FORCED_DATA);
assertThat(response.code()).isEqualTo(200); assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Content-Encoding")).isEqualTo("gzip"); assertThat(response.header("Content-Encoding")).isEqualTo("gzip");
assertThat(response.header("Content-Length")).isEqualTo(String.valueOf(expectedData.length)); assertThat(response.header(HttpHeaders.CONTENT_LENGTH))
.isEqualTo(String.valueOf(expectedData.length));
assertThat(response.body().bytes()).isEqualTo(expectedData); assertThat(response.body().bytes()).isEqualTo(expectedData);
} }
} }
@ -568,12 +572,12 @@ public class WebServerDispatcherTest {
Request request = Request request =
new Request.Builder() new Request.Builder()
.url(mockWebServer.url(GZIP_FORCED_PATH)) .url(mockWebServer.url(GZIP_FORCED_PATH))
.addHeader("Accept-Encoding", "identity") .addHeader(HttpHeaders.ACCEPT_ENCODING, "identity")
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
assertThat(response.code()).isEqualTo(200); assertThat(response.code()).isEqualTo(200);
assertThat(response.header("Content-Encoding")).isEqualTo("identity"); assertThat(response.header("Content-Encoding")).isEqualTo("identity");
assertThat(response.header("Content-Length")) assertThat(response.header(HttpHeaders.CONTENT_LENGTH))
.isEqualTo(String.valueOf(GZIP_FORCED_DATA.length)); .isEqualTo(String.valueOf(GZIP_FORCED_DATA.length));
assertThat(response.body().bytes()).isEqualTo(GZIP_FORCED_DATA); assertThat(response.body().bytes()).isEqualTo(GZIP_FORCED_DATA);
} }