Skip to content

Commit

Permalink
addresses issue #493 make the mock client work with the mock response…
Browse files Browse the repository at this point in the history
… because that is what people want
  • Loading branch information
ryber committed Sep 13, 2023
1 parent 8cd0100 commit 2fb12a3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,36 @@
import kong.unirest.core.json.JSONElement;

public interface ExpectedResponse {
/**
* adds a header to the expected response
* @param key the header key
* @param key the header value
* @return this ExpectedResponse
*/
ExpectedResponse withHeader(String key, String value);

/**
* adds a collection of headers to the expected response
* @param headers the headers
* @return This ExpectedResponse
*/
ExpectedResponse withHeaders(Headers headers);

/**
* sets the status of the expected response
* @param httpStatus the http status code
* @return this ExpectedResponse
*/
ExpectedResponse withStatus(int httpStatus);

/**
* sets the status of the expected response
* @param httpStatus the http status code
* @param statusMessage the status message
* @return this ExpectedResponse
*/
ExpectedResponse withStatus(int httpStatus, String statusMessage);

/**
* expect a string response
* @param body the expected response body
Expand Down
18 changes: 17 additions & 1 deletion unirest-mocks/src/main/java/kong/unirest/core/Invocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,20 @@ public ExpectedResponse thenReturn(JSONElement jsonObject) {

@Override
public ExpectedResponse thenReturn(Object pojo) {
this.response = o -> o.writeValue(pojo);
if(pojo instanceof MockResponse){
var res = (MockResponse)pojo;
return thenReturn(res);
} else {
this.response = o -> o.writeValue(pojo);
}
return this;
}

private ExpectedResponse thenReturn(MockResponse res) {
this.response = o -> res.getBody() == null ? null : String.valueOf(res.getBody());
return withStatus(res.getStatus()).withHeaders(res.getHeaders());
}

@Override
public ExpectedResponse thenReturn(Supplier<String> supplier) {
this.response = o -> supplier.get();
Expand Down Expand Up @@ -228,6 +238,12 @@ public ExpectedResponse withHeader(String key, String value) {
return this;
}

@Override
public ExpectedResponse withHeaders(Headers value) {
value.all().forEach(h -> withHeader(h.getName(), h.getValue()));
return this;
}

@Override
public ExpectedResponse withStatus(int httpStatus) {
return withStatus(httpStatus,"");
Expand Down
26 changes: 26 additions & 0 deletions unirest-mocks/src/test/java/kong/tests/AssertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import kong.unirest.core.*;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.function.Supplier;

import static kong.unirest.core.HttpMethod.GET;
Expand Down Expand Up @@ -223,6 +224,31 @@ void assertPostWithNoBody() {
"\t null", ex.getMessage());
}

@Test
void returnAMockResponseObject() {
client.expect(HttpMethod.POST, path)
.thenReturn(MockResponse.of(500, "error")
.withHeader("cool", "beans"));

HttpResponse<String> response = Unirest.post(path).asString();

assertEquals(500, response.getStatus());
assertEquals("error", response.getBody());
assertEquals(List.of("beans"), response.getHeaders().get("cool"));
}

@Test
void returnAMockResponseObjectWithoutStuff() {
client.expect(HttpMethod.POST, path)
.thenReturn(MockResponse.of(500, null));

HttpResponse<String> response = Unirest.post(path).asString();

assertEquals(500, response.getStatus());
assertEquals(null, response.getBody());
assertEquals(List.of(), response.getHeaders().get("cool"));
}

private static class BodyBuddy implements Supplier<String>{
String body;
@Override
Expand Down

0 comments on commit 2fb12a3

Please sign in to comment.