From 8923ee9ef79a41198b4348c29ebdd6f0ccee4dcd Mon Sep 17 00:00:00 2001 From: Richard Harrington Date: Sun, 12 Oct 2014 11:29:02 -0400 Subject: [PATCH] standardizes routing; adds check for request method --- src/robotwar/handler.clj | 50 +++++++++++++++++++--------------- test/robotwar/handler_test.clj | 4 +++ 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/robotwar/handler.clj b/src/robotwar/handler.clj index b6d54ea..a43e3a8 100644 --- a/src/robotwar/handler.clj +++ b/src/robotwar/handler.clj @@ -29,7 +29,7 @@ (take 5))) (defn add-game - "a function to update the games-store atom state. + "a function to update the games-store atom state. It keeps a running store of games, which is added to upon request from a browser, who is then assigned an id number" @@ -54,28 +54,34 @@ (def games-store (atom {:next-id 0 :games {}})) +(defn consistently-typed-re-matches [re s] + (when-let [match (re-matches re s)] + (if (string? match) [match] match))) -(defn handler [request] - - (let [match (re-matches #"\/worlds\/(\d+)\/(\d+)" (request :uri))] - (if match - (let [[_ id n] match] - (response (take-drop-send - games-store - (Integer/parseInt id) - (Integer/parseInt n)))) - - (case (request :uri) - "/program-names" (response - {:names (map name (keys source-programs/programs))}) - - "/init" (let [programs ((request :query-params) "programs") - next-id (:next-id @games-store)] - (swap! games-store add-game programs) - (response {:id next-id - :game-info game-info})) - - (not-found "Not Found"))))) +(defn handler [{:keys [uri query-params request-method] :as request}] + (let [route (fn [acceptable-request-method re action] + (when (= request-method acceptable-request-method) + (when-let [[_ & url-params] (consistently-typed-re-matches re uri)] + (apply action url-params))))] + (or + (route :get #"\/worlds\/(\d+)\/(\d+)" + (fn [id n] + (response (take-drop-send + games-store + (Integer/parseInt id) + (Integer/parseInt n))))) + (route :get #"\/program-names" + (fn [] + (response + {:names (map name (keys source-programs/programs))}))) + (route :get #"\/init" + (fn [] + (let [programs (query-params "programs") + next-id (:next-id @games-store)] + (swap! games-store add-game programs) + (response {:id next-id + :game-info game-info})))) + (not-found "Not Found")))) (def app (-> handler diff --git a/test/robotwar/handler_test.clj b/test/robotwar/handler_test.clj index f287db5..3e84777 100644 --- a/test/robotwar/handler_test.clj +++ b/test/robotwar/handler_test.clj @@ -14,6 +14,10 @@ (let [response (app (mock/request :get "/invalid"))] (is (= (:status response) 404)))) + (testing "unsupported http request method" + (let [response (app (mock/request :put "/program-names"))] + (is (= (:status response) 404)))) + (testing "files" (let [response (app (mock/request :get "/index.html"))] (is (= (:status response) 200))