|
| 1 | +(ns cider.nrepl.middleware.content-type-test |
| 2 | + (:require |
| 3 | + [cider.nrepl.middleware.content-type :as content-type] |
| 4 | + [cider.nrepl.test-session :as session] |
| 5 | + [clojure.string :as str] |
| 6 | + [clojure.test :refer :all])) |
| 7 | + |
| 8 | +(use-fixtures :each session/session-fixture) |
| 9 | + |
| 10 | +(defmethod content-type/content-type-response :graphviz [{:keys [value] :as response}] |
| 11 | + (let [{:keys [name edges]} value] |
| 12 | + (assoc response |
| 13 | + :content-type ["text/vnd.graphviz" {}] |
| 14 | + :body |
| 15 | + (str "graph " name " {\n" |
| 16 | + (str/join "\n" |
| 17 | + (for [[from to] edges] |
| 18 | + (str from " -- " to ";"))) |
| 19 | + "\n}")))) |
| 20 | + |
| 21 | +(deftest content-type-middleware-test |
| 22 | + (testing "java.net.URI" |
| 23 | + (is (= {:body "" |
| 24 | + :content-type ["message/external-body" |
| 25 | + {:URL "https://lambdaisland.com" |
| 26 | + :access-type "URL"}] |
| 27 | + :status #{"done"}} |
| 28 | + (select-keys (session/message {:op "eval" |
| 29 | + :code "(java.net.URI. \"https://lambdaisland.com\")" |
| 30 | + :content-type "true"}) |
| 31 | + [:body :content-type :content-transfer-encoding :status])))) |
| 32 | + |
| 33 | + (testing "java.net.URL" |
| 34 | + (is (= {:body "" |
| 35 | + :content-type ["message/external-body" |
| 36 | + {:URL "https://lambdaisland.com" |
| 37 | + :access-type "URL"}] |
| 38 | + :status #{"done"}} |
| 39 | + (select-keys (session/message {:op "eval" |
| 40 | + :code "(java.net.URL. \"https://lambdaisland.com\")" |
| 41 | + :content-type "true"}) |
| 42 | + [:body :content-type :content-transfer-encoding :status])))) |
| 43 | + |
| 44 | + (testing "java.io.File" |
| 45 | + (let [f (java.io.File/createTempFile "foo" ".txt")] |
| 46 | + (is (= {:body "" |
| 47 | + :content-type |
| 48 | + ["message/external-body" |
| 49 | + {:URL (str "file:" f) :access-type "URL"}] |
| 50 | + :status #{"done"}} |
| 51 | + (-> {:op "eval" |
| 52 | + :code (str "(java.io.File. " (pr-str (str f)) ")") |
| 53 | + :content-type "true"} |
| 54 | + session/message |
| 55 | + (select-keys [:body :content-type :content-transfer-encoding :status])))))) |
| 56 | + |
| 57 | + (testing "java.awt.image.RenderedImage" |
| 58 | + (is (= {:body "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR4XmNgAAIAAAUAAQYUdaMAAAAASUVORK5CYII=", |
| 59 | + :content-type ["image/png" {}] |
| 60 | + :content-transfer-encoding "base64" |
| 61 | + :status #{"done"}} |
| 62 | + (-> {:op "eval" |
| 63 | + :code "(java.awt.image.BufferedImage. 1 1 java.awt.image.BufferedImage/TYPE_INT_ARGB)" |
| 64 | + :content-type "true"} |
| 65 | + session/message |
| 66 | + (select-keys [:body :content-type :content-transfer-encoding :status]))))) |
| 67 | + |
| 68 | + (testing "custom type implementation" |
| 69 | + (is (= {:body "graph foo {\na -- b;\nb -- c;\n}" |
| 70 | + :content-type ["text/vnd.graphviz" {}] |
| 71 | + :status #{"done"}} |
| 72 | + (-> {:op "eval" |
| 73 | + :code (str "^{:type :graphviz} " |
| 74 | + (pr-str |
| 75 | + {:name "foo" |
| 76 | + :edges [["a" "b"] ["b" "c"]]})) |
| 77 | + :content-type "true"} |
| 78 | + session/message |
| 79 | + (select-keys [:body :content-type :content-transfer-encoding :status])))))) |
0 commit comments