diff --git a/lib/faraday_middleware/response/parse_marshal.rb b/lib/faraday_middleware/response/parse_marshal.rb index 98e2e018..e5f3cd18 100644 --- a/lib/faraday_middleware/response/parse_marshal.rb +++ b/lib/faraday_middleware/response/parse_marshal.rb @@ -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 diff --git a/lib/faraday_middleware/response/parse_xml.rb b/lib/faraday_middleware/response/parse_xml.rb index 231ff0cd..db6b2f35 100644 --- a/lib/faraday_middleware/response/parse_xml.rb +++ b/lib/faraday_middleware/response/parse_xml.rb @@ -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 diff --git a/lib/faraday_middleware/response/parse_yaml.rb b/lib/faraday_middleware/response/parse_yaml.rb index 8564fa8f..8c1a277a 100644 --- a/lib/faraday_middleware/response/parse_yaml.rb +++ b/lib/faraday_middleware/response/parse_yaml.rb @@ -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 diff --git a/spec/unit/oauth2_spec.rb b/spec/unit/oauth2_spec.rb index 9443f970..9a1fa8d4 100644 --- a/spec/unit/oauth2_spec.rb +++ b/spec/unit/oauth2_spec.rb @@ -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') @@ -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 @@ -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') @@ -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)) @@ -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 @@ -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 @@ -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') @@ -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 @@ -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") diff --git a/spec/unit/parse_json_spec.rb b/spec/unit/parse_json_spec.rb index baa1edd4..5d0ce327 100644 --- a/spec/unit/parse_json_spec.rb +++ b/spec/unit/parse_json_spec.rb @@ -121,7 +121,7 @@ context "JSON options" do let(:body) { '{"a": 1}' } - let(:result) { {} } + let(:result) { {a: 1} } let(:options) do { :parser_options => { @@ -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 diff --git a/spec/unit/parse_xml_spec.rb b/spec/unit/parse_xml_spec.rb index b9bdd415..6915ae31 100644 --- a/spec/unit/parse_xml_spec.rb +++ b/spec/unit/parse_xml_spec.rb @@ -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 diff --git a/spec/unit/parse_yaml_spec.rb b/spec/unit/parse_yaml_spec.rb index b034aab7..4895bac0 100644 --- a/spec/unit/parse_yaml_spec.rb +++ b/spec/unit/parse_yaml_spec.rb @@ -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