简体   繁体   中英

Clojure - test a Pedestal route

I would like to write tests for a Pedestal web-service.

If I have :

(defn pong
  [request]
  (ring-resp/response "pong"))

(defroutes routes[[["/" {:get pong}]]])

How would I write a test for that ?

(deftest alive-system
  (testing "ping-pong route"
    ;; How do I test my route ?
    ;; If possible :
    ;; - I would like to have direct access to it
    ;;   ie. no need to bind pedestal to a port would be nice
    ;; - The ability to omit some interceptors would be nice also,
    ;;   as it would allow me to receive plain Clojure data structures
    ;;   instead of, for instance, JSON which I would have to parse.
    ...)

Edit : Here is what I tried :

(deftest alive-system
  (testing "ping-pong route"
    (let [response (my-other.ns/routes (mock/request :get "/ping"))]
      (is (= (:status response) 200))
      (is (= (:body response) "pong")))))

But I get an exception :

ERROR in (alive-system) (service_test.clj:13)
Uncaught exception, not in assertion.
expected: nil
  actual: java.lang.ClassCastException: clojure.lang.LazySeq cannot be cast to clojure.lang.IFn

So after asking on the issue I linked ohpaulez replied :

@nha - Thanks for using Pedestal! Sorry your question didn't get an answer on StackOverflow - I don't think anyone monitors SO for Pedestal questions. The best place to ask those kinds of questions is on the mailing list .

Pedestal ships with its own utility for making requests directly to the servlet (similar to ring/mock, although I've never used mock myself) called response-for . The Pedestal Service template produces a test automatically for you. Check out one of the samples for an example.

Also note that said response-for doesn't yet support asynchronous responses (so my routes that uses asynchronous interceptors with core.async failed - I had to make them synchronous).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM