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();
}
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();
}
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();
}
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();
}
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);
}
Aggregations