Allow agents using WebRequestConcern to disable URL encoding

Dominik Sander 9 年 前
コミット
f05fc4c148
共有3 個のファイルを変更した51 個の追加0 個の削除を含む
  1. 17 0
      app/concerns/web_request_concern.rb
  2. 1 0
      app/models/agents/rss_agent.rb
  3. 33 0
      spec/support/shared_examples/web_request_concern.rb

+ 17 - 0
app/concerns/web_request_concern.rb

@@ -2,6 +2,18 @@ require 'faraday'
2 2
 require 'faraday_middleware'
3 3
 
4 4
 module WebRequestConcern
5
+  module DoNotEncoder
6
+    def self.encode(params)
7
+      params.map do |key, value|
8
+        value.nil? ? "#{key}" : "#{key}=#{value}"
9
+      end.join('&')
10
+    end
11
+
12
+    def self.decode(val)
13
+      [val]
14
+    end
15
+  end
16
+
5 17
   extend ActiveSupport::Concern
6 18
 
7 19
   def validate_web_request_options!
@@ -38,6 +50,11 @@ module WebRequestConcern
38 50
 
39 51
       builder.use FaradayMiddleware::FollowRedirects
40 52
       builder.request :url_encoded
53
+
54
+      if boolify(options['disable_url_encoding'])
55
+        builder.options.params_encoder = DoNotEncoder
56
+      end
57
+
41 58
       if userinfo = basic_auth_credentials
42 59
         builder.request :basic_auth, *userinfo
43 60
       end

+ 1 - 0
app/models/agents/rss_agent.rb

@@ -26,6 +26,7 @@ module Agents
26 26
           * `headers` - When present, it should be a hash of headers to send with the request.
27 27
           * `basic_auth` - Specify HTTP basic auth parameters: `"username:password"`, or `["username", "password"]`.
28 28
           * `disable_ssl_verification` - Set to `true` to disable ssl verification.
29
+          * `disable_url_encoding` - Set to `true` to disable url encoding.
29 30
           * `user_agent` - A custom User-Agent name (default: "Faraday v#{Faraday::VERSION}").
30 31
           * `max_events_per_run` - Limit number of events created (items parsed) per run for feed.
31 32
       MD

+ 33 - 0
spec/support/shared_examples/web_request_concern.rb

@@ -141,5 +141,38 @@ shared_examples_for WebRequestConcern do
141 141
       agent.options['disable_ssl_verification'] = false
142 142
       expect(agent.faraday.ssl.verify).to eq(true)
143 143
     end
144
+
145
+    it "should use faradays default params_encoder" do
146
+      expect(agent.faraday.options.params_encoder).to eq(nil)
147
+      agent.options['disable_url_encoding'] = 'false'
148
+      expect(agent.faraday.options.params_encoder).to eq(nil)
149
+      agent.options['disable_url_encoding'] = false
150
+      expect(agent.faraday.options.params_encoder).to eq(nil)
151
+    end
152
+
153
+    it "should use WebRequestConcern::DoNotEncoder when disable_url_encoding is truthy" do
154
+      agent.options['disable_url_encoding'] = true
155
+      expect(agent.faraday.options.params_encoder).to eq(WebRequestConcern::DoNotEncoder)
156
+      agent.options['disable_url_encoding'] = 'true'
157
+      expect(agent.faraday.options.params_encoder).to eq(WebRequestConcern::DoNotEncoder)
158
+    end
159
+  end
160
+
161
+  describe WebRequestConcern::DoNotEncoder do
162
+    it "should not encode special characters" do
163
+      expect(WebRequestConcern::DoNotEncoder.encode('GetRss?CategoryNr=39207' => 'test')).to eq('GetRss?CategoryNr=39207=test')
164
+    end
165
+
166
+    it "should work without a value present" do
167
+      expect(WebRequestConcern::DoNotEncoder.encode('GetRss?CategoryNr=39207' => nil)).to eq('GetRss?CategoryNr=39207')
168
+    end
169
+
170
+    it "should work without an empty value" do
171
+      expect(WebRequestConcern::DoNotEncoder.encode('GetRss?CategoryNr=39207' => '')).to eq('GetRss?CategoryNr=39207=')
172
+    end
173
+
174
+    it "should return the value when decoding" do
175
+      expect(WebRequestConcern::DoNotEncoder.decode('val')).to eq(['val'])
176
+    end
144 177
   end
145 178
 end