add coercion for numeric params
CI / Test + coverage (push) Successful in 17s

This commit is contained in:
2026-09-19 06:32:12 -09:00
parent 4e0ddb601d
commit 0e3ea5a714
3 changed files with 51 additions and 18 deletions
+19 -8
View File
@@ -1,16 +1,12 @@
(ns tbd.math.routes-test
(:require [clojure.edn :as edn]
[clojure.test :refer [deftest is testing]]
[reitit.ring :as ring]
[ring.middleware.params :refer [wrap-params]]
[tbd.math.routes :as math]))
;; wrap-params populates :query-params, as wrap-defaults does in tbd.core.
(def handler
(wrap-params (ring/ring-handler (ring/router (math/routes)))))
[muuntaja.core :as m]
[tbd.core :as core]))
;; Coercion/validation is configured on the router in tbd.core, so go through the real app.
(defn add [query]
(let [resp (handler {:request-method :get :uri "/api/add" :query-string query})]
(let [resp (core/app {:request-method :get :uri "/api/add" :query-string query})]
(-> resp
(update :body edn/read-string))))
@@ -27,4 +23,19 @@
(doseq [query [nil "x=2" "y=3" "x=abc&y=3" "x=2&y=abc"]]
(let [resp (add query)]
(is (= 400 (:status resp)) (str "query: " query))
(is (= "application/edn; charset=utf-8" (get-in resp [:headers "Content-Type"])))
(is (contains? (:body resp) :error))))))
(deftest add-route-swagger-test
(let [spec (-> (core/app {:request-method :get :uri "/swagger.json"
:headers {"accept" "application/json"}})
:body
(->> (m/decode "application/json")))
params (->> (:paths spec)
(some (fn [[path v]] (when (= "/api/add" (str (namespace path) "/" (name path))) v)))
:get
:parameters)]
(testing "x and y are documented as required numeric query params"
(is (= [{:name "x" :in "query" :type "number" :required true}
{:name "y" :in "query" :type "number" :required true}]
(map #(select-keys % [:name :in :type :required]) params))))))