|
6 | 6 | [clojure.test :refer [deftest is testing are]]))
|
7 | 7 |
|
8 | 8 | (deftest get-test
|
9 |
| - (is (str/includes? (curl/get "https://httpstat.us/200") |
| 9 | + (is (str/includes? (:body (curl/get "https://httpstat.us/200")) |
10 | 10 | "200"))
|
11 | 11 | (is (= 200
|
12 | 12 | (-> (curl/get "https://httpstat.us/200"
|
13 | 13 | {:headers {"Accept" "application/json"}})
|
| 14 | + :body |
14 | 15 | (json/parse-string true)
|
15 | 16 | :code)))
|
16 | 17 | (testing "query params"
|
17 | 18 | (is (= {:foo1 "bar1", :foo2 "bar2"}
|
18 | 19 | (-> (curl/get "https://postman-echo.com/get" {:query-params {"foo1" "bar1" "foo2" "bar2"}})
|
| 20 | + :body |
19 | 21 | (json/parse-string true)
|
20 | 22 | :args)))))
|
21 | 23 |
|
22 | 24 | (deftest head-test
|
23 |
| - (is (str/includes? (curl/head "https://postman-echo.com/head") |
24 |
| - "200 OK"))) |
| 25 | + (is (= 200 (:status (curl/head "https://postman-echo.com/head"))))) |
25 | 26 |
|
26 | 27 | (deftest post-test
|
27 |
| - (is (subs (curl/post "https://postman-echo.com/post") |
| 28 | + (is (subs (:body (curl/post "https://postman-echo.com/post")) |
28 | 29 | 0 10))
|
29 | 30 | (is (str/includes?
|
30 |
| - (curl/post "https://postman-echo.com/post" |
31 |
| - {:body "From Clojure"}) |
| 31 | + (:body (curl/post "https://postman-echo.com/post" |
| 32 | + {:body "From Clojure"})) |
32 | 33 | "From Clojure"))
|
33 | 34 | (testing "file-body"
|
34 | 35 | (is (str/includes?
|
35 |
| - (curl/post "https://postman-echo.com/post" |
36 |
| - {:body (io/file "README.md")}) |
| 36 | + (:body (curl/post "https://postman-echo.com/post" |
| 37 | + {:body (io/file "README.md")})) |
37 | 38 | "babashka.curl")))
|
38 | 39 | (testing "form-params"
|
39 | 40 | (is (str/includes?
|
40 |
| - (curl/post "https://postman-echo.com/post" |
41 |
| - {:form-params {"name" "michiel"}}) |
| 41 | + (:body (curl/post "https://postman-echo.com/post" |
| 42 | + {:form-params {"name" "michiel"}})) |
42 | 43 | "michiel"))))
|
43 | 44 |
|
44 | 45 | (deftest patch-test
|
45 | 46 | (is (str/includes?
|
46 |
| - (curl/patch "https://postman-echo.com/patch" |
47 |
| - {:body "hello"}) |
| 47 | + (:body (curl/patch "https://postman-echo.com/patch" |
| 48 | + {:body "hello"})) |
48 | 49 | "hello")))
|
49 | 50 |
|
50 | 51 | (deftest basic-auth-test
|
51 | 52 | (is (re-find #"authenticated.*true"
|
52 |
| - (curl/get "https://postman-echo.com/basic-auth" {:basic-auth ["postman" "password"]})))) |
| 53 | + (:body |
| 54 | + (curl/get "https://postman-echo.com/basic-auth" |
| 55 | + {:basic-auth ["postman" "password"]}))))) |
53 | 56 |
|
54 | 57 | (deftest raw-args-test
|
55 |
| - (is (str/includes? |
56 |
| - (curl/post "https://postman-echo.com/post" |
57 |
| - {:body "From Clojure" |
58 |
| - :raw-args ["-D" "-"]}) |
59 |
| - "200 OK"))) |
| 58 | + (is (= 200 (:status (curl/post "https://postman-echo.com/post" |
| 59 | + {:body "From Clojure" |
| 60 | + :raw-args ["-D" "-"]}))))) |
60 | 61 |
|
61 | 62 | (deftest get-response-object-test
|
62 |
| - (let [response (curl/get "https://httpstat.us/200" {:response true})] |
| 63 | + (let [response (curl/get "https://httpstat.us/200")] |
63 | 64 | (is (map? response))
|
64 | 65 | (is (= 200 (:status response)))
|
65 | 66 | (is (= "200 OK" (:body response)))
|
66 | 67 | (is (= "Microsoft-IIS/10.0" (get-in response [:headers "server"]))))
|
67 | 68 |
|
68 | 69 | (testing "response object as stream"
|
69 |
| - (let [response (curl/get "https://httpstat.us/200" {:response true :as :stream})] |
| 70 | + (let [response (curl/get "https://httpstat.us/200" {:as :stream})] |
70 | 71 | (is (map? response))
|
71 | 72 | (is (= 200 (:status response)))
|
72 | 73 | (is (instance? java.io.InputStream (:body response)))
|
73 | 74 | (is (= "200 OK" (slurp (:body response))))))
|
74 | 75 |
|
75 | 76 | (testing "response object with following redirect"
|
76 | 77 | (let [response (curl/get "https://httpbin.org/redirect-to?url=https://www.httpbin.org"
|
77 |
| - {:raw-args ["-L"] |
78 |
| - :response true})] |
| 78 | + {:raw-args ["-L"]})] |
79 | 79 | (is (map? response))
|
80 | 80 | (is (= 200 (:status response)))
|
81 | 81 | (is (= 302 (-> response :redirects first :status)))
|
82 | 82 | (is (= "https://www.httpbin.org" (get-in response [:redirects 0 :headers "location"])))))
|
83 | 83 |
|
84 | 84 | (testing "response object without fully following redirects"
|
85 | 85 | (let [response (curl/get "https://httpbin.org/redirect-to?url=https://www.httpbin.org"
|
86 |
| - {:response true |
87 |
| - :raw-args ["--max-redirs" "0"]})] |
| 86 | + {:raw-args ["--max-redirs" "0"]})] |
88 | 87 | (is (map? response))
|
89 | 88 | (is (= 302 (:status response)))
|
90 | 89 | (is (= "" (:body response)))
|
|
95 | 94 | (is (= 200
|
96 | 95 | (-> (curl/get "https://httpstat.us/200"
|
97 | 96 | {:accept :json})
|
| 97 | + :body |
98 | 98 | (json/parse-string true)
|
99 | 99 | :code))))
|
100 | 100 |
|
101 | 101 | (deftest url-encode-query-params-test
|
102 | 102 | (is (= {"my query param?" "hello there"}
|
103 | 103 | (-> (curl/get "https://postman-echo.com/get" {:query-params {"my query param?" "hello there"}})
|
| 104 | + :body |
104 | 105 | (json/parse-string)
|
105 | 106 | (get "args")))))
|
106 | 107 |
|
|
110 | 111 | :port 443
|
111 | 112 | :path "/get"
|
112 | 113 | :query "q=test"}})
|
| 114 | + :body |
113 | 115 | (json/parse-string true))]
|
114 | 116 | (is (= {:q "test"} (:args response)))
|
115 | 117 | (is (= "httpbin.org" (get-in response [:headers :Host])))))
|
|
118 | 120 | (testing "download image"
|
119 | 121 | (let [tmp-file (java.io.File/createTempFile "icon" ".png")]
|
120 | 122 | (.deleteOnExit tmp-file)
|
121 |
| - (io/copy (curl/get "https://github.com/borkdude/babashka/raw/master/logo/icon.png" {:as :stream}) |
| 123 | + (io/copy (:body (curl/get "https://github.com/borkdude/babashka/raw/master/logo/icon.png" {:as :stream})) |
122 | 124 | tmp-file)
|
123 | 125 | (is (= (.length (io/file "test" "icon.png"))
|
124 | 126 | (.length tmp-file)))))
|
125 | 127 | (testing "download image with response headers"
|
126 | 128 | (let [tmp-file (java.io.File/createTempFile "icon" ".png")]
|
127 | 129 | (.deleteOnExit tmp-file)
|
128 |
| - (let [resp (curl/get "https://github.com/borkdude/babashka/raw/master/logo/icon.png" {:as :stream |
129 |
| - :response true})] |
| 130 | + (let [resp (curl/get "https://github.com/borkdude/babashka/raw/master/logo/icon.png" {:as :stream})] |
130 | 131 | (is (= 200 (:status resp)))
|
131 | 132 | (io/copy (:body resp) tmp-file))
|
132 | 133 | (is (= (.length (io/file "test" "icon.png"))
|
|
0 commit comments