<commit_before>(ns ring.util.servlet-test
  (:use clojure.test
        ring.util.servlet))

(defn- enumeration [coll]
  (let [e (atom coll)]
    (proxy [java.util.Enumeration] []
      (hasMoreElements [] (not (empty? @e)))
      (nextElement [] (let [f (first @e)] (swap! e rest) f)))))

(defn- servlet-request [request]
  (proxy [javax.servlet.http.HttpServletRequest] []
    (getServerPort [] (request :server-port))
    (getServerName [] (request :server-name))
    (getRemoteAddr [] (request :remote-addr))
    (getRequestURI [] (request :uri))
    (getQueryString [] (request :query-string))
    (getScheme [] (str (request :scheme)))
    (getMethod [] (-> request :request-method str .toUpperCase))
    (getHeaderNames [] (enumeration (keys (request :headers))))
    (getHeader [name] (get-in request [:headers name]))
    (getContentType [] (request :content-type))
    (getContentLength [] (or (request :content-length) -1))
    (getCharacterEncoding [] (request :character-encoding))
    (getInputStream [] (request :body))))

(defn- servlet-response [response]
  (proxy [javax.servlet.http.HttpServletResponse] []
    (setStatus [status]
      (swap! response assoc :status status))
    (setHeader [name value]
      (swap! response assoc-in [:headers name] value))
    (setCharacterEncoding [value]
      (swap! response assoc-in [:headers "Content-Type"] value))))

(defn- servlet-config []
  (proxy [javax.servlet.ServletConfig] []
    (getServletContext [] nil)))

(deftest servlet-test
  (testing "request"
    (let [req  {:server-port    8080
                :server-name    "foobar"
                :remote-addr    "127.0.0.1"
                :uri            "/foo"
                :query-string   "a=b"
                :scheme         :http
                :request-method :get
                :headers        {"X-Server" "Foo"}
                :content-type   "text/plain"
                :content-length 10
                :character-encoding "UTF-8"
                :body           nil}
          resp (atom {})
          svlt (servlet (fn [r]
                          (is (= (:uri r) (:uri req)))
                          {:status 200, :headers {}}))]
      (doto svlt
        (.init (servlet-config))
        (.service (servlet-request req)
                  (servlet-response resp))))))
<commit_msg>Fleshed out first servlet unit test<commit_after>(ns ring.util.servlet-test
  (:use clojure.test
        ring.util.servlet))

(defn- enumeration [coll]
  (let [e (atom coll)]
    (proxy [java.util.Enumeration] []
      (hasMoreElements [] (not (empty? @e)))
      (nextElement [] (let [f (first @e)] (swap! e rest) f)))))

(defn- servlet-request [request]
  (proxy [javax.servlet.http.HttpServletRequest] []
    (getServerPort [] (request :server-port))
    (getServerName [] (request :server-name))
    (getRemoteAddr [] (request :remote-addr))
    (getRequestURI [] (request :uri))
    (getQueryString [] (request :query-string))
    (getScheme [] (name (request :scheme)))
    (getMethod [] (-> request :request-method name .toUpperCase))
    (getHeaderNames [] (enumeration (keys (request :headers))))
    (getHeader [name] (get-in request [:headers name]))
    (getContentType [] (request :content-type))
    (getContentLength [] (or (request :content-length) -1))
    (getCharacterEncoding [] (request :character-encoding))
    (getInputStream [] (request :body))))

(defn- servlet-response [response]
  (proxy [javax.servlet.http.HttpServletResponse] []
    (setStatus [status]
      (swap! response assoc :status status))
    (setHeader [name value]
      (swap! response assoc-in [:headers name] value))
    (setCharacterEncoding [value]
      (swap! response assoc-in [:headers "Content-Type"] value))))

(defn- servlet-config []
  (proxy [javax.servlet.ServletConfig] []
    (getServletContext [] nil)))

(deftest servlet-test
  (testing "request"
    (let [req  {:server-port    8080
                :server-name    "foobar"
                :remote-addr    "127.0.0.1"
                :uri            "/foo"
                :query-string   "a=b"
                :scheme         :http
                :request-method :get
                :headers        {"X-Server" "Foo"}
                :content-type   "text/plain"
                :content-length 10
                :character-encoding "UTF-8"
                :body           nil}
          resp (atom {})
          svlt (servlet (fn [r]
                          (are [k v] (= (r k) v)
                            :server-port    8080
                            :server-name    "foobar"
                            :remote-addr    "127.0.0.1"
                            :uri            "/foo"
                            :query-string   "a=b"
                            :scheme         :http
                            :request-method :get
                            :headers        {"x-server" "Foo"}
                            :content-type   "text/plain"
                            :content-length 10
                            :character-encoding "UTF-8"
                            :body           nil)
                          {:status 200, :headers {}}))]
      (doto svlt
        (.init (servlet-config))
        (.service (servlet-request req)
                  (servlet-response resp))))))

{'fraction_non_alphanumeric': 0.1437735849056604, 'fraction_numerical': 0.008867924528301888, 'mean_word_length': 1.8256929637526653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3390846', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1526, 'n_words': 371}