Search in sources :

Example 6 with SparkTestUtil

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

the class ServletTest method setup.

@BeforeClass
public static void setup() throws InterruptedException {
    testUtil = new SparkTestUtil(PORT);
    final Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    // Set some timeout options to make debugging easier.
    connector.setIdleTimeout(1000 * 60 * 60);
    connector.setSoLingerTime(-1);
    connector.setPort(PORT);
    server.setConnectors(new Connector[] { connector });
    WebAppContext bb = new WebAppContext();
    bb.setServer(server);
    bb.setContextPath(SOMEPATH);
    bb.setWar("src/test/webapp");
    server.setHandler(bb);
    CountDownLatch latch = new CountDownLatch(1);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                LOGGER.info(">>> STARTING EMBEDDED JETTY SERVER for jUnit testing of SparkFilter");
                server.start();
                latch.countDown();
                System.in.read();
                LOGGER.info(">>> STOPPING EMBEDDED JETTY SERVER");
                server.stop();
                server.join();
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(100);
            }
        }
    }).start();
    latch.await();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Server(org.eclipse.jetty.server.Server) SparkTestUtil(spark.util.SparkTestUtil) CountDownLatch(java.util.concurrent.CountDownLatch) BeforeClass(org.junit.BeforeClass)

Example 7 with SparkTestUtil

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

the class MultipleFiltersTest method setup.

@BeforeClass
public static void setup() {
    http = new SparkTestUtil(4567);
    before("/user", initializeCounter, incrementCounter, loadUser);
    after("/user", incrementCounter, (req, res) -> {
        int counter = req.attribute("counter");
        Assert.assertEquals(counter, 2);
    });
    get("/user", (request, response) -> {
        Assert.assertEquals((int) request.attribute("counter"), 1);
        return ((User) request.attribute("user")).name();
    });
    awaitInitialization();
}
Also used : SparkTestUtil(spark.util.SparkTestUtil) BeforeClass(org.junit.BeforeClass)

Example 8 with SparkTestUtil

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

the class MultipleServicesTest method setup.

@BeforeClass
public static void setup() throws Exception {
    firstClient = new SparkTestUtil(4567);
    secondClient = new SparkTestUtil(1234);
    first = igniteFirstService();
    second = igniteSecondService();
    first.awaitInitialization();
    second.awaitInitialization();
}
Also used : SparkTestUtil(spark.util.SparkTestUtil) BeforeClass(org.junit.BeforeClass)

Example 9 with SparkTestUtil

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

the class GenericIntegrationTest method setup.

@BeforeClass
public static void setup() throws IOException {
    testUtil = new SparkTestUtil(4567);
    tmpExternalFile = new File(System.getProperty("java.io.tmpdir"), "externalFile.html");
    FileWriter writer = new FileWriter(tmpExternalFile);
    writer.write("Content of external file");
    writer.flush();
    writer.close();
    staticFileLocation("/public");
    externalStaticFileLocation(System.getProperty("java.io.tmpdir"));
    webSocket("/ws", WebSocketTestHandler.class);
    before("/secretcontent/*", (q, a) -> {
        halt(401, "Go Away!");
    });
    before("/protected/*", "application/xml", (q, a) -> {
        halt(401, "Go Away!");
    });
    before("/protected/*", "application/json", (q, a) -> {
        halt(401, "{\"message\": \"Go Away!\"}");
    });
    get("/hi", "application/json", (q, a) -> "{\"message\": \"Hello World\"}");
    get("/hi", (q, a) -> "Hello World!");
    get("/binaryhi", (q, a) -> "Hello World!".getBytes());
    get("/bytebufferhi", (q, a) -> ByteBuffer.wrap("Hello World!".getBytes()));
    get("/inputstreamhi", (q, a) -> new ByteArrayInputStream("Hello World!".getBytes("utf-8")));
    get("/param/:param", (q, a) -> "echo: " + q.params(":param"));
    path("/firstPath", () -> {
        before("/*", (q, a) -> a.header("before-filter-ran", "true"));
        get("/test", (q, a) -> "Single path-prefix works");
        path("/secondPath", () -> {
            get("/test", (q, a) -> "Nested path-prefix works");
            path("/thirdPath", () -> {
                get("/test", (q, a) -> "Very nested path-prefix works");
            });
        });
    });
    get("/paramandwild/:param/stuff/*", (q, a) -> "paramandwild: " + q.params(":param") + q.splat()[0]);
    get("/paramwithmaj/:paramWithMaj", (q, a) -> "echo: " + q.params(":paramWithMaj"));
    get("/templateView", (q, a) -> new ModelAndView("Hello", "my view"), new TemplateEngine() {

        @Override
        public String render(ModelAndView modelAndView) {
            return modelAndView.getModel() + " from " + modelAndView.getViewName();
        }
    });
    get("/", (q, a) -> "Hello Root!");
    post("/poster", (q, a) -> {
        String body = q.body();
        // created
        a.status(201);
        return "Body was: " + body;
    });
    post("/post_via_get", (q, a) -> {
        // created
        a.status(201);
        return "Method Override Worked";
    });
    get("/post_via_get", (q, a) -> "Method Override Did Not Work");
    patch("/patcher", (q, a) -> {
        String body = q.body();
        a.status(200);
        return "Body was: " + body;
    });
    get("/session_reset", (q, a) -> {
        String key = "session_reset";
        Session session = q.session();
        session.attribute(key, "11111");
        session.invalidate();
        session = q.session();
        session.attribute(key, "22222");
        return session.attribute(key);
    });
    get("/ip", (request, response) -> request.ip());
    after("/hi", (q, a) -> {
        if (q.requestMethod().equalsIgnoreCase("get")) {
            Assert.assertNotNull(a.body());
        }
        a.header("after", "foobar");
    });
    get("/throwexception", (q, a) -> {
        throw new UnsupportedOperationException();
    });
    get("/throwsubclassofbaseexception", (q, a) -> {
        throw new SubclassOfBaseException();
    });
    get("/thrownotfound", (q, a) -> {
        throw new NotFoundException();
    });
    get("/throwmeyling", (q, a) -> {
        throw new JWGmeligMeylingException();
    });
    exception(JWGmeligMeylingException.class, (meylingException, q, a) -> {
        a.body(meylingException.trustButVerify());
    });
    exception(UnsupportedOperationException.class, (exception, q, a) -> {
        a.body("Exception handled");
    });
    exception(BaseException.class, (exception, q, a) -> {
        a.body("Exception handled");
    });
    exception(NotFoundException.class, (exception, q, a) -> {
        a.status(404);
        a.body(NOT_FOUND_BRO);
    });
    get("/exception", (request, response) -> {
        throw new RuntimeException();
    });
    afterAfter("/exception", (request, response) -> {
        response.body("done executed for exception");
    });
    post("/nice", (request, response) -> "nice response");
    afterAfter("/nice", (request, response) -> {
        response.header("post-process", "nice done response");
    });
    afterAfter((request, response) -> {
        response.header("post-process-all", "nice done response after all");
    });
    Spark.awaitInitialization();
}
Also used : FileWriter(java.io.FileWriter) NotFoundException(spark.examples.exception.NotFoundException) ByteArrayInputStream(java.io.ByteArrayInputStream) SubclassOfBaseException(spark.examples.exception.SubclassOfBaseException) JWGmeligMeylingException(spark.examples.exception.JWGmeligMeylingException) SparkTestUtil(spark.util.SparkTestUtil) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 10 with SparkTestUtil

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

the class StaticFilesFromArchiveTest method setup.

@BeforeClass
public static void setup() throws Exception {
    setupClassLoader();
    testUtil = new SparkTestUtil(4567);
    Class<?> sparkClass = classLoader.loadClass("spark.Spark");
    Method staticFileLocationMethod = sparkClass.getMethod("staticFileLocation", String.class);
    staticFileLocationMethod.invoke(null, "/public-jar");
    Method initMethod = sparkClass.getMethod("init");
    initMethod.invoke(null);
    Method awaitInitializationMethod = sparkClass.getMethod("awaitInitialization");
    awaitInitializationMethod.invoke(null);
}
Also used : SparkTestUtil(spark.util.SparkTestUtil) Method(java.lang.reflect.Method) BeforeClass(org.junit.BeforeClass)

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