Skip to content

Commit

Permalink
issue #394 use the configured Object Mapper rather than always Json
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Jan 9, 2021
1 parent 685d8f8 commit 3feb93a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## 3.11.10 (pending)
* issue #394 use the configured Object Mapper rather than always Json
* internal pre-factorings to get ready for Unirest 4

## 3.11.09
Expand Down
2 changes: 1 addition & 1 deletion unirest-mocks/src/main/java/kong/unirest/Expectation.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public interface Expectation {
ExpectedResponse thenReturn(JSONElement jsonObject);

/**
* expect a json response as defined by a pojo
* expect a object response as defined by a pojo using the requests / configuration object mapper
* @param pojo the expected response body
* @return The ExpectedResponse
*/
Expand Down
19 changes: 13 additions & 6 deletions unirest-mocks/src/main/java/kong/unirest/Invocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.lang.System.lineSeparator;

class Invocation implements Expectation, ExpectedResponse {
private Routes routes;
private String response;
private Function<ObjectMapper, String> response = o -> null;
private Headers expectedHeaders = new Headers();
private Headers expectedQueryParams = new Headers();
private List<HttpRequest> requests = new ArrayList<>();
Expand All @@ -60,24 +61,30 @@ public Invocation(Routes routes, HttpRequest request) {

@Override
public ExpectedResponse thenReturn(String body) {
this.response = body;
this.response = o -> body;
return this;
}

@Override
public ExpectedResponse thenReturn(JSONElement jsonObject) {
this.response = jsonObject.toString();
this.response = o -> jsonObject.toString();
return this;
}

@Override
public ExpectedResponse thenReturn(Object pojo) {
this.response = new JsonObjectMapper().writeValue(pojo);
this.response = o -> o.writeValue(pojo);
return this;
}

RawResponse getResponse(Config config) {
return new MockRawResponse(response, responseHeaders, responseStatus, responseText, config);
RawResponse getResponse(Config config, HttpRequest request) {
return new MockRawResponse(response.apply(getObjectMapper(request, config)), responseHeaders, responseStatus, responseText, config);
}

private ObjectMapper getObjectMapper(HttpRequest request, Config config) {
return Util.tryCast(request, BaseRequest.class)
.map(BaseRequest::getObjectMapper)
.orElseGet(() -> config.getObjectMapper());
}

private Headers allHeaders() {
Expand Down
4 changes: 2 additions & 2 deletions unirest-mocks/src/main/java/kong/unirest/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ boolean matches(HttpRequest request) {

RawResponse exchange(HttpRequest request, Config config) {
return getBestMatch(request)
.map(invocation -> invocation.getResponse(config))
.map(invocation -> invocation.getResponse(config, request))
.orElseGet(() -> {
Invocation i = new Invocation(this);
i.log(request);
invokes.add(i);
return i.getResponse(config);
return i.getResponse(config, request);
});
}

Expand Down
3 changes: 3 additions & 0 deletions unirest-mocks/src/test/java/kong/tests/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

package kong.tests;

import kong.unirest.JsonObjectMapper;
import kong.unirest.MockClient;
import kong.unirest.Unirest;
import kong.unirest.UnirestAssertion;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -41,6 +43,7 @@ public class Base {
@BeforeEach
public void setUp() {
client = MockClient.register();
Unirest.config().setObjectMapper(new JsonObjectMapper());
}

@AfterEach
Expand Down
42 changes: 42 additions & 0 deletions unirest-mocks/src/test/java/kong/tests/ExpectedResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,36 @@ void setReturnAsJson() {
void canPassInAndReturnObjectsAsJson() {
client.expect(HttpMethod.GET, path)
.thenReturn(new Pojo("apple"));

Pojo pojo = Unirest.get(path).asObject(Pojo.class).getBody();
assertEquals("apple", pojo.fruit);
}

@Test
void willUseRequestObjectMapperIfSupplied() {
client.expect(HttpMethod.GET, path)
.thenReturn(new Pojo("apple"));

String pojo = Unirest.get(path)
.withObjectMapper(new DerpMapper())
.asString()
.getBody();

assertEquals("derp", pojo);
}

@Test
void willUseConfigObjectMapperIfSupplied() {
client.expect(HttpMethod.GET, path)
.thenReturn(new Pojo("apple"));

Unirest.config().setObjectMapper(new DerpMapper());

String pojo = Unirest.get(path)
.asString()
.getBody();

assertEquals("derp", pojo);
}

@Test
Expand All @@ -119,4 +149,16 @@ void canReturnEmptyWithHeaders() {
assertEquals(null, rez.getBody());
assertEquals("grover", rez.getHeaders().getFirst("monster"));
}

private class DerpMapper implements kong.unirest.ObjectMapper {
@Override
public <T> T readValue(String value, Class<T> valueType) {
return (T)value;
}

@Override
public String writeValue(Object value) {
return "derp";
}
}
}

0 comments on commit 3feb93a

Please sign in to comment.