Search in sources :

Example 1 with SparkTestUtil

use of spark.util.SparkTestUtil in project spark by perwendel.

the class StaticFilesTest method setup.

@BeforeClass
public static void setup() throws IOException {
    testUtil = new SparkTestUtil(4567);
    tmpExternalFile = new File(System.getProperty("java.io.tmpdir"), EXTERNAL_FILE_NAME_HTML);
    FileWriter writer = new FileWriter(tmpExternalFile);
    writer.write(CONTENT_OF_EXTERNAL_FILE);
    writer.flush();
    writer.close();
    staticFiles.location("/public");
    staticFiles.externalLocation(System.getProperty("java.io.tmpdir"));
    get("/hello", (q, a) -> FO_SHIZZY);
    get("/*", (q, a) -> {
        throw new NotFoundException();
    });
    exception(NotFoundException.class, (e, request, response) -> {
        response.status(404);
        response.body(NOT_FOUND_BRO);
    });
    Spark.awaitInitialization();
}
Also used : FileWriter(java.io.FileWriter) NotFoundException(spark.examples.exception.NotFoundException) SparkTestUtil(spark.util.SparkTestUtil) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 2 with SparkTestUtil

use of spark.util.SparkTestUtil in project spark by perwendel.

the class StaticFilesTestExternal method setup.

@BeforeClass
public static void setup() throws IOException {
    testUtil = new SparkTestUtil(4567);
    String directoryRoot = System.getProperty("java.io.tmpdir") + "sparkish";
    new File(directoryRoot).mkdirs();
    tmpExternalFile1 = new File(directoryRoot, EXTERNAL_FILE_NAME_HTML);
    FileWriter writer = new FileWriter(tmpExternalFile1);
    writer.write(CONTENT_OF_EXTERNAL_FILE);
    writer.flush();
    writer.close();
    File root = new File(directoryRoot);
    folderOutsideStaticFiles = new File(root.getAbsolutePath() + "/../dumpsterstuff");
    folderOutsideStaticFiles.mkdirs();
    String newFilePath = root.getAbsolutePath() + "/../dumpsterstuff/Spark.class";
    tmpExternalFile2 = new File(newFilePath);
    tmpExternalFile2.createNewFile();
    staticFiles.externalLocation(directoryRoot);
    get("/hello", (q, a) -> FO_SHIZZY);
    get("/*", (q, a) -> {
        throw new NotFoundException();
    });
    exception(NotFoundException.class, (e, request, response) -> {
        response.status(404);
        response.body(NOT_FOUND_BRO);
    });
    Spark.awaitInitialization();
}
Also used : FileWriter(java.io.FileWriter) NotFoundException(spark.examples.exception.NotFoundException) SparkTestUtil(spark.util.SparkTestUtil) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 3 with SparkTestUtil

use of spark.util.SparkTestUtil in project spark by perwendel.

the class BodyAvailabilityTest method setup.

@BeforeClass
public static void setup() {
    LOGGER.debug("setup()");
    testUtil = new SparkTestUtil(4567);
    beforeBody = null;
    routeBody = null;
    afterBody = null;
    before("/hello", (req, res) -> {
        LOGGER.debug("before-req.body() = " + req.body());
        beforeBody = req.body();
    });
    post("/hello", (req, res) -> {
        LOGGER.debug("get-req.body() = " + req.body());
        routeBody = req.body();
        return req.body();
    });
    after("/hello", (req, res) -> {
        LOGGER.debug("after-before-req.body() = " + req.body());
        afterBody = req.body();
    });
    Spark.awaitInitialization();
}
Also used : SparkTestUtil(spark.util.SparkTestUtil) BeforeClass(org.junit.BeforeClass)

Example 4 with SparkTestUtil

use of spark.util.SparkTestUtil in project spark by perwendel.

the class ResponseWrapperDelegationTest method setup.

@BeforeClass
public static void setup() throws IOException {
    testUtil = new SparkTestUtil(4567);
    get("/204", (q, a) -> {
        a.status(204);
        return "";
    });
    after("/204", (q, a) -> {
        if (a.status() == 204) {
            a.status(200);
            a.body("ok");
        }
    });
    get("/json", (q, a) -> {
        a.type("application/json");
        return "{\"status\": \"ok\"}";
    });
    after("/json", (q, a) -> {
        if ("application/json".equalsIgnoreCase(a.type())) {
            a.type("text/plain");
        }
    });
    exception(Exception.class, (exception, q, a) -> {
        exception.printStackTrace();
    });
    Spark.awaitInitialization();
}
Also used : SparkTestUtil(spark.util.SparkTestUtil) BeforeClass(org.junit.BeforeClass)

Example 5 with SparkTestUtil

use of spark.util.SparkTestUtil in project spark by perwendel.

the class ServicePortIntegrationTest method testGetPort_withRandomPort.

@Test
public void testGetPort_withRandomPort() throws Exception {
    int actualPort = service.port();
    LOGGER.info("got port ");
    SparkTestUtil testUtil = new SparkTestUtil(actualPort);
    SparkTestUtil.UrlResponse response = testUtil.doMethod("GET", "/hi", null);
    Assert.assertEquals(200, response.status);
    Assert.assertEquals("Hello World!", response.body);
}
Also used : SparkTestUtil(spark.util.SparkTestUtil) Test(org.junit.Test)

Aggregations

SparkTestUtil (spark.util.SparkTestUtil)18 BeforeClass (org.junit.BeforeClass)16 File (java.io.File)5 FileWriter (java.io.FileWriter)5 NotFoundException (spark.examples.exception.NotFoundException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Server (org.eclipse.jetty.server.Server)1 ServerConnector (org.eclipse.jetty.server.ServerConnector)1 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)1 Test (org.junit.Test)1 JWGmeligMeylingException (spark.examples.exception.JWGmeligMeylingException)1 SubclassOfBaseException (spark.examples.exception.SubclassOfBaseException)1