|
2 | 2 | (:require
|
3 | 3 | [cheshire.core :as json]
|
4 | 4 | [expectations.clojure.test :refer [defexpect expect expecting side-effects]]
|
| 5 | + [ring.middleware.cookies :as cookies-middleware] |
| 6 | + [ring.mock.request :as mock] |
5 | 7 | [sentry-clj.core :as sentry]
|
6 | 8 | [sentry-clj.ring :as ring]
|
7 | 9 | [sentry-clj.tracing :as st])
|
|
28 | 30 |
|
29 | 31 | (defn ^:private wrapped
|
30 | 32 | [req]
|
31 |
| - (if (:ok req) "woo" (throw e))) |
| 33 | + (if (:ok req) "w00t" (throw e))) |
32 | 34 |
|
33 |
| -(def ^:private req |
| 35 | +(def ^:private hello-world-request |
34 | 36 | {:scheme :https
|
35 | 37 | :uri "/hello-world"
|
36 | 38 | :request-method :get
|
|
55 | 57 | (defexpect wrap-report-exceptions-test
|
56 | 58 | (expecting "passing through"
|
57 | 59 | (let [handler (ring/wrap-report-exceptions wrapped {})]
|
58 |
| - (expect "woo" (handler (assoc req :ok true)))))) |
| 60 | + (expect "w00t" (handler (assoc hello-world-request :ok true)))))) |
59 | 61 |
|
60 | 62 | (defexpect with-defaults-test
|
61 | 63 | (expecting "with defaults"
|
|
72 | 74 | (expect {:status 500
|
73 | 75 | :headers {"Content-Type" "text/html"}
|
74 | 76 | :body "<html><head><title>Error</title></head><body><p>Internal Server Error</p></body></html>"}
|
75 |
| - (handler req)) |
| 77 | + (handler hello-world-request)) |
76 | 78 | (expect {"breadcrumbs" (),
|
77 | 79 | "contexts" {},
|
78 | 80 | "request" {"data" {"one" 1},
|
|
98 | 100 | handler (ring/wrap-report-exceptions wrapped {:preprocess-fn preprocess
|
99 | 101 | :postprocess-fn postprocess
|
100 | 102 | :error-fn error})]
|
101 |
| - (expect (assoc req :exception e) (handler req)) |
| 103 | + (expect (assoc hello-world-request :exception e) (handler hello-world-request)) |
102 | 104 | (expect {"breadcrumbs" (),
|
103 | 105 | "contexts" {},
|
104 | 106 | "environment" "qa",
|
|
123 | 125 |
|
124 | 126 | (defexpect wrap-sentry-tracing-test
|
125 | 127 | (let [sentry-options (get-test-options {:traces-sample-rate 1.0 :debug true})
|
126 |
| - ok-req (assoc req :ok true)] |
| 128 | + ok-req (assoc hello-world-request :ok true)] |
127 | 129 | (Sentry/init ^SentryOptions sentry-options)
|
128 | 130 | (expecting "passing through"
|
129 | 131 | (let [handler (ring/wrap-sentry-tracing wrapped)]
|
130 |
| - (expect "woo" (handler ok-req)))) |
| 132 | + (expect "w00t" (handler ok-req)))) |
131 | 133 | (expecting "preprocess fn called"
|
132 | 134 | (let [handler (ring/wrap-sentry-tracing wrapped {:preprocess-fn preprocess})]
|
133 | 135 | (expect (fn [_transaction]
|
|
138 | 140 | (deliver p (.getData (.getRequest scope))))))
|
139 | 141 | @p)))
|
140 | 142 | (side-effects [st/finish-transaction!]
|
141 |
| - (expect "woo" (handler ok-req)))))))) |
| 143 | + (expect "w00t" (handler ok-req)))))))) |
| 144 | + |
| 145 | +(defexpect cookie-decoding-test |
| 146 | + (expecting "cookies are decoded properly" |
| 147 | + (let [event {:throwable e |
| 148 | + :request {:url "https://example.com/hello-world" |
| 149 | + :method nil |
| 150 | + :data {:one 1} |
| 151 | + :query-string "" |
| 152 | + :headers {"cookie" "sessionId=1234" "ok" 2 "host" "example.com"} |
| 153 | + :env {:session "{}" "REMOTE_ADDR" "127.0.0.1"}} |
| 154 | + :user {:ip_address "127.0.0.1"}} |
| 155 | + sentry-event (strip-timestamp (serialize event)) |
| 156 | + handler (-> (ring/wrap-report-exceptions wrapped {}) |
| 157 | + (cookies-middleware/wrap-cookies)) |
| 158 | + request (-> (assoc hello-world-request :ok false) |
| 159 | + (mock/cookie :sessionId "1234")) |
| 160 | + {:keys [status] :as response} (handler request)] |
| 161 | + (expect 500 status) |
| 162 | + (expect {"breadcrumbs" (), |
| 163 | + "contexts" {}, |
| 164 | + "request" {"cookies" "sessionId=1234", |
| 165 | + "data" {"one" 1}, |
| 166 | + "env" {"REMOTE_ADDR" "127.0.0.1", "session" "{}"}, |
| 167 | + "headers" {"cookie" "sessionId=1234", "host" "example.com", "ok" 2}, |
| 168 | + "query_string" "", |
| 169 | + "url" "https://example.com/hello-world"}, |
| 170 | + "sdk" {"version" "blah"}, |
| 171 | + "user" {}} sentry-event)))) |
0 commit comments