Skip to content

Commit

Permalink
simplify request+headers building for now
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenet committed Jan 3, 2024
1 parent dc18d06 commit 141b42a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 163 deletions.
1 change: 0 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.12.0-alpha1"}
com.github.strojure/zmap {:mvn/version "1.3.26"}
io.helidon.http/helidon-http {:mvn/version "4.0.1"}
io.helidon.webserver/helidon-webserver {:mvn/version "4.0.2"}
io.helidon.webserver/helidon-webserver-websocket {:mvn/version "4.0.2"}
Expand Down
146 changes: 0 additions & 146 deletions src/s_exp/hirundo/http/header.clj

This file was deleted.

29 changes: 15 additions & 14 deletions src/s_exp/hirundo/http/request.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
(ns s-exp.hirundo.http.request
(:require [clojure.string :as str]
[s-exp.hirundo.http.header :as h]
[strojure.zmap.core :as zmap])
(:require [clojure.string :as str])
(:import (clojure.lang PersistentHashMap)
(io.helidon.common.uri UriQuery UriPath)
(io.helidon.http HttpPrologue Headers)
(io.helidon.http HttpPrologue Headers Header)
(io.helidon.webserver.http ServerRequest ServerResponse)))

(defn ring-headers
[^Headers headers]
(h/->HeaderMapProxy headers nil))
(-> (reduce (fn [m ^Header h]
(assoc! m
(.lowerCase (.headerName h))
(.value h)))
(transient {})
headers)
persistent!))

(defn ring-method [^HttpPrologue prologue]
(let [method (-> prologue .method .text)]
Expand Down Expand Up @@ -46,14 +50,11 @@
body (let [content (.content server-request)]
(when-not (.consumed content) (.inputStream content)))
ring-request (-> (.asTransient PersistentHashMap/EMPTY)
;; delayed
(.assoc :server-port (zmap/delay (.port (.localPeer server-request))))
(.assoc :server-name (zmap/delay (.host (.localPeer server-request))))
(.assoc :remote-addr (zmap/delay
(let [address ^java.net.InetSocketAddress (.address (.remotePeer server-request))]
(-> address .getAddress .getHostAddress))))
(.assoc :ssl-client-cert (zmap/delay (some-> server-request .remotePeer .tlsCertificates (.orElse nil) first)))
;; realized
(.assoc :server-port (.port (.localPeer server-request)))
(.assoc :server-name (.host (.localPeer server-request)))
(.assoc :remote-addr (let [address ^java.net.InetSocketAddress (.address (.remotePeer server-request))]
(-> address .getAddress .getHostAddress)))
(.assoc :ssl-client-cert (some-> server-request .remotePeer .tlsCertificates (.orElse nil) first))
(.assoc :uri (ring-path (.path server-request)))
(.assoc :scheme (if (.isSecure server-request) :https :http))
(.assoc :protocol (ring-protocol (.prologue server-request)))
Expand All @@ -65,4 +66,4 @@
ring-request (cond-> ring-request
qs (.assoc :query-string (ring-query (.query server-request)))
body (.assoc :body body))]
(zmap/wrap (.persistent ring-request))))
(.persistent ring-request)))
16 changes: 14 additions & 2 deletions test/s_exp/hirundo_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
(with-server {:http-handler (fn [req] {:status 201})}
(is (-> (client/get *endpoint*) :status (= 201)))))

(deftest test-headers
(with-server {:http-handler (fn [req]
{:body (str (count (:headers req)))})}
(is (-> (client/get *endpoint*) :body (= "4"))))
(with-server {:http-handler (fn [req]
{:body (str (:headers req))})}
(is (-> (client/get *endpoint*) :status (= 200)))))

(deftest test-query-string
(with-server {:http-handler (fn [req] {:body (:query-string req)})}
(is (-> (client/get (str *endpoint* "?foo=bar")) :body (= "foo=bar"))))
Expand Down Expand Up @@ -78,6 +86,11 @@
(with-server {:http-handler (fn [req] {:body (java.io.ByteArrayInputStream. (.getBytes "yes"))})}
(is (-> (client/get *endpoint*) :body (= "yes")))))

(deftest resp-map-decoding
(with-server {:http-handler (fn [req]
{:body (str (select-keys req [:something]))})}
(is (status-ok? (client/get (str *endpoint* ""))))))

(defn tls []
(let [b (doto (TlsConfig/builder)
(.sslContext (ls/ssl-context "test/server.key"
Expand Down Expand Up @@ -119,7 +132,7 @@
(defmacro with-ws-client
[options & body]
`(binding [*client* (wsc/connect (str (str/replace *endpoint* "http" "ws") "/ws")
~@(into [] cat options))]
~@(into [] cat options))]

(try
~@body
Expand Down Expand Up @@ -163,4 +176,3 @@
#"Not Found"
(with-ws-client {:subprotocols ["foo"]}))
"Incorrect subprotocols"))))

0 comments on commit 141b42a

Please sign in to comment.