Skip to content

Commit

Permalink
Add more --index-document-path tests
Browse files Browse the repository at this point in the history
Also tweaks test setup a bit to suppress logging.
  • Loading branch information
Dan Dorman committed Jan 29, 2020
1 parent 2eaee78 commit aa9859e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/http/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@
;; we rely on `inject-content-length` being
;; invoked earlier :expressionless:
len (get headers "content-length" "-")]
(log/infof "\"%s %s HTTP/1.1\" %s %s" method' uri status len))
(log/infof "\"%s %s HTTP/1.1\" %s %s" method' uri status len))
response))))

(defn password-prompt! []
Expand Down
22 changes: 15 additions & 7 deletions test/http/server_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
[byte-streams :refer [convert]]
[clojure.string :as str]
[clojure.test :refer :all]
[clojure.tools.logging :as log]
[clojure.tools.logging.impl :as log-impl]
[http.server :as nasus])
(:import [java.io File]))

Expand All @@ -27,19 +29,20 @@
(-> response :body (convert String)))

(defmacro with-nasus [var-name opts & body]
`(let [~var-name (nasus/start (merge {:port 0} ~opts))]
(try
~@body
(finally
(nasus/stop ~var-name)))))
`(with-redefs [log/*logger-factory* log-impl/disabled-logger-factory]
(let [~var-name (nasus/start (merge {:port 0} ~opts))]
(try
~@body
(finally
(nasus/stop ~var-name))))))

;; tests

(deftest text-listing-test
(with-nasus server {:dir (fs-path (pwd) "test_resources" "dir")}
(let [response @(http/get (url server "/"))]
(is (= (:status response) 200))
(is (= (body response) "bar.html\r\nfoo.txt\r\n")))))
(is (= (body response) "bar.html\r\nfoo.txt\r\nsub\r\n")))))

(deftest html-listing-test
(with-nasus server {:dir (fs-path (pwd) "test_resources" "dir")}
Expand All @@ -53,6 +56,7 @@
<ul>\r
<li><a href=\"bar.html\">bar.html</a></li>\r
<li><a href=\"foo.txt\">foo.txt</a></li>\r
<li><a href=\"sub\">sub</a></li>\r
</ul>\r
<hr/>\r
")))))
Expand All @@ -73,4 +77,8 @@
(with-nasus server {:dir (fs-path (pwd) "test_resources" "dir")
:index-document-path "bar.html"}
(let [response @(http/get (url server "/"))]
(is (= (body response) "<!DOCTYPE html>\nhello")))))
(is (= (body response) "<!DOCTYPE html>\nbar\n") "returns index document"))
(let [response @(http/get (url server "/foo.txt"))]
(is (= (body response) "foo\n") "returns explicit file"))
(let [response @(http/get (url server "/sub/") {:throw-exceptions false})]
(is (= (:status response) 404) "no index doc returns not found"))))
2 changes: 1 addition & 1 deletion test_resources/dir/bar.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<!DOCTYPE html>
hello
bar
1 change: 1 addition & 0 deletions test_resources/dir/sub/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a

0 comments on commit aa9859e

Please sign in to comment.