Skip to content

Commit

Permalink
respect Faraday::RequestOptions#params_encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed May 13, 2013
1 parent 3a9e033 commit 7cdff32
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/faraday/adapter/test.rb
Expand Up @@ -144,10 +144,11 @@ def configure
def call(env)
super
normalized_path = Faraday::Utils.normalize_path(env[:url])
params_encoder = env.request.params_encoder || Faraday::Utils.default_params_encoder

if stub = stubs.match(env[:method], normalized_path, env.request_headers, env[:body])
env[:params] = (query = env[:url].query) ?
Faraday::Utils.parse_nested_query(query) :
params_encoder.decode(query) :
{}
status, headers, body = stub.block.call(env)
save_response(env, status, body, headers)
Expand Down
29 changes: 29 additions & 0 deletions test/adapters/test_middleware_test.rb
Expand Up @@ -69,6 +69,35 @@ def test_yields_env_to_stubs
assert_equal 'a', @conn.get('http://foo.com/hello?a=1').body
end

def test_parses_params_with_default_encoder
@stubs.get '/hello' do |env|
assert_equal '1', env[:params]['a']['b']
[200, {}, 'a']
end

assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
end

def test_parses_params_with_nested_encoder
@stubs.get '/hello' do |env|
assert_equal '1', env[:params]['a']['b']
[200, {}, 'a']
end

@conn.options.params_encoder = Faraday::NestedParamsEncoder
assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
end

def test_parses_params_with_flat_encoder
@stubs.get '/hello' do |env|
assert_equal '1', env[:params]['a[b]']
[200, {}, 'a']
end

@conn.options.params_encoder = Faraday::FlatParamsEncoder
assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
end

def test_raises_an_error_if_no_stub_is_found_for_request
assert_raises Stubs::NotFound do
@conn.get('/invalid'){ [200, {}, []] }
Expand Down

0 comments on commit 7cdff32

Please sign in to comment.