0

I'm doing tests on my luminus application and I want to test my post fuction as below. However, the data is posted on the body of the request object as a byte input stream. How do i make the data to be posted on the params key of the request object? I got this example from this link http://www.jarrodctaylor.com/posts/Compojure-Address-Book-Part-1/

(defn example-post [request]
  (let [post-value (get-in request [:params :example-post])]
    (str "You posted: " post-value)))

  (fact "Test POST"
    (let [response (app (mock/request :post "/post" {:example-post "Some data"}))]
      (:status response) => 200
      (:body response) => "You posted: Some data")))
joeabala
  • 266
  • 3
  • 11
  • Your question really has nothing to do with Midje. If you don't have `params` middleware in `app`, `:params` won't be populated. – muhuk Feb 18 '16 at 10:53
  • when i carry out my normal requests, the data is stored in the :params key of the request object so i just parse it using (-> req :params :data) so i dont think that might be the issue. – joeabala Feb 18 '16 at 11:15
  • You were right @muhuk, many thanks – joeabala Feb 18 '16 at 13:35

1 Answers1

0

Got the answer, I was binding the mock/request in the ring handler function defroutes app-routes as opposed to the app var:

(defroutes app-routes
           (GET "/" [] tests)
           (POST "/post" [] example-post)
           (not-found "invalid request"))

(def app
  (wrap-defaults app-routes (assoc-in site-defaults [:security :anti-forgery] false)))

The correct way:

 (fact "Test POST"
    (let [response (app (mock/request :post "/post" {:example-post "Some data"}))]
      (:status response) => 200
      (:body response) => "You posted: Some data")))

Incorrect way

 (fact "Test POST"
    (let [response (app-routes (mock/request :post "/post" {:example-post "Some data"}))]
      (:status response) => 200
      (:body response) => "You posted: Some data")))
Community
  • 1
  • 1
joeabala
  • 266
  • 3
  • 11