1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
(ns puppetlabs.trapperkeeper.services.webrouting.webrouting-service-handlers-test
(:import (servlet SimpleServlet))
(:require [clojure.test :refer :all]
[schema.test :as schema-test]
[puppetlabs.trapperkeeper.services :as tk-services]
[puppetlabs.trapperkeeper.services.webrouting.webrouting-service :refer :all]
[puppetlabs.trapperkeeper.services.webserver.jetty9-service :refer [jetty9-service]]
[puppetlabs.trapperkeeper.app :refer [get-service]]
[puppetlabs.trapperkeeper.testutils.webrouting.common :refer :all]
[puppetlabs.trapperkeeper.testutils.bootstrap :refer [with-app-with-config]]
[puppetlabs.trapperkeeper.testutils.logging
:refer [with-test-logging]]
[puppetlabs.trapperkeeper.testutils.webserver :as testutils]))
(use-fixtures :once
schema-test/validate-schemas
testutils/assert-clean-shutdown)
(def dev-resources-dir "./dev-resources/")
(defprotocol TestDummy
(dummy [this]))
(tk-services/defservice test-dummy
TestDummy
[]
(dummy [this]
"This is a dummy function. Please ignore."))
(def webrouting-plaintext-multiserver-config
{:webserver {:bar {:port 8080
:default-server true}
:foo {:port 9000}}
:web-router-service
{:puppetlabs.trapperkeeper.services.webrouting.webrouting-service-handlers-test/test-dummy
{:route "/foo"
:server "foo"}}})
(def webrouting-plaintext-multiroute-config
{:webserver {:port 8080}
:web-router-service
{:puppetlabs.trapperkeeper.services.webrouting.webrouting-service-handlers-test/test-dummy
{:quux "/foo"
:foo "/bar"}}})
(deftest add-context-handler-test
(testing "static content context with web routing"
(with-app-with-config app
[jetty9-service
webrouting-service
test-dummy]
webrouting-plaintext-config
(let [s (get-service app :WebroutingService)
add-context-handler (partial add-context-handler s)
resource "logback.xml"
svc (get-service app :TestDummy)]
(add-context-handler svc dev-resources-dir)
(let [response (http-get (str "http://localhost:8080/foo/" resource))]
(is (= (:status response) 200))
(is (= (:body response) (slurp (str dev-resources-dir resource))))))))
(testing "static content context with multiple routes"
(with-app-with-config app
[jetty9-service
webrouting-service
test-dummy]
webrouting-plaintext-multiroute-config
(let [s (get-service app :WebroutingService)
add-context-handler (partial add-context-handler s)
resource "logback.xml"
svc (get-service app :TestDummy)]
(add-context-handler svc dev-resources-dir {:route-id :quux})
(add-context-handler svc dev-resources-dir {:route-id :foo})
(let [response (http-get (str "http://localhost:8080/foo/" resource))]
(is (= (:status response) 200))
(is (= (:body response) (slurp (str dev-resources-dir resource)))))
(let [response (http-get (str "http://localhost:8080/bar/" resource))]
(is (= (:status response) 200))
(is (= (:body response) (slurp (str dev-resources-dir resource)))))))))
(deftest ring-handler-test-web-routing
(testing "ring request over http succeeds with web-routing"
(with-app-with-config app
[jetty9-service
webrouting-service
test-dummy]
webrouting-plaintext-config
(let [s (get-service app :WebroutingService)
add-ring-handler (partial add-ring-handler s)
body "Hi World"
ring-handler (fn [req] {:status 200 :body body})
svc (get-service app :TestDummy)]
(add-ring-handler svc ring-handler)
(let [response (http-get "http://localhost:8080/foo")]
(is (= (:status response) 200))
(is (= (:body response) body))))))
(testing "ring request over http succeeds with multiple web-routes"
(with-app-with-config app
[jetty9-service
webrouting-service
test-dummy]
webrouting-plaintext-multiroute-config
(let [s (get-service app :WebroutingService)
add-ring-handler (partial add-ring-handler s)
body "Hi World"
ring-handler (fn [req] {:status 200 :body body})
svc (get-service app :TestDummy)]
(add-ring-handler svc ring-handler {:route-id :quux})
(add-ring-handler svc ring-handler {:route-id :foo})
(let [response (http-get "http://localhost:8080/foo")]
(is (= (:status response) 200))
(is (= (:body response) body)))
(let [response (http-get "http://localhost:8080/bar")]
(is (= (:status response) 200))
(is (= (:body response) body)))))))
(deftest servlet-test-web-routing
(testing "request to servlet over http succeeds with web routing"
(with-app-with-config app
[jetty9-service
webrouting-service
test-dummy]
webrouting-plaintext-config
(let [s (get-service app :WebroutingService)
add-servlet-handler (partial add-servlet-handler s)
body "Hey there"
servlet (SimpleServlet. body)
svc (get-service app :TestDummy)]
(add-servlet-handler svc servlet)
(let [response (http-get "http://localhost:8080/foo")]
(is (= (:status response) 200))
(is (= (:body response) body))))))
(testing "request to servlet over http succeeds with multiple web routes"
(with-app-with-config app
[jetty9-service
webrouting-service
test-dummy]
webrouting-plaintext-multiroute-config
(let [s (get-service app :WebroutingService)
add-servlet-handler (partial add-servlet-handler s)
body "Hey there"
servlet (SimpleServlet. body)
svc (get-service app :TestDummy)]
(add-servlet-handler svc servlet {:route-id :quux})
(add-servlet-handler svc servlet {:route-id :foo})
(let [response (http-get "http://localhost:8080/foo")]
(is (= (:status response) 200))
(is (= (:body response) body)))
(let [response (http-get "http://localhost:8080/bar")]
(is (= (:status response) 200))
(is (= (:body response) body)))))))
(deftest war-test-web-routing
(testing "WAR support with web routing"
(with-app-with-config app
[jetty9-service
webrouting-service
test-dummy]
webrouting-plaintext-config
(let [s (get-service app :WebroutingService)
add-war-handler (partial add-war-handler s)
war "helloWorld.war"
svc (get-service app :TestDummy)]
(add-war-handler svc (str dev-resources-dir war))
(let [response (http-get "http://localhost:8080/foo/hello")]
(is (= (:status response) 200))
(is (= (:body response)
"<html>\n<head><title>Hello World Servlet</title></head>\n<body>Hello World!!</body>\n</html>\n"))))))
(testing "WAR support with multiple web routes"
(with-app-with-config app
[jetty9-service
webrouting-service
test-dummy]
webrouting-plaintext-multiroute-config
(let [s (get-service app :WebroutingService)
add-war-handler (partial add-war-handler s)
war "helloWorld.war"
svc (get-service app :TestDummy)]
(add-war-handler svc (str dev-resources-dir war) {:route-id :quux})
(add-war-handler svc (str dev-resources-dir war) {:route-id :foo})
(let [response (http-get "http://localhost:8080/foo/hello")]
(is (= (:status response) 200))
(is (= (:body response)
"<html>\n<head><title>Hello World Servlet</title></head>\n<body>Hello World!!</body>\n</html>\n")))
(let [response (http-get "http://localhost:8080/bar/hello")]
(is (= (:status response) 200))
(is (= (:body response)
"<html>\n<head><title>Hello World Servlet</title></head>\n<body>Hello World!!</body>\n</html>\n")))))))
(deftest endpoints-test-web-routing
(testing (str "get-registered-endpoints and log-registered-endpoints are "
"successful with the web-routing service")
(with-test-logging
(with-app-with-config app
[jetty9-service
webrouting-service
test-dummy]
webrouting-plaintext-config
(let [s (get-service app :WebroutingService)
get-registered-endpoints (partial get-registered-endpoints s)
log-registered-endpoints (partial log-registered-endpoints s)
add-ring-handler (partial add-ring-handler s)
ring-handler (fn [req] {:status 200 :body "Hi world"})
svc (get-service app :TestDummy)]
(add-ring-handler svc ring-handler)
(let [endpoints (get-registered-endpoints)]
(is (= endpoints {"/foo" [{:type :ring}]})))
(log-registered-endpoints)
(is (logged? #"^\{\"\/foo\" \[\{:type :ring}\]\}$"))
(is (logged? #"^\{\"\/foo\" \[\{:type :ring}\]\}$" :info)))))))
|