Fork me on GitHub
#pedestal
<
2018-07-12
>
david_clojurian09:07:28

Hello, I'm trying to use response_for for my test. When I'm running the server with lein run und call the endpoint with curl everything is working fine. But when I'm using response_for the :edn-params isn't filled. Any ideas?

jcf12:07:28

@david_clojurian one thing to look out for when using response-for is the request map is different to the one you'd get from a 'real' request. If you're using things like Accept/Host headers etc. in your code that may cause issues.

david_clojurian12:07:47

@jcf The body is empty. When I'm looking at the code of pedestal io.pedestal.test/servlet-response-for I'm not sure that the body is processed at all. response_for isn't working for me when I want to do a POST with a body.

jcf12:07:33

What does your test code look like?

jcf12:07:52

(io.pedestal.test/response-for app :body "foo")

jcf12:07:54

Something like that?

david_clojurian12:07:56

(let [r (response-for service :post "/test-suites"
                          :body "{:test-suite/name        \"Suite-Name\"}"
                          :headers {"content-type" "application/edn"})
          uri (str "/test-suites/" (-> r
                                       :body
                                       (edn/read-string)))]
      (-> r
          :status
          (= 200)
          (is (-> r :body))))

jcf12:07:03

(is (= 200 (:status r)))
(is (= 'foo (:body r)))

jcf12:07:40

If you haven't already, add some logging to your code inside your route/interceptor that logs the context. Let's see what's there.

jcf12:07:34

(def log-context-interceptor
  {:name ::log-context-interceptor
   :enter (fn [context] (log/debug :context context) context)})

jcf12:07:02

Something like that'll if you don't have somewhere to add the logging already. Then you can add that to your interceptor chain.

jcf12:07:55

BTW, if you want to short-circuit tests when the status isn't 200 etc. this pattern can work quite well:

(when (is (= 200 (:status response))
          (with-out-str (clojure.pprint/pprint response)))
  ;; These test assertions won't be executed if the status isn't 200.
  (is (= 'foo (:body response))))

👍 4
jcf12:07:16

Useful if you've got loads of assertions you don't need to check if things are utterly broken. 🙂

jcf12:07:16

@david_clojurian the Content-Type header in that response map is "".

jcf12:07:50

Empty string in both the request and the response from the looks of it.

david_clojurian12:07:15

As you saw before

jcf12:07:33

Try it with the "Content-Type" capitalisation just to make sure that's not it.

jcf12:07:56

Ring always lowercases things but either Aleph and/or Pedestal sometimes don't.

jcf12:07:52

(def ^:private secure-headers
  {"Content-Security-Policy" "object-src 'none'"
   "Strict-Transport-Security" "max-age=31536000; includeSubdomains"
   "X-Content-Type-Options" "nosniff"
   "X-Download-Options" "noopen"
   "X-Frame-Options" "DENY"
   "X-Permitted-Cross-Domain-Policies" "none"
   "X-XSS-Protection" "1; mode=block"})

(def ^:const ^:private html+utf8 "text/html;charset=UTF-8")

(deftest hello-world
  (t/with-system [{:keys [service]} (t/system)]
    (let [response (t/response-for service :get "/fmt")]
      (when (is (= 200 (:status response))
                (t/pprint-str response))
        (is (= (assoc secure-headers "Content-Type" html+utf8)
               (:headers response)))))))

(deftest about
  (t/with-system [{:keys [service]} (t/system)]
    (let [response (t/response-for service :get "/api/site/v1/about")]
      (is (= 200 (:status response)))
      (is (= (assoc secure-headers "Content-Type" "text/plain")
             (dissoc (:headers response) "vaserequest-id")))
      (is (= "This came from site.edn!"
             (:body response))))))

jcf12:07:15

That's some test code in one of my personal projects. I'm using the HTTP header capitalisation, and those tests pass.

jcf12:07:09

I wrap response-for to make it take a proper map because kvs are not very nice to work with.

david_clojurian12:07:31

With capitalisation everthing works fine! Thank you very much. 😀

jcf12:07:38

You're welcome!

jcf12:07:58

Hope you have fun with Pedestal. It's a great piece of software.

david_clojurian12:07:06

Yes it is. I love the simplicity of the serverside events. Very impressive.