Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Accounted for edge conditions in query-string and request

  • Loading branch information...
commit d0d6aab2ececd73cd854c95d48cbc9ba99b7e36d 1 parent 30c1647
James Reeves authored

Showing 2 changed files with 49 additions and 33 deletions. Show diff stats Hide diff stats

  1. +34 27 src/ring/mock/request.clj
  2. +15 6 test/ring/mock/test/request.clj
61 src/ring/mock/request.clj
@@ -13,15 +13,6 @@
13 13 (str (URLEncoder/encode (name k)) "="
14 14 (URLEncoder/encode (str v))))))
15 15
16   -(defn- append-query
17   - "Create a query string from a URI and a map of parameters."
18   - [uri params]
19   - (let [query (.getRawQuery uri)]
20   - (if (or query params)
21   - (string/join "&"
22   - (remove string/blank?
23   - [query (encode-params params)])))))
24   -
25 16 (defn header
26 17 "Add a HTTP header to the request map."
27 18 [request header value]
@@ -42,10 +33,26 @@
42 33 (assoc :content-length length)
43 34 (header :content-length length)))
44 35
  36 +(defn- combined-query
  37 + "Create a query string from a URI and a map of parameters."
  38 + [request params]
  39 + (let [query (:query-string request)]
  40 + (if (or query params)
  41 + (string/join "&"
  42 + (remove string/blank?
  43 + [query (encode-params params)])))))
  44 +
  45 +(defn- merge-query
  46 + "Merge the supplied parameters into the query string of the request."
  47 + [request params]
  48 + (assoc request :query-string (combined-query request params)))
  49 +
45 50 (defn query-string
46   - "Add a map of parameters to the query string of the request."
  51 + "Set the query string of the request to a string or a map of parameters."
47 52 [request params]
48   - (assoc request :query-string (encode-params params)))
  53 + (if (map? params)
  54 + (assoc request :query-string (encode-params params))
  55 + (assoc request :query-string params)))
49 56
50 57 (defmulti body
51 58 "Set the body of the request. The supplied body value can be a string or
@@ -66,6 +73,9 @@
66 73 (content-type "application/x-www-form-urlencoded")
67 74 (body (encode-params params))))
68 75
  76 +(defmethod body nil [request params]
  77 + request)
  78 +
69 79 (defn request
70 80 "Create a minimal valid request map from a HTTP method keyword, a string
71 81 containing a URI, and an optional map of parameters that will be added to
@@ -80,19 +90,16 @@
80 90 scheme (.getScheme uri)
81 91 path (.getRawPath uri)
82 92 query (.getRawQuery uri)
83   - req {:server-port (or port 80)
84   - :server-name host
85   - :remote-addr "localhost"
86   - :uri (if (string/blank? path) "/" path)
87   - :query-string query
88   - :scheme (or (keyword scheme) :http)
89   - :request-method method
90   - :headers {"host" (if port
91   - (str host ":" port)
92   - host)}}]
93   - (if params
94   - (case method
95   - :get (assoc req :query-string (append-query uri params))
96   - :post (body req params)
97   - req)
98   - req))))
  93 + request {:server-port (or port 80)
  94 + :server-name host
  95 + :remote-addr "localhost"
  96 + :uri (if (string/blank? path) "/" path)
  97 + :query-string query
  98 + :scheme (or (keyword scheme) :http)
  99 + :request-method method
  100 + :headers {"host" (if port
  101 + (str host ":" port)
  102 + host)}}]
  103 + (if (#{:get :head} method)
  104 + (merge-query request params)
  105 + (body request params)))))
21 test/ring/mock/test/request.clj
@@ -66,7 +66,10 @@
66 66 (let [req (request :post "/?a=b")]
67 67 (is (nil? (:body req)))
68 68 (is (= (:query-string req)
69   - "a=b")))))
  69 + "a=b"))))
  70 + (testing "added params in :put"
  71 + (let [req (request :put "/" {:x "y" :z "n"})]
  72 + (is (= (slurp (:body req)) "x=y&z=n")))))
70 73
71 74 (deftest test-header
72 75 (is (= (header {} "X-Foo" "Bar")
@@ -85,11 +88,17 @@
85 88 :headers {"content-length" "10"}})))
86 89
87 90 (deftest test-query-string
88   - (is (= (-> {}
89   - (query-string {:a "b"})
90   - (query-string {:c "d"})
91   - :query-string)
92   - "c=d")))
  91 + (testing "string"
  92 + (is (= (query-string {} "a=b")
  93 + {:query-string "a=b"})))
  94 + (testing "map of params"
  95 + (is (= (query-string {} {:a "b"})
  96 + {:query-string "a=b"})))
  97 + (testing "overwriting"
  98 + (is (= (-> {}
  99 + (query-string {:a "b"})
  100 + (query-string {:c "d"}))
  101 + {:query-string "c=d"}))))
93 102
94 103 (defn- slurp* [stream]
95 104 (let [writer (StringWriter.)]

0 comments on commit d0d6aab

Please sign in to comment.
Something went wrong with that request. Please try again.