From f48b6159183bb71bf6fe45765f8bc2c32f47c5e8 Mon Sep 17 00:00:00 2001 From: onionpancakes <639985+onionpancakes@users.noreply.github.com> Date: Mon, 24 Jun 2024 12:53:19 -0700 Subject: [PATCH] Impl HttpResponse for ResponseProxy --- src/dev/onionpancakes/hop/impl/response.clj | 17 ++++++ .../hop/tests/test_impl_response.clj | 58 ++++++++++++------- 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/src/dev/onionpancakes/hop/impl/response.clj b/src/dev/onionpancakes/hop/impl/response.clj index e0e9a2d..4b1df6f 100644 --- a/src/dev/onionpancakes/hop/impl/response.clj +++ b/src/dev/onionpancakes/hop/impl/response.clj @@ -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)) diff --git a/test/dev/onionpancakes/hop/tests/test_impl_response.clj b/test/dev/onionpancakes/hop/tests/test_impl_response.clj index 81039f3..b33a12f 100644 --- a/test/dev/onionpancakes/hop/tests/test_impl_response.clj +++ b/test/dev/onionpancakes/hop/tests/test_impl_response.clj @@ -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)) @@ -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))))