|
186 | 186 | (apply concat)
|
187 | 187 | (partition 2)))
|
188 | 188 |
|
| 189 | +(def default-ssl-server-options |
| 190 | + {:port port |
| 191 | + :ssl-context ssl/server-ssl-context}) |
| 192 | + |
| 193 | +(def default-ssl-client-options |
| 194 | + {:ssl-context ssl/client-ssl-context-opts}) |
| 195 | + |
189 | 196 | (defmacro with-server [server & body]
|
190 | 197 | `(let [server# ~server]
|
191 |
| - (binding [*pool* (http/connection-pool {:connection-options (merge *connection-options* {:insecure? true})})] |
| 198 | + (binding [*pool* (http/connection-pool {:connection-options *connection-options*})] |
192 | 199 | (try
|
193 | 200 | ~@body
|
194 | 201 | (finally
|
|
207 | 214 | (with-server (http/start-server ~handler {:port port :compression-level 3 :shutdown-timeout 0})
|
208 | 215 | ~@body)))
|
209 | 216 |
|
210 |
| -(def default-ssl-options {:port port, :ssl-context (netty/self-signed-ssl-context)}) |
| 217 | + |
211 | 218 |
|
212 | 219 | (defmacro with-handler-options
|
213 | 220 | [handler options & body]
|
|
247 | 254 |
|
248 | 255 |
|
249 | 256 | (deftest test-ssl-response-formats
|
250 |
| - (with-handler-options basic-handler default-ssl-options |
251 |
| - (doseq [[path result] expected-results] |
252 |
| - (is |
253 |
| - (= result |
254 |
| - (bs/to-string |
255 |
| - (:body |
256 |
| - @(http-get (str "https://localhost:" port "/" path))))) |
257 |
| - (str path "path failed"))))) |
| 257 | + (binding [*connection-options* default-ssl-client-options] |
| 258 | + (with-handler-options basic-handler default-ssl-server-options |
| 259 | + (doseq [[path result] expected-results] |
| 260 | + (is |
| 261 | + (= result |
| 262 | + (bs/to-string |
| 263 | + (:body |
| 264 | + @(http-get (str "https://localhost:" port "/" path))))) |
| 265 | + (str path "path failed")))))) |
258 | 266 |
|
259 | 267 | (deftest test-files
|
260 | 268 | (let [client-url (str "http://localhost:" port)]
|
|
269 | 277 | {:body (io/file "test/file.txt")}))))))))
|
270 | 278 |
|
271 | 279 | (deftest test-ssl-files
|
272 |
| - (let [client-url (str "https://localhost:" port) |
273 |
| - client-options {:connection-options {:ssl-context ssl/client-ssl-context}} |
274 |
| - client-pool (http/connection-pool client-options)] |
275 |
| - (with-handler-options identity (merge default-ssl-options {:ssl-context ssl/server-ssl-context}) |
276 |
| - (is (str/blank? |
277 |
| - (bs/to-string |
278 |
| - (:body @(http-put client-url |
279 |
| - {:body (io/file "test/empty.txt") |
280 |
| - :pool client-pool}))))) |
281 |
| - (is (= (slurp "test/file.txt" :encoding "UTF-8") |
| 280 | + (binding [*connection-options* default-ssl-client-options] |
| 281 | + (let [client-url (str "https://localhost:" port)] |
| 282 | + (with-handler-options identity default-ssl-server-options |
| 283 | + (is (str/blank? |
282 | 284 | (bs/to-string
|
283 | 285 | (:body @(http-put client-url
|
284 |
| - {:body (io/file "test/file.txt") |
285 |
| - :pool client-pool})))))))) |
| 286 | + {:body (io/file "test/empty.txt")}))))) |
| 287 | + (is (= (slurp "test/file.txt" :encoding "UTF-8") |
| 288 | + (bs/to-string |
| 289 | + (:body @(http-put client-url |
| 290 | + {:body (io/file "test/file.txt")}))))))))) |
286 | 291 |
|
287 | 292 | (defn ssl-session-capture-handler [ssl-session-atom]
|
288 | 293 | (fn [req]
|
289 | 294 | (reset! ssl-session-atom (http.core/ring-request-ssl-session req))
|
290 | 295 | {:status 200 :body "ok"}))
|
291 | 296 |
|
292 | 297 | (deftest test-ssl-session-access
|
293 |
| - (let [ssl-session (atom nil)] |
294 |
| - (with-handler-options |
295 |
| - (ssl-session-capture-handler ssl-session) |
296 |
| - default-ssl-options |
297 |
| - (is (= 200 (:status @(http-get (str "https://localhost:" port))))) |
298 |
| - (is (some? @ssl-session)) |
299 |
| - (when-let [^SSLSession s @ssl-session] |
300 |
| - (is (.isValid s)) |
301 |
| - (is (not (str/includes? "NULL" (.getCipherSuite s)))))))) |
| 298 | + (binding [*connection-options* default-ssl-client-options] |
| 299 | + (let [ssl-session (atom nil)] |
| 300 | + (with-handler-options |
| 301 | + (ssl-session-capture-handler ssl-session) |
| 302 | + default-ssl-server-options |
| 303 | + (is (= 200 (:status @(http-get (str "https://localhost:" port))))) |
| 304 | + (is (some? @ssl-session)) |
| 305 | + (when-let [^SSLSession s @ssl-session] |
| 306 | + (is (.isValid s)) |
| 307 | + (is (not (str/includes? "NULL" (.getCipherSuite s))))))))) |
302 | 308 |
|
303 | 309 | (deftest test-ssl-with-plain-client-request
|
304 |
| - (let [ssl-session (atom nil)] |
305 |
| - (with-handler-options |
306 |
| - (ssl-session-capture-handler ssl-session) |
307 |
| - default-ssl-options |
308 |
| - ;; Note the intentionally wrong "http" scheme here |
309 |
| - (is (some-> (http-get (str "http://localhost:" port)) |
310 |
| - (d/catch identity) |
311 |
| - deref |
312 |
| - ex-message |
313 |
| - (str/includes? "connection was closed"))) |
314 |
| - (is (nil? @ssl-session))))) |
| 310 | + (binding [*connection-options* default-ssl-client-options] |
| 311 | + (let [ssl-session (atom nil)] |
| 312 | + (with-handler-options |
| 313 | + (ssl-session-capture-handler ssl-session) |
| 314 | + default-ssl-server-options |
| 315 | + ;; Note the intentionally wrong "http" scheme here |
| 316 | + (is (some-> (http-get (str "http://localhost:" port)) |
| 317 | + (d/catch identity) |
| 318 | + deref |
| 319 | + ex-message |
| 320 | + (str/includes? "connection was closed"))) |
| 321 | + (is (nil? @ssl-session)))))) |
| 322 | + |
| 323 | +(deftest test-ssl-endpoint-identification |
| 324 | + (binding [*connection-options* {:ssl-context ssl/wrong-hostname-client-ssl-context-opts}] |
| 325 | + (let [ssl-session (atom nil)] |
| 326 | + (with-handler-options |
| 327 | + (ssl-session-capture-handler ssl-session) |
| 328 | + (assoc default-ssl-server-options :ssl-context ssl/wrong-hostname-server-ssl-context-opts) |
| 329 | + (is (thrown-with-msg? javax.net.ssl.SSLHandshakeException |
| 330 | + #"^No name matching localhost found$" |
| 331 | + @(http-get (str "https://localhost:" port)))) |
| 332 | + (is (nil? @ssl-session)))))) |
| 333 | + |
| 334 | +(deftest test-disabling-ssl-endpoint-identification |
| 335 | + (binding [*connection-options* {:ssl-context ssl/wrong-hostname-client-ssl-context-opts |
| 336 | + :ssl-endpoint-id-alg nil}] |
| 337 | + (let [ssl-session (atom nil)] |
| 338 | + (with-handler-options |
| 339 | + (ssl-session-capture-handler ssl-session) |
| 340 | + (assoc default-ssl-server-options :ssl-context ssl/wrong-hostname-server-ssl-context-opts) |
| 341 | + (is (= 200 (:status @(http-get (str "https://localhost:" port))))) |
| 342 | + (is (some? @ssl-session)))))) |
315 | 343 |
|
316 | 344 | (deftest test-invalid-body
|
317 | 345 | (let [client-url (str "http://localhost:" port)]
|
|
0 commit comments