babashka/test/babashka/http_connection_test.clj

24 lines
834 B
Clojure
Raw Normal View History

2020-01-04 19:04:29 +00:00
(ns babashka.http-connection-test
(:require
[babashka.test-utils :as tu]
[clojure.string :as str]
[clojure.test :as t :refer [deftest is]]))
2020-01-04 19:04:29 +00:00
(defn bb [& args]
(apply tu/bb nil (map str args)))
(deftest open-connection-test
(is (try (= "\"1\"" (str/trim (bb "-e" "
2020-01-04 19:04:29 +00:00
(require '[cheshire.core :as json])
2020-01-05 16:45:25 +00:00
(let [conn ^java.net.HttpURLConnection (.openConnection (java.net.URL. \"https://postman-echo.com/get?foo=1\"))]
2020-01-04 19:04:29 +00:00
(.setConnectTimeout conn 1000)
(.setRequestProperty conn \"Content-Type\" \"application/json\") ;; nonsensical, but to test if this method exists
(.connect conn)
(let [is (.getInputStream conn)
err (.getErrorStream conn)
response (json/decode (slurp is) true)]
(-> response :args :foo)))
")))
(catch Exception e
(str/includes? (str e) "timed out")))))