How to test HTTP requests with cljs-http using lein doo phantom

When I run my tests with lein doo phantom, I get a -1 status response and an empty string as a body. However, when I run the test in repl, I can get the request data with a status response of 200 and the corresponding data in the body. Is it because the first multi-channel channel is returned first, as indicated below, which gives me the wrong answer? If so, how can I explain this?

https://github.com/r0man/cljs-http#async-response-handling

I also thought maybe I need to use a timeout to wait for the request to complete. If so, how would I apply this with my existing code? It seems that cljs-http has: a timeout as a parameter, but I was not sure if it worked properly (assuming this is the cause of the problem).

(deftest test-async
 (async done
      (go (let [response (<! (http/get "http://localhost:3000/api/user/1"
                                          {:with-credentials? false
                                           :query-params {"id" 1}}))]
            (is (= {:status 200}
                   (select-keys response [:status]))))
          (done))))
+4
source share
1 answer

Since you run your test under phantomjs. Phantomjs by default disables access to the XHR cross domain, and your js tests run on the local host, all external ajax calls are prohibited.

you can set --web-security=falseyour test to perform ajax cross domain.

In project.cljadd

:doo {:paths {:phantom "phantomjs --web-security=false"}}

more information about phantomjs

http://phantomjs.org/api/command-line.html

+2

Source: https://habr.com/ru/post/1668560/


All Articles