Skip to content

Commit

Permalink
Add tests for form and multipart header values and overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Nov 16, 2018
1 parent 69a81a9 commit 07cb4ae
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion retrofit/src/test/java/retrofit2/RequestFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,8 @@ Call<ResponseBody> method(@Part("ping") String ping, @Part("kit") RequestBody ki
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");

RequestBody body = request.body();
assertThat(body.contentType().toString()).startsWith("multipart/form-data; boundary=");

Buffer buffer = new Buffer();
body.writeTo(buffer);
String bodyString = buffer.readUtf8();
Expand Down Expand Up @@ -2316,7 +2318,9 @@ Call<ResponseBody> method(@Field("foo") String foo, @Field("ping") String ping)
}
}
Request request = buildRequest(Example.class, "bar", "pong");
assertBody(request.body(), "foo=bar&ping=pong");
RequestBody body = request.body();
assertBody(body, "foo=bar&ping=pong");
assertThat(body.contentType().toString()).isEqualTo("application/x-www-form-urlencoded");
}

@Test public void formEncodedWithEncodedNameFieldParam() {
Expand Down Expand Up @@ -2639,6 +2643,36 @@ Call<ResponseBody> method(@Body RequestBody body) {
assertThat(request.body().contentType().toString()).isEqualTo("text/not-plain");
}

@Test public void contentTypeAnnotationHeaderOverridesFormEncoding() {
class Example {
@FormUrlEncoded //
@POST("/foo") //
@Headers("Content-Type: text/not-plain") //
Call<ResponseBody> method(@Field("foo") String foo, @Field("ping") String ping) {
return null;
}
}
Request request = buildRequest(Example.class, "bar", "pong");
assertThat(request.body().contentType().toString()).isEqualTo("text/not-plain");
}

@Test public void contentTypeAnnotationHeaderOverridesMultipart() {
class Example {
@Multipart //
@POST("/foo/bar/") //
@Headers("Content-Type: text/not-plain") //
Call<ResponseBody> method(@Part("ping") String ping, @Part("kit") RequestBody kit) {
return null;
}
}

Request request = buildRequest(Example.class, "pong", RequestBody.create(
TEXT_PLAIN, "kat"));

RequestBody body = request.body();
assertThat(request.body().contentType().toString()).isEqualTo("text/not-plain");
}

@Test public void malformedContentTypeHeaderThrows() {
class Example {
@POST("/") //
Expand Down

0 comments on commit 07cb4ae

Please sign in to comment.