Skip to content

Commit

Permalink
Impl HttpResponse for ResponseProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
onionpancakes committed Jun 24, 2024
1 parent f75fb70 commit f48b615
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
17 changes: 17 additions & 0 deletions src/dev/onionpancakes/hop/impl/response.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@
(declare response-proxy-function)

(deftype ResponseProxy [^HttpResponse response headers]
HttpResponse
(body [_]
(.body response))
(headers [_]
(.headers response))
(previousResponse [_]
(.previousResponse response))
(request [_]
(.request response))
(sslSession [_]
(.sslSession response))
(statusCode [_]
(.statusCode response))
(uri [_]
(.uri response))
(version [_]
(.version response))
clojure.lang.ILookup
(valAt [this k]
(.get this k))
Expand Down
58 changes: 36 additions & 22 deletions test/dev/onionpancakes/hop/tests/test_impl_response.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@
[java.util.function BiPredicate]))

(def example-response-object
(reify HttpResponse
(body [this]
"Body")
(headers [this]
(HttpHeaders/of {"content-encoding" ["gzip"]
"content-type" ["text/plain;charset=utf-8"]}
(reify java.util.function.BiPredicate
(test [_ _ _] true))))
(previousResponse [this]
(Optional/ofNullable nil))
(request [this]
(.. (HttpRequest/newBuilder)
(uri (java.net.URI. "http://www.example.com"))
(build)))
(sslSession [this]
(Optional/ofNullable nil))
(statusCode [this]
(int 200))
(uri [this]
(java.net.URI. "http://www.example.com"))
(version [this]
HttpClient$Version/HTTP_1_1)))
(let [body "Body"
headers (HttpHeaders/of {"content-encoding" ["gzip"]
"content-type" ["text/plain;charset=utf-8"]}
(reify java.util.function.BiPredicate
(test [_ _ _] true)))
prevResp (Optional/ofNullable nil)
req (.. (HttpRequest/newBuilder)
(uri (java.net.URI. "http://www.example.com"))
(build))
ssl (Optional/ofNullable nil)
status (int 200)
uri (java.net.URI. "http://www.example.com")
version HttpClient$Version/HTTP_1_1]
(reify HttpResponse
(body [_] body)
(headers [_] headers)
(previousResponse [_] prevResp)
(request [_] req)
(sslSession [_] ssl)
(statusCode [_] status)
(uri [_] uri)
(version [_] version))))

(def ^java.util.Map example-response-proxy
(response/response-proxy example-response-object))
Expand Down Expand Up @@ -100,3 +100,17 @@
(.keySet example-response-proxy) (.keySet example-response-map)
(.size example-response-proxy) (.size example-response-map)
(set (.values example-response-proxy)) (set (.values example-response-map))))

(deftest test-response-proxy-http-response
(are [value expected] (identical? value expected)
(.body example-response-proxy) (.body example-response-object)
(.headers example-response-proxy) (.headers example-response-object)
(.previousResponse example-response-proxy) (.previousResponse example-response-object)
(.request example-response-proxy) (.request example-response-object)
(.sslSession example-response-proxy) (.sslSession example-response-object)
;; Primitives can't be identical, use value test instead.
#_#_
(.statusCode example-response-proxy) (.statusCode example-response-object)
(.uri example-response-proxy) (.uri example-response-object)
(.version example-response-proxy) (.version example-response-object))
(is (= (.statusCode example-response-proxy) (.statusCode example-response-object))))

0 comments on commit f48b615

Please sign in to comment.