Skip to content

Commit

Permalink
Adds support for parser_options on MultiXML and SafeYAML parsers
Browse files Browse the repository at this point in the history
Tests improvements
  • Loading branch information
iMacTia committed Aug 3, 2017
1 parent 52ca881 commit 638a3b3
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/faraday_middleware/response/parse_marshal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module FaradayMiddleware
# Public: Restore marshalled Ruby objects in response bodies.
class ParseMarshal < ResponseMiddleware
define_parser do |body|
::Marshal.load body unless body.empty?
::Marshal.load(body) unless body.empty?
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/faraday_middleware/response/parse_xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module FaradayMiddleware
class ParseXml < ResponseMiddleware
dependency 'multi_xml'

define_parser do |body|
::MultiXml.parse(body)
define_parser do |body, parser_options|
::MultiXml.parse(body, parser_options || {})
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/faraday_middleware/response/parse_yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ module FaradayMiddleware
class ParseYaml < ResponseMiddleware
dependency 'safe_yaml/load'

define_parser do |body|
SafeYAML.load body
define_parser do |body, parser_options|
if SafeYAML::YAML_ENGINE == 'psych'
SafeYAML.load(body, nil, parser_options || {})
else
SafeYAML.load(body, parser_options || {})
end
end
end
end
Expand Down
18 changes: 9 additions & 9 deletions spec/unit/oauth2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def make_app
end

context "no token configured" do
let(:options) { nil }
let(:options) { [nil, {:token_type => :param}] }

it "doesn't add params" do
request = perform(:q => 'hello')
Expand All @@ -45,7 +45,7 @@ def make_app
end

context "bearer token type configured" do
let(:options) { [nil, {:token_type => 'bearer'}] }
let(:options) { [nil, {:token_type => :bearer}] }

it "doesn't add headers" do
expect(auth_header(perform)).to be_nil
Expand All @@ -59,7 +59,7 @@ def make_app
end

context "default token configured" do
let(:options) { 'XYZ' }
let(:options) { ['XYZ', {:token_type => :param}] }

it "adds token param" do
expect(query_params(perform(:q => 'hello'))).to eq('q' => 'hello', 'access_token' => 'XYZ')
Expand All @@ -82,7 +82,7 @@ def make_app
end

context "bearer token type configured" do
let(:options) { ['XYZ', {:token_type => 'bearer'}] }
let(:options) { ['XYZ', {:token_type => :bearer}] }

it "adds token header" do
expect(auth_header(perform)).to eq(%(Bearer XYZ))
Expand All @@ -101,7 +101,7 @@ def make_app
end

context "existing Authorization header" do
let(:options) { 'XYZ' }
let(:options) { ['XYZ', {:token_type => :param}] }
subject { perform({:q => 'hello'}, 'Authorization' => 'custom') }

it "adds token param" do
Expand All @@ -113,7 +113,7 @@ def make_app
end

context "bearer token type configured" do
let(:options) { ['XYZ', {:token_type => 'bearer'}] }
let(:options) { ['XYZ', {:token_type => :bearer}] }
subject { perform({:q => 'hello'}, 'Authorization' => 'custom') }

it "doesn't override existing header" do
Expand All @@ -123,7 +123,7 @@ def make_app
end

context "custom param name configured" do
let(:options) { ['XYZ', {:param_name => :oauth}] }
let(:options) { ['XYZ', {:token_type => :param, :param_name => :oauth}] }

it "adds token param" do
expect(query_params(perform)).to eq('oauth' => 'XYZ')
Expand All @@ -137,7 +137,7 @@ def make_app
end

context "options without token configuration" do
let(:options) { [{:param_name => :oauth}] }
let(:options) { [{:token_type => :param, :param_name => :oauth}] }

it "doesn't add param" do
expect(query_params(perform)).to be_empty
Expand All @@ -149,7 +149,7 @@ def make_app
end

context "invalid param name configured" do
let(:options) { ['XYZ', {:param_name => nil}] }
let(:options) { ['XYZ', {:token_type => :param, :param_name => nil}] }

it "raises error" do
expect{ make_app }.to raise_error(ArgumentError, ":param_name can't be blank")
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/parse_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@

context "JSON options" do
let(:body) { '{"a": 1}' }
let(:result) { {} }
let(:result) { {a: 1} }
let(:options) do
{
:parser_options => {
Expand All @@ -131,12 +131,12 @@
end

it "passes relevant options to JSON parse" do
allow(::JSON).to receive(:parse)
expect(::JSON).to receive(:parse)
.with(body, options[:parser_options])
.and_return(result)

response = process(body)
expect(response.body).to be(result)
expect(response.body).to eq(result)
end
end
end
19 changes: 19 additions & 0 deletions spec/unit/parse_xml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,23 @@
expect{ process(data) }.to raise_error(Faraday::Error::ParsingError)
end
end

context "MultiXml options" do
let(:options) do
{
:parser_options => {
:symbolize_names => true
}
}
end

it "passes relevant options to MultiXml parse" do
expect(::MultiXml).to receive(:parse)
.with(xml, options[:parser_options])
.and_return(user)

response = process(xml)
expect(response.body).to be(user)
end
end
end
21 changes: 21 additions & 0 deletions spec/unit/parse_yaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,25 @@
it "chokes on invalid yaml" do
expect{ process('{!') }.to raise_error(Faraday::Error::ParsingError)
end

context "SafeYAML options" do
let(:body) { 'a: 1' }
let(:result) { {a: 1} }
let(:options) do
{
:parser_options => {
:symbolize_names => true
}
}
end

it "passes relevant options to SafeYAML load" do
expect(::SafeYAML).to receive(:load)
.with(body, nil, options[:parser_options])
.and_return(result)

response = process(body)
expect(response.body).to be(result)
end
end
end

0 comments on commit 638a3b3

Please sign in to comment.