diff --git a/pom.xml b/pom.xml
index 2a72785ec..4bd2331ad 100644
--- a/pom.xml
+++ b/pom.xml
@@ -312,9 +312,7 @@
-
-
+
diff --git a/unirest/pom.xml b/unirest/pom.xml
index 061703d68..354467ade 100644
--- a/unirest/pom.xml
+++ b/unirest/pom.xml
@@ -68,23 +68,18 @@
${jackson.version}
test
+
- com.sparkjava
- spark-core
- 2.9.3
+ org.apache.commons
+ commons-io
+ 1.3.2
test
-
-
- org.eclipse.jetty.websocket
- websocket-server
-
-
-
+
- org.eclipse.jetty
- jetty-server
- 9.4.38.v20210224
+ io.javalin
+ javalin
+ 3.13.4
test
diff --git a/unirest/src/test/java/BehaviorTests/AsEmptyTest.java b/unirest/src/test/java/BehaviorTests/AsEmptyTest.java
index 44dd80aa7..43fdf40e9 100644
--- a/unirest/src/test/java/BehaviorTests/AsEmptyTest.java
+++ b/unirest/src/test/java/BehaviorTests/AsEmptyTest.java
@@ -42,7 +42,7 @@ void canDoAEmptyRequestThatDoesNotParseBodyAtAll() {
assertNull(res.getBody());
assertEquals(200, res.getStatus());
- assertEquals("json", res.getHeaders().getFirst("Content-Type"));
+ assertEquals("json;charset=utf-8", res.getHeaders().getFirst("Content-Type"));
}
@Test
@@ -52,18 +52,18 @@ void canDoEmptyAsync() throws Exception {
assertNull(res.getBody());
assertEquals(200, res.getStatus());
- assertEquals("json", res.getHeaders().getFirst("Content-Type"));
+ assertEquals("json;charset=utf-8", res.getHeaders().getFirst("Content-Type"));
}
@Test
void canDoEmptyAsyncWithCallback() {
- MockServer.addResponseHeader("Content-Type", "json");
+ MockServer.addResponseHeader("Content-Type", "json;charset=utf-8");
Unirest.get(MockServer.GET)
.asEmptyAsync(res -> {
assertNull(res.getBody());
assertEquals(200, res.getStatus());
- assertEquals("json", res.getHeaders().getFirst("Content-Type"));
+ assertEquals("json;charset=utf-8", res.getHeaders().getFirst("Content-Type"));
asyncSuccess();
});
diff --git a/unirest/src/test/java/BehaviorTests/AsStringTest.java b/unirest/src/test/java/BehaviorTests/AsStringTest.java
index 1bc19a177..d3cb422ea 100644
--- a/unirest/src/test/java/BehaviorTests/AsStringTest.java
+++ b/unirest/src/test/java/BehaviorTests/AsStringTest.java
@@ -25,6 +25,7 @@
package BehaviorTests;
+import io.javalin.core.util.Header;
import org.junit.jupiter.api.Test;
import kong.unirest.HttpResponse;
import kong.unirest.TestUtil;
@@ -36,6 +37,13 @@
class AsStringTest extends BddTest {
+ @Test
+ void simpleExample() {
+ MockServer.setStringResponse("Hi Mom");
+ String body = Unirest.get(MockServer.GET).asString().getBody();
+ assertEquals("Hi Mom", body);
+ }
+
@Test
void whenNoBodyIsReturned() {
HttpResponse i = Unirest.get(MockServer.NOBODY).asString();
@@ -48,6 +56,7 @@ void whenNoBodyIsReturned() {
void canParseGzippedStringResponse() {
HttpResponse i = Unirest.get(MockServer.GZIP)
.queryString("foo", "bar")
+ .header(Header.ACCEPT_ENCODING, "gzip")
.asString();
RequestCapture cap = TestUtil.readValue(i.getBody(), RequestCapture.class);
diff --git a/unirest/src/test/java/BehaviorTests/MetricsTest.java b/unirest/src/test/java/BehaviorTests/MetricsTest.java
index 2f42d0e25..5f6a6e476 100644
--- a/unirest/src/test/java/BehaviorTests/MetricsTest.java
+++ b/unirest/src/test/java/BehaviorTests/MetricsTest.java
@@ -38,7 +38,6 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class MetricsTest extends BddTest {
@Test
@@ -168,8 +167,8 @@ void metricAsALambda() {
}
@Test
- void showWhatSparkDoes() {
- HashMap map = Unirest.get(SPARKLE)
+ void showWhatJavalinDoes() {
+ HashMap map = Unirest.get(JAVALIN)
.routeParam("spark", "joy")
.queryString("food", "hamberders")
.queryString("colour", "red")
@@ -179,7 +178,7 @@ void showWhatSparkDoes() {
assertEquals("localhost:4567", map.get("host()"));
assertEquals("/sparkle/joy/yippy", map.get("uri()")); // this is different from what the Spark doc says.
assertEquals("http://localhost:4567/sparkle/joy/yippy", map.get("url()"));
- assertNull(map.get("contextPath()"));
+ assertEquals("", map.get("contextPath()"));
assertEquals("/sparkle/joy/yippy", map.get("pathInfo()"));
assertEquals("food=hamberders&colour=red", map.get("queryString()"));
}
diff --git a/unirest/src/test/java/BehaviorTests/MockServer.java b/unirest/src/test/java/BehaviorTests/MockServer.java
index 31084d187..36da900ac 100644
--- a/unirest/src/test/java/BehaviorTests/MockServer.java
+++ b/unirest/src/test/java/BehaviorTests/MockServer.java
@@ -26,250 +26,273 @@
package BehaviorTests;
-import org.eclipse.jetty.util.UrlEncoded;
-import spark.Request;
-import spark.Response;
-import spark.Spark;
-import spark.utils.IOUtils;
+import com.google.common.base.Strings;
+import io.javalin.Javalin;
+import io.javalin.http.Context;
import kong.unirest.JacksonObjectMapper;
import kong.unirest.TestUtil;
+import org.apache.commons.io.IOUtils;
+import org.eclipse.jetty.util.UrlEncoded;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
-import java.io.File;
-import java.io.FileInputStream;
+import java.io.*;
+import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
-import static spark.Spark.*;
public class MockServer {
public static int timesCalled;
private static int pages = 1;
- private static int onPage = 1;
- private static final List> responseHeaders = new ArrayList<>();
- private static final List cookies = new ArrayList<>();
-
- private static final JacksonObjectMapper om = new JacksonObjectMapper();
- private static String responseBody;
- public static final int PORT = 4567;
- public static final String HOST = "http://localhost:" + PORT;
- public static final String WINDOWS_LATIN_1_FILE = HOST + "/data/cp1250.txt";
- public static final String REDIRECT = HOST + "/redirect";
- public static final String SPARKLE = HOST + "/sparkle/{spark}/yippy";
- public static final String BINARYFILE = HOST + "/binary";
- public static final String NOBODY = HOST + "/nobody";
- public static final String PAGED = HOST + "/paged";
- public static final String PROXY = "localhost:4567";
- public static final String POST = HOST + "/post";
- public static final String GET = HOST + "/get";
- public static final String ERROR_RESPONSE = HOST + "/error";
- public static final String DELETE = HOST + "/delete";
- public static final String GZIP = HOST + "/gzip";
- public static final String EMPTY_GZIP = HOST + "/empty-gzip";
- public static final String PATCH = HOST + "/patch";
- public static final String INVALID_REQUEST = HOST + "/invalid";
- public static final String PASSED_PATH_PARAM = GET + "/{params}/passed";
- public static final String PASSED_PATH_PARAM_MULTI = PASSED_PATH_PARAM + "/{another}";
- public static final String CHEESE = HOST + "/cheese";
- public static final String ALTGET = "http://127.0.0.1:" + PORT + "/get";
- public static final String ECHO_RAW = HOST + "/raw";
-
-
- public static void setJsonAsResponse(Object o){
- responseBody = om.writeValue(o);
- }
-
- public static void reset(){
- responseBody = null;
- responseHeaders.clear();
- cookies.clear();
- pages = 1;
- onPage = 1;
- timesCalled = 0;
- }
-
- static {
- Spark.staticFileLocation("public/");
- port(PORT);
- Spark.notFound(MockServer::notFound);
- Spark.before((p,f) -> timesCalled++);
- delete("/delete", MockServer::jsonResponse);
- get("/sparkle/:spark/yippy", MockServer::sparkle);
- post("/post", MockServer::jsonResponse);
- get("/get", MockServer::jsonResponse);
- get("/gzip", MockServer::gzipResponse);
- post("/empty-gzip", MockServer::emptyGzipResponse);
- get("/redirect", MockServer::redirect);
- patch("/patch", MockServer::jsonResponse);
- get("/invalid", MockServer::inValid);
- options("/get", MockServer::jsonResponse);
- get("/nobody", MockServer::nobody);
- head("/get", MockServer::jsonResponse);
- put("/post", MockServer::jsonResponse);
- get("/get/:params/passed", MockServer::jsonResponse);
- get("/get/:params/passed/:another", MockServer::jsonResponse);
- get("/proxy", MockServer::proxiedResponse);
- get("/binary", MockServer::file);
- get("/paged", MockServer::paged);
- post("/raw", MockServer::echo);
- get("/error", MockServer::error);
- Runtime.getRuntime().addShutdownHook(new Thread(Spark::stop));
- try {
- new CountDownLatch(1).await(2, TimeUnit.SECONDS);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- private static Object sparkle(Request request, Response response) {
- Map sparks = new HashMap<>();
- sparks.put("contentType()", request.contentType());
- sparks.put("contextPath()", request.contextPath());
- sparks.put("host()", request.host());
- sparks.put("ip()", request.ip());
- sparks.put("pathInfo()", request.pathInfo());
- sparks.put("port()", String.valueOf(request.port()));
- sparks.put("protocol()", request.protocol());
- sparks.put("scheme()", request.scheme());
- sparks.put("servletPath()", request.servletPath());
- sparks.put("requestMethod()", request.requestMethod());
- sparks.put("splat()", String.join(" | ", request.splat()));
- sparks.put("uri()", request.uri());
- sparks.put("url()", request.url());
- sparks.put("userAgent()", request.userAgent());
- sparks.put("queryString()", request.queryString());
-
- return om.writeValue(sparks);
- }
-
- private static Object error(Request request, Response response) {
- return Spark.halt(400, responseBody);
- }
-
- private static Object echo(Request request, Response response) {
- return request.body();
- }
-
- private static Object paged(Request request, Response response) {
- if(pages > onPage){
- onPage++;
- response.header("nextPage", PAGED + "?page=" + onPage);
- }
- return jsonResponse(request, response);
- }
-
- private static Object notFound(Request req, Response res) {
- RequestCapture value = getRequestCapture(req, res);
- value.setStatus(404);
- return om.writeValue(value);
- }
-
- private static Object file(Request request, Response response) throws Exception {
- File f = TestUtil.rezFile("/spidey.jpg");
- response.raw().setContentType("application/octet-stream");
- response.raw().setHeader("Content-Disposition", "attachment;filename=image.jpg");
- response.raw().setHeader("Content-Length", String.valueOf(f.length()));
- response.status(200);
- final ServletOutputStream out = response.raw().getOutputStream();
- final FileInputStream in = new FileInputStream(f);
- IOUtils.copy(in, out);
- out.close();
- in.close();
- return null;
- }
-
- private static Object nobody(Request request, Response response) {
- Spark.halt(200);
- return null;
- }
-
- private static Object redirect(Request request, Response response) {
- response.redirect("/get", 301);
- return null;
- }
-
- private static Object inValid(Request request, Response response) {
- response.status(400);
- return "You did something bad";
- }
-
- private static Object emptyGzipResponse(Request request, Response response) throws Exception {
- response.raw().setHeader("Content-Encoding", "gzip");
- response.raw().setContentType("application/json");
- response.raw().setStatus(200);
- response.raw().getOutputStream().close();
- return null;
- }
-
- private static Object gzipResponse(Request request, Response response) {
- response.header("Content-Encoding", "gzip");
- return jsonResponse(request, response);
- }
-
- private static Object proxiedResponse(Request req, Response res) {
- return simpleResponse(req, res)
- .orElseGet(() -> {
- RequestCapture value = getRequestCapture(req, res);
- value.setIsProxied(true);
- return om.writeValue(value);
- });
- }
-
- private static Optional