Java Code Examples for org.rapidoid.setup.On#req()
The following examples show how to use
org.rapidoid.setup.On#req() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpChunkedResponseTest.java From rapidoid with Apache License 2.0 | 6 votes |
@Test public void testChunkedEncoding() { On.req((req, resp) -> { resp.chunk("ab".getBytes()); resp.chunk("c".getBytes()); resp.chunk("d".getBytes()); return resp; }); getReq("/"); assertTimeout(Duration.ofSeconds(20), () -> { Self.get("/").expect("abcd").execute(); Self.get("/").expect("abcd").benchmark(1, 100, REQUESTS); Self.post("/").expect("abcd").benchmark(1, 100, REQUESTS); }); }
Example 2
Source File: AsyncHttpServerTest.java From rapidoid with Apache License 2.0 | 6 votes |
@Test public void testAsyncHttpServerNested() { On.req(req -> { U.must(!req.isAsync()); req.async(); U.must(req.isAsync()); async(() -> appendTo(req, "O", false, () -> async(() -> { appendTo(req, "K", true, null); // req.done() is in "appendTo" }))); return req; }); assertTimeout(Duration.ofSeconds(20), () -> { Self.get("/").expect("OK").execute(); Self.get("/").expect("OK").benchmark(1, 100, REQUESTS); Self.post("/").expect("OK").benchmark(1, 100, REQUESTS); }); }
Example 3
Source File: AsyncHttpServerTest.java From rapidoid with Apache License 2.0 | 6 votes |
@Test public void testAsyncHttpServer2() { On.req(req -> async(() -> { Resp resp = req.response(); resp.chunk("A".getBytes()); async(() -> { resp.chunk("SYNC".getBytes()); IO.close(resp.out(), false); req.done(); }); })); assertTimeout(Duration.ofSeconds(20), () -> { Self.get("/").expect("ASYNC").execute(); Self.get("/").expect("ASYNC").benchmark(1, 100, REQUESTS); Self.post("/").expect("ASYNC").benchmark(1, 100, REQUESTS); }); }
Example 4
Source File: HttpChunkedStreamTest.java From rapidoid with Apache License 2.0 | 6 votes |
@Test public void testChunkedEncoding() { On.req(req -> { OutputStream out = req.out(); out.write("ab".getBytes()); out.write("c".getBytes()); out.flush(); out.write("d".getBytes()); out.close(); return req; }); getReq("/"); assertTimeout(Duration.ofSeconds(20), () -> { Self.get("/").expect("abcd").execute(); Self.get("/").expect("abcd").benchmark(1, 100, REQUESTS); Self.post("/").expect("abcd").benchmark(1, 100, REQUESTS); }); }
Example 5
Source File: Main.java From rapidoid with Apache License 2.0 | 5 votes |
public static void main(String[] args) { On.req(req -> { int counter = req.token("n", 0) + 1; req.token().put("n", counter); return counter; }); }
Example 6
Source File: Main.java From rapidoid with Apache License 2.0 | 5 votes |
public static void main(String[] args) { On.req(req -> { int counter = req.session("n", 0) + 1; req.session().put("n", counter); return counter; }); }
Example 7
Source File: HttpResponseStreamTest.java From rapidoid with Apache License 2.0 | 5 votes |
@Test public void testOutputStream() { On.req(req -> { IO.write(req.response().out(), req.data().toString()); req.response().out().close(); // FIXME remove this, auto-close on Req done // req.done(); // FIXME fails (job already finished) return req; }); getReq("/?msg=hello!"); postData("/", U.map("x", 123)); }
Example 8
Source File: HttpChunkedResponseTest.java From rapidoid with Apache License 2.0 | 5 votes |
@Test public void testChunkedEncodingAsync() { On.req((req, resp) -> { U.must(!req.isAsync()); req.async(); U.must(req.isAsync()); async(() -> { resp.chunk("ab".getBytes()); async(() -> { resp.chunk("c".getBytes()); async(() -> { resp.chunk("d".getBytes()); req.done(); }); }); }); return req; }); getReq("/"); assertTimeout(Duration.ofSeconds(20), () -> { Self.get("/").expect("abcd").execute(); Self.get("/").expect("abcd").benchmark(1, 100, REQUESTS); Self.post("/").expect("abcd").benchmark(1, 100, REQUESTS); }); }
Example 9
Source File: AsyncHttpServerTest.java From rapidoid with Apache License 2.0 | 5 votes |
@Test public void testAsyncHttpServer() { On.req(req -> { U.must(!req.isAsync()); req.async(); U.must(req.isAsync()); async(() -> { IO.write(req.out(), "O"); async(() -> { IO.write(req.out(), "K"); try { req.out().close(); } catch (IOException e) { e.printStackTrace(); } req.done(); }); }); return req; }); assertTimeout(Duration.ofSeconds(20), () -> { Self.get("/").expect("OK").execute(); Self.get("/").expect("OK").benchmark(1, 100, REQUESTS); Self.post("/").expect("OK").benchmark(1, 100, REQUESTS); }); }
Example 10
Source File: HttpTokenTest.java From rapidoid with Apache License 2.0 | 5 votes |
@Test public void testHttpToken() { On.req((ReqRespHandler) (req, resp) -> { Log.info("Token", "data", req.token()); int n = req.token("n", 0) + 1; resp.token("n", n); int m = req.token("m", 10) + 1; resp.token("m", m); return n + ":" + m; }); HttpClient client = HTTP.client().keepCookies(true); eq(client.get(localhost("/a")).fetch(), "1:11"); eq(client.get(localhost("/b")).fetch(), "2:12"); eq(client.get(localhost("/c")).fetch(), "3:13"); client.close(); client = HTTP.client().keepCookies(true); // do it again eq(client.get(localhost("/a")).fetch(), "1:11"); eq(client.get(localhost("/b")).fetch(), "2:12"); eq(client.get(localhost("/c")).fetch(), "3:13"); client.close(); }
Example 11
Source File: CustomizationTest.java From rapidoid with Apache License 2.0 | 5 votes |
@Test public void customJsonBodyParser() { My.jsonRequestBodyParser((req, body) -> U.map("uri", req.uri(), "parsed", JSON.parse(body))); On.post("/abc").json(req -> req.data()); On.req(req -> req.posted()); postData("/abc?multipart", U.map("x", 13579, "foo", "bar")); postData("/abc2?multipart", U.map("x", 13579, "foo", "bar")); postJson("/abc?custom", U.map("x", 13579, "foo", "bar")); postJson("/abc2?custom", U.map("x", 13579, "foo", "bar")); }
Example 12
Source File: HttpChunkedStreamTest.java From rapidoid with Apache License 2.0 | 5 votes |
@Test public void testChunkedEncodingAsync() { On.req(req -> { U.must(!req.isAsync()); req.async(); U.must(req.isAsync()); OutputStream out = req.out(); async(() -> { out.write("ab".getBytes()); out.flush(); async(() -> { out.write("c".getBytes()); out.flush(); async(() -> { out.write("d".getBytes()); out.close(); req.done(); }); }); }); return req; }); getReq("/"); assertTimeout(Duration.ofSeconds(20), () -> { Self.get("/").expect("abcd").execute(); Self.get("/").expect("abcd").benchmark(1, 100, REQUESTS); Self.post("/").expect("abcd").benchmark(1, 100, REQUESTS); }); }
Example 13
Source File: HttpCookiesTest.java From rapidoid with Apache License 2.0 | 5 votes |
@Test public void testHttpCookies() { On.req((ReqRespHandler) (req, resp) -> { resp.cookie(req.uri(), "" + req.cookies().size()); return resp.json(req.cookies()); }); multiThreaded(THREADS, ROUNDS, this::checkCookies); }
Example 14
Source File: HttpCookiesTest.java From rapidoid with Apache License 2.0 | 5 votes |
@Test public void testNoCookies() { On.req((ReqRespHandler) (req, resp) -> { isTrue(req.cookies().isEmpty()); return req.cookies().size(); }); multiThreaded(THREADS, ROUNDS, () -> { checkNoCookies(true); checkNoCookies(false); }); }
Example 15
Source File: HttpServerTest.java From rapidoid with Apache License 2.0 | 5 votes |
@Test public void testHttpServer() { On.get("/").html("home"); On.req((ReqHandler) req -> req.response().json("abc")); eq(HTTP.get(localhost("/")).fetch(), "home"); eq(HTTP.post(localhost("/")).fetch(), "\"abc\""); }
Example 16
Source File: MicroServicesTest.java From rapidoid with Apache License 2.0 | 4 votes |
@Test public void testMicroserviceCommunication() { On.req((ReqHandler) req -> U.num(req.param("n")) + 1); // a blocking call eq(REST.get(localhost("/?n=7"), Integer.class).intValue(), 8); eq(REST.post(localhost("/?n=7"), Integer.class).intValue(), 8); int count = 1000; final CountDownLatch latch = new CountDownLatch(count); Msc.startMeasure(); RapidoidThread loop = Msc.loop(() -> { System.out.println(latch); U.sleep(1000); }); for (int i = 0; i < count; i++) { final int expected = i + 1; Callback<Integer> callback = (result, error) -> { if (result != null) { eq(result.intValue(), expected); } else { registerError(error); } latch.countDown(); }; if (i % 2 == 0) { REST.get(localhost("/?n=" + i), Integer.class, callback); } else { REST.post(localhost("/?n=" + i), Integer.class, callback); } } Wait.on(latch); Msc.endMeasure(count, "calls"); loop.interrupt(); }
Example 17
Source File: HttpServerHeadersTest.java From rapidoid with Apache License 2.0 | 4 votes |
@Test public void shouldHandleVariousHttpRequests() { On.get("/fileabc").html((ReqHandler) x -> x.response().filename("abc.txt").result("abcde")); On.get("/bin").serve((ReqHandler) x -> { x.response().contentType(MediaType.BINARY); return "bin"; }); On.get("/session").html((ReqRespHandler) (x, resp) -> { if (x.cookie("ses", null) == null) { resp.cookie("ses", "023B"); } resp.cookie("key" + Rnd.rnd(100), "val" + Rnd.rnd(100)); return resp.html("oki"); }); On.get("/async").html((ReqHandler) x -> { x.async(); Jobs.schedule(() -> x.response().result("now").done(), 50, TimeUnit.MILLISECONDS); return x; }); On.get("/testfile1").html((ReqHandler) x -> IO.file("test1.txt")); On.get("/rabbit.jpg").html((ReqHandler) x -> x.response().file(IO.file("rabbit.jpg"))); On.get("/ab").html((ReqHandler) x -> x.response().file(IO.file("ab.html"))); On.req((ReqHandler) x -> { x.cookies().put("asd", "f"); return x.response().html("a<b>b</b>c"); }); byte[] ab = IO.loadBytes("ab.html"); for (int i = 0; i < N; i++) { eq(get("/"), "a<b>b</b>c"); eq(get("/xy"), "a<b>b</b>c"); eq(get("/async"), "now"); eq(get("/session"), "oki"); eq(get("/bin"), "bin"); eq(get("/fileabc"), "abcde"); eq(get("/testfile1"), "TEST1"); eq(getBytes("/ab"), ab); } }
Example 18
Source File: HttpClientTest.java From rapidoid with Apache License 2.0 | 3 votes |
@Test public void testHttpClientOnLocalServer() { On.req(req -> SIMPLE_RESPONSE); for (int k = 0; k < 3; k++) { ResultCounterCallback<String> cb = new ResultCounterCallback<>(); HttpClientCallback hcb = new HttpClientBodyCallback(cb); HttpClient client = new HttpClient(); int count1 = 1000; for (int i = 0; i < count1; i++) { client.get("localhost", 8080, GET_LOCALHOST, hcb); } waiting(); while (cb.getResultCount() < count1) { timeout(50000); } eq(cb.getResults(), U.set(SIMPLE_RESPONSE)); client.shutdown(); } }
Example 19
Source File: Main.java From rapidoid with Apache License 2.0 | 3 votes |
public static void main(String[] args) { /* Generic handlers match any request (in the declaration order) */ On.req(req -> req.data().isEmpty() ? "Simple: " + req.uri() : null); /* The next handler is executed if the previous returns [NOT FOUND] */ On.req(req -> U.list(req.verb(), req.uri(), req.data())); }