Permalink
Browse files

Merge pull request #60 from jcrossley3/replace

Resolve relative to root context better
  • Loading branch information...
2 parents 3ab2dde + 41ab423 commit 7ab1bb67823f117c8134ece18d4bd9b3d6470684 @weavejester committed Sep 17, 2012
Showing with 20 additions and 1 deletion.
  1. +4 −1 src/hiccup/util.clj
  2. +16 −0 test/hiccup/test/util.clj
View
@@ -26,7 +26,10 @@
(if (or (.getHost u)
(not (-> (.getPath u) (.startsWith "/"))))
(str u)
- (str *base-url* u)))
+ (let [base (str *base-url*)]
+ (if (.endsWith base "/")
+ (str (subs base 0 (dec (count base))) u)
+ (str base u)))))
Object
(to-str [x] (str x))
nil
@@ -30,6 +30,22 @@
(is (= (to-str (to-uri "https://example.com/bar")) "https://example.com/bar"))
(is (= (to-str (to-uri "bar")) "bar"))
(is (= (to-str (to-uri "../bar")) "../bar"))
+ (is (= (to-str (to-uri "//example.com/bar")) "//example.com/bar"))))
+ (testing "with base URL for root context"
+ (with-base-url "/"
+ (is (= (to-str (to-uri "/bar")) "/bar"))
+ (is (= (to-str (to-uri "http://example.com")) "http://example.com"))
+ (is (= (to-str (to-uri "https://example.com/bar")) "https://example.com/bar"))
+ (is (= (to-str (to-uri "bar")) "bar"))
+ (is (= (to-str (to-uri "../bar")) "../bar"))
+ (is (= (to-str (to-uri "//example.com/bar")) "//example.com/bar"))))
+ (testing "with base URL containing trailing slash"
+ (with-base-url "/foo/"
+ (is (= (to-str (to-uri "/bar")) "/foo/bar"))
+ (is (= (to-str (to-uri "http://example.com")) "http://example.com"))
+ (is (= (to-str (to-uri "https://example.com/bar")) "https://example.com/bar"))
+ (is (= (to-str (to-uri "bar")) "bar"))
+ (is (= (to-str (to-uri "../bar")) "../bar"))
(is (= (to-str (to-uri "//example.com/bar")) "//example.com/bar")))))
(deftest test-url-encode

0 comments on commit 7ab1bb6

Please sign in to comment.