 (in-ns 'io.aviso.rook-test)
 
 (deftest namespace-middleware-test
-  (let [test-mw (is (namespace-middleware (fn [request] ((:test-fun request) request)) 'io.aviso.rook-test))
-        test-mw2 (is (namespace-middleware (fn [request] ((:test-fun request) request)) 'io.aviso.rook-test2))]
-    (is (= {:arg-resolvers nil :metadata (meta #'io.aviso.rook-test/index) :function #'io.aviso.rook-test/index
-            :namespace     'io.aviso.rook-test}
-           (test-mw (assoc (mkrequest :get "/?limit=100") :test-fun :rook))))
-    (is (= {:arg-resolvers nil :metadata (meta #'io.aviso.rook-test/show) :function #'io.aviso.rook-test/show
-            :namespace     'io.aviso.rook-test}
-           (test-mw (assoc (mkrequest :get "/123") :test-fun :rook))))
-    (is (= {:arg-resolvers nil :metadata (meta #'io.aviso.rook-test/activate) :function #'io.aviso.rook-test/activate
-            :namespace     'io.aviso.rook-test}
-           (test-mw (assoc (mkrequest :post "/123/activate") :test-fun :rook))))
-    (is (nil? (test-mw (assoc (mkrequest :get "/123/activate") :test-fun :rook))))
-    (is (nil? (test-mw (assoc (mkrequest :put "/") :test-fun :rook))))
-    (is (nil? (test-mw (assoc (mkrequest :put "/123") :test-fun :rook))))
-    (is (= {:arg-resolvers nil
-            :metadata      (meta #'io.aviso.rook-test2/index)
-            :function      #'io.aviso.rook-test2/index
-            :namespace     'io.aviso.rook-test2}
-           (test-mw2 (assoc (mkrequest :get "/?offset=100") :test-fun :rook))))))
+
+  (are [method path namespace expected-function]
+    (= expected-function
+       (-> (mkrequest method path namespace)
+           (get-in [:rook :function])))
+
+    :get "/?limit=100" 'io.aviso.rook-test #'index
+
+    :get "/123" 'io.aviso.rook-test #'show
+
+    :post "/123/activate" 'io.aviso.rook-test #'activate
+
+    :get "/123/activate" 'io.aviso.rook-test nil
+
+    :put "/" 'io.aviso.rook-test nil
+
+    :put "/123" 'io.aviso.rook-test nil
+
+    :get "/?offset-100" 'io.aviso.rook-test2 #'io.aviso.rook-test2/index))
 
 (deftest namespace-handler-test
-  (when-let [test-mw (is (namespace-middleware rook-handler 'io.aviso.rook-test))]
-    (is (= {:status 200 :body "limit=100"}
-           (test-mw (mkrequest :get "/?limit=100"))))
-    (is (= {:status 200 :body "id=123"}
-           (test-mw (mkrequest :get "/123"))))
-    (is (= "test1=1test,id=123,test2=,test3=,test4=,request=13,meth="
-           (test-mw (mkrequest :post "/123/activate?test1=1test"))))
-    (is (nil? (test-mw (assoc (mkrequest :get "/123/activate") :test-fun :rook))))
-    (is (nil? (test-mw (assoc (mkrequest :put "/") :test-fun :rook))))
-    (is (nil? (test-mw (assoc (mkrequest :put "/123") :test-fun :rook))))))
+
+  (let [test-mw (-> (namespace-middleware rook-handler 'io.aviso.rook-test)
+                    param-handling)]
+
+    (are [method path expected-result]
+      (= expected-result
+         (-> (mock/request method path) test-mw))
+
+      :get "/?limit=100" {:status 200 :body "limit=100"}
+
+      :get "/123" {:status 200 :body "id=123"}
+
+      :post "/123/activate?test1=1test" "test1=1test,id=123,test2=,test3=,test4=,request=13,meth="
+
+      :get "/123/activate" nil
+
+      :put "/" nil
+
+      :put "/123" nil)))
 
 
 (deftest complete-handler-test
-  (when-let [test-mw (is (namespace-middleware
-                           (arg-resolver-middleware rook-handler
-                                                    (build-map-arg-resolver :test1 "TEST!" :test2 "TEST@" :test3 "TEST#" :request-method :1234)
-                                                    (build-fn-arg-resolver :test4 (fn [request] (str "test$" (:uri request))))
-                                                    #'request-arg-resolver)
-                           'io.aviso.rook-test))]
+  (let [test-mw (-> (namespace-middleware rook-handler 'io.aviso.rook-test)
+                    (arg-resolver-middleware
+                      (build-map-arg-resolver :test1 "TEST!" :test2 "TEST@" :test3 "TEST#" :request-method :1234)
+                      (build-fn-arg-resolver :test4 (fn [request] (str "test$" (:uri request))))
+                      #'request-arg-resolver)
+                    param-handling)]
     (is (= "test1=TEST!,id=123,test2=TEST@,test3=TEST#,test4=test$/123/activate,request=13,meth=:1234"
-           (test-mw (mkrequest :post "/123/activate?test1=1test"))))))
+           (->
+             (mock/request :post "/123/activate?test1=1test")
+             test-mw)))))
 
 (deftest arg-resolver-test
   (let [map-resolver (build-map-arg-resolver :test1 "TEST!" :test2 "TEST@" :test3 "TEST#" :request-method :1234)
         fn-resolver (build-fn-arg-resolver :test4 (fn [request] (str "test$" (:uri request))))
         arg-resolvers1 [map-resolver fn-resolver #'request-arg-resolver]
         arg-resolvers2 [#'request-arg-resolver map-resolver fn-resolver]
-        test-request (mkrequest :post "/123/activate?test_value=TT!")]
+        handler (-> identity param-handling)
+        test-request (->
+                       (mock/request :post "/123/activate?test_value=TT!")
+                       handler)]
     (is (= "TEST!" (extract-argument-value 'test1 test-request [map-resolver])))
     (is (= "TEST@" (extract-argument-value 'test2 test-request [map-resolver])))
     (is (= "TEST#" (extract-argument-value 'test3 test-request [map-resolver])))

    (is (= "test$/123/activate" (extract-argument-value 'test4 test-request arg-resolvers2)))))

(deftest nested-context-test
  (let [test-mw (is (compojure/context "/merchant" []
                                       (namespace-middleware
                                         (compojure/routes
                                           (compojure/context "/:id/activate" []

       ",request=" (count request)
       ",meth=" request-method))


(defn param-handling [handler]
  (-> handler
      ring.middleware.keyword-params/wrap-keyword-params
      ring.middleware.params/wrap-params))

(defn mkrequest [method path namespace]
  (let [handler (-> (namespace-middleware identity namespace)
                    param-handling)]
    (handler (mock/request method path))))

(defn u-show [id]
  (str "!" id "!"))
