OLD | NEW |
1 // Copyright (C) 2008 Google Inc. | 1 // Copyright (C) 2008 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 package com.google.caja.service; | 15 package com.google.caja.service; |
16 | 16 |
| 17 import com.google.caja.util.CajaTestCase; |
17 import com.google.caja.util.Strings; | 18 import com.google.caja.util.Strings; |
| 19 import com.google.caja.reporting.MessageLevel; |
18 import com.google.caja.reporting.TestBuildInfo; | 20 import com.google.caja.reporting.TestBuildInfo; |
19 | 21 |
20 import java.io.BufferedReader; | 22 import java.io.BufferedReader; |
21 import java.io.ByteArrayOutputStream; | 23 import java.io.ByteArrayOutputStream; |
22 import java.io.IOException; | 24 import java.io.IOException; |
23 import java.io.OutputStream; | 25 import java.io.OutputStream; |
24 import java.io.OutputStreamWriter; | 26 import java.io.OutputStreamWriter; |
25 import java.io.PrintWriter; | 27 import java.io.PrintWriter; |
26 import java.io.StringWriter; | 28 import java.io.StringWriter; |
27 import java.io.UnsupportedEncodingException; | 29 import java.io.UnsupportedEncodingException; |
(...skipping 10 matching lines...) Expand all Loading... |
38 import java.util.regex.Matcher; | 40 import java.util.regex.Matcher; |
39 import java.util.regex.Pattern; | 41 import java.util.regex.Pattern; |
40 | 42 |
41 import javax.servlet.ServletInputStream; | 43 import javax.servlet.ServletInputStream; |
42 import javax.servlet.ServletOutputStream; | 44 import javax.servlet.ServletOutputStream; |
43 import javax.servlet.http.Cookie; | 45 import javax.servlet.http.Cookie; |
44 import javax.servlet.http.HttpServletRequest; | 46 import javax.servlet.http.HttpServletRequest; |
45 import javax.servlet.http.HttpServletResponse; | 47 import javax.servlet.http.HttpServletResponse; |
46 import javax.servlet.http.HttpSession; | 48 import javax.servlet.http.HttpSession; |
47 | 49 |
48 import junit.framework.TestCase; | |
49 | |
50 /** | 50 /** |
51 * Tests the running the cajoler as a webservice | 51 * Tests the running the cajoler as a webservice |
52 * | 52 * |
53 * @author jasvir@google.com (Jasvir Nagra) | 53 * @author jasvir@google.com (Jasvir Nagra) |
54 */ | 54 */ |
55 public class CajolingServiceTest extends TestCase { | 55 public class CajolingServiceTest extends CajaTestCase { |
56 private CajolingService service; | 56 private CajolingService service; |
57 private Map<URI, CajolingService.FetchedData> uriContent; | 57 private Map<URI, CajolingService.FetchedData> uriContent; |
58 | 58 |
59 @Override | 59 @Override |
60 protected void setUp() throws Exception { | 60 protected void setUp() throws Exception { |
61 super.setUp(); | 61 super.setUp(); |
62 | 62 |
63 service = new CajolingService(new TestBuildInfo()) { | 63 service = new CajolingService(new TestBuildInfo()) { |
64 @Override | 64 @Override |
65 protected CajolingService.FetchedData fetch(URI uri) throws IOException { | 65 protected CajolingService.FetchedData fetch(URI uri) throws IOException { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 TestHttpServletResponse resp = new TestHttpServletResponse(); | 103 TestHttpServletResponse resp = new TestHttpServletResponse(); |
104 service.doGet(req, resp); | 104 service.doGet(req, resp); |
105 return resp.getOutputObject(); | 105 return resp.getOutputObject(); |
106 } | 106 } |
107 | 107 |
108 public void testSimpleJs() throws Exception { | 108 public void testSimpleJs() throws Exception { |
109 registerUri("http://foo/bar.js", "g(1);", "text/javascript"); | 109 registerUri("http://foo/bar.js", "g(1);", "text/javascript"); |
110 assertEquals( | 110 assertEquals( |
111 valijaModule("moduleResult___ = $v.cf($v.ro('g'), [ 1 ]);"), | 111 valijaModule("moduleResult___ = $v.cf($v.ro('g'), [ 1 ]);"), |
112 request("?url=http://foo/bar.js&mime-type=text/javascript" + | 112 request("?url=http://foo/bar.js&mime-type=text/javascript" + |
113 "&transform=VALIJA")); | 113 "&transform=VALIJA")); |
114 } | 114 } |
115 | 115 |
116 public void testInnocentJs() throws Exception { | 116 public void testInnocentJs() throws Exception { |
117 registerUri("http://foo/innocent.js", "for (var k in x) { k; }", | 117 registerUri("http://foo/innocent.js", "for (var k in x) { k; }", |
118 "text/javascript"); | 118 "text/javascript"); |
119 assertEquals("{\n" + | 119 assertEquals("{\n" + |
120 " var x0___;\n" + | 120 " var x0___;\n" + |
121 " for (x0___ in x) {\n" + | 121 " for (x0___ in x) {\n" + |
122 " if (x0___.match(/___$/)) { continue; }\n" + | 122 " if (x0___.match(/___$/)) { continue; }\n" + |
123 " k = x0___;\n" + | 123 " k = x0___;\n" + |
124 " { k; }\n" + | 124 " { k; }\n" + |
125 " }\n" + | 125 " }\n" + |
126 "}", | 126 "}", |
127 request("?url=http://foo/innocent.js&mime-type=text/javascript" + | 127 request("?url=http://foo/innocent.js&mime-type=text/javascript" + |
128 "&transform=INNOCENT")); | 128 "&transform=INNOCENT")); |
129 } | 129 } |
130 | 130 |
131 public void testVbScriptRejected() throws Exception { | 131 public void testVbScriptRejected() throws Exception { |
132 registerUri("http://foo/bar.vb", "zoicks()", "text/vbscript"); | 132 registerUri("http://foo/bar.vb", "zoicks()", "text/vbscript"); |
133 assertEquals( | 133 assertEquals( |
134 "ERROR", | 134 "ERROR", |
135 request("?url=http://foo/bar.vb&mime-type=text/javascript")); | 135 request("?url=http://foo/bar.vb&mime-type=text/javascript")); |
136 | 136 |
137 } | 137 } |
138 | 138 |
(...skipping 16 matching lines...) Expand all Loading... |
155 } | 155 } |
156 | 156 |
157 public void testNotImage() throws Exception { | 157 public void testNotImage() throws Exception { |
158 registerUri("http://foo/bar.gif", "foo()", "text/javascript"); | 158 registerUri("http://foo/bar.gif", "foo()", "text/javascript"); |
159 assertEquals("ERROR", request("?url=http://foo/bar.gif&mime-type=image/*")); | 159 assertEquals("ERROR", request("?url=http://foo/bar.gif&mime-type=image/*")); |
160 } | 160 } |
161 | 161 |
162 public void testHtml() throws Exception { | 162 public void testHtml() throws Exception { |
163 String htmlEnvelope = ( | 163 String htmlEnvelope = ( |
164 "<html>" + | 164 "<html>" + |
165 " <head><title>Caja Test</title></head>" + | 165 "<head><title>Caja Test</title></head>" + |
166 " <body>" + | 166 "<body>" + |
167 " %s" + | 167 "%s" + |
168 " </body>" + | 168 "</body>" + |
169 "</html>"); | 169 "</html>"); |
170 | 170 |
171 registerUri("http://foo/bar.js", "foo()", "text/javascript"); | 171 registerUri("http://foo/bar.js", "foo()", "text/javascript"); |
172 registerUri("http://foo/bar.html", | 172 registerUri("http://foo/bar.html", |
173 String.format( | 173 String.format( |
174 htmlEnvelope, | 174 htmlEnvelope, |
175 "<script src=bar.js></script><p>Hello, World!</p>"), | 175 "<p>Hello, World!</p><script src=bar.js></script>"), |
176 "text/html"); | 176 "text/html"); |
| 177 assertMessagesLessSevereThan(MessageLevel.WARNING); |
177 assertEquals( | 178 assertEquals( |
178 "{\n" | 179 "<p>Hello, World!</p><script type=\"text/javascript\">{" |
179 + " ___.loadModule({\n" | 180 + "___.loadModule({" |
180 + " 'instantiate': function (___, IMPORTS___) {\n" | 181 + "'instantiate':function(___,IMPORTS___){" |
181 + " var moduleResult___ = ___.NO_RESULT;\n" | 182 + "var moduleResult___=___.NO_RESULT;" |
182 + " var $v = ___.readImport(IMPORTS___, '$v', {\n" | 183 + "var\n$v=___.readImport(IMPORTS___,'$v',{" |
183 + " 'getOuters': {\n" | 184 + "'getOuters':{'()':{}}," |
184 + " '()': { }\n" | 185 + "'initOuter':{'()':{}}," |
185 + " },\n" | 186 + "'cf':{'()':{}}," |
186 + " 'initOuter': {\n" | 187 + "'ro':{'()':{}}" |
187 + " '()': { }\n" | 188 + "});" |
188 + " }\n" | 189 + "var\n$dis=$v.getOuters();" |
189 + " });\n" | 190 + "$v.initOuter('onerror');" |
190 + " var $dis = $v.getOuters();\n" | 191 + "try{" |
191 + " moduleResult___ = $v.initOuter('onerror');\n" | 192 + "{moduleResult___=$v.cf($v.ro('foo'),[]);}" |
192 + " IMPORTS___.htmlEmitter___.pc(' ').b('p').f(fa
lse)\n" | 193 + "}catch(ex___){" |
193 + " .ih('Hello, World!').e('p').pc(' ').cd();\n" | 194 + "___.getNewModuleHandler().handleUncaughtException(" |
194 + " return moduleResult___;\n" | 195 + "ex___,$v.ro('onerror'),'bar.js','1');" |
195 + " },\n" | 196 + "}" |
196 + " 'cajolerName': 'com.google.caja',\n" | 197 + "return moduleResult___;" |
197 + " 'cajolerVersion': 'testBuildVersion',\n" | 198 + "}," |
198 + " 'cajoledDate': 0\n" | 199 + "'cajolerName':'com.google.caja'," |
199 + " });\n" | 200 + "'cajolerVersion':'testBuildVersion'," |
200 + "}", | 201 + "'cajoledDate':0" |
| 202 + "});" |
| 203 + "}</script>", |
201 (String) request("?url=http://foo/bar.html&mime-type=*/*")); | 204 (String) request("?url=http://foo/bar.html&mime-type=*/*")); |
202 } | 205 } |
203 | 206 |
204 public void testGadget() throws Exception { | 207 public void testGadget() throws Exception { |
205 String moduleEnvelope = ( | 208 String moduleEnvelope = ( |
206 "<Module><ModulePrefs /><Content type=\"html\">" | 209 "<Module><ModulePrefs /><Content type=\"html\">" |
207 + "<![CDATA[%s]]>" | 210 + "<![CDATA[%s]]>" |
208 + "</Content></Module>"); | 211 + "</Content></Module>"); |
209 | 212 |
210 registerUri("http://foo/bar.js", "foo()", "text/javascript"); | 213 registerUri("http://foo/bar.js", "foo()", "text/javascript"); |
211 registerUri("http://foo/bar.xml", | 214 registerUri("http://foo/bar.xml", |
212 String.format( | 215 String.format( |
213 moduleEnvelope, | 216 moduleEnvelope, |
214 "<script src=bar.js></script><p>Hello, World!</p>"), | 217 "<script src=bar.js></script><p>Hello, World!</p>"), |
215 "application/xml"); | 218 "application/xml"); |
216 // TODO(mikesamuel): why are scripts not fetched? | 219 String indent = " "; |
217 assertEquals( | 220 assertEquals( |
218 String.format( | 221 String.format( |
219 moduleEnvelope, | 222 moduleEnvelope, |
220 "<script type=\"text/javascript\">{\n" | 223 "<p>Hello, World!</p><script type=\"text/javascript\">{\n" |
221 + " ___.loadModule({\n" | 224 + " ___.loadModule({\n" |
222 + " 'instantiate': function (___, IMPORTS___) {\n" | 225 + indent + " 'instantiate': function (___, IMPORTS___) {\n" |
223 + " var moduleResult___ = ___.NO_RESULT;\n" | 226 + indent + " var moduleResult___ = ___.NO_RESULT;\n" |
224 + " IMPORTS___.htmlEmitter___.b('p').f(false)\n" | 227 + indent + " var foo = ___.readImport(IMPORTS___, 'foo');\n" |
225 + " .ih('Hello, World!').e('p').cd();\n" | 228 + indent + " var onerror = ___.readImport(" |
226 + " return moduleResult___;\n" | 229 + "IMPORTS___, 'onerror');\n" |
227 + " },\n" | 230 + indent + " try {\n" |
228 + " 'cajolerName': 'com.google.caja',\n" | 231 + indent + " {\n" |
229 + " 'cajolerVersion': 'testBuildVersion',\n" | 232 + indent + " moduleResult___ = foo.CALL___();\n" |
230 + " 'cajoledDate': 0\n" | 233 + indent + " }\n" |
231 + " });\n" | 234 + indent + " } catch (ex___) {\n" |
| 235 + indent + " ___.getNewModuleHandler()" |
| 236 + ".handleUncaughtException(ex___,\n" |
| 237 + indent + " onerror, 'bar.js', '1');\n" |
| 238 + indent + " }\n" |
| 239 + indent + " return moduleResult___;\n" |
| 240 + indent + " },\n" |
| 241 + indent + " 'cajolerName': 'com.google.caja',\n" |
| 242 + indent + " 'cajolerVersion': 'testBuildVersion',\n" |
| 243 + indent + " 'cajoledDate': 0\n" |
| 244 + indent + "});\n" |
232 + "}</script>"), | 245 + "}</script>"), |
233 request("?url=http://foo/bar.xml&mime-type=*/*")); | 246 (String) request("?url=http://foo/bar.xml&mime-type=*/*")); |
234 } | 247 } |
235 | 248 |
236 private static String valijaModule(String... lines) { | 249 private static String valijaModule(String... lines) { |
| 250 String indent = " "; |
237 String prefix = ( | 251 String prefix = ( |
238 "" | 252 "" |
239 + "{\n" | 253 + "{\n" |
240 + " ___.loadModule({\n" | 254 + " ___.loadModule({\n" |
241 + " 'instantiate': function (___, IMPORTS___) {\n" | 255 + indent + " 'instantiate': function (___, IMPORTS___) {\n" |
242 + " var moduleResult___ = ___.NO_RESULT;\n" | 256 + indent + " var moduleResult___ = ___.NO_RESULT;\n" |
243 + " var $v = ___.readImport(IMPORTS___, '$v', {\n" | 257 + indent + " var $v = ___.readImport(IMPORTS___, '$v', {\n" |
244 + " 'getOuters': {\n" | 258 + indent + " 'getOuters': {\n" |
245 + " '()': { }\n" | 259 + indent + " '()': { }\n" |
246 + " },\n" | 260 + indent + " },\n" |
247 + " 'initOuter': {\n" | 261 + indent + " 'initOuter': {\n" |
248 + " '()': { }\n" | 262 + indent + " '()': { }\n" |
249 + " },\n" | 263 + indent + " },\n" |
250 + " 'cf': {\n" | 264 + indent + " 'cf': {\n" |
251 + " '()': { }\n" | 265 + indent + " '()': { }\n" |
252 + " },\n" | 266 + indent + " },\n" |
253 + " 'ro': {\n" | 267 + indent + " 'ro': {\n" |
254 + " '()': { }\n" | 268 + indent + " '()': { }\n" |
255 + " }\n" | 269 + indent + " }\n" |
256 + " });\n" | 270 + indent + " });\n" |
257 + " var $dis = $v.getOuters();\n" | 271 + indent + " var $dis = $v.getOuters();\n" |
258 + " $v.initOuter('onerror');\n" | 272 + indent + " $v.initOuter('onerror');\n" |
259 ); | 273 ); |
260 String suffix = ( | 274 String suffix = ( |
261 "" | 275 "" |
262 + " return moduleResult___;\n" | 276 + indent + " return moduleResult___;\n" |
263 + " },\n" | 277 + indent + " },\n" |
264 + " 'cajolerName': 'com.google.caja',\n" | 278 + indent + " 'cajolerName': 'com.google.caja',\n" |
265 + " 'cajolerVersion': 'testBuildVersion',\n" | 279 + indent + " 'cajolerVersion': 'testBuildVersion',\n" |
266 + " 'cajoledDate': 0\n" | 280 + indent + " 'cajoledDate': 0\n" |
267 + " });\n" | 281 + indent + "});\n" |
268 + "}" | 282 + "}" |
269 ); | 283 ); |
270 StringBuilder sb = new StringBuilder(); | 284 StringBuilder sb = new StringBuilder(); |
271 sb.append(prefix); | 285 sb.append(prefix); |
272 for (String line : lines) { | 286 for (String line : lines) { |
273 sb.append(" ").append(line).append('\n'); | 287 sb.append(" ").append(line).append('\n'); |
274 } | 288 } |
275 sb.append(suffix); | 289 sb.append(suffix); |
276 return sb.toString(); | 290 return sb.toString(); |
277 } | 291 } |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 return new String(bytes, enc); | 488 return new String(bytes, enc); |
475 } catch (UnsupportedEncodingException ex) { | 489 } catch (UnsupportedEncodingException ex) { |
476 throw new RuntimeException(ex); | 490 throw new RuntimeException(ex); |
477 } | 491 } |
478 } | 492 } |
479 return bytes; | 493 return bytes; |
480 } | 494 } |
481 return ((StringWriter) output).toString(); | 495 return ((StringWriter) output).toString(); |
482 } | 496 } |
483 } | 497 } |
OLD | NEW |