Last active
December 20, 2015 10:39
-
-
Save jprudent/6116831 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Anything you type in here will be executed | |
;; immediately with the results shown on the | |
;; right. | |
(use 'clojure.test) | |
(with-test | |
(defn extract-scheme | |
"extract scheme from url" | |
[url] | |
(nth (re-find #"([a-zA-Z]+)://.*" url) 1)) | |
(is (= "http" (extract-scheme "http://user:password@host/path1;f1=vf1/path2;f2=vf2?q1=vf1&q2=vf2#fragment"))) | |
(is (= "ftp" (extract-scheme "ftp://jprudent.github.io"))) | |
) | |
(with-test | |
(defn extract-user | |
"extract user from url" | |
[url] | |
(nth (re-find #".+://(?:([a-zA-Z]+)(?::([a-zA-Z]*))?@)?.*" url) 1)) | |
(is (= "user" (extract-user "http://user:password@host/path1;f1=vf1/path2;f2=vf2?q1=vf1&q2=vf2#fragment"))) | |
(is (= "user" (extract-user "http://user@host/"))) | |
(is (nil? (extract-user "http://@host/"))) | |
(is (nil? (extract-user "ftp://jprudent.github.io"))) | |
) | |
(with-test | |
(defn extract-password | |
"extract password from url" | |
[url] | |
(nth (re-find #".+://(?:([a-zA-Z]*)(?::([a-zA-Z]*))?@)?.*" url) 2)) | |
(is (= "password" (extract-password "http://user:password@host/path1;f1=vf1/path2;f2=vf2?q1=vf1&q2=vf2#fragment"))) | |
(is (nil? (extract-password "http://user@host/"))) | |
(is (= "" (extract-password "http://user:@host/"))) | |
(is (nil? (extract-password "ftp://jprudent.github.io"))) | |
) | |
(with-test | |
(defn extract-host | |
"extract host from url" | |
[url] | |
(nth (re-find #".+://(?:.+@)?(.+?)(?:/.*)*$" url) 1)) | |
(is (= "host" (extract-host "http://user:password@host/path1;f1=vf1/path2;f2=vf2?q1=vf1&q2=vf2#fragment"))) | |
(is (= "host" (extract-host "http://user@host/"))) | |
(is (= "host" (extract-host "http://user:@host"))) | |
(is (= "jprudent.github.io" (extract-host "ftp://jprudent.github.io"))) | |
) | |
(with-test-out (run-tests)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment