mikr 2019-12-11 11:31:30 +01:00
parent b7253c639b
commit 746d9c7fff
1 changed files with 38 additions and 4 deletions

View File

@ -38,9 +38,15 @@ public class OkHttpPostingLiveTest {
@Test
public void whenSendPostRequest_thenCorrect() throws IOException {
final RequestBody formBody = new FormBody.Builder().add("username", "test").add("password", "test").build();
final RequestBody formBody = new FormBody.Builder()
.add("username", "test")
.add("password", "test")
.build();
final Request request = new Request.Builder().url(BASE_URL + "/users").post(formBody).build();
final Request request = new Request.Builder()
.url(BASE_URL + "/users")
.post(formBody)
.build();
final Call call = client.newCall(request);
final Response response = call.execute();
@ -52,7 +58,11 @@ public class OkHttpPostingLiveTest {
public void whenSendPostRequestWithAuthorization_thenCorrect() throws IOException {
final String postBody = "test post";
final Request request = new Request.Builder().url(URL_SECURED_BY_BASIC_AUTHENTICATION).addHeader("Authorization", Credentials.basic("test", "test")).post(RequestBody.create(MediaType.parse("text/x-markdown; charset=utf-8"), "test post")).build();
final Request request = new Request.Builder()
.url(URL_SECURED_BY_BASIC_AUTHENTICATION)
.addHeader("Authorization", Credentials.basic("test", "test"))
.post(RequestBody.create(MediaType.parse("text/x-markdown"), "test post"))
.build();
final Call call = client.newCall(request);
final Response response = call.execute();
@ -64,7 +74,7 @@ public class OkHttpPostingLiveTest {
public void whenPostJson_thenCorrect() throws IOException {
final String json = "{\"id\":1,\"name\":\"John\"}";
final RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "{\"id\":1,\"name\":\"John\"}");
final RequestBody body = RequestBody.create(MediaType.parse("application/json"), "{\"id\":1,\"name\":\"John\"}");
final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build();
final Call call = client.newCall(request);
@ -73,6 +83,30 @@ public class OkHttpPostingLiveTest {
assertThat(response.code(), equalTo(200));
}
@Test
public void whenPostJsonWithoutCharset_thenCharsetIsUtf8() throws IOException {
final String json = "{\"id\":1,\"name\":\"John\"}";
final RequestBody body = RequestBody.create(
MediaType.parse("application/json"), json);
String charset = body.contentType().charset().displayName();
assertThat(charset, equalTo("UTF-8"));
}
@Test
public void whenPostJsonWithUtf16Charset_thenCharsetIsUtf16() throws IOException {
final String json = "{\"id\":1,\"name\":\"John\"}";
final RequestBody body = RequestBody.create(
MediaType.parse("application/json; charset=utf-16"), json);
String charset = body.contentType().charset().displayName();
assertThat(charset, equalTo("UTF-16"));
}
@Test
public void whenSendMultipartRequest_thenCorrect() throws IOException {
final RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("username", "test").addFormDataPart("password", "test")