File: absolute_redirects_test.clj

package info (click to toggle)
ring-headers-clojure 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 120 kB
  • sloc: xml: 81; makefile: 27
file content (75 lines) | stat: -rw-r--r-- 3,176 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(ns ring.middleware.absolute-redirects-test
  (:use clojure.test
        ring.middleware.absolute-redirects
        [ring.mock.request :only [request]]
        [ring.util.response :only [redirect response content-type created]]))

(deftest test-wrap-absolute-redirects
  (testing "relative redirects"
    (let [handler (wrap-absolute-redirects (constantly (redirect "/foo")))
          resp    (handler (request :get "/"))]
      (is (= (:status resp) 302))
      (is (= (:headers resp) {"Location" "http://localhost/foo"}))))

  (testing "absolute redirects"
    (let [handler (wrap-absolute-redirects (constantly (redirect "http://example.com")))
          resp    (handler (request :get "/"))]
      (is (= (:status resp) 302))
      (is (= (:headers resp) {"Location" "http://example.com"}))))

  (testing "up path redirects"
    (let [handler (wrap-absolute-redirects (constantly (redirect "../foo")))
          resp    (handler (request :get "/bar/baz.html"))]
      (is (= (:status resp) 302))
      (is (= (:headers resp) {"Location" "http://localhost/foo"}))))

  (testing "no redirects"
    (let [handler (wrap-absolute-redirects (constantly (response "hello")))
          resp    (handler (request :get "/bar/baz.html"))]
      (is (= (:status resp) 200))
      (is (= (:headers resp) {}))
      (is (= (:body resp) "hello"))))

  (testing "additional headers"
    (let [handler (wrap-absolute-redirects
                   (constantly (-> (redirect "/foo")
                                   (content-type "text/plain"))))
          resp    (handler (request :get "/"))]
      (is (= (:status resp) 302))
      (is (= (:headers resp) {"Location" "http://localhost/foo"
                              "Content-Type" "text/plain"}))))
  (testing "resource creation"
    (let [handler (wrap-absolute-redirects (constantly (created "/bar/1")))
          resp    (handler (request :post "/bar"))]
      (is (= (:status resp) 201))
      (is (= (:headers resp) {"Location" "http://localhost/bar/1"})))))

(deftest test-wrap-absolute-redirects-cps
  (testing "relative redirects"
    (let [handler (wrap-absolute-redirects (fn [_ respond _] (respond (redirect "/foo"))))
          resp    (promise)
          ex      (promise)]
      (handler (request :get "/") resp ex)
      (is (not (realized? ex)))
      (is (= (:status @resp) 302))
      (is (= (:headers @resp) {"Location" "http://localhost/foo"}))))

  (testing "absolute redirects"
    (let [handler (wrap-absolute-redirects
                   (fn [_ respond _] (respond (redirect "http://example.com"))))
          resp    (promise)
          ex      (promise)]
      (handler (request :get "/") resp ex)
      (is (not (realized? ex)))
      (is (= (:status @resp) 302))
      (is (= (:headers @resp) {"Location" "http://example.com"}))))

  (testing "no redirects"
    (let [handler (wrap-absolute-redirects (fn [_ respond _] (respond (response "hello"))))
          resp    (promise)
          ex      (promise)]
      (handler (request :get "/bar/baz.html") resp ex)
      (is (not (realized? ex)))
      (is (= (:status @resp) 200))
      (is (= (:headers @resp) {}))
      (is (= (:body @resp) "hello")))))