Skip to content

Commit

Permalink
allow consumers to use a handler to get the expected response, this w…
Browse files Browse the repository at this point in the history
…ould allow them to add additional inspections on the request
  • Loading branch information
ryber committed Oct 12, 2023
1 parent bc7e7ec commit ef7ec0f
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import kong.unirest.core.json.JSONElement;

import java.util.function.Function;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -96,4 +97,6 @@ public interface Expectation {
* @return The ExpectedResponse
*/
ExpectedResponse thenReturn(Supplier<String> supplier);

void thenReturn(Function<HttpRequest<?>, ExpectedResponse> fun);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
import java.util.function.Supplier;

public interface ExpectedResponse {
static ExpectedResponse of(int status) {
return new ExpectedResponseRecord().withStatus(status);
}

/**
* adds a header to the expected response
* @param key the header key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

class ExpectedResponseRecord implements ExpectedResponse {
class ExpectedResponseRecord implements ExpectedResponse, ResponseBuilder {
private Function<ObjectMapper, String> response = o -> null;
private Headers responseHeaders = new Headers();
private int responseStatus = 200;
Expand Down
25 changes: 16 additions & 9 deletions unirest-mocks/src/main/java/kong/unirest/core/Invocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import kong.unirest.core.json.JSONElement;

import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -42,7 +43,8 @@ class Invocation implements Expectation {
private Boolean expected = false;
private BodyMatcher expectedBody;
private MatchStatus expectedBodyStatus;
private ExpectedResponseRecord responseRecord = new ExpectedResponseRecord();
private ExpectedResponseRecord expectedResponse = new ExpectedResponseRecord();
private Function<HttpRequest<?>, ExpectedResponse> functionalResponse = r -> expectedResponse;

public Invocation(Routes routes){
this.routes = routes;
Expand All @@ -56,7 +58,7 @@ public Invocation(Routes routes, HttpRequest request) {

Invocation(Routes routes, Invocation other) {
this.routes = routes;
this.responseRecord = other.responseRecord;
this.expectedResponse = other.expectedResponse;
}

Invocation() {
Expand All @@ -65,27 +67,33 @@ public Invocation(Routes routes, HttpRequest request) {

@Override
public ExpectedResponse thenReturn(String body) {
return responseRecord.thenReturn(body);
return expectedResponse.thenReturn(body);
}

@Override
public ExpectedResponse thenReturn(JSONElement jsonObject) {
return responseRecord.thenReturn(jsonObject);
return expectedResponse.thenReturn(jsonObject);
}

@Override
public ExpectedResponse thenReturn(Object pojo) {
return responseRecord.thenReturn(pojo);
return expectedResponse.thenReturn(pojo);
}

@Override
public ExpectedResponse thenReturn(Supplier<String> supplier) {
return responseRecord.thenReturn(supplier);
return expectedResponse.thenReturn(supplier);
}

@Override
public void thenReturn(Function<HttpRequest<?>, ExpectedResponse> fun) {
this.functionalResponse = fun;
}

RawResponse getResponse(Config config, HttpRequest request) {
return responseRecord.toRawResponse(config, request);
return tryCast(functionalResponse.apply(request), ExpectedResponseRecord.class)
.map(e -> e.toRawResponse(config, request))
.orElseThrow(() -> new UnirestException("No Result Configured For Response"));
}

private Headers allHeaders() {
Expand Down Expand Up @@ -128,7 +136,7 @@ public Expectation body(BodyMatcher matcher) {

@Override
public ExpectedResponse thenReturn() {
return responseRecord;
return expectedResponse;
}

public void verify() {
Expand Down Expand Up @@ -205,7 +213,6 @@ public List<HttpRequest> getRequests() {
return requests;
}


public Boolean isExpected() {
return expected;
}
Expand Down
30 changes: 30 additions & 0 deletions unirest-mocks/src/main/java/kong/unirest/core/ResponseBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package kong.unirest.core;

interface ResponseBuilder {
RawResponse toRawResponse(Config config, HttpRequest request);
}
50 changes: 50 additions & 0 deletions unirest-mocks/src/test/java/kong/tests/ResponseHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package kong.tests;

import kong.unirest.core.ExpectedResponse;
import kong.unirest.core.HttpMethod;
import kong.unirest.core.HttpResponse;
import kong.unirest.core.Unirest;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ResponseHandlerTest extends Base {

@Test
void canSetAFullRequestHandler() {
client.expect(HttpMethod.GET, "/fruit")
.thenReturn(r -> ExpectedResponse.of(200)
.thenReturn("Hello Jane")
.withHeader("foo", "bar"));

HttpResponse<String> string = Unirest.get("/fruit").asString();
assertEquals("Hello Jane", string.getBody());
assertEquals("bar", string.getHeaders().getFirst("foo"));
assertEquals(200, string.getStatus());
}
}

0 comments on commit ef7ec0f

Please sign in to comment.