This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-07-12
Channels
- # aleph (1)
- # beginners (81)
- # boot (20)
- # cider (46)
- # cljs-dev (6)
- # cljsjs (6)
- # cljsrn (8)
- # clojars (2)
- # clojure (104)
- # clojure-berlin (3)
- # clojure-italy (4)
- # clojure-losangeles (2)
- # clojure-nl (16)
- # clojure-spec (16)
- # clojure-uk (28)
- # clojurescript (88)
- # core-logic (31)
- # cursive (8)
- # data-science (3)
- # datascript (1)
- # datomic (95)
- # docs (1)
- # emacs (6)
- # figwheel-main (24)
- # fulcro (106)
- # graphql (5)
- # hyperfiddle (2)
- # midje (2)
- # nrepl (1)
- # off-topic (14)
- # om-next (1)
- # parinfer (2)
- # pedestal (26)
- # portkey (2)
- # re-frame (11)
- # reagent (27)
- # ring (6)
- # rum (4)
- # shadow-cljs (33)
- # spacemacs (10)
- # specter (53)
- # tools-deps (17)
- # vim (31)
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?
@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.
@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.
(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))))
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.
(def log-context-interceptor
{:name ::log-context-interceptor
:enter (fn [context] (log/debug :context context) context)})
Something like that'll if you don't have somewhere to add the logging already. Then you can add that to your interceptor chain.
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))))
Useful if you've got loads of assertions you don't need to check if things are utterly broken. 🙂
@david_clojurian the Content-Type
header in that response map is ""
.
But I set it
As you saw before
(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))))))
That's some test code in one of my personal projects. I'm using the HTTP header capitalisation, and those tests pass.
I wrap response-for
to make it take a proper map because kvs are not very nice to work with.
With capitalisation everthing works fine! Thank you very much. 😀
Yes it is. I love the simplicity of the serverside events. Very impressive.