1 | // Copyright (C) 2009 Google Inc. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package com.google.caja.ancillary.servlet; |
16 | |
17 | import com.google.caja.service.TestHttpServletRequest; |
18 | import com.google.caja.service.TestHttpServletResponse; |
19 | import com.google.caja.util.CajaTestCase; |
20 | |
21 | import java.util.Arrays; |
22 | |
23 | public class StaticFilesTest extends CajaTestCase { |
24 | public final void testBogusUrls() { |
25 | StaticFiles f = new StaticFiles("cacheId"); |
26 | assertFalse(f.exists("files/bogus")); |
27 | assertFalse(f.exists("files/bogus.html")); |
28 | assertTrue(f.exists("files/index.js")); |
29 | assertTrue(f.exists("files/styles.css")); |
30 | } |
31 | |
32 | public final void testServesBinary() throws Exception { |
33 | StaticFiles f = new StaticFiles("cacheId"); |
34 | TestHttpServletRequest req = new TestHttpServletRequest(""); |
35 | TestHttpServletResponse resp = new TestHttpServletResponse(); |
36 | f.serve("files/tools-28.gif", req, resp); |
37 | assertEquals(200, resp.getStatus()); |
38 | assertEquals("image/gif", resp.getHeaders().get("content-type")); |
39 | byte[] bytes = (byte[]) resp.getOutputObject(); |
40 | byte[] first6 = new byte[6]; |
41 | byte[] golden = "GIF89a".getBytes("UTF-8"); |
42 | System.arraycopy(bytes, 0, first6, 0, 6); |
43 | assertTrue( |
44 | Arrays.toString(first6) + " != " + Arrays.toString(golden), |
45 | Arrays.equals(golden, first6)); |
46 | } |
47 | |
48 | public final void testServesTextCompressed() throws Exception { |
49 | StaticFiles f = new StaticFiles("cacheId"); |
50 | TestHttpServletRequest req = new TestHttpServletRequest(""); |
51 | TestHttpServletResponse resp = new TestHttpServletResponse(); |
52 | f.serve("files/styles.css", req, resp); |
53 | assertEquals(200, resp.getStatus()); |
54 | assertEquals("text/css; charset=UTF-8", |
55 | resp.getHeaders().get("content-type")); |
56 | String css = (String) resp.getOutputObject(); |
57 | assertTrue(css.startsWith("body{background:#fff")); |
58 | assertFalse(css.matches("\\s[{}]|[{}]\\s")); |
59 | } |
60 | |
61 | public final void test404s() throws Exception { |
62 | StaticFiles f = new StaticFiles("cacheId"); |
63 | TestHttpServletRequest req = new TestHttpServletRequest(""); |
64 | TestHttpServletResponse resp = new TestHttpServletResponse(); |
65 | f.serve("files/nosuchfile.txt", req, resp); |
66 | assertEquals(404, resp.getStatus()); |
67 | } |
68 | |
69 | public final void testTip() throws Exception { |
70 | StaticFiles f = new StaticFiles("cacheId"); |
71 | TestHttpServletRequest req = new TestHttpServletRequest(""); |
72 | TestHttpServletResponse resp = new TestHttpServletResponse(); |
73 | f.serve("files/NO_SIDE_EFFECT_tip.html", req, resp); |
74 | assertEquals(200, resp.getStatus()); |
75 | assertTrue(((String) resp.getOutputObject()) |
76 | .contains("Operation Has No Effect</title>")); |
77 | } |
78 | } |