|
12 | 12 | [ring.util.codec :refer [form-decode-str]] |
13 | 13 | [slingshot.slingshot :refer [try+]]) |
14 | 14 | (:import java.io.ByteArrayInputStream |
| 15 | + java.io.PipedInputStream |
| 16 | + java.io.PipedOutputStream |
15 | 17 | java.net.UnknownHostException |
16 | 18 | org.apache.http.HttpEntity |
17 | 19 | org.apache.logging.log4j.LogManager)) |
|
1754 | 1756 | (is (= (.getMessage e) |
1755 | 1757 | (str "only :flatten-nested-keys or :ignore-nested-query-string/" |
1756 | 1758 | ":flatten-nested-keys may be specified, not both")))))) |
| 1759 | + |
| 1760 | +(defn transit-resp [body] |
| 1761 | + {:body body |
| 1762 | + :status 200 |
| 1763 | + :headers {"content-type" "application/transit-json"}}) |
| 1764 | + |
| 1765 | +(deftest issue-609-empty-transit-response |
| 1766 | + (testing "Body is available right away" |
| 1767 | + (is (= {:foo "bar"} |
| 1768 | + (:body (client/coerce-response-body |
| 1769 | + {:as :transit+json} |
| 1770 | + (transit-resp (ByteArrayInputStream. |
| 1771 | + (.getBytes "[\"^ \",\"~:foo\",\"bar\"]")))))))) |
| 1772 | + |
| 1773 | + (testing "Empty body is read as nil" |
| 1774 | + (is (nil? (:body (client/coerce-response-body |
| 1775 | + {:as :transit+json} |
| 1776 | + (transit-resp (ByteArrayInputStream. (.getBytes "")))))))) |
| 1777 | + |
| 1778 | + (testing "Body is read correctly even if the data becomes available later" |
| 1779 | + ;; Ensure both streams are closed (normally done inside future). |
| 1780 | + (with-open [o (PipedOutputStream.) |
| 1781 | + i (PipedInputStream.)] |
| 1782 | + (.connect i o) |
| 1783 | + (future |
| 1784 | + (Thread/sleep 10) |
| 1785 | + (.write o (.getBytes "[\"^ \",\"~:foo\",\"bar\"]")) |
| 1786 | + ;; Close right now, with-open will wait until test is done. |
| 1787 | + (.close o)) |
| 1788 | + (is (= {:foo "bar"} |
| 1789 | + (:body (client/coerce-response-body |
| 1790 | + {:as :transit+json} |
| 1791 | + (transit-resp i)))))))) |
0 commit comments