|
| 1 | +package org.jooby; |
| 2 | + |
| 3 | +import org.jooby.handlers.AssetHandler; |
| 4 | +import org.jooby.test.ServerFeature; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +public class AssetHandlerFeature extends ServerFeature { |
| 8 | + |
| 9 | + { |
| 10 | + assets("/assets/**", new AssetHandler("/", getClass())); |
| 11 | + } |
| 12 | + |
| 13 | + @Test |
| 14 | + public void jsAsset() throws Exception { |
| 15 | + request() |
| 16 | + .get("/assets/file.js") |
| 17 | + .expect(200) |
| 18 | + .header("Content-Type", "application/javascript;charset=UTF-8") |
| 19 | + .header("Content-Length", 15) |
| 20 | + .header("Last-Modified", lastModified -> { |
| 21 | + request() |
| 22 | + .get("/assets/file.js") |
| 23 | + .header("If-Modified-Since", lastModified) |
| 24 | + .expect(304) |
| 25 | + .empty(); |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void cssAsset() throws Exception { |
| 31 | + request() |
| 32 | + .get("/assets/file.css") |
| 33 | + .expect(200) |
| 34 | + .header("Content-Type", "text/css;charset=UTF-8") |
| 35 | + .header("Content-Length", 22) |
| 36 | + .header("Last-Modified", lastModified -> { |
| 37 | + request() |
| 38 | + .get("/assets/file.css") |
| 39 | + .header("If-Modified-Since", lastModified) |
| 40 | + .expect(304) |
| 41 | + .empty(); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void imageAsset() throws Exception { |
| 47 | + request() |
| 48 | + .get("/assets/favicon.ico") |
| 49 | + .expect(200) |
| 50 | + .header("Content-Type", "image/x-icon") |
| 51 | + .header("Content-Length", 2238) |
| 52 | + .header("Last-Modified", lastModified -> { |
| 53 | + request() |
| 54 | + .get("/assets/favicon.ico") |
| 55 | + .header("If-Modified-Since", lastModified) |
| 56 | + .expect(304) |
| 57 | + .empty(); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void emptyFile() throws Exception { |
| 63 | + request() |
| 64 | + .get("/assets/empty.css") |
| 65 | + .expect(200) |
| 66 | + .header("Content-Type", "text/css;charset=UTF-8") |
| 67 | + .header("Content-Length", 0) |
| 68 | + .header("Last-Modified", lastModified -> { |
| 69 | + request() |
| 70 | + .get("/assets/empty.css") |
| 71 | + .header("If-Modified-Since", lastModified) |
| 72 | + .expect(304) |
| 73 | + .empty(); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void largeFile() throws Exception { |
| 79 | + request() |
| 80 | + .get("/assets/jquery-2.1.4.js") |
| 81 | + .expect(200) |
| 82 | + .header("Content-Type", "application/javascript;charset=UTF-8") |
| 83 | + .header("Content-Length", 247597) |
| 84 | + .header("Last-Modified", lastModified -> { |
| 85 | + request() |
| 86 | + .get("/assets/file.js") |
| 87 | + .header("If-Modified-Since", lastModified) |
| 88 | + .expect(304) |
| 89 | + .empty(); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments