Normalize URL in `to_uri` and `uri_expand` liquid filters

Akinori MUSHA 7 years ago
parent
commit
0fcd8e285e
3 changed files with 11 additions and 6 deletions
  1. 1 0
      CHANGES.md
  2. 4 4
      app/concerns/liquid_interpolatable.rb
  3. 6 2
      spec/concerns/liquid_interpolatable_spec.rb

+ 1 - 0
CHANGES.md

@@ -2,6 +2,7 @@
2 2
 
3 3
 | DateOfChange   | Changes                                                                                                      |
4 4
 |----------------|--------------------------------------------------------------------------------------------------------------|
5
+| Oct 17, 2016   | Normalize URL in `to_uri` and `uri_expand` liquid filters.                                                   |
5 6
 | Oct 06, 2016   | `RssAgent` is reimplemented migrating its underlying feed parser from FeedNormalizer to Feedjira. [1564](https://github.com/cantino/huginn/pull/1564)     |
6 7
 | Oct 05, 2016   | Migrate to Rails 5. [1688](https://github.com/cantino/huginn/pull/1688)                                      |
7 8
 | Oct 05, 2016   | Improve URL normalization in `WebsiteAgent`. [1719](https://github.com/cantino/huginn/pull/1719)             |

+ 4 - 4
app/concerns/liquid_interpolatable.rb

@@ -130,9 +130,9 @@ module LiquidInterpolatable
130 130
     # fragment.
131 131
     def to_uri(uri, base_uri = nil)
132 132
       if base_uri
133
-        URI(base_uri) + uri.to_s
133
+        Utils.normalize_uri(base_uri) + Utils.normalize_uri(uri.to_s)
134 134
       else
135
-        URI(uri.to_s)
135
+        Utils.normalize_uri(uri.to_s)
136 136
       end
137 137
     rescue URI::Error
138 138
       nil
@@ -151,7 +151,7 @@ module LiquidInterpolatable
151 151
       else
152 152
         url = url.to_s
153 153
         begin
154
-          uri = URI(url)
154
+          uri = Utils.normalize_uri(url)
155 155
         rescue URI::Error
156 156
           return url
157 157
         end
@@ -172,7 +172,7 @@ module LiquidInterpolatable
172 172
             case response.status
173 173
             when 301, 302, 303, 307
174 174
               if location = response['location']
175
-                uri += location
175
+                uri += Utils.normalize_uri(location)
176 176
                 next
177 177
               end
178 178
             end

+ 6 - 2
spec/concerns/liquid_interpolatable_spec.rb

@@ -106,6 +106,10 @@ describe LiquidInterpolatable::Filters do
106 106
       expect(@filter.to_uri(123, 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/123'))
107 107
     end
108 108
 
109
+    it 'should normalize a URL' do
110
+      expect(@filter.to_uri('a[]', 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/a%5B%5D'))
111
+    end
112
+
109 113
     it 'should return a URI value in interpolation' do
110 114
       expect(@agent.interpolated['foo']).to eq('/dir/1')
111 115
     end
@@ -140,8 +144,8 @@ describe LiquidInterpolatable::Filters do
140 144
       expect(@filter.uri_expand(nil)).to eq('')
141 145
       expect(@filter.uri_expand('')).to eq('')
142 146
       expect(@filter.uri_expand(5)).to eq('5')
143
-      expect(@filter.uri_expand([])).to eq('[]')
144
-      expect(@filter.uri_expand({})).to eq('{}')
147
+      expect(@filter.uri_expand([])).to eq('%5B%5D')
148
+      expect(@filter.uri_expand({})).to eq('%7B%7D')
145 149
       expect(@filter.uri_expand(URI('/'))).to eq('/')
146 150
       expect(@filter.uri_expand(URI('http:google.com'))).to eq('http:google.com')
147 151
       expect(@filter.uri_expand(URI('http:/google.com'))).to eq('http:/google.com')