Last active
December 30, 2019 16:32
-
-
Save conan/8f0c879c47d14d5713f7a0986f81285d 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
(require | |
'[cemerick.url :as url] | |
'[clojure.spec.alpha :as s] | |
'[clojure.spec.gen.alpha :as sgen]) | |
(defn non-empty-string-alphanumeric | |
[] | |
(sgen/such-that #(not= "" %) | |
(sgen/string-alphanumeric))) | |
(defn url-gen | |
"Generator for generating URLs; note that it may generate | |
http URLs on port 443 and https URLs on port 80, and only | |
uses alphanumerics" | |
[] | |
(sgen/fmap | |
(partial apply (comp str url/->URL)) | |
(sgen/tuple | |
;; protocol | |
(sgen/elements #{"http" "https"}) | |
;; username | |
(sgen/string-alphanumeric) | |
;; password | |
(sgen/string-alphanumeric) | |
;; host | |
(sgen/string-alphanumeric) | |
;; port | |
(sgen/choose 1 65535) | |
;; path | |
(sgen/fmap #(->> % | |
(interleave (repeat "/")) | |
(apply str)) | |
(sgen/not-empty | |
(sgen/vector | |
(non-empty-string-alphanumeric)))) | |
;; query | |
(sgen/map | |
(non-empty-string-alphanumeric) | |
(non-empty-string-alphanumeric) | |
{:max-elements 2}) | |
;; anchor | |
(sgen/string-alphanumeric)))) | |
(s/def ::url (s/with-gen | |
(s/and string? | |
#(try | |
(url/url %) | |
(catch Throwable t false))) | |
url-gen)) | |
(sgen/generate (url-gen)) | |
(s/valid? ::url "http://conan.is") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added this gist to https://github.com/ioRekz/spectator 👍