Skip to content

Commit 56920f1

Browse files
committed
Fixed how the Uri class handles authority and scheme-relative values.
Fixes #18 Fixes #22
1 parent 5a33bba commit 56920f1

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/org/bovinegenius/exploding_fish.clj

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
UniformResourceIdentifier
8585
(scheme [self] (:scheme data))
8686
(scheme [self new-scheme] (-> (assoc data :scheme new-scheme)
87+
(build/prepare-map)
8788
(parse/clean-map *uri-keys*)
8889
(Uri. metadata)))
8990
(scheme-relative [self] (:scheme-relative data))
@@ -92,13 +93,15 @@
9293
ssp-data (parse/scheme-specific ssp)
9394
auth-data (parse/authority (:authority ssp-data))]
9495
(-> (merge new-data ssp-data auth-data)
96+
(build/prepare-map)
9597
(parse/clean-map *uri-keys*)
9698
(Uri. metadata))))
9799
(authority [self] (:authority data))
98100
(authority [self authority]
99101
(let [new-data (assoc data :authority authority)
100102
auth-data (parse/authority authority)]
101103
(-> (merge new-data auth-data)
104+
(build/prepare-map)
102105
(parse/clean-map *uri-keys*)
103106
(Uri. metadata))))
104107
(user-info [self] (:user-info data))
@@ -109,6 +112,7 @@
109112
ssp (build/scheme-specific new-data)
110113
new-data (assoc new-data :scheme-relative ssp)]
111114
(-> new-data
115+
(build/prepare-map)
112116
(parse/clean-map *uri-keys*)
113117
(Uri. metadata))))
114118
(host [self] (:host self))
@@ -119,6 +123,7 @@
119123
ssp (build/scheme-specific new-data)
120124
new-data (assoc new-data :scheme-relative ssp)]
121125
(-> new-data
126+
(build/prepare-map)
122127
(parse/clean-map *uri-keys*)
123128
(Uri. metadata))))
124129
(port [self] (:port self))
@@ -131,6 +136,7 @@
131136
ssp (build/scheme-specific new-data)
132137
new-data (assoc new-data :scheme-relative ssp)]
133138
(-> new-data
139+
(build/prepare-map)
134140
(parse/clean-map *uri-keys*)
135141
(Uri. metadata))))
136142
(path [self] (:path self))
@@ -139,6 +145,7 @@
139145
ssp (build/scheme-specific new-data)
140146
new-data (assoc new-data :scheme-relative ssp)]
141147
(-> new-data
148+
(build/prepare-map)
142149
(parse/clean-map *uri-keys*)
143150
(Uri. metadata))))
144151
(query [self] (:query self))
@@ -147,11 +154,13 @@
147154
ssp (build/scheme-specific new-data)
148155
new-data (assoc new-data :scheme-relative ssp)]
149156
(-> new-data
157+
(build/prepare-map)
150158
(parse/clean-map *uri-keys*)
151159
(Uri. metadata))))
152160
(fragment [self] (:fragment self))
153161
(fragment [self fragment]
154162
(-> (assoc data :fragment fragment)
163+
(build/prepare-map)
155164
(parse/clean-map *uri-keys*)
156165
(Uri. metadata)))
157166

@@ -168,7 +177,7 @@
168177
:path (path self value)
169178
:query (query self value)
170179
:fragment (fragment self value)
171-
(Uri. (assoc data key value) metadata)))
180+
(Uri. (parse/clean-map (build/prepare-map (assoc data key value)) *uri-keys*) metadata)))
172181
(entryAt [_ key] (find data key))
173182

174183
clojure.lang.ILookup
@@ -190,7 +199,7 @@
190199
:path (path self nil)
191200
:query (query self nil)
192201
:fragment (fragment self nil)
193-
(Uri. (dissoc data key) metadata)))
202+
(Uri. (parse/clean-map (build/prepare-map (dissoc data key)) *uri-keys*) metadata)))
194203

195204
Iterable
196205
(iterator [_] (.iterator data))
@@ -252,9 +261,9 @@ another Uri object."
252261
[uri]
253262
(condp instance? uri
254263
Uri uri
255-
clojure.lang.IPersistentMap (Uri. uri (meta uri))
256-
java.lang.String (Uri. (parse/uri uri) nil)
257-
(Uri. (-> uri str str parse/uri) nil)))
264+
clojure.lang.IPersistentMap (Uri. (parse/clean-map (build/prepare-map uri) *uri-keys*) (meta uri))
265+
java.lang.String (Uri. (parse/clean-map (build/prepare-map (parse/uri uri)) *uri-keys*) nil)
266+
(Uri. (-> uri str str parse/uri build/prepare-map (parse/clean-map *uri-keys*)) nil)))
258267

259268
(extend java.net.URI
260269
UniformResourceIdentifier

src/org/bovinegenius/exploding_fish/constructor.clj

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2121
;; DEALINGS IN THE SOFTWARE.
2222

23-
(ns org.bovinegenius.exploding-fish.constructor)
23+
(ns org.bovinegenius.exploding-fish.constructor
24+
(:require (org.bovinegenius.exploding-fish
25+
[parser :as parse])))
2426

2527
(defn ^String authority
2628
"Takes a map with :user-info, :host, and :port keys, and returns a
@@ -50,3 +52,16 @@ string."
5052
(scheme-specific
5153
(assoc uri-data :authority auth)))]
5254
(str scheme ssp fragment)))
55+
56+
(defn prepare-map
57+
"Takes a map representing a URI, and populates missing data."
58+
[{:keys [scheme scheme-relative fragment] :as uri-data}]
59+
(let [auth (or (:authority uri-data) (authority uri-data))
60+
auth (if (empty? auth) nil auth)
61+
ssp (or scheme-relative
62+
(scheme-specific
63+
(assoc uri-data :authority auth)))
64+
ssp (if (empty? ssp) nil ssp)]
65+
(assoc uri-data
66+
:authority auth
67+
:scheme-relative ssp)))

test/org/bovinegenius/uri_test.clj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
(:require (org.bovinegenius.exploding-fish [parser :as parse]))
66
(:import (java.net URI URL)))
77

8+
(deftest ensure-authority-test
9+
(let [uritest (uri {:scheme "http" :host "localhost" :port 3001 :path "/tags" :query "name=comp&from=0"})
10+
my-uri (uri {:scheme "http" :host "localhost" :port 3000})]
11+
(is (= (str (param uritest "from" 10)) "http://localhost:3001/tags?name=comp&from=10"))
12+
(is (= (str (path my-uri "/foo")) "http://localhost:3000/foo"))))
13+
814
(deftest uri-test
915
(let [uri-string "http://www.fred.net/"]
1016
(is (= [:host "www.fred.net"] (find (uri uri-string) :host)))

0 commit comments

Comments
 (0)