diff options
| author | sostler <sbostler@gmail.com> | 2010-02-10 01:08:44 -0500 |
|---|---|---|
| committer | sostler <sbostler@gmail.com> | 2010-02-10 01:08:44 -0500 |
| commit | d3fcf8d56122514c3dcbac004fcf105a4e899352 (patch) | |
| tree | 484ded214ddba3a6cc44e40d129fe1fc403e91fc /compojure-3.2/test | |
| parent | 079e2e9c1d3d5fb0f19515bfb566864565c43213 (diff) | |
Added custom compojure tree
Diffstat (limited to 'compojure-3.2/test')
| -rwxr-xr-x | compojure-3.2/test/compojure/crypto_test.clj | 35 | ||||
| -rwxr-xr-x | compojure-3.2/test/compojure/html/form_helpers_test.clj | 118 | ||||
| -rwxr-xr-x | compojure-3.2/test/compojure/html/gen_test.clj | 93 | ||||
| -rwxr-xr-x | compojure-3.2/test/compojure/html/page_helpers_test.clj | 71 | ||||
| -rwxr-xr-x | compojure-3.2/test/compojure/http/helpers_test.clj | 21 | ||||
| -rwxr-xr-x | compojure-3.2/test/compojure/http/middleware_test.clj | 114 | ||||
| -rwxr-xr-x | compojure-3.2/test/compojure/http/request_test.clj | 51 | ||||
| -rwxr-xr-x | compojure-3.2/test/compojure/http/response_test.clj | 46 | ||||
| -rwxr-xr-x | compojure-3.2/test/compojure/http/routes_test.clj | 168 | ||||
| -rwxr-xr-x | compojure-3.2/test/compojure/http/session_test.clj | 73 | ||||
| -rwxr-xr-x | compojure-3.2/test/compojure/str_utils_test.clj | 7 | ||||
| -rwxr-xr-x | compojure-3.2/test/compojure/validation_test.clj | 37 | ||||
| -rwxr-xr-x | compojure-3.2/test/helpers.clj | 14 | ||||
| -rwxr-xr-x | compojure-3.2/test/run.clj | 17 |
14 files changed, 865 insertions, 0 deletions
diff --git a/compojure-3.2/test/compojure/crypto_test.clj b/compojure-3.2/test/compojure/crypto_test.clj new file mode 100755 index 0000000..ee309e6 --- /dev/null +++ b/compojure-3.2/test/compojure/crypto_test.clj @@ -0,0 +1,35 @@ +(ns compojure.crypto-test + (:use compojure.crypto + clojure.contrib.test-is)) + +(deftest secret-key-length + (are (= (count (gen-secret-key {:key-size _1})) _2) + 256 32 + 128 16)) + +(deftest secret-key-uniqueness + (let [a (gen-secret-key {:key-size 128}) + b (gen-secret-key {:key-size 128})] + (is (not= a b)))) + +(def secret-key + (.getBytes "0123456789ABCDEF")) + +(deftest seal-string + (is (not= (seal secret-key "Foobar") "Foobar"))) + +(deftest seal-uniqueness + (let [a (seal secret-key "Foobar") + b (seal secret-key "Foobar")] + (is (not= a b)))) + +(deftest seal-then-unseal + (are (= (unseal secret-key (seal secret-key _1)) _1) + "Foobar" + [1 2 3] + {:a 10})) + +(deftest seal-then-tamper + (let [data (seal secret-key "Foobar") + data (apply str "A" (rest data))] + (is (nil? (unseal secret-key "Foobar"))))) diff --git a/compojure-3.2/test/compojure/html/form_helpers_test.clj b/compojure-3.2/test/compojure/html/form_helpers_test.clj new file mode 100755 index 0000000..b01f3ed --- /dev/null +++ b/compojure-3.2/test/compojure/html/form_helpers_test.clj @@ -0,0 +1,118 @@ +(ns compojure.html.form-helpers-test + (:use compojure.html.form-helpers + clojure.contrib.test-is)) + +(defn attribute + "Get an attribute from a tag vector." + [tag key] + ((second tag) key)) + +(deftest test-hidden-field + (testing "passing in only name" + (is (= [:input {:type "hidden", :name "foo", :id "foo"}] + (hidden-field "foo")))) + (testing "passing in name and value" + (is (= [:input {:value "hidden", :type "hidden", :name "foo", :id "foo"}] + (hidden-field "foo" "hidden"))))) + +(deftest test-text-field + (testing "passing in only name" + (is (= [:input {:type "text", :id "foo", :name "foo"}] + (text-field :foo)))) + (testing "passing in name and value" + (is (= [:input {:value :text-field, :type "text", :name "foo", :id "foo"}] + (text-field :foo :text-field))))) + +(deftest test-password-field + (is (= [:input {:type "password", :id "foo", :name "foo" :value ""}] + (password-field "foo")))) + +(deftest test-check-box + (testing "passing in only name" + (is (= [:input {:type "checkbox" :id "foo" :name "foo" :value "true" :checked nil}] + (check-box :foo)))) + (testing "passing in name and checked" + (is (= [:input {:type "checkbox", :name "foo", :id "foo", :value "true", :checked true}] + (check-box :foo true)))) + (testing "passing in name, checked, and value" + (is (= [:input {:type "checkbox", :name "foo", :id "foo", :value "checkbox", :checked false}] + (check-box :foo false "checkbox"))))) + +(deftest test-radio-button + (testing "passing in only name" + (is (= [:input {:type "radio" :id "foo_true" :name "foo" :value "true" :checked nil}] + (radio-button :foo)))) + (testing "passing in name and checked" + (is (= [:input {:type "radio", :name "foo", :id "foo_true", :value "true", :checked true}] + (radio-button :foo true)))) + (testing "passing in name, checked, and value" + (is (= [:input {:type "radio", :name "foo", :id "foo_radio", :value "radio", :checked false}] + (radio-button :foo false "radio"))))) + +(deftest test-select-options + (testing "passing in only options" + (is (= '([:option {:value "1"} "a"] + [:option {:value "2"} "b"] + [:option {:value "3"} "c"]) + (select-options [["a" "1"] ["b" "2"] ["c" "3"]])))) + (testing "passing in options and selected" + (is (= '([:option {:selected "selected" :value "1"} "a"] + [:option {:value "2"} "b"]) + (select-options [["a" "1"] ["b" "2"]] "1"))))) + +(deftest test-drop-down + (testing "passing in name and options" + (is (= [:select {:name "foo", :id "foo"} + '([:option {:value "1"} "a"] + [:option {:value "2"} "b"])] + (drop-down :foo [["a" "1"] ["b" "2"]])))) + (testing "passing in name, options, and selected" + (is (= [:select {:id "foo" :name "foo"} + '([:option {:value "1"} "a"] + [:option {:value "2" :selected "selected"} "b"] + [:option {:value "3"} "c"])] + (drop-down :foo [["a" "1"] ["b" "2"] ["c" "3"]] "2"))))) + +(deftest test-text-area + (testing "passing in only name" + (is (= [:textarea {:name "text", :id "text"} nil] + (text-area "text")))) + (testing "passing in name and value" + (is (= [:textarea {:name "text", :id "text"} "textarea"] + (text-area "text" "textarea"))))) + +(deftest test-label + (is (= [:label {:for "label"} "labeltext"] + (label "label" "labeltext")))) + +(deftest test-submit-button + (is (= [:input {:type "submit", :value "submit"}] + (submit-button "submit")))) + +(deftest test-reset-button + (is (= [:input {:type "reset", :value "reset"}] + (reset-button "reset")))) + +(deftest test-form-to + (let [form (form-to [:post "action"] [])] + (is (= (attribute form :method) "POST")))) + +(deftest test-form-to-update + (let [form (form-to [:update "action"] [])] + (is (= (attribute form :method) "POST")) + (let [hidden (nth form 2)] + (is (= (attribute hidden :value) "UPDATE")) + (is (= (attribute hidden :name) "_method")) + (is (= (attribute hidden :type) "hidden"))))) + +(deftest test-form-to-attrs + (let [form (form-to {:class "class" } [:post "action"] [])] + (is (= (attribute form :class) "class")))) + +(deftest form-input-attrs + (let [field (text-field {:style "color: red;"} :foo)] + (is (= (attribute field :style) "color: red;")))) + +(deftest test-with-params + (is (= (with-params {:foo "bar"} (text-field :foo)) + [:input {:type "text", :id "foo", :name "foo", :value "bar"}])))
\ No newline at end of file diff --git a/compojure-3.2/test/compojure/html/gen_test.clj b/compojure-3.2/test/compojure/html/gen_test.clj new file mode 100755 index 0000000..5d64b7c --- /dev/null +++ b/compojure-3.2/test/compojure/html/gen_test.clj @@ -0,0 +1,93 @@ +(ns compojure.html.gen-test + (:use compojure.html.gen + clojure.contrib.test-is)) + +(deftest tag-text + (is (= (html [:text "Lorem Ipsum"]) "<text>Lorem Ipsum</text>"))) + +(deftest empty-tags + (is (= (html [:text]) "<text />"))) + +(deftest empty-block-tags + (is (= (html [:div]) "<div></div>")) + (is (= (html [:h1]) "<h1></h1>")) + (is (= (html [:script]) "<script></script>"))) + +(deftest empty-links-tag + (is (= (html [:a]) "<a></a>"))) + +(deftest tags-can-be-strs + (is (= (html ["div"] "<div></div>")))) + +(deftest tags-can-be-symbols + (is (= (html ['div] "<div></div>")))) + +(deftest tag-concatenation + (is (= (html [:body "foo" "bar"]) "<body>foobar</body>")) + (is (= (html [:body [:p] [:br]])) "<body><p /><br /></body>")) + +(deftest tag-seq-expand + (is (= (html [:body (list "foo" "bar")]) + "<body>foobar</body>"))) + +(deftest html-seq-expand + (is (= (html (list [:p "a"] [:p "b"])) + "<p>a</p><p>b</p>"))) + +(deftest nested-tags + (is (= (html [:div [:p]] "<div><p /></div>"))) + (is (= (html [:div [:b]] "<div><b></b></div>"))) + (is (= (html [:p [:span [:a "foo"]]]) + "<p><span><a>foo</a></span></p>"))) + +(deftest attribute-maps + (is (= (html [:xml {:a "1", :b "2"}]) + "<xml a=\"1\" b=\"2\" />"))) + +(deftest blank-attribute-map + (is (= (html [:xml {}]) "<xml />"))) + +(deftest escaped-chars + (is (= (escape-html "\"") """)) + (is (= (escape-html "<") "<")) + (is (= (escape-html ">") ">")) + (is (= (escape-html "&") "&"))) + +(deftest escaped-attrs + (is (= (html [:div {:id "\""}]) + "<div id=\""\"></div>"))) + +(deftest attrs-can-be-strs + (is (= (html [:img {"id" "foo"}]) "<img id=\"foo\" />"))) + +(deftest attrs-can-be-symbols + (is (= (html [:img {'id "foo"}]) "<img id=\"foo\" />"))) + +(deftest attr-keys-different-types + (is (= (html [:xml {:a "1", 'b "2", "c" "3"}]) + "<xml a=\"1\" b=\"2\" c=\"3\" />"))) + +(deftest tag-class-sugar + (is (= (html [:div.foo]) "<div class=\"foo\"></div>"))) + +(deftest tag-many-class-sugar + (is (= (html [:div.a.b]) "<div class=\"a b\"></div>")) + (is (= (html [:div.a.b.c]) "<div class=\"a b c\"></div>"))) + +(deftest tag-id-sugar + (is (= (html [:div#foo]) "<div id=\"foo\"></div>"))) + +(deftest tag-id-and-classes + (is (= (html [:div#foo.bar.baz]) + "<div class=\"bar baz\" id=\"foo\"></div>"))) + +(deftest html-not-indented + (is (= (html [:p "Lorem\nIpsum"]) "<p>Lorem\nIpsum</p>"))) + +(deftest attrs-bool-true + (is (= (html [:input {:type "checkbox" :checked true}]) + "<input checked=\"checked\" type=\"checkbox\" />"))) + +(deftest attrs-bool-false + (is (= (html [:input {:type "checkbox" :checked false}]) + "<input type=\"checkbox\" />"))) diff --git a/compojure-3.2/test/compojure/html/page_helpers_test.clj b/compojure-3.2/test/compojure/html/page_helpers_test.clj new file mode 100755 index 0000000..fe29481 --- /dev/null +++ b/compojure-3.2/test/compojure/html/page_helpers_test.clj @@ -0,0 +1,71 @@ +(ns compojure.html.page-helpers-test + (:use compojure.html.page-helpers + clojure.contrib.test-is)) + +(deftest test-doctype + (testing "html4" + (is (= (doctype :html4) + (str "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" " + "\"http://www.w3.org/TR/html4/strict.dtd\">\n")))) + (testing "xhtml-strict" + (is (= (doctype :xhtml-strict) + (str "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n")))) + (testing "html5" + (is (= (doctype :html5) + (str "<!DOCTYPE html>")))) + (testing "xhtml-transitional" + (is (= (doctype :xhtml-transitional) + (str "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" " + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"))))) + +(deftest test-xhtml-tag + (is (= (xhtml-tag "test") + [:html {:xmlns "http://www.w3.org/1999/xhtml", "xml:lang" "test", :lang "test"} nil]))) + +(deftest test-include-js + (testing "one" + (is (= (include-js "foo.js") + '([:script {:type "text/javascript", :src "foo.js"}])))) + (testing "many" + (is (= (include-js "foo.js" "bar.js" "baz.js") + '([:script {:type "text/javascript", :src "foo.js"}] + [:script {:type "text/javascript", :src "bar.js"}] + [:script {:type "text/javascript", :src "baz.js"}]))))) + +(deftest test-include-css + (testing "one" + (is (= (include-css "foo.css") + '([:link {:type "text/css" :href "foo.css" :rel "stylesheet"}])))) + (testing "many" + (is (= (include-css "foo.css" "bar.css" "baz.css") + '([:link {:type "text/css", :href "foo.css", :rel "stylesheet"}] + [:link {:type "text/css", :href "bar.css", :rel "stylesheet"}] + [:link {:type "text/css", :href "baz.css", :rel "stylesheet"}]))))) + +(deftest test-javascript-tag + (is (= (javascript-tag "alert('hi');") + [:script {:type "text/javascript"} + (str "//<![CDATA[\n" "alert('hi');" "\n//]]>")]))) + +(deftest test-link-to + (is (= (link-to "http://compojure.org") + [:a {:href "http://compojure.org"} nil]))) + +(deftest test-url-encode + (is (= (url-encode "foo&bar*/baz.net") + (str "foo%26bar*%2Fbaz.net")))) + +(deftest test-url-params + (is (= (url-params "http://example.com" {:lang "en", :offset 10}) + "http://example.com?lang=en&offset=10"))) + +(deftest test-unordered-list + (is (= (unordered-list ["a" "b"]) + [:ul {} + '([:li "a"] [:li "b"])]))) + +(deftest test-ordered-list + (is (= (ordered-list ["b" "a"]) + [:ol {} + '([:li "b"] [:li "a"])])))
\ No newline at end of file diff --git a/compojure-3.2/test/compojure/http/helpers_test.clj b/compojure-3.2/test/compojure/http/helpers_test.clj new file mode 100755 index 0000000..fdd9a68 --- /dev/null +++ b/compojure-3.2/test/compojure/http/helpers_test.clj @@ -0,0 +1,21 @@ +(ns compojure.http.helpers-test + (:use compojure.http.helpers + compojure.http.routes + compojure.control + clojure.contrib.test-is)) + +(deftest test-set-cookie + (is (= (set-cookie :foo "bar") + {:headers {"Set-Cookie" "foo=bar"}}))) + +(deftest test-set-cookie-path + (is (= (set-cookie :a "b", :path "/") + {:headers {"Set-Cookie" "a=b; path=/"}}))) + +(deftest test-content-type + (is (= (content-type "text/html") + {:headers {"Content-Type" "text/html"}}))) + +(deftest test-safe-path + (is (not (safe-path? "/basedir/compojure" "../private/secret.txt"))) + (is (safe-path? "/basedir/compojure" "public/index.html"))) diff --git a/compojure-3.2/test/compojure/http/middleware_test.clj b/compojure-3.2/test/compojure/http/middleware_test.clj new file mode 100755 index 0000000..a3ddc8a --- /dev/null +++ b/compojure-3.2/test/compojure/http/middleware_test.clj @@ -0,0 +1,114 @@ +(ns compojure.http.middleware-test + (:use compojure.http.middleware + compojure.http.routes + clojure.contrib.test-is)) + +(deftest test-header-option + (is (= (header-option [:name "value"]) + "name=value"))) + +(deftest test-header-option-true + (is (= (header-option [:name true]) + "name"))) + +(deftest test-header-option-false + (is (nil? (header-option [:name false])))) + +(deftest test-header-options-multi + (let [m {:name "value", + :false false, + :true true}] + (is (= (header-options m ", ") + "name=value, true")))) + +(deftest test-header-options-single + (let [m {:name "value"}] + (is (= (header-options m ", ") + "name=value")))) + +(defn mock-middleware-response [f & args] + (let [routes (routes (GET "/foo" [{:headers {"k1" "v1" "k2" "v2"}} "body"])) + request {:request-method :get, + :uri "/foo"}] + ((apply f (conj args routes)) request))) + +(deftest test-with-headers + (let [headers {"name1" "value1", "name2" "value2"} + response (mock-middleware-response with-headers headers)] + (is (= "value1" (get (:headers response) "name1"))) + (is (= "value2" (get (:headers response) "name2"))) + (is (= "v1" (get (:headers response) "k1"))))) + +(deftest test-with-headers-overwrites + (let [headers {"k1" "vnew"} + response (mock-middleware-response with-headers headers)] + (is (= "vnew" (get (:headers response) "k1"))) + (is (= "v2" (get (:headers response) "k2"))))) + +(deftest test-with-cache-control + (let [m {:max-age 3600 :public false :must-revalidate true}] + (let [response (mock-middleware-response with-cache-control m)] + (is (= "max-age=3600, must-revalidate" + (get (:headers response) "Cache-Control")))))) + +(defn run-ignore-trailing-slash-paths + [route-path uri] + (let [routes (routes (GET route-path "foo")) + request {:request-method :get + :uri uri} + response ((ignore-trailing-slash routes) request)] + (= (:body response) "foo"))) + +(deftest test-ignore-trailing-slash-paths + (are (run-ignore-trailing-slash-paths _1 _2) + "/" "/" + "/foo" "/foo" + "/foo" "/foo/" + "/foo/bar" "/foo/bar/")) + +(defn run-with-context + [route-path uri context] + (let [routes (routes (GET route-path "foo")) + request {:request-method :get + :uri uri} + response ((with-context routes context) request)] + (= (:body response) "foo"))) + +(deftest test-with-context + (are (run-with-context _1 _2 "/context") + "/" "/context" + "/home" "/context/home" + "/asset/1" "/context/asset/1")) + +(deftest test-without-context + (are (not (run-with-context _1 _2 "/context")) + "/" "/" + "/home" "/home" + "/asset/1" "/asset/1")) + +(defn run-mimetypes + [uri type options] + (let [routes (routes (GET uri "foo")) + request {:request-method :get + :uri uri} + response ((with-mimetypes routes options) request) + result (get (:headers response) "Content-Type")] + (= type result))) + +(deftest test-with-default-mimetypes + (are (run-mimetypes _1 _2 {}) + "/" "text/html" + "/foobar" "text/html" + "/file.pdf" "application/pdf" + "/files/bar.css" "text/css")) + +(deftest test-with-custom-mimetypes + (let [options {:mimetypes {"foo" "test/foo" + "bar" "test/bar"} + :default "test/default"}] + (are (run-mimetypes _1 _2 options) + "/" "test/default" + "/foobar" "test/default" + "/file.pdf" "test/default" + "/file.foo" "test/foo" + "/files/file.bar" "test/bar")))
\ No newline at end of file diff --git a/compojure-3.2/test/compojure/http/request_test.clj b/compojure-3.2/test/compojure/http/request_test.clj new file mode 100755 index 0000000..5b19dc3 --- /dev/null +++ b/compojure-3.2/test/compojure/http/request_test.clj @@ -0,0 +1,51 @@ +(ns compojure.http.request-test + (:use compojure.http.request + clojure.contrib.test-is + test.helpers)) + +(deftest query-params + (are (= (parse-query-params {:query-string _1}) _2) + "a=1" {:a "1"} + "a=1&b=2" {:a "1", :b "2"})) + +(deftest query-params-plus + (is (= (parse-query-params {:query-string "a=1+2"}) + {:a "1 2"}))) + +(deftest query-params-space + (is (= (parse-query-params {:query-string "a=1%202"}) + {:a "1 2"}))) + +(deftest query-params-invalid + (are (= (parse-query-params {:query-string _1}) _2) + "" {} + "=" {} + "=1" {} + "a=1&=" {:a "1"})) + +(deftest urlencoded-charset + (is (urlencoded-form? + {:content-type "application/x-www-form-urlencoded; charset=UTF8"}))) + +(deftest form-params + (are (= (parse-form-params (form-request _1)) _2) + "a=1" {:a "1"} + "a=1&b=2" {:a "1", :b "2"})) + +(deftest assoc-params-empty + (is (= (assoc-params {}) + {:form-params {}, :query-params {}, :params {}}))) + +(deftest assoc-params-merge + (let [request {:form-params {:a "1"}, :query-params {:b "2"}}] + (is (= (assoc-params request) + (assoc request :params {:a "1", :b "2"}))))) + +(deftest assoc-params-twice + (let [request (form-request "a=1")] + (is (= (:form-params (-> request assoc-params assoc-params)) + {:a "1"})))) + +(deftest request-cookies + (is (= (parse-cookies {:headers {"cookie" "a=1;b=2"}}) + {:a "1", :b "2"}))) diff --git a/compojure-3.2/test/compojure/http/response_test.clj b/compojure-3.2/test/compojure/http/response_test.clj new file mode 100755 index 0000000..45a0d01 --- /dev/null +++ b/compojure-3.2/test/compojure/http/response_test.clj @@ -0,0 +1,46 @@ +(ns compojure.http.response-test + (:use compojure.http.response + clojure.contrib.test-is)) + +(deftest nil-response + (is (= (create-response {} nil) + {:status 200, :headers {}}))) + +(deftest int-response + (is (= (:status (create-response {} 404)) + 404))) + +(deftest next-response + (is (nil? (create-response {} :next)))) + +(deftest string-response + (is (= (:body (create-response {} "Lorem Ipsum")) + "Lorem Ipsum"))) + +(deftest seq-response + (is (= (:body (create-response {} (list "a" "b" "c"))) + (list "a" "b" "c")))) + +(deftest map-response + (let [response {:status 200 + :headers {"Content-Type" "text/plain"} + :body "Lorem Ipsum"}] + (is (= (create-response {} response) response)))) + +(deftest vector-string-response + (is (= (:body (create-response {} ["Foo" "Bar" "Baz"])) + "FooBarBaz"))) + +(deftest vector-int-response + (is (= (:status (create-response {} [200 500 403])) + 403))) + +(deftest default-content-type + (let [response {:headers {"Foo" "Bar"}}] + (is (= (:headers (update-response {} response "Foo")) + {"Foo" "Bar" "Content-Type" "text/html"})))) + +(deftest supplied-content-type + (let [response {:headers {"Content-Type" "text/plain" "Foo" "Bar"}}] + (is (= (:headers (update-response {} response "Foo")) + {"Content-Type" "text/plain" "Foo" "Bar"})))) diff --git a/compojure-3.2/test/compojure/http/routes_test.clj b/compojure-3.2/test/compojure/http/routes_test.clj new file mode 100755 index 0000000..7d9b936 --- /dev/null +++ b/compojure-3.2/test/compojure/http/routes_test.clj @@ -0,0 +1,168 @@ +(ns compojure.http.routes-test + (:use compojure.http.routes + clojure.contrib.test-is + test.helpers)) + +(deftest fixed-path + (are (match-uri (compile-uri-matcher _1) _1) + "/" + "/foo" + "/foo/bar" + "/foo/bar.html")) + +(deftest nil-paths + (is (match-uri (compile-uri-matcher "/") nil))) + +(deftest keyword-paths + (are (= (match-uri (compile-uri-matcher _1) _2) _3) + "/:x" "/foo" {:x "foo"} + "/foo/:x" "/foo/bar" {:x "bar"} + "/a/b/:c" "/a/b/c" {:c "c"} + "/:a/b/:c" "/a/b/c" {:a "a", :c "c"})) + +(deftest keywords-match-extensions + (are (= (match-uri (compile-uri-matcher _1) _2) _3) + "/foo.:ext" "/foo.txt" {:ext "txt"} + "/:x.:y" "/foo.txt" {:x "foo", :y "txt"})) + +(deftest hyphen-keywords + (are (= (match-uri (compile-uri-matcher _1) _2) _3) + "/:foo-bar" "/baz" {:foo-bar "baz"} + "/:foo-" "/baz" {:foo- "baz"})) + +(deftest urlencoded-keywords + (are (= (match-uri (compile-uri-matcher _1) _2) _3) + "/:x" "/foo%20bar" {:x "foo bar"} + "/:x" "/foo+bar" {:x "foo bar"})) + +(deftest same-keyword-many-times + (are (= (match-uri (compile-uri-matcher _1) _2) _3) + "/:x/:x/:x" "/a/b/c" {:x ["a" "b" "c"]} + "/:x/b/:x" "/a/b/c" {:x ["a" "c"]})) + +(deftest wildcard-paths + (are (= (match-uri (compile-uri-matcher _1) _2) _3) + "/*" "/foo" {:* "foo"} + "/*" "/foo.txt" {:* "foo.txt"} + "/*" "/foo/bar" {:* "foo/bar"} + "/foo/*" "/foo/bar/baz" {:* "bar/baz"} + "/a/*/d" "/a/b/c/d" {:* "b/c"})) + +(deftest url-paths + (is (match-uri (compile-uri-matcher "http://localhost") + "http://localhost"))) + +(deftest url-port-paths + (let [matcher (compile-uri-matcher "localhost:8080")] + (is (match-uri matcher "localhost:8080")) + (is (not (match-uri matcher "localhost:7070"))))) + +(deftest unmatched-paths + (is (nil? (match-uri (compile-uri-matcher "/foo") "/bar")))) + +(deftest regex-paths + (is (match-uri #"/[A-Z][a-z]" "/Ab")) + (is (not (match-uri #"/[A-Z][a-z]" "/ab")))) + +(deftest regex-path-params + (are (= (match-uri _1 _2) _3) + #"/foo/(\w+)" "/foo/bar" ["bar"] + #"/(\w+)/(\d+)" "/foo/10" ["foo" "10"])) + +(deftest assoc-route-map + (is (= (assoc-route-params {:params {}} {"foo" "bar"}) + {:route-params {"foo" "bar"}, :params {"foo" "bar"}}))) + +(deftest assoc-route-vector + (is (= (assoc-route-params {:params {}} ["foo" "bar"]) + {:route-params ["foo" "bar"], :params {}}))) + +(deftest route-response + (let [route (GET "/" "Lorem Ipsum") + request {:request-method :get, :uri "/"} + response (route request)] + (is (= response {:status 200, + :headers {"Content-Type" "text/html"}, + :body "Lorem Ipsum"})))) + +(defn- route-body + [route method uri] + (:body (route {:request-method method, :uri uri}))) + +(deftest route-methods + (are (= (route-body _1 _2 "/") _3) + (GET "/" "a") :get "a" + (POST "/" "b") :post "b" + (PUT "/" "c") :put "c" + (HEAD "/" "d") :head "d" + (DELETE "/" "e") :delete "e")) + +(deftest route-any + (are (= (route-body (ANY "/" _2) _1 "/") _2) + :get "a" + :post "b" + :put "c" + :delete "d")) + +(deftest route-var-paths + (let [path "/foo/bar"] + (is (= (route-body (GET path "pass") :get path) + "pass")))) + +(deftest route-not-match + (let [route (GET "/" "Lorem Ipsum") + request {:request-method :get, :uri "/foo"}] + (is (nil? (route request))))) + +(deftest route-match-form-method + (let [routes (routes (DELETE "/foo" "body")) + request {:request-method :post + :uri "/foo" + :content-type "application/x-www-form-urlencoded" + :body (input-stream "_method=DELETE&a=1")}] + (is (= (:status (routes request)) + 200)))) + +(deftest route-not-match-form-method + (let [routes (routes (DELETE "/foo" "body")) + request {:request-method :post + :uri "/foo" + :content-type "application/x-www-form-urlencoded" + :body (input-stream "a=1")}] + (is (nil? (routes request))))) + +(deftest route-match-form-method-not-post + (let [routes (routes (POST "/foo" "post") (DELETE "/foo" "delete")) + request {:request-method :post + :uri "/foo" + :content-type "application/x-www-form-urlencoded", + :body (input-stream "_method=DELETE&a=1")}] + (is (= (:body (routes request)) + "delete")))) + +(deftest route-keywords + (let [route (GET "/:foo" + (is (= (:route-params request) {:foo "bar"})) + "")] + (route {:request-method :get, :uri "/bar"}))) + +(deftest combine-routes + (let [r1 (fn [req] (if (= (:uri req) "/") {:body "x"})) + r2 (fn [req] {:body "y"}) + rs (routes r1 r2)] + (is (rs {:uri "/"}) "x") + (is (rs {:uri "/foo"}) "y"))) + +(deftest route-params + (let [site (routes + (GET "/:route" + (is (= (params :route) "yes")) + (is (= (params :query) "yes")) + (is (= (params :form) "yes")) + (is (request :params) params) + :next))] + (site (merge + {:request-method :get + :uri "/yes" + :query-string "query=yes"} + (form-request "form=yes"))))) diff --git a/compojure-3.2/test/compojure/http/session_test.clj b/compojure-3.2/test/compojure/http/session_test.clj new file mode 100755 index 0000000..0e30793 --- /dev/null +++ b/compojure-3.2/test/compojure/http/session_test.clj @@ -0,0 +1,73 @@ +(ns compojure.http.session-test + (:use compojure.crypto + compojure.encodings + compojure.http.session + clojure.contrib.test-is) + (:import javax.crypto.spec.IvParameterSpec + javax.crypto.spec.SecretKeySpec)) + +;; Memory sessions + +(deftest create-memory-session + (is (= (keys (create-session {:type :memory})) + [:id]))) + +(deftest memory-session-cookie + (let [repo {:type :memory} + session (create-session repo)] + (is (= (session-cookie repo true session) (session :id))) + (is (nil? (session-cookie repo false session))))) + +(deftest read-memory-session + (binding [memory-sessions (ref {::mock-id ::mock-session})] + (is (= (read-session {:type :memory} ::mock-id) + ::mock-session)))) + +(deftest write-memory-session + (binding [memory-sessions (ref {})] + (let [session (create-session {:type :memory})] + (write-session {:type :memory} session) + (is (= (memory-sessions (session :id)) + session))))) + +(deftest destroy-memory-sessions + (let [mock-session {:id ::mock-id}] + (binding [memory-sessions (ref {::mock-id mock-session})] + (destroy-session {:type :memory} mock-session) + (is (not (contains? @memory-sessions ::mock-id)))))) + +;; Cookie sessions + +(deftest create-cookie-session + (is (= (create-session {:type :cookie}) {}))) + +;; Associating session with request + +(defmethod create-session ::mock [repository] + {:id :new}) + +(defmethod read-session ::mock [repository id] + {:id :current}) + +(deftest assoc-nil-session + (let [request (assoc-session {} {:type ::mock})] + (is (:new-session? request)) + (is (= (:session request) {:id :new})))) + +(deftest assoc-expired-session + (let [session {:expires-at (timestamp-after 0)} + request (assoc-session {:session session} {:type ::mock})] + (is (:new-session? request)) + (is (= (:session request) {:id :new})))) + +(deftest assoc-existing-session + (let [cookies {:compojure-session "current"} + request (assoc-session {:cookies cookies} {:type ::mock})] + (is (not (:new-session? request))) + (is (= (:session request) {:id :current})))) + +(deftest assoc-flash-data + (let [session {:flash {:message "test"}} + request (assoc-flash {:session session})] + (is (not (contains? (request :session) :flash))) + (is (= (request :flash) {:message "test"})))) diff --git a/compojure-3.2/test/compojure/str_utils_test.clj b/compojure-3.2/test/compojure/str_utils_test.clj new file mode 100755 index 0000000..6b01bfb --- /dev/null +++ b/compojure-3.2/test/compojure/str_utils_test.clj @@ -0,0 +1,7 @@ +(ns compojure.str-utils-test + (:use compojure.str-utils + clojure.contrib.test-is)) + +(deftest test-escape + (is (= (escape "aeiou" "hello world") + "h\\ell\\o w\\orld"))) diff --git a/compojure-3.2/test/compojure/validation_test.clj b/compojure-3.2/test/compojure/validation_test.clj new file mode 100755 index 0000000..c203a81 --- /dev/null +++ b/compojure-3.2/test/compojure/validation_test.clj @@ -0,0 +1,37 @@ +(ns compojure.validation-test + (:use compojure.html.form-helpers + compojure.validation + clojure.contrib.test-is)) + +(deftest passes-validate + (is (= (validate {:a 1} :a (constantly true) "fail") + {}))) + +(deftest fails-validate + (is (= (validate {:a 1} :a (constantly false) "fail") + {:a ["fail"]}))) + +(deftest error-class-errors + (binding [*errors* {:foo "bar"}] + (is (= ((error-class text-field) :foo) + [:div.error (text-field :foo)])))) + +(deftest error-class-no-errors + (binding [*errors* {}] + (is (= ((error-class text-field) :foo) + (text-field :foo))))) + +(deftest merge-errors-test + (are (= (apply merge-errors _1) _2) + [{}] {} + [{} {} {}] {} + [{:a ["f"]}] {:a ["f"]} + [{:a ["f"]} {:b ["g"]}] {:a ["f"], :b ["g"]} + [{:a ["f"]} {:a ["g"]}] {:a ["f" "g"]})) + +(deftest validation-test + (let [params {:a 1, :b 2} + pred #(= % 2) + mesg "isn't 2"] + (is (= (validation params [:a pred mesg] [:b pred mesg]) + {:a ["isn't 2"]})))) diff --git a/compojure-3.2/test/helpers.clj b/compojure-3.2/test/helpers.clj new file mode 100755 index 0000000..9f2704b --- /dev/null +++ b/compojure-3.2/test/helpers.clj @@ -0,0 +1,14 @@ +(ns test.helpers + (:import java.io.ByteArrayInputStream) + (:import java.io.File)) + +(defn input-stream [s] + (ByteArrayInputStream. (.getBytes s))) + +(defn form-request [body] + {:content-type "application/x-www-form-urlencoded" + :body (input-stream body)}) + +(defn temp-file [] + (doto (File/createTempFile "compojure" "test") + (.deleteOnExit))) diff --git a/compojure-3.2/test/run.clj b/compojure-3.2/test/run.clj new file mode 100755 index 0000000..a651691 --- /dev/null +++ b/compojure-3.2/test/run.clj @@ -0,0 +1,17 @@ +(use 'clojure.contrib.find-namespaces + 'clojure.test) + +(defn find-tests [] + (filter + #(re-find #"-test" (str %)) (find-namespaces-in-dir (java.io.File. "test")))) + +(defn require-tests [] + (doseq [test (find-tests)] + (require test))) + +(require-tests) +(let [results (apply merge-with + (map test-ns (find-tests)))] + (if (or (> (results :fail) 0) + (> (results :error) 0)) + (System/exit -1) + (System/exit 0))) |
