Fork me on GitHub
#yada
<
2017-10-19
>
nha19:10:13

Note: the above does not seem related to the media-type. I've tried edn and json, it behave similarly. It there a way to make a POST to a resource in the REPL?

mccraigmccraig19:10:44

huh i've not had any problems with POST resources with bodies at all - and i have lots of them

mccraigmccraig19:10:18

your resource definition looks pretty much like one of mine

mccraigmccraig19:10:58

although i've never used the response-for test fn

nha19:10:02

Thanks - look similar indeed. Maybe it is that yada/response-for is not supposed to be used like that? (it works for GET requests though)

danielcompton19:10:49

I have this test function I use a lot

(defn req
  "Like yada.test/response-for, but takes a fully formed request (maybe a mock request)."
  ([o request]
   (let [h        (yada.handler/as-handler o)
         response @(h request)]
     (cond-> response
             (:body response) (update :body b/to-string)))))

mccraigmccraig19:10:21

dunno - it looks from the source like response-for should work with non-GET methods

danielcompton19:10:23

(req (test.system/routes) (-> (mock/request :put "/my/put/path" "cdeeee89f8910a13494ff85010d044834c4ad448")
                                       (basic-auth)))

nha19:10:23

Example get:

(:body (yada/response-for
         (yada/resource {:methods {:get {:consumes #{"application/json"}
                                         :produces #{"application/json"}
                                         :parameters {:query {:foo s/Str}}
                                         :response (fn [ctx]
                                                     {:parameters (:parameters ctx)})}}})
         :get
         "some-route?foo=abc"
         {:headers {"accept" "application/json"}}))
I will try your method now Daniel 🙂

danielcompton19:10:57

But 415 is a media type problem, so it may not help

nha19:10:45

Yes - I wish there was a debug mode with extra logging

danielcompton19:10:15

Not a bad idea. I think liberator has something like that, where it reports back the path it went down

nha19:10:10

Hm am I doing something wrong?

(let [body (json/generate-string {:foo "a-string"})
      l (str (count body))]
  (req ["/test/" (yada/resource {:methods {:put {:consumes #{"application/json"}
                                                 :produces #{"application/json"}
                                                 :parameters {:body {:foo s/Str}}
                                                 :response (fn [ctx]
                                                             {:parameters (:parameters ctx)})}}})]
       (-> (mock/request :put "/test/" {:body body})
           (mock/content-type "application/json")
           (mock/content-length l))))

nha19:10:24

I get :

{:status 400, :headers {"content-length" "32", "content-type" "text/plain;charset=utf-8"}, :body "Malformed body\r\n\r\n{:status 400}\n"}

nha19:10:15

Ah I have to remove the extra body. It works !

nha19:10:07

Thanks a lot guys 😄

nha19:10:31

Not the first time I am saved in this channel 😛