Skip to content

Commit

Permalink
some light test refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Aug 22, 2024
1 parent f2ef351 commit 030bc68
Show file tree
Hide file tree
Showing 38 changed files with 243 additions and 248 deletions.
14 changes: 7 additions & 7 deletions unirest-bdd-tests/src/test/java/BehaviorTests/AsBytesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ class AsBytesTest extends BddTest {

@Test
void getGetResultAsBytes() {
byte[] content = Unirest.get(MockServer.GET)
var content = Unirest.get(MockServer.GET)
.asBytes()
.getBody();

RequestCapture cap = om.readValue(content, RequestCapture.class);
var cap = om.readValue(content, RequestCapture.class);

cap.assertStatus(200);
}

@Test
void getGetBinaryResultAsBytes() {
byte[] content = Unirest.get(MockServer.BINARYFILE)
var content = Unirest.get(MockServer.BINARYFILE)
.asBytes()
.getBody();

Expand All @@ -55,12 +55,12 @@ void getGetBinaryResultAsBytes() {

@Test
void getGetResultAsBytesAsync() throws Exception {
byte[] content = Unirest.get(MockServer.GET)
var content = Unirest.get(MockServer.GET)
.asBytesAsync()
.get()
.getBody();

RequestCapture cap = om.readValue(content, RequestCapture.class);
var cap = om.readValue(content, RequestCapture.class);

cap.assertStatus(200);
}
Expand All @@ -83,7 +83,7 @@ void getGetResultAsBytesAsyncCallback() throws Exception {
@Test // https://github.com/Kong/unirest-java/issues/424
void mappingErrorsFromAsBytes() {
MockServer.setStringResponse("howdy");
String r = Unirest.get(MockServer.ERROR_RESPONSE)
var r = Unirest.get(MockServer.ERROR_RESPONSE)
.asBytes()
.mapError(String.class);

Expand All @@ -93,7 +93,7 @@ void mappingErrorsFromAsBytes() {
@Test // https://github.com/Kong/unirest-java/issues/424
void mappingErrorsFromAsBytesMapped() {
MockServer.setJsonAsResponse(new Foo("howdy"));
Foo r = Unirest.get(MockServer.ERROR_RESPONSE)
var r = Unirest.get(MockServer.ERROR_RESPONSE)
.asBytes()
.mapError(Foo.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void asEmptyParams() {
@Test
void canDoAEmptyRequestThatDoesNotParseBodyAtAll() {
MockServer.addResponseHeader("Content-Type", "json");
HttpResponse<Empty> res = Unirest.get(MockServer.GET).asEmpty();
var res = Unirest.get(MockServer.GET).asEmpty();

assertNull(res.getBody());
assertEquals(200, res.getStatus());
Expand All @@ -58,7 +58,7 @@ void canDoAEmptyRequestThatDoesNotParseBodyAtAll() {
@Test
void canDoEmptyAsync() throws Exception {
MockServer.addResponseHeader("Content-Type", "json");
HttpResponse<Empty> res = Unirest.get(MockServer.GET).asEmptyAsync().get();
var res = Unirest.get(MockServer.GET).asEmptyAsync().get();

assertNull(res.getBody());
assertEquals(200, res.getStatus());
Expand Down
25 changes: 13 additions & 12 deletions unirest-bdd-tests/src/test/java/BehaviorTests/AsFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -54,7 +55,7 @@ public void tearDown() {

@Test
void canSaveContentsIntoFile() {
File result = Unirest.get(MockServer.GET)
var result = Unirest.get(MockServer.GET)
.queryString("talking","heads")
.queryString("param3", "こんにちは")
.asFile(test.toString())
Expand All @@ -70,7 +71,7 @@ void canSaveContentsIntoFile() {

@Test
void canSaveContentsIntoFileAsync() throws Exception {
File result = Unirest.get(MockServer.GET)
var result = Unirest.get(MockServer.GET)
.queryString("talking","heads")
.queryString("param3", "こんにちは")
.asFileAsync(test.toString())
Expand Down Expand Up @@ -104,9 +105,9 @@ void canSaveContentsIntoFileAsyncWithCallback() throws Exception {

@Test
void canDownloadABinaryFile() throws Exception {
File f1 = TestUtil.rezFile("/spidey.jpg");
var f1 = TestUtil.rezFile("/spidey.jpg");

File f2 = Unirest.get(MockServer.BINARYFILE)
var f2 = Unirest.get(MockServer.BINARYFILE)
.asFile(test.toString())
.getBody();

Expand All @@ -118,20 +119,21 @@ void byDefaultFailWhenAttemptingToOverride() {
Unirest.get(MockServer.BINARYFILE)
.asFile(test.toString());

HttpResponse<File> f2 = Unirest.get(MockServer.BINARYFILE)
var f2 = Unirest.get(MockServer.BINARYFILE)
.asFile(test.toString());

assertTrue(f2.getParsingError().isPresent());
assertTrue(f2.getParsingError().get().getCause().getCause() instanceof FileAlreadyExistsException);
assertThat(f2.getParsingError().get().getCause().getCause())
.isInstanceOf(FileAlreadyExistsException.class);
}

@Test
void canOverrideExistingFiles() {
File f1 = Unirest.get(MockServer.BINARYFILE)
var f1 = Unirest.get(MockServer.BINARYFILE)
.asFile(test.toString())
.getBody();

File f2 = Unirest.get(MockServer.BINARYFILE)
var f2 = Unirest.get(MockServer.BINARYFILE)
.asFile(test.toString(), StandardCopyOption.REPLACE_EXISTING)
.getBody();

Expand All @@ -140,23 +142,22 @@ void canOverrideExistingFiles() {

@Test
void canOverrideExistingFiles_Async() throws Exception {
File f1 = Unirest.get(MockServer.BINARYFILE)
var f1 = Unirest.get(MockServer.BINARYFILE)
.asFileAsync(test.toString())
.get()
.getBody();

File f2 = Unirest.get(MockServer.BINARYFILE)
var f2 = Unirest.get(MockServer.BINARYFILE)
.asFileAsync(test.toString(), StandardCopyOption.REPLACE_EXISTING)
.get()
.getBody();

assertEquals(f1, f2);
}


@Test
void dontReadFilesWhenInError() {
HttpResponse<File> f1 = Unirest.get(MockServer.BINARYFILE)
var f1 = Unirest.get(MockServer.BINARYFILE)
.downloadMonitor((field, fileName, bytesWritten, totalBytes) -> {
throw new RuntimeException("hey hey hey");
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AsGenericTypeTest extends BddTest {
void canGetAListOfObjects() {
MockServer.setJsonAsResponse(foos);

List<Foo> foos = Unirest.get(MockServer.GET)
var foos = Unirest.get(MockServer.GET)
.asObject(new GenericType<List<Foo>>(){})
.getBody();

Expand All @@ -59,7 +59,7 @@ void canGetAListOfObjects() {
void canGetAListOfObjectsAsync() throws ExecutionException, InterruptedException {
MockServer.setJsonAsResponse(foos);

List<Foo> foos = Unirest.get(MockServer.GET)
var foos = Unirest.get(MockServer.GET)
.asObjectAsync(new GenericType<List<Foo>>(){})
.get()
.getBody();
Expand All @@ -86,11 +86,11 @@ void canGetAListOfObjectsAsyncWithCallback() {
void soManyLayersOfGenerics() {
MockServer.setJsonAsResponse(new WeirdType<>(foos, "hey"));

WeirdType<List<Foo>> foos = Unirest.get(MockServer.GET)
var foos = Unirest.get(MockServer.GET)
.asObject(new GenericType<WeirdType<List<Foo>>>(){})
.getBody();

List<Foo> someTees = foos.getSomeTees();
var someTees = foos.getSomeTees();
assertTheFoos(someTees);
}

Expand All @@ -100,11 +100,11 @@ void itAlsoWorksWithGson() {

MockServer.setJsonAsResponse(new WeirdType<>(foos, "hey"));

WeirdType<List<Foo>> foos = Unirest.get(MockServer.GET)
var foos = Unirest.get(MockServer.GET)
.asObject(new GenericType<WeirdType<List<Foo>>>(){})
.getBody();

List<Foo> someTees = foos.getSomeTees();
var someTees = foos.getSomeTees();
assertTheFoos(someTees);
}

Expand Down
17 changes: 9 additions & 8 deletions unirest-bdd-tests/src/test/java/BehaviorTests/AsJsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AsJsonTest extends BddTest {

@Test
void whenNoBodyIsReturned() {
HttpResponse<JsonNode> i = Unirest.get(MockServer.NOBODY).asJson();
var i = Unirest.get(MockServer.NOBODY).asJson();

assertEquals(HttpStatus.OK, i.getStatus());
assertEquals("{}", i.getBody().toString());
Expand All @@ -47,15 +47,15 @@ void whenNoBodyIsReturned() {
@Test
void toStringAObject() {
MockServer.setStringResponse(new JSONObject().put("f", 1).put("a", Arrays.asList(2, 3, 4)).toString());
HttpResponse<JsonNode> i = Unirest.get(MockServer.GET).asJson();
var i = Unirest.get(MockServer.GET).asJson();

assertEquals("{\"f\":1,\"a\":[2,3,4]}", i.getBody().toString());
}

@Test
void toPrettyStringAObject() {
MockServer.setStringResponse(new JSONObject().put("f", 1).put("a", Arrays.asList(2, 3, 4)).toString());
HttpResponse<JsonNode> i = Unirest.get(MockServer.GET).asJson();
var i = Unirest.get(MockServer.GET).asJson();

assertEquals("{\n" +
" \"f\": 1,\n" +
Expand All @@ -69,7 +69,7 @@ void toPrettyStringAObject() {

@Test
void canGetBinaryResponse() {
HttpResponse<JsonNode> i = Unirest.get(MockServer.GET)
var i = Unirest.get(MockServer.GET)
.queryString("foo", "bar")
.asJson();

Expand All @@ -78,7 +78,7 @@ void canGetBinaryResponse() {

@Test
void canGetBinaryResponseAsync() throws Exception {
CompletableFuture<HttpResponse<JsonNode>> r = Unirest.get(MockServer.GET)
var r = Unirest.get(MockServer.GET)
.queryString("foo", "bar")
.asJsonAsync();

Expand All @@ -99,12 +99,13 @@ void canGetBinaryResponseAsyncWithCallback() {

@Test
void failureToReturnValidJsonWillResultInAnEmptyNode() {
HttpResponse<JsonNode> response = Unirest.get(MockServer.INVALID_REQUEST).asJson();
var response = Unirest.get(MockServer.INVALID_REQUEST).asJson();

assertEquals(HttpStatus.BAD_REQUEST, response.getStatus());
assertNull(response.getBody());
assertTrue(response.getParsingError().isPresent());
UnirestParsingException ex = response.getParsingError().get();

var ex = response.getParsingError().get();
assertEquals("You did something bad", ex.getOriginalBody());
assertEquals("kong.unirest.core.json.JSONException: Invalid JSON",
response.getParsingError().get().getMessage());
Expand All @@ -130,7 +131,7 @@ void failureToReturnValidJsonWillResultInAnEmptyNodeAsync() {
void doNotEscapeHTML() {
MockServer.setStringResponse("{\"test\":\"it's a && b || c + 1!?\"}");

JSONObject test = Unirest.get(MockServer.GET)
var test = Unirest.get(MockServer.GET)
.asJson()
.getBody()
.getObject();
Expand Down
32 changes: 17 additions & 15 deletions unirest-bdd-tests/src/test/java/BehaviorTests/AsObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class AsObjectTest extends BddTest {
@Test
void basicJsonObjectMapperIsTheDefault() {
Unirest.config().reset(true);
assertTrue(Unirest.config().getObjectMapper() instanceof GsonObjectMapper);
assertThat(Unirest.config().getObjectMapper())
.isInstanceOf(GsonObjectMapper.class);
}

@Test
Expand All @@ -60,10 +61,10 @@ void basicJsonObjectMapperIsGoodEnough() {

@Test
void whenNoBodyIsReturned() {
HttpResponse<RequestCapture> i = Unirest.get(MockServer.NOBODY).asObject(RequestCapture.class);
var i = Unirest.get(MockServer.NOBODY).asObject(RequestCapture.class);

assertEquals(200, i.getStatus());
assertEquals(null, i.getBody());
assertNull(i.getBody());
}

@Test
Expand Down Expand Up @@ -102,7 +103,7 @@ void canGetObjectResponseAsyncWithCallback() {
void canPassAnObjectMapperAsPartOfARequest(){
Unirest.config().setObjectMapper(null);

TestingMapper mapper = new TestingMapper();
var mapper = new TestingMapper();
Unirest.post(MockServer.POST)
.queryString("foo", "bar")
.withObjectMapper(mapper)
Expand Down Expand Up @@ -153,7 +154,7 @@ void setNoCharSetAfterBody() {

@Test
void ifTheObjectMapperFailsReturnEmptyAndAddToParsingError() {
HttpResponse<RequestCapture> request = Unirest.get(MockServer.INVALID_REQUEST)
var request = Unirest.get(MockServer.INVALID_REQUEST)
.asObject(RequestCapture.class);

assertNull(request.getBody());
Expand All @@ -167,7 +168,7 @@ void ifTheObjectMapperFailsReturnEmptyAndAddToParsingError() {

@Test
void ifTheObjectMapperFailsReturnEmptyAndAddToParsingErrorObGenericTypes() {
HttpResponse<RequestCapture> request = Unirest.get(MockServer.INVALID_REQUEST)
var request = Unirest.get(MockServer.INVALID_REQUEST)
.asObject(new GenericType<RequestCapture>() {});

assertNull(request.getBody());
Expand All @@ -180,7 +181,7 @@ void ifTheObjectMapperFailsReturnEmptyAndAddToParsingErrorObGenericTypes() {

@Test
void unirestExceptionsAreAlsoParseExceptions() {
HttpResponse<RequestCapture> request = Unirest.get(MockServer.INVALID_REQUEST)
var request = Unirest.get(MockServer.INVALID_REQUEST)
.asObject(new GenericType<RequestCapture>() {});

assertNull(request.getBody());
Expand All @@ -193,14 +194,14 @@ void unirestExceptionsAreAlsoParseExceptions() {

@Test
void canSetObjectMapperToFailOnUnknown() {
com.fasterxml.jackson.databind.ObjectMapper jack = new com.fasterxml.jackson.databind.ObjectMapper();
var jack = new com.fasterxml.jackson.databind.ObjectMapper();
jack.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);

Unirest.config().setObjectMapper(new JacksonObjectMapper(jack));

MockServer.setStringResponse("{\"foo\": [1,2,3] }");

Error error = Unirest.get(MockServer.GET)
var error = Unirest.get(MockServer.GET)
.asObject(RequestCapture.class)
.mapError(Error.class);

Expand All @@ -216,21 +217,22 @@ void parsingExceptions() {
.getParsingError()
.get();

assertThat(ex)
.isInstanceOf(UnirestParsingException.class)
.hasMessage("kong.unirest.core.UnirestException: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'hi': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n" +
" at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 3]");
assertParsingError(ex);

var ex2 = Unirest.get(MockServer.ERROR_RESPONSE)
.asObject(new GenericType<SomeTestClass>() {})
.getParsingError()
.get();

assertThat(ex2)
assertParsingError(ex2);

}

private static void assertParsingError(UnirestParsingException e) {
assertThat(e)
.isInstanceOf(UnirestParsingException.class)
.hasMessage("kong.unirest.core.UnirestException: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'hi': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n" +
" at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 3]");

}

static class SomeTestClass {}
Expand Down
Loading

0 comments on commit 030bc68

Please sign in to comment.