Skip to content

Commit

Permalink
side effect of #362. Should also bypass object mapper when dealing wi…
Browse files Browse the repository at this point in the history
…th native json types
  • Loading branch information
ryber committed Aug 2, 2020
1 parent 664425f commit 925ec01
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions unirest/src/main/java/kong/unirest/HttpRequestBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package kong.unirest;

import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONElement;
import kong.unirest.json.JSONObject;

import java.io.File;
Expand Down Expand Up @@ -109,6 +110,10 @@ public RequestBodyEntity body(String body) {
public RequestBodyEntity body(Object body) {
if(body instanceof String){
return body((String)body);
} else if (body instanceof JsonNode){
return body((JsonNode) body);
} else if (body instanceof JSONElement){
return body(body.toString());
}
return body(getObjectMapper().writeValue(body));
}
Expand Down
40 changes: 40 additions & 0 deletions unirest/src/test/java/BehaviorTests/UniBodyPostingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,44 @@ void stringPassedToObjectGetsPassedToString() {
Object value = request.getBody().get().uniPart().getValue();
assertEquals("{\"body\": \"sample\"}", value);
}

@Test
void jsonNodePassedToObjectGetsPassedToString() {
Object body = new JsonNode("{\"body\": \"sample\"}");
RequestBodyEntity request = Unirest.post(MockServer.POST)
.basicAuth("foo", "bar")
.header("Content-Type", "application/json")
.queryString("foo", "bar")
.body(body);

Object value = request.getBody().get().uniPart().getValue();
assertEquals("{\"body\":\"sample\"}", value);
}

@Test
void jsonObjectPassedToObjectGetsPassedToString() {
Object body = new JSONObject("{\"body\": \"sample\"}");
RequestBodyEntity request = Unirest.post(MockServer.POST)
.basicAuth("foo", "bar")
.header("Content-Type", "application/json")
.queryString("foo", "bar")
.body(body);

Object value = request.getBody().get().uniPart().getValue();
assertEquals("{\"body\":\"sample\"}", value);
}

@Test
void jsonArrayPassedToObjectGetsPassedToString() {
Object body = new JSONArray("[\"body\", \"sample\"]");
RequestBodyEntity request = Unirest.post(MockServer.POST)
.basicAuth("foo", "bar")
.header("Content-Type", "application/json")
.queryString("foo", "bar")
.body(body);

Object value = request.getBody().get().uniPart().getValue();
assertEquals("[\"body\",\"sample\"]", value);
}

}

0 comments on commit 925ec01

Please sign in to comment.