|
268 | 268 | (with-http2-server handler# server-options# ~@body)
|
269 | 269 | (with-http1-server handler# server-options# ~@body)))
|
270 | 270 |
|
| 271 | +(defmacro with-http-ssl-servers |
| 272 | + "Run the same body of tests for each HTTP version with SSL enabled" |
| 273 | + [handler server-options & body] |
| 274 | + `(with-redefs [*use-tls?* true] |
| 275 | + (let [handler# ~handler |
| 276 | + server-options# ~server-options] |
| 277 | + (with-http2-server handler# server-options# ~@body) |
| 278 | + (with-http1-server handler# (merge http1-ssl-server-options server-options#) ~@body)))) |
| 279 | + |
271 | 280 | (defmacro with-handler [handler & body]
|
272 | 281 | `(with-http-servers ~handler {}
|
273 | 282 | ~@body))
|
|
391 | 400 |
|
392 | 401 |
|
393 | 402 | (deftest test-ssl-response-formats
|
394 |
| - (with-redefs [*use-tls?* true] |
395 |
| - (with-http-servers basic-handler http1-ssl-server-options |
396 |
| - (doseq [[path result] expected-results] |
397 |
| - (is |
398 |
| - (= result |
399 |
| - (bs/to-string |
400 |
| - (:body |
401 |
| - @(http-get (str "/" path))))) |
402 |
| - (str path "path failed")))))) |
| 403 | + (with-http-ssl-servers basic-handler {} |
| 404 | + (doseq [[path result] expected-results] |
| 405 | + (is |
| 406 | + (= result |
| 407 | + (bs/to-string |
| 408 | + (:body |
| 409 | + @(http-get (str "/" path))))) |
| 410 | + (str path "path failed"))))) |
403 | 411 |
|
404 | 412 | (deftest test-files
|
405 | 413 | (with-handler echo-handler
|
|
413 | 421 | bs/to-string)))))
|
414 | 422 |
|
415 | 423 | (deftest test-ssl-files
|
416 |
| - (with-redefs [*use-tls?* true] |
417 |
| - (let [client-path "/" |
418 |
| - client-options {:connection-options {:ssl-context test-ssl/client-ssl-context}} |
419 |
| - client-pool (http/connection-pool client-options)] |
420 |
| - (with-http-servers echo-handler |
421 |
| - (merge http1-ssl-server-options {:ssl-context test-ssl/server-ssl-context}) |
422 |
| - (is (str/blank? |
423 |
| - (-> @(http-put client-path |
424 |
| - {:body (io/file "test/empty.txt") |
425 |
| - :pool client-pool}) |
426 |
| - :body |
427 |
| - bs/to-string))) |
428 |
| - (is (= (slurp "test/file.txt" :encoding "UTF-8") |
429 |
| - (-> @(http-put client-path |
430 |
| - {:body (io/file "test/file.txt") |
431 |
| - :pool client-pool}) |
432 |
| - :body |
433 |
| - bs/to-string))))))) |
| 424 | + (let [client-path "/" |
| 425 | + client-options {:connection-options {:ssl-context test-ssl/client-ssl-context}} |
| 426 | + client-pool (http/connection-pool client-options)] |
| 427 | + (with-http-ssl-servers echo-handler {:ssl-context test-ssl/server-ssl-context} |
| 428 | + (is (str/blank? |
| 429 | + (-> @(http-put client-path |
| 430 | + {:body (io/file "test/empty.txt") |
| 431 | + :pool client-pool}) |
| 432 | + :body |
| 433 | + bs/to-string))) |
| 434 | + (is (= (slurp "test/file.txt" :encoding "UTF-8") |
| 435 | + (-> @(http-put client-path |
| 436 | + {:body (io/file "test/file.txt") |
| 437 | + :pool client-pool}) |
| 438 | + :body |
| 439 | + bs/to-string)))))) |
434 | 440 |
|
435 | 441 | (defn ssl-session-capture-handler [ssl-session-atom]
|
436 | 442 | (fn [req]
|
437 | 443 | (reset! ssl-session-atom (http.core/ring-request-ssl-session req))
|
438 | 444 | {:status 200 :body "ok"}))
|
439 | 445 |
|
440 | 446 | (deftest test-ssl-session-access
|
441 |
| - (with-redefs [*use-tls?* true] |
442 |
| - (let [ssl-session (atom nil)] |
443 |
| - (with-http1-server |
444 |
| - (ssl-session-capture-handler ssl-session) |
445 |
| - http1-ssl-server-options |
446 |
| - (is (= 200 (:status @(http-get "/")))) |
447 |
| - (is (some? @ssl-session)) |
448 |
| - (when-let [^SSLSession s @ssl-session] |
449 |
| - (is (.isValid s)) |
450 |
| - (is (not (str/includes? "NULL" (.getCipherSuite s))))))))) |
| 447 | + (let [ssl-session (atom nil)] |
| 448 | + (with-http-ssl-servers (ssl-session-capture-handler ssl-session) {} |
| 449 | + (reset! ssl-session nil) |
| 450 | + (is (= 200 (:status @(http-get "/")))) |
| 451 | + (is (some? @ssl-session)) |
| 452 | + (when-let [^SSLSession s @ssl-session] |
| 453 | + (is (.isValid s)) |
| 454 | + (is (not (str/includes? "NULL" (.getCipherSuite s)))))))) |
451 | 455 |
|
452 | 456 | (deftest test-ssl-with-plain-client-request
|
453 |
| - (with-redefs [*use-tls?* false] ; intentionally wrong |
454 |
| - (let [ssl-session (atom nil)] |
455 |
| - (with-http1-server |
456 |
| - (ssl-session-capture-handler ssl-session) |
457 |
| - http1-ssl-server-options |
458 |
| - ;; Note the intentionally wrong "http" scheme here |
| 457 | + (let [ssl-session (atom nil)] |
| 458 | + (with-http-ssl-servers (ssl-session-capture-handler ssl-session) {} |
| 459 | + (reset! ssl-session nil) |
| 460 | + (with-redefs [*use-tls?* false] ; will make http-get use http instead of https |
459 | 461 | (is (some-> (http-get "/")
|
460 | 462 | (d/catch identity)
|
461 | 463 | deref
|
462 | 464 | ex-message
|
463 |
| - (str/includes? "connection was closed"))) |
464 |
| - (is (nil? @ssl-session)))))) |
| 465 | + (str/includes? "connection was closed")))) |
| 466 | + (is (nil? @ssl-session))))) |
465 | 467 |
|
466 | 468 | (deftest test-ssl-endpoint-identification
|
467 |
| - (with-redefs [*use-tls?* true] ; with-redefs for non-clj threads |
468 |
| - (binding [*connection-options* {:insecure? false |
469 |
| - :ssl-context test-ssl/wrong-hostname-client-ssl-context-opts}] |
470 |
| - (let [ssl-session (atom nil)] |
471 |
| - (with-http1-server |
| 469 | + (binding [*connection-options* {:insecure? false |
| 470 | + :ssl-context test-ssl/wrong-hostname-client-ssl-context-opts}] |
| 471 | + (let [ssl-session (atom nil)] |
| 472 | + (with-http-ssl-servers |
472 | 473 | (ssl-session-capture-handler ssl-session)
|
473 |
| - (assoc http-server-options :ssl-context test-ssl/wrong-hostname-server-ssl-context-opts) |
474 |
| - |
475 |
| - (try |
476 |
| - @(http-get "/") |
477 |
| - (is (= true false) "Should have thrown an exception") |
478 |
| - |
479 |
| - (catch Exception e |
480 |
| - (is (= SSLHandshakeException |
481 |
| - (class e))) |
482 |
| - |
483 |
| - ;; Should have a hostname mismatch cause in the ex chain |
484 |
| - (is (loop [^Exception ex e] |
485 |
| - (if ex |
486 |
| - (if (re-find #"(?i:No name matching localhost found)" |
487 |
| - (.getMessage ex)) |
488 |
| - true |
489 |
| - (recur (.getCause ex))) |
490 |
| - false)) |
491 |
| - "No hostname mismatch cause found in exception chain"))) |
492 |
| - (is (nil? @ssl-session))))))) |
| 474 | + {:ssl-context test-ssl/wrong-hostname-server-ssl-context-opts} |
| 475 | + (reset! ssl-session nil) |
| 476 | + (try |
| 477 | + @(http-get "/") |
| 478 | + (is (= true false) "Should have thrown an exception") |
| 479 | + |
| 480 | + (catch Exception e |
| 481 | + (is (= SSLHandshakeException |
| 482 | + (class e))) |
| 483 | + |
| 484 | + ;; Should have a hostname mismatch cause in the ex chain |
| 485 | + (is (loop [^Exception ex e] |
| 486 | + (if ex |
| 487 | + (if (re-find #"(?i:No name matching localhost found)" |
| 488 | + (.getMessage ex)) |
| 489 | + true |
| 490 | + (recur (.getCause ex))) |
| 491 | + false)) |
| 492 | + "No hostname mismatch cause found in exception chain"))) |
| 493 | + (is (nil? @ssl-session)))))) |
493 | 494 |
|
494 | 495 | (deftest test-disabling-ssl-endpoint-identification
|
495 |
| - (with-redefs [*use-tls?* true] ; with-redefs for non-clj threads |
496 |
| - (binding [*connection-options* {:insecure? false |
497 |
| - :ssl-context test-ssl/wrong-hostname-client-ssl-context-opts |
498 |
| - :ssl-endpoint-id-alg nil}] |
499 |
| - (let [ssl-session (atom nil)] |
500 |
| - (with-http1-server |
| 496 | + (binding [*connection-options* {:insecure? false |
| 497 | + :ssl-context test-ssl/wrong-hostname-client-ssl-context-opts |
| 498 | + :ssl-endpoint-id-alg nil}] |
| 499 | + (let [ssl-session (atom nil)] |
| 500 | + (with-http-ssl-servers |
501 | 501 | (ssl-session-capture-handler ssl-session)
|
502 |
| - (assoc http-server-options :ssl-context test-ssl/wrong-hostname-server-ssl-context-opts) |
503 |
| - |
504 |
| - (is (= 200 (:status @(http-get "/")))) |
505 |
| - (is (some? @ssl-session))))))) |
| 502 | + {:ssl-context test-ssl/wrong-hostname-server-ssl-context-opts} |
| 503 | + (reset! ssl-session nil) |
| 504 | + (is (= 200 (:status @(http-get "/")))) |
| 505 | + (is (some? @ssl-session)))))) |
506 | 506 |
|
507 | 507 | (deftest test-invalid-body
|
508 | 508 | (let [client-url "/"]
|
|
0 commit comments