diff --git a/app/concerns/web_request_concern.rb b/app/concerns/web_request_concern.rb index 47bfd3f5..71c9c091 100644 --- a/app/concerns/web_request_concern.rb +++ b/app/concerns/web_request_concern.rb @@ -2,6 +2,18 @@ require 'faraday' require 'faraday_middleware' module WebRequestConcern + module DoNotEncoder + def self.encode(params) + params.map do |key, value| + value.nil? ? "#{key}" : "#{key}=#{value}" + end.join('&') + end + + def self.decode(val) + [val] + end + end + extend ActiveSupport::Concern def validate_web_request_options! @@ -38,6 +50,11 @@ module WebRequestConcern builder.use FaradayMiddleware::FollowRedirects builder.request :url_encoded + + if boolify(options['disable_url_encoding']) + builder.options.params_encoder = DoNotEncoder + end + if userinfo = basic_auth_credentials builder.request :basic_auth, *userinfo end diff --git a/app/models/agents/rss_agent.rb b/app/models/agents/rss_agent.rb index 8e2b9c39..9780f4e9 100644 --- a/app/models/agents/rss_agent.rb +++ b/app/models/agents/rss_agent.rb @@ -26,6 +26,7 @@ module Agents * `headers` - When present, it should be a hash of headers to send with the request. * `basic_auth` - Specify HTTP basic auth parameters: `"username:password"`, or `["username", "password"]`. * `disable_ssl_verification` - Set to `true` to disable ssl verification. + * `disable_url_encoding` - Set to `true` to disable url encoding. * `user_agent` - A custom User-Agent name (default: "Faraday v#{Faraday::VERSION}"). * `max_events_per_run` - Limit number of events created (items parsed) per run for feed. MD diff --git a/spec/support/shared_examples/web_request_concern.rb b/spec/support/shared_examples/web_request_concern.rb index f9dab9c1..53df7129 100644 --- a/spec/support/shared_examples/web_request_concern.rb +++ b/spec/support/shared_examples/web_request_concern.rb @@ -141,5 +141,38 @@ shared_examples_for WebRequestConcern do agent.options['disable_ssl_verification'] = false expect(agent.faraday.ssl.verify).to eq(true) end + + it "should use faradays default params_encoder" do + expect(agent.faraday.options.params_encoder).to eq(nil) + agent.options['disable_url_encoding'] = 'false' + expect(agent.faraday.options.params_encoder).to eq(nil) + agent.options['disable_url_encoding'] = false + expect(agent.faraday.options.params_encoder).to eq(nil) + end + + it "should use WebRequestConcern::DoNotEncoder when disable_url_encoding is truthy" do + agent.options['disable_url_encoding'] = true + expect(agent.faraday.options.params_encoder).to eq(WebRequestConcern::DoNotEncoder) + agent.options['disable_url_encoding'] = 'true' + expect(agent.faraday.options.params_encoder).to eq(WebRequestConcern::DoNotEncoder) + end + end + + describe WebRequestConcern::DoNotEncoder do + it "should not encode special characters" do + expect(WebRequestConcern::DoNotEncoder.encode('GetRss?CategoryNr=39207' => 'test')).to eq('GetRss?CategoryNr=39207=test') + end + + it "should work without a value present" do + expect(WebRequestConcern::DoNotEncoder.encode('GetRss?CategoryNr=39207' => nil)).to eq('GetRss?CategoryNr=39207') + end + + it "should work without an empty value" do + expect(WebRequestConcern::DoNotEncoder.encode('GetRss?CategoryNr=39207' => '')).to eq('GetRss?CategoryNr=39207=') + end + + it "should return the value when decoding" do + expect(WebRequestConcern::DoNotEncoder.decode('val')).to eq(['val']) + end end end