summaryrefslogtreecommitdiff
path: root/test/ruuter/core_test.clj
blob: c735f1a3584c6bf744532aa7b22843d6061e762c (plain)
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
(ns ruuter.core-test
  (:require [clojure.test :refer :all]
            [ruuter.core :as ruuter]))

(deftest path->regex-path-test
  (let [testfn #'ruuter/path->regex-path]
    (testing "Converting a path to a regex path with no params"
      (is (= "/hello/world" (testfn "/hello/world"))))
    (testing "Converting a path to a regex path with params"
      (is (= "/hello/.*" (testfn "/hello/:who")))
      (is (= "/.*/.*/.*" (testfn "/:these/:are/:params"))))))


(deftest path+uri->path-params-test
  (let [testfn #'ruuter/path+uri->path-params]
    (testing "No params returns an empty map"
      (is (= {} (testfn "/hello/world" "/hello/world"))))
    (testing "Having a param returns a map accordingly"
      (is (= {:who "world"} (testfn "/hello/:who" "/hello/world"))))
    (testing "Multiple params returns a map accordingly"
      (is (= {:who "world"
              :why "because"} (testfn "/hello/:who/:why" "/hello/world/because"))))))


(deftest match-route-test
  (let [testfn #'ruuter/match-route]
    (testing "Find a route that exists"
      (is (= {:path "/hello"
              :regex-path "/hello"
              :method :get
              :response {:status 200
                         :body "Hello."}}
             (testfn [{:path "/hello"
                       :method :get
                       :response {:status 200
                                  :body "Hello."}}] "/hello" :get))))
    (testing "No route found"
      (is (= nil
             (testfn [] "/hello" :get))))))


(deftest route+req->response-test
  (let [testfn #'ruuter/route+req->response]
    (testing "Returning a map when the response is a direct map"
      (= {:status 200
          :body "Hello."}
         (testfn {:path "/hello"
                  :response {:status 200
                             :body "Hello."}}
                 {:uri "/hello"})))
    (testing "Returning a map via a fn when the response is a fn"
      (= {:status 200
          :body "Hello, world."}
         (testfn {:path "/hello/:who"
                  :response (fn [req]
                              {:status 200
                               :body (str "Hello, " (:who (:params req)))})}
                 {:uri "/hello/world"})))
    (testing "Returning an error map when route is invalid"
      (= {:status 404
          :body "Not found."}
         (testfn nil {:uri "/hello"})))))