From 746d9c7fff775d77c34a363e8231188229798a92 Mon Sep 17 00:00:00 2001 From: mikr Date: Wed, 11 Dec 2019 11:31:30 +0100 Subject: [PATCH] http://team.baeldung.com/browse/BAEL-11412 --- .../okhttp/OkHttpPostingLiveTest.java | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/libraries-http/src/test/java/com/baeldung/okhttp/OkHttpPostingLiveTest.java b/libraries-http/src/test/java/com/baeldung/okhttp/OkHttpPostingLiveTest.java index 19e689c093..175cf28e78 100644 --- a/libraries-http/src/test/java/com/baeldung/okhttp/OkHttpPostingLiveTest.java +++ b/libraries-http/src/test/java/com/baeldung/okhttp/OkHttpPostingLiveTest.java @@ -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")