Back
with-base-url (clj)
(source)macro
(with-base-url base-url & body)
Sets a base URL that will be prepended onto relative URIs. Note that for this
to work correctly, it needs to be placed outside the [[hiccup.core/html]] or
[[hiccup2.core/html]] macros.
Examples
hiccup
(ns hiccup.util_test
(:require [clojure.test :refer :all]
[hiccup.util :refer :all])
(:import java.net.URI))
(deftest test-to-uri
(testing "with no base URL"
(is (= (to-str (to-uri "foo")) "foo"))
(is (= (to-str (to-uri "/foo/bar")) "/foo/bar"))
(is (= (to-str (to-uri "/foo#bar")) "/foo#bar")))
(testing "with base URL"
(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"))))
(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")))))
weavejester/hiccup
(ns hiccup.util_test
(:require [clojure.test :refer :all]
[hiccup.util :refer :all])
(:import java.net.URI))
(deftest test-to-uri
(testing "with no base URL"
(is (= (to-str (to-uri "foo")) "foo"))
(is (= (to-str (to-uri "/foo/bar")) "/foo/bar"))
(is (= (to-str (to-uri "/foo#bar")) "/foo#bar")))
(testing "with base URL"
(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"))))
(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")))))