Permalink
Browse files

Added start-server fn

  • Loading branch information...
1 parent 2b07305 commit 53f79ecf2e98560a95a3b3f1a982198f66d0604c @andrewvc committed Sep 9, 2012
Showing with 32 additions and 4 deletions.
  1. +13 −2 src/noir_async/core.clj
  2. +19 −2 test/noir_async/test/core.clj
View
15 src/noir_async/core.clj
@@ -3,7 +3,8 @@
(require [lamina.core :as lc]
[aleph.http :as ah]
[aleph.formats :as af]
- [noir.core :as nc]))
+ [noir.core :as nc]
+ [noir.server :as n-srv]))
(defn- format-one-shot
"Format responses for a one shot response."
@@ -147,4 +148,14 @@
[route request-bindings conn-binding & body]
`(nc/custom-handler ~route {~request-bindings :params}
(ah/wrap-aleph-handler
- (defaleph-handler ~conn-binding ~@body))))
+ (defaleph-handler ~conn-binding ~@body))))
+
+(defn start-server
+ "Starts a new noir/aleph server in the given mode and port. If the views arg is passed in, it will load views from that path via noir-server/load-views"
+ ([mode port]
+ (start-server mode port nil))
+ ([mode port views]
+ (when views (n-srv/load-views views))
+ (ah/start-http-server
+ (ah/wrap-ring-handler (n-srv/gen-handler {:mode mode}))
+ {:port port :websocket true})))
View
21 test/noir_async/test/core.clj
@@ -4,7 +4,9 @@
lamina.core
clojure.test)
(:require [noir-async.core :as na]
- [noir.server :as nr-server] ))
+ [noir.server :as nr-server]
+ [aleph.http :as http])
+ (:import java.net.ConnectException))
(def good-response-str "ohai!")
(def good-response-map {:status 200 :body good-response-str})
@@ -160,4 +162,19 @@
(test-message-receipt results 2 "first-sent-message"))
(testing "server-side closes"
(na/close-connection c)
- (is (closed? (:request-channel c)))))))
+ (is (closed? (:request-channel c)))))))
+
+(deftest server-start
+ (testing "Server start"
+ (let [s-port 15882
+ srv (na/start-server :prod s-port)
+ resp @(http/http-request
+ {:url (str "http://localhost:" s-port) :method :get})]
+ (testing "request status"
+ (is (= 404 (:status resp))))
+ (testing "stoping the server"
+ (is (= :lamina/realized @(srv))) ; Not sure of this stopping it... hence the next test
+ (is (= java.net.ConnectException
+ (try
+ @(http/http-request {:timeout 300 :url (str "http://localhost:" s-port) :method :get})
+ (catch Exception e (class e)))))))))

0 comments on commit 53f79ec

Please sign in to comment.