Skip to content

Fix dropped values from queries by using FlatParamsEncoder #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Metrics/MethodLength:
# Offense count: 3
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 391
Max: 398

# Offense count: 1
Style/AsciiComments:
Expand Down
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ matrix:
- rvm: 2.3.1
- rvm: 2.3.0
- rvm: 2.2.5
- rvm: 2.4.0
- rvm: rbx-2
- rvm: ruby-head
- rvm: jruby-head
- rvm: jruby-9.1.6.0
- rvm: jruby-9.1.7.0
allow_failures:
- rvm: ruby-head
- rvm: jruby-head
- rvm: rbx-2

before_install:
- gem update --system
- gem install bundler

bundler_args: --without development
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.8.3 (Next)

* [#115](https://github.com/codegram/hyperclient/pull/115): Fix dropped values from queries by using FlatParamsEncoder - [@ivoanjo](https://github.com/ivoanjo).
* Your contribution here.

### 0.8.2 (December 31, 2016)
Expand Down
5 changes: 5 additions & 0 deletions features/api_navigation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Feature: API navigation
When I search for a post with a templated link
Then the API should receive the request with all the params

Scenario: Templated links with multiple values
Given I connect to the API
When I search for posts by tag with a templated link
Then the API should receive the request for posts by tag with all the params

Scenario: Attributes
Given I connect to the API
When I load a single post
Expand Down
8 changes: 8 additions & 0 deletions features/steps/api_navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class Spinach::Features::ApiNavigation < Spinach::FeatureSteps
assert_requested :get, 'http://api.example.org/search?q=something'
end

step 'I search for posts by tag with a templated link' do
api._links.tagged._expand(tags: %w(foo bar))._resource
end

step 'the API should receive the request for posts by tag with all the params' do
assert_requested :get, 'http://api.example.org/search?tags=foo&tags=bar'
end

step 'I load a single post' do
@post = api._links.posts._links.last_post
end
Expand Down
2 changes: 2 additions & 0 deletions features/support/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module API
include Spinach::Fixtures

before do
WebMock::Config.instance.query_values_notation = :flat_array

stub_request(:any, %r{api.example.org*}).to_return(body: root_response, headers: { 'Content-Type' => 'application/hal+json' })
stub_request(:get, 'api.example.org/posts').to_return(body: posts_response, headers: { 'Content-Type' => 'application/hal+json' })
stub_request(:get, 'api.example.org/posts/1').to_return(body: post_response, headers: { 'Content-Type' => 'application/hal+json' })
Expand Down
1 change: 1 addition & 0 deletions features/support/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def root_response
"self": { "href": "/" },
"posts": { "href": "/posts" },
"search": { "href": "/search{?q}", "templated": true },
"tagged": { "href": "/search{?tags*}", "templated": true },
"api:authors": { "href": "/authors" },
"next": { "href": "/page2" }
}
Expand Down
2 changes: 2 additions & 0 deletions lib/hyperclient/entry_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def initialize(url, &_block)
@entry_point = self
@options = { async: true }
@connection = nil
@resource = nil
yield self if block_given?
end

Expand Down Expand Up @@ -138,6 +139,7 @@ def default_faraday_block
connection.request :hal_json
connection.response :hal_json, content_type: /\bjson$/
connection.adapter :net_http
connection.options.params_encoder = Faraday::FlatParamsEncoder
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/hyperclient/entry_point_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ module Hyperclient

it 'creates a Faraday connection with the default block' do
handlers = entry_point.connection.builder.handlers

handlers.must_include Faraday::Response::RaiseError
handlers.must_include FaradayMiddleware::FollowRedirects
handlers.must_include FaradayMiddleware::EncodeHalJson
handlers.must_include FaradayMiddleware::ParseHalJson
handlers.must_include Faraday::Adapter::NetHttp

entry_point.connection.options.params_encoder.must_equal Faraday::FlatParamsEncoder
end

it 'raises a ConnectionAlreadyInitializedError if attempting to modify headers' do
Expand Down Expand Up @@ -172,12 +175,15 @@ module Hyperclient

it 'creates a Faraday connection with the default block plus any additional handlers' do
handlers = entry_point.connection.builder.handlers

handlers.must_include Faraday::Request::OAuth
handlers.must_include Faraday::Response::RaiseError
handlers.must_include FaradayMiddleware::FollowRedirects
handlers.must_include FaradayMiddleware::EncodeHalJson
handlers.must_include FaradayMiddleware::ParseHalJson
handlers.must_include Faraday::Adapter::NetHttp

entry_point.connection.options.params_encoder.must_equal Faraday::FlatParamsEncoder
end
end
end
Expand Down