')"
17 | (.getText ^JsExpr (ordain-as-safe jsexpr :html))))))
18 |
19 | (testing "marks html safe"
20 | (is (instance? SanitizedContent (ordain-as-safe "" :html))))
21 |
22 | (testing "renders safe html properly"
23 | (is (= "" (.getContent ^SanitizedContent$ConstantContent
24 | (ordain-as-safe "" :html))))))
25 |
26 | (deftest content-type-test
27 | (is (= "text/css; charset=utf-8" (content-type :css)))
28 | (is (= "text/html; charset=utf-8" (content-type :html)))
29 | (is (= "text/javascript; charset=utf-8" (content-type :js)))
30 | (is (= "text/plain; charset=utf-8" (content-type :some-other-kind))))
31 |
32 | (deftest clean-html-test
33 | (testing "Cleaning an HTML string"
34 | (is (= (ordain-as-safe "woo woo();" :html)
35 | (clean-html "woo woo();"))))
36 | (testing "Cleaning an HTML string with optional safe tags"
37 | (is (= (ordain-as-safe "woo woo();" :html)
38 | (clean-html "woo woo();" :span)))))
39 |
40 | (deftest compile-to-js-test
41 | (testing "Compiling a missing template to Javascript"
42 | (is (thrown? IllegalArgumentException
43 | (compile-to-js "bad.soy"))))
44 |
45 | (testing "Compiling templates to Javascript"
46 | (set-cache-options! {})
47 | (let [js (.. (ScriptEngineManager.)
48 | (getEngineByName "nashorn"))]
49 | (.eval js ^String (slurp "resources/soy-clj/soyutils.js"))
50 | (.eval js ^String (compile-to-js "example.soy"))
51 | (is (= "Hello world!"
52 | (.eval js "examples.simple.helloWorld().content"))))
53 | (is (= [[:js ["example.soy"]]]
54 | (keys (guava-cache/cache->map @@#'soy-clj/cache)))))
55 |
56 | (testing "Compiling cached templates to Javascript"
57 | (set-cache-options! {})
58 | (compile-to-js "example.soy")
59 | (let [js (.. (ScriptEngineManager.)
60 | (getEngineByName "nashorn"))]
61 | (.eval js ^String (slurp "resources/soy-clj/soyutils.js"))
62 | (.eval js ^String (compile-to-js "example.soy"))
63 | (is (= "Hello world!"
64 | (.eval js "examples.simple.helloWorld().content"))))
65 | (is (= [[:js ["example.soy"]]]
66 | (keys (guava-cache/cache->map @@#'soy-clj/cache))))))
67 |
68 | (deftest parse-test
69 | (testing "Parsing a template from a resource"
70 | (is (parse (io/resource "example.soy"))))
71 | (testing "Overriding the builder"
72 | (binding [*builder-fn* (fn [] (let [builder (SoyFileSet/builder)]
73 | (.add builder (io/file "test/example.soy"))
74 | builder))]
75 | (is (= ["Hello, Mr. World!" :text]
76 | (render (parse "other-example.soy") "examples.simple.exampleText"
77 | {:name "Mr. World"}
78 | :text)))))
79 | (testing "Preprocessing a template"
80 | (set-cache-options! {})
81 | (binding [*preprocessor-fn* #(str %
82 | "\n\n"
83 | (->> (slurp "test/other-example.soy")
84 | (string/split-lines)
85 | (drop 1)
86 | (string/join "\n")))]
87 | (is (= ["Yes, boss." :html]
88 | (render (parse "example.soy") "examples.simple.addition"
89 | {:name "boss"}))))))
90 |
91 | (deftest render-test
92 | (testing "Rendering a template"
93 | (is (= [(str "Welcome"
94 | "Bonjour Mr. World!
")
95 | :html]
96 | (render (parse "example.soy") "examples.simple.helloName"
97 | {:name "Mr. World"
98 | :greeting-word "Bonjour"}))))
99 | (testing "Rendering a text template"
100 | (is (= ["Hello, Mr. World!"
101 | :text]
102 | (render (parse "example.soy") "examples.simple.exampleText"
103 | {:name "Mr. World"}
104 | :text))))
105 | (testing "Contextually auto-escaping"
106 | (is (= [(str "', "
108 | "alert('XSS'), '")
109 | :html]
110 | (render (parse "example.soy") "examples.simple.example"
111 | {:name "', alert('XSS'), '"}))))
112 | (testing "Passing complex structures to nested templates"
113 | (is (= [(str "Welcome"
114 | "Hello Alice!
"
115 | "WelcomeHello Bob!
"
116 | "Welcome"
117 | "Hello Carol!
"
118 | "WelcomeHello Dave!
")
119 | :html]
120 | (render (parse "example.soy") "examples.simple.helloNames"
121 | {"name" "Alice"
122 | :additional-names ["Bob" "Carol" "Dave"]}))))
123 | (testing "Passing pre-sanitized HTML"
124 | (is (= [(str ""
125 | "WelcomeBonjour Mr. World!
")
126 | :html]
127 | (render (parse "example.soy") "examples.simple.helloName"
128 | {:name (ordain-as-safe "Mr. World" :html)
129 | :greeting-word "Bonjour"})))))
130 |
--------------------------------------------------------------------------------