Merge pull request #14 from Medeah/master

Remove dependency on Compojure
This commit is contained in:
Richard Harrington 2014-10-11 16:43:27 -04:00
commit 47f922512f
3 changed files with 67 additions and 24 deletions

View File

@ -6,8 +6,8 @@
:dependencies [[org.clojure/clojure "1.5.1"]
[org.clojure/core.match "0.2.0-rc5"]
[clj-time "0.5.1"]
[ring/ring-json "0.2.0"]
[compojure "1.1.5"]]
[ring/ring-json "0.3.1"]
[ring/ring-core "1.3.1"]]
:plugins [[lein-ring "0.8.5"]]
:ring {:handler robotwar.handler/app}
:profiles {:dev {:dependencies [[midje "1.5.1"]

View File

@ -1,11 +1,10 @@
(ns robotwar.handler
(:use [compojure.core]
[clojure.string :only [split]]
(:use [clojure.string :only [split]]
[robotwar.constants])
(:require [compojure.handler :as handler]
[ring.middleware.json :as middleware]
[ring.util.response :as response]
[compojure.route :as route]
(:require [ring.middleware.json :refer [wrap-json-response wrap-json-body]]
[ring.middleware.file :refer [wrap-file]]
[ring.middleware.params :refer [wrap-params]]
[ring.util.response :refer [response not-found]]
[robotwar.source-programs :as source-programs]
[robotwar.world :as world]
[robotwar.browser :as browser]))
@ -54,21 +53,32 @@
(def games-store (atom {:next-id 0
:games {}}))
(defroutes app-routes
(GET "/program-names" [] (response/response
{:names (map name (keys source-programs/programs))}))
(GET "/init" [programs] (let [next-id (:next-id @games-store)]
(swap! games-store add-game programs)
(response/response {:id next-id
:game-info game-info})))
(GET "/worlds/:id/:n" [id n] (response/response (take-drop-send
games-store
(Integer/parseInt id)
(Integer/parseInt n))))
(route/files "/")
(route/not-found "Not Found"))
(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")))))
(def app
(-> (handler/site app-routes)
(middleware/wrap-json-response)
(middleware/wrap-json-body)))
(-> handler
(wrap-file "public")
(wrap-json-response)
(wrap-json-body)
(wrap-params)))

View File

@ -0,0 +1,33 @@
(ns robotwar.handler-test
(:require [clojure.test :refer :all]
[robotwar.handler :refer :all]
[ring.mock.request :as mock]))
(deftest app-handler-test
(testing "program-names"
(let [response (app (mock/request :get "/program-names"))]
(is (= (:status response) 200))
(is (.contains (:body response) "mover"))))
(testing "not-found route"
(let [response (app (mock/request :get "/invalid"))]
(is (= (:status response) 404))))
(testing "files"
(let [response (app (mock/request :get "/index.html"))]
(is (= (:status response) 200))
(is (.contains (slurp (:body response)) "Welcome to the future")))
(let [response (app (mock/request :get "/js/main.js"))]
(is (= (:status response) 200))
(is (.contains (slurp (:body response)) "function"))))
(testing "worlds route"
(let [response (app (mock/request :get "/worlds/0/99"))]
(is (= (:status response) 200))
(is (.contains (:body response) "["))))
(testing "init route"
(let [response (app (mock/request :get "/init?programs=mover"))]
(is (= (:status response) 200))
(is (.contains (:body response) "game-info")))))