Permalink
Browse files

make NestedParamsEncoder the default in 0.9

  • Loading branch information...
1 parent af73cdd commit b035bcb99003a244d3a30a5b0f423c5ec759e190 @technoweenie committed Dec 28, 2012
Showing with 33 additions and 11 deletions.
  1. +1 −1 lib/faraday/options.rb
  2. +5 −3 lib/faraday/utils.rb
  3. +27 −7 test/connection_test.rb
View
@@ -92,7 +92,7 @@ class RequestOptions < Options.new(:params_encoder, :proxy, :bind,
:oauth)
def params_encoder
- self[:params_encoder] ||= NestedParamsEncoder
+ self[:params_encoder] ||= Faraday::Utils.default_params_encoder
end
end
View
@@ -175,11 +175,13 @@ def parse_nested_query(query)
NestedParamsEncoder.decode(query)
end
- class << self
- attr_accessor :default_params_encoder
+ def default_params_encoder
+ @default_params_encoder ||= NestedParamsEncoder
end
- self.default_params_encoder = FlatParamsEncoder
+ class << self
+ attr_writer :default_params_encoder
+ end
# Stolen from Rack
def normalize_params(params, name, v = nil)
View
@@ -3,6 +3,10 @@
class TestConnection < Faraday::TestCase
+ def teardown
+ Faraday::Utils.default_params_encoder = nil
+ end
+
def with_env(key, proxy)
old_value = ENV.fetch(key, false)
ENV[key] = proxy
@@ -323,13 +327,6 @@ def app() lambda { |env| env } end
end
end
- def get(*args)
- env = @conn.get(*args) do |req|
- yield req if block_given?
- end
- env[:url].query
- end
-
def assert_query_equal(expected, query)
assert_equal expected, query.split('&').sort
end
@@ -390,14 +387,37 @@ def test_overrides_all_request_params
end
def test_array_params_in_url
+ Faraday::Utils.default_params_encoder = nil
create_connection 'http://a.co/page1?color[]=red&color[]=blue'
query = get
assert_equal "color%5B%5D=red&color%5B%5D=blue", query
end
def test_array_params_in_params
+ Faraday::Utils.default_params_encoder = nil
create_connection 'http://a.co/page1', :params => {:color => ['red', 'blue']}
query = get
assert_equal "color%5B%5D=red&color%5B%5D=blue", query
end
+
+ def test_array_params_in_url_with_flat_params
+ Faraday::Utils.default_params_encoder = Faraday::FlatParamsEncoder
+ create_connection 'http://a.co/page1?color=red&color=blue'
+ query = get
+ assert_equal "color=red&color=blue", query
+ end
+
+ def test_array_params_in_params_with_flat_params
+ Faraday::Utils.default_params_encoder = Faraday::FlatParamsEncoder
+ create_connection 'http://a.co/page1', :params => {:color => ['red', 'blue']}
+ query = get
+ assert_equal "color=red&color=blue", query
+ end
+
+ def get(*args)
+ env = @conn.get(*args) do |req|
+ yield req if block_given?
+ end
+ env[:url].query
+ end
end

0 comments on commit b035bcb

Please sign in to comment.