提交 728cb3b6 编写于 作者: J Jake Wharton

Eagerly catch malformed content type headers.

上级 f8521711
......@@ -75,7 +75,11 @@ final class RequestBuilder {
void addHeader(String name, String value) {
if ("Content-Type".equalsIgnoreCase(name)) {
contentType = MediaType.parse(value);
MediaType type = MediaType.parse(value);
if (type == null) {
throw new IllegalArgumentException("Malformed content type: " + value);
}
contentType = type;
} else {
requestBuilder.addHeader(name, value);
}
......
......@@ -314,7 +314,11 @@ final class ServiceMethod<T> {
String headerName = header.substring(0, colon);
String headerValue = header.substring(colon + 1).trim();
if ("Content-Type".equalsIgnoreCase(headerName)) {
contentType = MediaType.parse(headerValue);
MediaType type = MediaType.parse(headerValue);
if (type == null) {
throw methodError("Malformed content type: %s", headerValue);
}
contentType = type;
} else {
builder.add(headerName, headerValue);
}
......
......@@ -2395,6 +2395,24 @@ public final class RequestBuilderTest {
assertThat(request.body().contentType().toString()).isEqualTo("text/not-plain");
}
@Test public void malformedContentTypeHeaderThrows() {
class Example {
@POST("/") //
@Headers("Content-Type: hello, world!") //
Call<ResponseBody> method(@Body RequestBody body) {
return null;
}
}
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "hi");
try {
buildRequest(Example.class, body);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Malformed content type: hello, world!\n"
+ " for method Example.method");
}
}
@Test public void contentTypeAnnotationHeaderAddsHeaderWithNoBody() {
class Example {
@DELETE("/") //
......@@ -2419,6 +2437,22 @@ public final class RequestBuilderTest {
assertThat(request.body().contentType().toString()).isEqualTo("text/not-plain");
}
@Test public void malformedContentTypeParameterThrows() {
class Example {
@POST("/") //
Call<ResponseBody> method(@Header("Content-Type") String contentType, @Body RequestBody body) {
return null;
}
}
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "hi");
try {
buildRequest(Example.class, "hello, world!", body);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Malformed content type: hello, world!");
}
}
@Test public void malformedAnnotationRelativeUrlThrows() {
class Example {
@GET("ftp://example.org")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册