1 | // Copyright (C) 2008 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.service; |
16 | |
17 | import junit.framework.AssertionFailedError; |
18 | |
19 | import java.util.regex.Matcher; |
20 | import java.util.regex.Pattern; |
21 | |
22 | /** |
23 | * @author jasvir@google.com (Jasvir Nagra) |
24 | */ |
25 | public class JsHandlerTest extends ServiceTestCase { |
26 | public final void testJs() throws Exception { |
27 | registerUri("http://foo/bar.js", "g(1);", "text/javascript"); |
28 | assertSubstringsInJson( |
29 | (String) requestGet("?url=http://foo/bar.js" |
30 | + "&input-mime-type=text/javascript"), |
31 | "js", |
32 | "moduleResult___=$v.cf($v.ro('g'),[1]);"); |
33 | } |
34 | |
35 | public final void testJsWithJsonpCallback() throws Exception { |
36 | registerUri("http://foo/bar.js", "g(1);", "text/javascript"); |
37 | |
38 | { |
39 | String s = (String) requestGet("?url=http://foo/bar.js" |
40 | + "&input-mime-type=text/javascript" |
41 | + "&alt=json-in-script" |
42 | + "&callback=foo"); |
43 | assertCallbackInJsonp(s, "foo"); |
44 | assertSubstringsInJsonp(s, "js", |
45 | "moduleResult___=$v.cf($v.ro('g'),[1]);"); |
46 | } |
47 | |
48 | try { |
49 | assertCallbackInJsonp( |
50 | (String) requestGet("?url=http://foo/bar.js" |
51 | + "&input-mime-type=text/javascript" |
52 | + "&alt=json-in-script" |
53 | + "&callback=foo.bar"), |
54 | "foo.bar"); |
55 | fail("Failed to reject non-identifier JSONP callback"); |
56 | } catch (AssertionFailedError e) { |
57 | // Success |
58 | } |
59 | |
60 | try { |
61 | assertCallbackInJsonp( |
62 | (String) requestGet("?url=http://foo/bar.js" |
63 | + "&input-mime-type=text/javascript" |
64 | + "&callback=foo.bar"), |
65 | "foo.bar"); |
66 | fail("Added JSONP callback when not requested"); |
67 | } catch (AssertionFailedError e) { |
68 | // Success |
69 | } |
70 | |
71 | try { |
72 | assertCallbackInJsonp( |
73 | (String) requestGet("?url=http://foo/bar.js" |
74 | + "&input-mime-type=text/javascript" |
75 | + "&alt=json" |
76 | + "&callback=foo.bar"), |
77 | "foo.bar"); |
78 | fail("Added JSONP callback when not requested"); |
79 | } catch (AssertionFailedError e) { |
80 | // Success |
81 | } |
82 | } |
83 | |
84 | public final void testCajitaJs() throws Exception { |
85 | registerUri("http://foo/bar.js", "g(1);", "text/javascript"); |
86 | assertSubstringsInJson( |
87 | (String) requestGet("?url=http://foo/bar.js" |
88 | + "&input-mime-type=text/javascript" |
89 | + "&directive=CAJITA"), |
90 | "js", |
91 | "var g=___.readImport(IMPORTS___,'g');", |
92 | "moduleResult___=g.CALL___(1);"); |
93 | } |
94 | } |