Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Apr 24, 2024
1 parent e37a56f commit 09b17f6
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class HttpRequestJsonPatch extends BaseRequest<JsonPatchRequest> implements Json
header("Content-Type", CONTENT_TYPE);
}

public HttpRequestJsonPatch(HttpRequestJsonPatch baseRequest) {
super(baseRequest);
this.items = baseRequest.items;
}

@Override
public JsonPatchRequest add(String path, Object value) {
items.add(path, value);
Expand Down Expand Up @@ -92,4 +97,8 @@ public boolean isMultiPart() {
public boolean isEntityBody() {
return true;
}

public JsonPatch getPatch() {
return items;
}
}
2 changes: 1 addition & 1 deletion unirest/src/main/java/kong/unirest/core/JsonPatchItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean equals(Object o) {
JsonPatchItem that = (JsonPatchItem) o;
return op == that.op &&
Objects.equals(path, that.path) &&
Objects.equals(toString(), that.toString());
Objects.equals(value, that.value);
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions unirest/src/main/java/kong/unirest/core/RequestFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public static <R extends BaseRequest> R copy(HttpRequest baseRequest) {
if(baseRequest instanceof HttpRequestMultiPart) {
return (R) new HttpRequestMultiPart((HttpRequestMultiPart)baseRequest);
}
if(baseRequest instanceof HttpRequestJsonPatch) {
return (R)new HttpRequestJsonPatch((HttpRequestJsonPatch)baseRequest);
}

throw new UnirestException("Cannot find matching type: " + baseRequest.getClass());
}
Expand Down
130 changes: 102 additions & 28 deletions unirest/src/test/java/kong/unirest/core/RequestFactoryTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kong.unirest.core;

import kong.unirest.core.json.JSONObject;
import org.assertj.core.api.AbstractAssert;
import org.junit.jupiter.api.Test;

Expand All @@ -9,28 +10,31 @@

import static kong.unirest.core.RequestFactoryTest.RequestAsserts.assertRequest;
import static kong.unirest.core.Util.tryCast;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

class RequestFactoryTest {

private final String url = "http://foo";
private final String headerKey = "header-key";
private final String headerValue = "header-value";
private final String queryKey = "query-key";
private final String queryValue = "query-value";
private final String urlWithQuery = url + "?" + queryKey + "=" + queryValue;
private final ProgressMonitor downloadMonitor = mock(ProgressMonitor.class);
private final ProgressMonitor uploadMonitor = mock(ProgressMonitor.class);
private final ObjectMapper om = mock(ObjectMapper.class);
String url = "http://foo";
String headerKey = "header-key";
String headerValue = "header-value";
String queryKey = "query-key";
String queryValue = "query-value";
String urlWithQuery = url + "?" + queryKey + "=" + queryValue;
ProgressMonitor downloadMonitor = mock(ProgressMonitor.class);
ProgressMonitor uploadMonitor = mock(ProgressMonitor.class);
ObjectMapper om = mock(ObjectMapper.class);
int timeout = 38393;

@Test
void copy_get() {
var req = Unirest.get(url)
.header(headerKey, headerValue)
.queryString(queryKey, queryValue)
.downloadMonitor(downloadMonitor)
.withObjectMapper(om);
.withObjectMapper(om)
.connectTimeout(timeout);

var copy = RequestFactory.copy(req);

Expand All @@ -39,7 +43,8 @@ void copy_get() {
.hasHeader(headerKey, headerValue)
.hasRoute(HttpMethod.GET, urlWithQuery)
.downloadMonitorIs(downloadMonitor)
.objectMapperIs(om);
.objectMapperIs(om)
.hasTimeout(timeout);
}

@Test
Expand All @@ -48,7 +53,8 @@ void copy_head() {
.header(headerKey, headerValue)
.queryString(queryKey, queryValue)
.downloadMonitor(downloadMonitor)
.withObjectMapper(om);
.withObjectMapper(om)
.connectTimeout(timeout);

var copy = RequestFactory.copy(req);

Expand All @@ -57,7 +63,8 @@ void copy_head() {
.hasHeader(headerKey, headerValue)
.hasRoute(HttpMethod.HEAD, urlWithQuery)
.downloadMonitorIs(downloadMonitor)
.objectMapperIs(om);
.objectMapperIs(om)
.hasTimeout(timeout);
}

@Test
Expand All @@ -67,7 +74,8 @@ void copy_delete() {
.queryString(queryKey, queryValue)
.charset(StandardCharsets.ISO_8859_1)
.downloadMonitor(downloadMonitor)
.withObjectMapper(om);
.withObjectMapper(om)
.connectTimeout(timeout);

var copy = RequestFactory.copy(req);

Expand All @@ -76,7 +84,8 @@ void copy_delete() {
.hasHeader(headerKey, headerValue)
.hasRoute(HttpMethod.DELETE, urlWithQuery)
.downloadMonitorIs(downloadMonitor)
.objectMapperIs(om);
.objectMapperIs(om)
.hasTimeout(timeout);
}

@Test
Expand All @@ -88,7 +97,8 @@ void copy_put_unibody() {
.body("hi mom")
.downloadMonitor(downloadMonitor)
.uploadMonitor(uploadMonitor)
.withObjectMapper(om);
.withObjectMapper(om)
.connectTimeout(timeout);

var copy = RequestFactory.copy(req);

Expand All @@ -100,7 +110,57 @@ void copy_put_unibody() {
.hasBody("hi mom")
.downloadMonitorIs(downloadMonitor)
.uploadMonitorIs(uploadMonitor)
.objectMapperIs(om);
.objectMapperIs(om)
.hasTimeout(timeout);
}

@Test
void copy_patch() {
var req = Unirest.patch(url)
.header(headerKey, headerValue)
.queryString(queryKey, queryValue)
.charset(StandardCharsets.ISO_8859_1)
.body("hi mom")
.downloadMonitor(downloadMonitor)
.uploadMonitor(uploadMonitor)
.withObjectMapper(om)
.connectTimeout(timeout);

var copy = RequestFactory.copy(req);

assertRequest(copy)
.isInstanceOf(HttpRequestUniBody.class)
.hasHeader(headerKey, headerValue)
.hasRoute(HttpMethod.PATCH, urlWithQuery)
.hasCharset(StandardCharsets.ISO_8859_1)
.hasBody("hi mom")
.downloadMonitorIs(downloadMonitor)
.uploadMonitorIs(uploadMonitor)
.objectMapperIs(om)
.hasTimeout(timeout);
}

@Test
void copy_json_patch() {
var req = Unirest.jsonPatch(url)
.header(headerKey, headerValue)
.queryString(queryKey, queryValue)
.add("/foo/bar", "one")
.downloadMonitor(downloadMonitor)
.withObjectMapper(om)
.connectTimeout(timeout);

var copy = RequestFactory.copy(req);

assertRequest(copy)
.isInstanceOf(HttpRequestJsonPatch.class)
.hasHeader(headerKey, headerValue)
.hasRoute(HttpMethod.PATCH, urlWithQuery)
.hasPatch(JsonPatchOperation.add, "/foo/bar", "one")
.hasCharset(StandardCharsets.UTF_8)
.downloadMonitorIs(downloadMonitor)
.objectMapperIs(om)
.hasTimeout(timeout);
}

@Test
Expand All @@ -111,7 +171,8 @@ void copy_post_unibody() {
.charset(StandardCharsets.ISO_8859_1)
.body("hi mom")
.downloadMonitor(downloadMonitor)
.withObjectMapper(om);
.withObjectMapper(om)
.connectTimeout(timeout);

var copy = RequestFactory.copy(req);

Expand All @@ -122,7 +183,8 @@ void copy_post_unibody() {
.hasCharset(StandardCharsets.ISO_8859_1)
.hasBody("hi mom")
.downloadMonitorIs(downloadMonitor)
.objectMapperIs(om);
.objectMapperIs(om)
.hasTimeout(timeout);
}

@Test
Expand All @@ -135,7 +197,8 @@ void copy_post_formBody() {
.boundary("my-boundary")
.downloadMonitor(downloadMonitor)
.uploadMonitor(uploadMonitor)
.withObjectMapper(om);
.withObjectMapper(om)
.connectTimeout(timeout);

var copy = RequestFactory.copy(req);

Expand All @@ -147,14 +210,14 @@ void copy_post_formBody() {
.hasBoundary("my-boundary")
.downloadMonitorIs(downloadMonitor)
.uploadMonitorIs(uploadMonitor)
.objectMapperIs(om);
.objectMapperIs(om)
.hasTimeout(timeout);
}



static class RequestAsserts extends AbstractAssert<RequestAsserts, HttpRequest> {

public static RequestAsserts assertRequest(HttpRequest request){
public static RequestAsserts assertRequest(HttpRequest request) {
return new RequestAsserts(request);
}

Expand Down Expand Up @@ -188,7 +251,7 @@ public RequestAsserts hasBody(String expected) {

private Body getBody() {
var b = tryAs(HttpRequest.class);
Body o = (Body)b.getBody().get();
Body o = (Body) b.getBody().get();
return o;
}

Expand All @@ -197,16 +260,16 @@ private <T> T tryAs(Class<T> clss) {
.orElseThrow(() -> err("Could not cast subject (%s) to (%s)", actual.getClass(), clss));
}

private AssertionError err(String mssg, Object... args){
private AssertionError err(String mssg, Object... args) {
return new AssertionError(String.format(mssg, args));
}

public RequestAsserts hasField(String key, String value) {
var body = tryAs(HttpRequestMultiPart.class)
.getBody()
.orElseThrow(() -> new AssertionError("No body found!"));
for (BodyPart part : body.multiParts()){
if(part.getName().equals(key) && part.getValue().equals(value)){
for (BodyPart part : body.multiParts()) {
if (part.getName().equals(key) && part.getValue().equals(value)) {
return this;
}
}
Expand All @@ -231,7 +294,7 @@ public RequestAsserts uploadMonitorIs(ProgressMonitor uploadMonitor) {
private ProgressMonitor getUploadMonitor() {
try {
return tryAs(HttpRequestUniBody.class).getMonitor();
}catch (AssertionError e){
} catch (AssertionError e) {
return tryAs(HttpRequestMultiPart.class).getMonitor();
}
}
Expand All @@ -240,5 +303,16 @@ public RequestAsserts objectMapperIs(ObjectMapper om) {
assertSame(om, tryAs(BaseRequest.class).getObjectMapper());
return this;
}

public RequestAsserts hasTimeout(int timeout) {
assertEquals(timeout, actual.getConnectTimeout());
return this;
}

public RequestAsserts hasPatch(JsonPatchOperation operation, String path, String value) {
var items = tryAs(HttpRequestJsonPatch.class).getPatch().getOperations();
assertThat(items).contains(new JsonPatchItem(operation, path, value));
return this;
}
}
}

0 comments on commit 09b17f6

Please sign in to comment.