Allow agents using WebRequestConcern to disable URL encoding

This commit is contained in:
Dominik Sander 2015-06-13 13:27:12 +02:00
parent 8b8221e174
commit f05fc4c148
3 changed files with 51 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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