Skip to content

Commit

Permalink
move some missing methods down to RequestBodyEntity so things can be …
Browse files Browse the repository at this point in the history
…done in different orders
  • Loading branch information
ryber committed Sep 26, 2020
1 parent 3bf3e47 commit 03e12b8
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 26 deletions.
9 changes: 1 addition & 8 deletions unirest/src/main/java/kong/unirest/HttpRequestBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,7 @@ public RequestBodyEntity body(String body) {

@Override
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));
return new HttpRequestUniBody(this).body(body);
}

@Override
Expand Down
19 changes: 19 additions & 0 deletions unirest/src/main/java/kong/unirest/HttpRequestUniBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

package kong.unirest;

import kong.unirest.json.JSONElement;

import java.nio.charset.Charset;
import java.util.Optional;

Expand All @@ -43,6 +45,23 @@ public RequestBodyEntity body(JsonNode jsonBody) {
return body(jsonBody.toString());
}

@Override
public RequestBodyEntity body(JSONElement jsonBody) {
return body(jsonBody.toString());
}

@Override
public RequestBodyEntity body(Object objectBody) {
if(objectBody instanceof String){
return body((String)objectBody);
} else if (objectBody instanceof JsonNode){
return body((JsonNode) objectBody);
} else if (objectBody instanceof JSONElement){
return body(objectBody.toString());
}
return body(getObjectMapper().writeValue(objectBody));
}

@Override
public RequestBodyEntity body(String bodyAsString) {
this.body = new UnibodyString(bodyAsString, charSet);
Expand Down
42 changes: 24 additions & 18 deletions unirest/src/main/java/kong/unirest/HttpRequestWithBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,12 @@
import java.util.Collection;
import java.util.Map;

/**
* A request Builder for POST and PUT operations with a body.
* will switch to a MultipartBody once http form variables are introduced.
* or to a RequestBodyEntity
*/
public interface HttpRequestWithBody extends HttpRequest<HttpRequestWithBody> {
/**
* Set the Charset encoding for the Content-Type. This is appended to the Content-Type Header
* (e.g. application/x-www-form-urlencoded; charset=US-ASCII)
* Default is UTF-8
* @param charset the charset
* @return this request builder
*/
HttpRequestWithBody charset(Charset charset);

/**
* Removes any Charset for the Content-Type for when servers cannot process it.
* (e.g. application/x-www-form-urlencoded)
* @return this request builder
*/
default HttpRequestWithBody noCharset() {
return charset(null);
}

/**
* Forces the request to send as multipart even if all params are simple
* @return The same MultipartBody
Expand Down Expand Up @@ -126,6 +113,25 @@ default HttpRequestWithBody noCharset() {
*/
MultipartBody field(String name, InputStream stream, ContentType contentType, String fileName);


/**
* Set the Charset encoding for the Content-Type. This is appended to the Content-Type Header
* (e.g. application/x-www-form-urlencoded; charset=US-ASCII)
* Default is UTF-8
* @param charset the charset
* @return this request builder
*/
HttpRequestWithBody charset(Charset charset);

/**
* Removes any Charset for the Content-Type for when servers cannot process it.
* (e.g. application/x-www-form-urlencoded)
* @return this request builder
*/
default HttpRequestWithBody noCharset() {
return charset(null);
}

/**
* Set a String as the body of the request
* @param body the String
Expand Down
50 changes: 50 additions & 0 deletions unirest/src/main/java/kong/unirest/RequestBodyEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,64 @@

package kong.unirest;

import kong.unirest.json.JSONElement;

import java.nio.charset.Charset;

public interface RequestBodyEntity extends HttpRequest<RequestBodyEntity>, Body {
/**
* Set a byte array as the body of the request
* @param bodyBytes the byte[]
* @return this request builder
*/
RequestBodyEntity body(byte[] bodyBytes);

/**
* Set a String as the body of the request
* @param bodyAsString the String
* @return this request builder
*/
RequestBodyEntity body(String bodyAsString);

/**
* Set JSON on the body
* @param jsonBody the JsonNode
* @return this request builder
*/
RequestBodyEntity body(JsonNode jsonBody);

/**
* Set JSON on the body
* @param body the JSONElement
* @return this request builder
*/
RequestBodyEntity body(JSONElement body);

/**
* Set a Object as the body of the request. This will be serialized with one of the following methods:
* - Strings are native
* - JSONElements use their native toString
* - Everything else will pass through the supplied ObjectMapper
* @param body the Object
* @return this request builder
*/
RequestBodyEntity body(Object body);

/**
* Set the Charset encoding for the Content-Type. This is appended to the Content-Type Header
* (e.g. application/x-www-form-urlencoded; charset=US-ASCII)
* Default is UTF-8
* @param charset the charset
* @return this request builder
*/
RequestBodyEntity charset(Charset charset);

/**
* Removes any Charset for the Content-Type for when servers cannot process it.
* (e.g. application/x-www-form-urlencoded)
* @return this request builder
*/
default RequestBodyEntity noCharset() {
return charset(null);
}
}
31 changes: 31 additions & 0 deletions unirest/src/test/java/BehaviorTests/AsObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import kong.unirest.*;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -120,6 +121,36 @@ public void canPassAnObjectMapperAsPartOfARequest(){
assertTrue(mapper.writeWasCalled);
}

@Test
void canOverrideBody() {
Unirest.post(MockServer.POST)
.body(new Foo("Apple"))
.body(new Foo("Orange"))
.asObject(RequestCapture.class)
.getBody()
.asserBody("{\"bar\":\"Orange\"}");
}

@Test
void setCharSetAfterBody() {
Unirest.post(MockServer.POST)
.body(new Foo("Orange"))
.charset(StandardCharsets.US_ASCII)
.asObject(RequestCapture.class)
.getBody()
.assertContentType("text/plain; charset=US-ASCII");
}

@Test
void setNoCharSetAfterBody() {
Unirest.post(MockServer.POST)
.body(new Foo("Orange"))
.noCharset()
.asObject(RequestCapture.class)
.getBody()
.assertContentType("text/plain");
}

@Test
public void ifTheObjectMapperFailsReturnEmptyAndAddToParsingError() {
HttpResponse<RequestCapture> request = Unirest.get(MockServer.INVALID_REQUEST)
Expand Down

0 comments on commit 03e12b8

Please sign in to comment.