|
| 1 | +package org.jooby.issues; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import org.jooby.FlashScope; |
| 7 | +import org.jooby.Results; |
| 8 | +import org.jooby.test.ServerFeature; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +public class Issue397 extends ServerFeature { |
| 12 | + |
| 13 | + { |
| 14 | + get("/397/noflash", req -> req.flash()); |
| 15 | + |
| 16 | + use(new FlashScope()); |
| 17 | + |
| 18 | + get("/397/flash", req -> req.flash()); |
| 19 | + |
| 20 | + get("/397/discard", req -> req.flash().remove("success")); |
| 21 | + |
| 22 | + post("/397/reset", req -> { |
| 23 | + req.flash("foo", "bar"); |
| 24 | + return Results.redirect("/397"); |
| 25 | + }); |
| 26 | + |
| 27 | + post("/397/flash", req -> { |
| 28 | + req.flash("success", "Thank you!"); |
| 29 | + return Results.redirect("/397"); |
| 30 | + }); |
| 31 | + |
| 32 | + get("/397/untouch", req -> "untouch"); |
| 33 | + |
| 34 | + err((req, rsp, err) -> { |
| 35 | + rsp.send(err.getMessage()); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void noFlashScope() throws Exception { |
| 41 | + request() |
| 42 | + .get("/397/noflash") |
| 43 | + .expect("Bad Request(400): Flash scope isn't available. Install via: use(new FlashScope());"); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void shouldCreateAndDestroyFlashCookie() throws Exception { |
| 48 | + request() |
| 49 | + .post("/397/flash") |
| 50 | + .expect(302) |
| 51 | + .header("Set-Cookie", setCookie -> { |
| 52 | + assertEquals("flash=success=Thank+you%21;Version=1", setCookie); |
| 53 | + request() |
| 54 | + .get("/397/flash") |
| 55 | + .header("Cookie", setCookie) |
| 56 | + .expect("{success=Thank you!}") |
| 57 | + .header("Set-Cookie", clearCookie -> { |
| 58 | + assertTrue(clearCookie.startsWith("flash=;Version=1;Max-Age=0;")); |
| 59 | + }); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void shouldNotCreateCookieWhenFlashStateDontChange() throws Exception { |
| 65 | + request() |
| 66 | + .get("/397/untouch") |
| 67 | + .expect(200) |
| 68 | + .header("Set-Cookie", setCookie -> { |
| 69 | + assertEquals(null, setCookie); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void shouldRecreateCookieOnReset() throws Exception { |
| 75 | + request() |
| 76 | + .post("/397/flash") |
| 77 | + .expect(302) |
| 78 | + .header("Set-Cookie", setCookie1 -> { |
| 79 | + assertEquals("flash=success=Thank+you%21;Version=1", setCookie1); |
| 80 | + request() |
| 81 | + .post("/397/reset") |
| 82 | + .header("Cookie", setCookie1) |
| 83 | + .expect(302) |
| 84 | + .header("Set-Cookie", setCookie2 -> { |
| 85 | + assertEquals("flash=success=Thank+you%21&foo=bar;Version=1", setCookie2); |
| 86 | + request() |
| 87 | + .get("/397/flash") |
| 88 | + .header("Cookie", setCookie2) |
| 89 | + .expect("{success=Thank you!, foo=bar}") |
| 90 | + .header("Set-Cookie", clearCookie -> { |
| 91 | + assertTrue(clearCookie.startsWith("flash=;Version=1;Max-Age=0;")); |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void shouldClearFlashCookieWhenEmpty() throws Exception { |
| 99 | + request() |
| 100 | + .get("/397/discard") |
| 101 | + .header("Cookie", "flash=success=OK;Version=1") |
| 102 | + .expect(200) |
| 103 | + .header("Set-Cookie", setCookie -> { |
| 104 | + assertTrue(setCookie.startsWith("flash=;Version=1;Max-Age=0;")); |
| 105 | + }); |
| 106 | + } |
| 107 | +} |
0 commit comments