-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathintegration_test.clj
More file actions
112 lines (91 loc) · 3.19 KB
/
integration_test.clj
File metadata and controls
112 lines (91 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
(ns ^:integration java-http-clj.integration-test
(:refer-clojure :exclude [send get])
(:require [cemerick.url :refer [url]]
[clojure.java.io :as io]
[clojure.test :refer :all]
[clojure.spec.test.alpha :as st]
[java-http-clj.core :refer :all]
[java-http-clj.specs]
[mount.core :as mount])
(:import [java.net.http HttpResponse]
[java.io File]
[java.util Arrays]
[java.util.concurrent CompletableFuture]))
(set! *warn-on-reflection* true)
(st/instrument)
(def port 8787)
(defn wrap-setup [f]
(mount/start (mount/with-args {:port port}))
(f)
(mount/stop))
(use-fixtures :once wrap-setup)
(def base-url
(assoc (url "http://localhost") :port port))
(defn make-url
([]
(str base-url))
([path]
(str (url base-url path)))
([path params]
(str (assoc (url base-url path) :query params))))
(def ^String s "some boring test string")
(defn all-tests [f]
(testing "request"
(let [{:keys [body headers status version]} (send (make-url))]
(is (= "ROOT" body))
; (is (= ["content-length" "content-type" "date" "server"] (-> headers keys sort)))
(is (= 200 status))
(is (= :http1.1 version))))
(testing "request-body-types"
(let [file-content
"hello"
file
(doto (File/createTempFile "tmp" ".tmp")
(spit file-content))
path
(.toPath file)
send-and-get-body
(fn [body]
(:body (f {:uri (make-url "echo")
:method :post
:body body})))]
(is (= "ROOT" (:body (send (make-url)))))
(is (= s (send-and-get-body s)))
(is (= s (send-and-get-body (.getBytes s))))
(is (= file-content (send-and-get-body file)))
(is (= file-content (send-and-get-body path)))
(is (= s (send-and-get-body (io/input-stream (.getBytes s)))))))
(testing "response-body-types"
(let [send-echo (fn [opts] (f (make-url "echo" {:message s}) opts))]
(is (= s (:body (send-echo {:as :string}))))
(is (Arrays/equals (.getBytes s) ^bytes (:body (send-echo {:as :byte-array}))))
(is (= s (-> (send-echo {:as :input-stream}) :body slurp)))))
(testing "raw-opt"
(is (instance? HttpResponse (f (make-url) {:raw? true}))))
(testing "client-opt"
;; default client doesn't follow redirects
(is (= 302 (:status (f (make-url "redir")))))
(let [client (build-client {:follow-redirects :always})
{:keys [body status]} (f (make-url "redir") {:client client})]
(is (= 200 status))
(is (= "did redirect" body)))))
(deftest test-send
(all-tests send))
(deftest test-send-async
(all-tests (comp deref send-async)))
(deftest async-stuff
(let [f (send-async (make-url))
r (.join f)]
(is (instance? CompletableFuture f))
(is (map? r)))
(testing "callback"
(let [r (.join (send-async (make-url) {} #(assoc % :call :back) nil))]
(is (= :back (:call r)))))
(testing "ex-handler"
(let [f (send-async
(make-url)
{}
(fn [_] (throw (Exception. "oops!")))
(fn [_] :exception))
r (.join f)]
(is (= :exception r)))))