diff --git a/src/cemerick/url.cljx b/src/cemerick/url.cljx index 75ff53d..1c0c8e0 100644 --- a/src/cemerick/url.cljx +++ b/src/cemerick/url.cljx @@ -83,6 +83,16 @@ (map->query query)))) (when anchor (str \# anchor)))))) +(defn normalize + [path] + (when-not (string/blank? path) + (let [original-ends-with-slash? (= (last path) \/) + normalized (pathetic/normalize path)] + (if (and original-ends-with-slash? + (not= (last normalized) \/)) + (str normalized "/") + normalized)))) + #+clj (defn- url* [url] @@ -93,7 +103,7 @@ (and (seq pass) pass) (.getHost url) (.getPort url) - (pathetic/normalize (.getPath url)) + (normalize (.getPath url)) (query->map (.getQuery url)) (.getRef url)))) @@ -114,7 +124,7 @@ (and (seq pass) pass) (.getDomain url) (translate-default (.getPort url) nil -1) - (pathetic/normalize (.getPath url)) + (normalize (.getPath url)) (query->map (translate-default (.getQuery url) "" nil)) (translate-default (.getFragment url) "" nil)))) @@ -135,7 +145,7 @@ (url* url))) ([base-url & path-segments] (let [base-url (if (instance? URL base-url) base-url (url base-url))] - (assoc base-url :path (pathetic/normalize (reduce pathetic/resolve - (:path base-url) - path-segments)))))) + (assoc base-url :path (normalize (reduce pathetic/resolve + (:path base-url) + path-segments)))))) diff --git a/test/cemerick/test_url.cljx b/test/cemerick/test_url.cljx index fe2e6d1..0ff6469 100644 --- a/test/cemerick/test_url.cljx +++ b/test/cemerick/test_url.cljx @@ -55,6 +55,7 @@ (deftest path-normalization (is (= "http://a/" (url-str "http://a/b/c/../.."))) + (is (= "http://test.a/a/" (url-str "http://test.a/a/"))) (is (= "http://a/b/c" (url-str "http://a/b/" "c"))) (is (= "http://a/b/c" (url-str "http://a/b/.." "b" "c")))