From 20584af875402a494272acd142770beaccb5ade9 Mon Sep 17 00:00:00 2001 From: James Reeves Date: Sun, 5 Nov 2023 21:18:15 +0000 Subject: [PATCH] Add tests for :on-ping key in listener map --- ring-core/test/ring/test/websocket.clj | 47 +++++++++++++++++--------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/ring-core/test/ring/test/websocket.clj b/ring-core/test/ring/test/websocket.clj index 48f73c6e..7a0fc01c 100644 --- a/ring-core/test/ring/test/websocket.clj +++ b/ring-core/test/ring/test/websocket.clj @@ -1,5 +1,5 @@ (ns ring.test.websocket - (:require [clojure.test :refer [deftest is]] + (:require [clojure.test :refer [deftest is testing]] [ring.websocket :as ws] [ring.websocket.protocols :as wsp])) @@ -12,18 +12,33 @@ {:headers {"sec-websocket-protocol" "mqtt, soap"}})))) (deftest test-map-listeners - (let [listener {:on-open (fn [s] [:on-open s]) - :on-message (fn [s m] [:on-message s m]) - :on-pong (fn [s d] [:on-pong s d]) - :on-error (fn [s e] [:on-error s e]) - :on-close (fn [s c r] [:on-close s c r])}] - (is (= [:on-open :sock] - (wsp/on-open listener :sock))) - (is (= [:on-message :sock "foo"] - (wsp/on-message listener :sock "foo"))) - (is (= [:on-pong :sock "data"] - (wsp/on-pong listener :sock "data"))) - (is (= [:on-error :sock "err"] - (wsp/on-error listener :sock "err"))) - (is (= [:on-close :sock 1000 "closed"] - (wsp/on-close listener :sock 1000 "closed"))))) + (testing "listener methods" + (let [listener {:on-open (fn [s] [:on-open s]) + :on-message (fn [s m] [:on-message s m]) + :on-pong (fn [s d] [:on-pong s d]) + :on-error (fn [s e] [:on-error s e]) + :on-close (fn [s c r] [:on-close s c r])}] + (is (= [:on-open :sock] + (wsp/on-open listener :sock))) + (is (= [:on-message :sock "foo"] + (wsp/on-message listener :sock "foo"))) + (is (= [:on-pong :sock "data"] + (wsp/on-pong listener :sock "data"))) + (is (= [:on-error :sock "err"] + (wsp/on-error listener :sock "err"))) + (is (= [:on-close :sock 1000 "closed"] + (wsp/on-close listener :sock 1000 "closed"))))) + (testing "default on-ping" + (let [pong? (promise) + sock (reify wsp/Socket + (-pong [_ _] (deliver pong? true)))] + (wsp/on-ping {} sock :data) + (is (true? (deref pong? 1 false))))) + (testing "custom on-ping" + (let [pong? (promise) + listener {:on-ping (fn [s d] [:on-ping s d])} + sock (reify wsp/Socket + (-pong [_ _] (deliver pong? true)))] + (is (= [:on-ping sock :data] + (wsp/on-ping listener sock :data))) + (is (false? (deref pong? 1 false))))))