blob: 3b7d4786007f8980de3e8eb85f348972bf41501b (
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
(ns ruuter.core-test
#?(:clj (:require [clojure.test :refer :all]
[ruuter.core :as ruuter]))
#?(:cljs (:require [cljs.test :refer-macros [deftest testing is]]
[ruuter.core :as ruuter])))
(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"))))
(testing "Multiple params, but one is optional"
(is (= {:who "world"}
(testfn "/hello/:who/:why?" "/hello/world")))
(is (= {:who "world"
:why "because"}
(testfn "/hello/:who/:why?" "/hello/world/because"))))
(testing "Multiple params, but all are optional"
(is (= {:who "world"
:why "because"}
(testfn "hello/:who?/:why?" "/hello/world/because")))
(is (= {:who "world"}
(testfn "/hello/:who?/:why?" "/hello/world"))))
(testing "Wildcard param"
(is (= {:everything "this/means/literally/everything"}
(testfn "/hello/:everything*" "/hello/this/means/literally/everything"))))
(testing "Normal params and wildcard param in the end"
(is (= {:id "123"
:path "foo.txt"}
(testfn "/user/:id/file/:path*" "/user/123/file/foo.txt")))
(is (= {:id "123"
:path "a/b/c/foo.txt"}
(testfn "/user/:id/file/:path*" "/user/123/file/a/b/c/foo.txt")))
(is (= {:id "123"
:path "a/b/c/foo.txt"
:sub-path "b/c/foo.txt"}
(testfn "/user/:id/file/:path*/:sub-path*" "/user/123/file/a/b/c/foo.txt"))))))
(deftest match-route-test
(let [testfn #'ruuter/match-route]
(testing "Find a route that exists"
(is (= {: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 "Returns a map when the response is a direct map"
(is (= {:status 200
:body "Hello."}
(testfn {:path "/hello"
:response {:status 200
:body "Hello."}}
{:uri "/hello"}))))
(testing "Returns a map via a fn when the response is a fn"
(is (= {:status 200
:body "Hello, world."}
(testfn {:path "/hello/:who"
:response (fn [req]
{:status 200
:body (str "Hello, " (:who (:params req)) ".")})}
{:uri "/hello/world"}))))
(testing "Returns an error map when route is invalid"
(is (= {:status 404
:body "Not found."}
(testfn nil {:uri "/hello"}))))
(testing "Returns a combined :params map"
(let [params (atom nil)]
(testfn {:path "/hello/:who"
:response (fn [req]
(reset! params (:params req))
{:status 200
:body ""})}
{:uri "/hello/world"
:params {:some-params :from-elsewhere}})
(is (= {:who "world"
:some-params :from-elsewhere}
@params))))
(testing "Overwrites :params if needed"
(let [params (atom nil)]
(testfn {:path "/hello/:who"
:response (fn [req]
(reset! params (:params req))
{:status 200
:body ""})}
{:uri "/hello/world"
:params {:who "overwritten"}})
(is (= {:who "overwritten"}
@params))))))
|