<commit_before>(ns puppetlabs.trapperkeeper.services.webserver.jetty9-service-handlers-test
  (:import (servlet SimpleServlet)
           (javax.servlet ServletContextListener))
  (:require [clojure.test :refer :all]
            [puppetlabs.trapperkeeper.services.webserver.jetty9-service :refer :all]
            [puppetlabs.trapperkeeper.testutils.webserver.common :refer :all]
            [puppetlabs.trapperkeeper.app :refer [get-service]]
            [puppetlabs.trapperkeeper.testutils.bootstrap :refer [with-app-with-config]]
            [puppetlabs.trapperkeeper.testutils.logging
             :refer [with-test-logging]]))

(def dev-resources-dir        "./dev-resources/")

(deftest static-content-test
  (testing "static content context"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s                   (get-service app :WebserverService)
            add-context-handler (partial add-context-handler s)
            path                "/resources"
            resource            "logback.xml"]
        (add-context-handler dev-resources-dir path)
        (let [response (http-get (str "http://localhost:8080" path "/" resource))]
          (is (= (:status response) 200))
          (is (= (:body response) (slurp (str dev-resources-dir resource))))))))

  (testing "customization of static content context"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s                   (get-service app :WebserverService)
            add-context-handler (partial add-context-handler s)
            path                "/resources"
            body                "Hey there"
            servlet-path        "/hey"
            servlet             (SimpleServlet. body)]
        (add-context-handler dev-resources-dir path
                             [(reify ServletContextListener
                                (contextInitialized [this event]
                                  (doto (.addServlet (.getServletContext event) "simple" servlet)
                                    (.addMapping (into-array [servlet-path]))))
                                (contextDestroyed [this event]))])
        (let [response (http-get (str "http://localhost:8080" path servlet-path))]
          (is (= (:status response) 200))
          (is (= (:body response) body)))))))


(deftest servlet-test
  (testing "request to servlet over http succeeds"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s                   (get-service app :WebserverService)
            add-servlet-handler (partial add-servlet-handler s)
            body                "Hey there"
            path                "/hey"
            servlet             (SimpleServlet. body)]
        (add-servlet-handler servlet path)
        (let [response (http-get
                         (str "http://localhost:8080" path))]
          (is (= (:status response) 200))
          (is (= (:body response) body))))))

  (testing "request to servlet initialized with empty param succeeds"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s                   (get-service app :WebserverService)
            add-servlet-handler (partial add-servlet-handler s)
            body                "Hey there"
            path                "/hey"
            servlet             (SimpleServlet. body)]
        (add-servlet-handler servlet path {})
        (let [response (http-get (str "http://localhost:8080" path))]
          (is (= (:status response) 200))
          (is (= (:body response) body))))))

  (testing "request to servlet initialized with non-empty params succeeds"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s                   (get-service app :WebserverService)
            add-servlet-handler (partial add-servlet-handler s)
            body                "Hey there"
            path                "/hey"
            init-param-one      "value of init param one"
            init-param-two      "value of init param two"
            servlet             (SimpleServlet. body)]
        (add-servlet-handler servlet
                             path
                             {"init-param-one" init-param-one
                              "init-param-two" init-param-two})
        (let [response (http-get
                         (str "http://localhost:8080" path "/init-param-one"))]
          (is (= (:status response) 200))
          (is (= (:body response) init-param-one)))
        (let [response (http-get
                         (str "http://localhost:8080" path "/init-param-two"))]
          (is (= (:status response) 200))
          (is (= (:body response) init-param-two)))))))

(deftest war-test
  (testing "WAR support"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s               (get-service app :WebserverService)
            add-war-handler (partial add-war-handler s)
            path            "/test"
            war             "helloWorld.war"]
        (add-war-handler (str dev-resources-dir war) path)
        (let [response (http-get (str "http://localhost:8080" path "/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
  (testing "Retrieve all endpoints"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s                        (get-service app :WebserverService)
            path-context             "/ernie"
            path-context2            "/gonzo"
            path-ring                "/bert"
            path-servlet             "/foo"
            path-servlet2            "/misspiggy"
            path-war                 "/bar"
            path-proxy               "/baz"
            get-registered-endpoints (partial get-registered-endpoints s)
            add-context-handler      (partial add-context-handler s)
            add-ring-handler         (partial add-ring-handler s)
            add-servlet-handler      (partial add-servlet-handler s)
            add-war-handler          (partial add-war-handler s)
            add-proxy-route          (partial add-proxy-route s)
            ring-handler             (fn [req] {:status 200 :body "Hi world"})
            body                     "This is a test"
            servlet                  (SimpleServlet. body)
            war                      "helloWorld.war"
            target                   {:host "0.0.0.0"
                                      :port 9000
                                      :path "/ernie"}
            target2                  {:host "localhost"
                                      :port 10000
                                      :path "/kermit"}]
        (add-context-handler dev-resources-dir path-context)
        (add-context-handler dev-resources-dir path-context2 [])
        (add-ring-handler ring-handler path-ring)
        (add-servlet-handler servlet path-servlet)
        (add-servlet-handler servlet path-servlet2 {})
        (add-war-handler (str dev-resources-dir war) path-war)
        (add-proxy-route target path-proxy)
        (add-proxy-route target2 path-proxy {})
        (let [endpoints (get-registered-endpoints)]
          (is (= endpoints #{{:type :context :base-path dev-resources-dir
                              :endpoint path-context}
                             {:type :context :base-path dev-resources-dir
                              :context-listeners [] :endpoint path-context2}<commit_msg>(TK-10) Add context-listener test case<commit_after>(ns puppetlabs.trapperkeeper.services.webserver.jetty9-service-handlers-test
  (:import (servlet SimpleServlet)
           (javax.servlet ServletContextListener))
  (:require [clojure.test :refer :all]
            [puppetlabs.trapperkeeper.services.webserver.jetty9-service :refer :all]
            [puppetlabs.trapperkeeper.testutils.webserver.common :refer :all]
            [puppetlabs.trapperkeeper.app :refer [get-service]]
            [puppetlabs.trapperkeeper.testutils.bootstrap :refer [with-app-with-config]]
            [puppetlabs.trapperkeeper.testutils.logging
             :refer [with-test-logging]]))

(def dev-resources-dir        "./dev-resources/")

(deftest static-content-test
  (testing "static content context"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s                   (get-service app :WebserverService)
            add-context-handler (partial add-context-handler s)
            path                "/resources"
            resource            "logback.xml"]
        (add-context-handler dev-resources-dir path)
        (let [response (http-get (str "http://localhost:8080" path "/" resource))]
          (is (= (:status response) 200))
          (is (= (:body response) (slurp (str dev-resources-dir resource))))))))

  (testing "customization of static content context"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s                   (get-service app :WebserverService)
            add-context-handler (partial add-context-handler s)
            path                "/resources"
            body                "Hey there"
            servlet-path        "/hey"
            servlet             (SimpleServlet. body)]
        (add-context-handler dev-resources-dir path
                             [(reify ServletContextListener
                                (contextInitialized [this event]
                                  (doto (.addServlet (.getServletContext event) "simple" servlet)
                                    (.addMapping (into-array [servlet-path]))))
                                (contextDestroyed [this event]))])
        (let [response (http-get (str "http://localhost:8080" path servlet-path))]
          (is (= (:status response) 200))
          (is (= (:body response) body)))))))


(deftest servlet-test
  (testing "request to servlet over http succeeds"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s                   (get-service app :WebserverService)
            add-servlet-handler (partial add-servlet-handler s)
            body                "Hey there"
            path                "/hey"
            servlet             (SimpleServlet. body)]
        (add-servlet-handler servlet path)
        (let [response (http-get
                         (str "http://localhost:8080" path))]
          (is (= (:status response) 200))
          (is (= (:body response) body))))))

  (testing "request to servlet initialized with empty param succeeds"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s                   (get-service app :WebserverService)
            add-servlet-handler (partial add-servlet-handler s)
            body                "Hey there"
            path                "/hey"
            servlet             (SimpleServlet. body)]
        (add-servlet-handler servlet path {})
        (let [response (http-get (str "http://localhost:8080" path))]
          (is (= (:status response) 200))
          (is (= (:body response) body))))))

  (testing "request to servlet initialized with non-empty params succeeds"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s                   (get-service app :WebserverService)
            add-servlet-handler (partial add-servlet-handler s)
            body                "Hey there"
            path                "/hey"
            init-param-one      "value of init param one"
            init-param-two      "value of init param two"
            servlet             (SimpleServlet. body)]
        (add-servlet-handler servlet
                             path
                             {"init-param-one" init-param-one
                              "init-param-two" init-param-two})
        (let [response (http-get
                         (str "http://localhost:8080" path "/init-param-one"))]
          (is (= (:status response) 200))
          (is (= (:body response) init-param-one)))
        (let [response (http-get
                         (str "http://localhost:8080" path "/init-param-two"))]
          (is (= (:status response) 200))
          (is (= (:body response) init-param-two)))))))

(deftest war-test
  (testing "WAR support"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s               (get-service app :WebserverService)
            add-war-handler (partial add-war-handler s)
            path            "/test"
            war             "helloWorld.war"]
        (add-war-handler (str dev-resources-dir war) path)
        (let [response (http-get (str "http://localhost:8080" path "/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
  (testing "Retrieve all endpoints"
    (with-app-with-config app
      [jetty9-service]
      jetty-plaintext-config
      (let [s                        (get-service app :WebserverService)
            path-context             "/ernie"
            path-context2            "/gonzo"
            path-context3            "/goblinking"
            path-ring                "/bert"
            path-servlet             "/foo"
            path-servlet2            "/misspiggy"
            path-war                 "/bar"
            path-proxy               "/baz"
            get-registered-endpoints (partial get-registered-endpoints s)
            add-context-handler      (partial add-context-handler s)
            add-ring-handler         (partial add-ring-handler s)
            add-servlet-handler      (partial add-servlet-handler s)
            add-war-handler          (partial add-war-handler s)
            add-proxy-route          (partial add-proxy-route s)
            ring-handler             (fn [req] {:status 200 :body "Hi world"})
            body                     "This is a test"
            servlet                  (SimpleServlet. body)
            context-listeners        [(reify ServletContextListener
                                        (contextInitialized [this event]
                                          (doto (.addServlet (.getServletContext event) "simple" servlet)
                                            (.addMapping (into-array [path-servlet]))))
                                        (contextDestroyed [this event]))]
            war                      "helloWorld.war"
            target                   {:host "0.0.0.0"
                                      :port 9000
                                      :path "/ernie"}
            target2                  {:host "localhost"
                                      :port 10000
                                      :path "/kermit"}]
        (add-context-handler dev-resources-dir path-context)
        (add-context-handler dev-resources-dir path-context2 [])
        (add-context-handler dev-resources-dir path-context3 context-listeners)
        (add-ring-handler ring-handler path-ring)
        (add-servlet-handler servlet path-servlet)
        (add-servlet-handler servlet path-servlet2 {})
        (add-war-handler (str dev-resources-dir war) path-war)
        (add-proxy-route target path-proxy)
        (add-proxy-route target2 path-proxy {})
        (let [endpoints (get-registered-endpoints)]
          (is (= endpoints #{{:type :context :base-path dev-resources-dir
                              :endpoint path-context}
                             {:type :context :base-path dev-resources-dir
                              :context-listeners [] :endpoint path-context2}
                             {:type :context :base-path dev-resources-dir
                              :context-listeners context-listeners :endpoint path-context3}
{'fraction_non_alphanumeric': 0.1132482584304297, 'fraction_numerical': 0.010295296220948153, 'mean_word_length': 1.4789119804400979, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '834315', 'n_tokens_mistral': 4454, 'n_tokens_neox': 4168, 'n_words': 1003}