Skip to content

Commit

Permalink
Fixes #14852 - do not pass in "data" param unless needed (#436)
Browse files Browse the repository at this point in the history
Previously, the "data" param was passed to all oauth requests. This caused
issues with `GET` requests, since `data` and `headers` gets mashed into
`*arguments`, and then the oauth gem will `shift` off the first argument if
there's a `POST` or `PUT` method. The end result was that headers weren't being
sent on `GET` requests.

Instead, only supply the "data" param if we are doing a `POST` or `PUT`.
  • Loading branch information
beav authored and mmoll committed Apr 28, 2016
1 parent e15f6f7 commit 02f4ae0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 6 additions & 2 deletions lib/puppet/provider/foreman_resource/rest_v3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def generate_token
OAuth::AccessToken.new(oauth_consumer)
end

def request(method, path, params = {}, data = nil, headers = {})
def request(method, path, params = {}, data = {}, headers = {})
base_url = resource[:base_url]
base_url += '/' unless base_url.end_with?('/')

Expand All @@ -70,7 +70,11 @@ def request(method, path, params = {}, data = nil, headers = {})
attempts = 0
begin
debug("Making #{method} request to #{uri}")
response = oauth_consumer.request(method, uri.to_s, generate_token, {}, data, headers)
if [:post, :put, :patch].include?(method)
response = oauth_consumer.request(method, uri.to_s, generate_token, {}, data, headers)
else
response = oauth_consumer.request(method, uri.to_s, generate_token, {}, headers)
end
debug("Received response #{response.code} from request to #{uri}")
response
rescue Timeout::Error => te
Expand Down
10 changes: 5 additions & 5 deletions spec/unit/foreman_resource_rest_v3_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@

it 'makes request via consumer and returns response' do
response = mock(:code => '200')
consumer.expects(:request).with(:get, 'https://foreman.example.com/api/v2/example', is_a(OAuth::AccessToken), {}, nil, is_a(Hash)).returns(response)
consumer.expects(:request).with(:get, 'https://foreman.example.com/api/v2/example', is_a(OAuth::AccessToken), {}, is_a(Hash)).returns(response)
expect(provider.request(:get, 'api/v2/example')).to eq(response)
end

it 'specifies foreman_user header' do
consumer.expects(:request).with(:get, anything, anything, anything, anything, has_entry('foreman_user', 'admin')).returns(mock(:code => '200'))
consumer.expects(:request).with(:get, anything, anything, anything, has_entry('foreman_user', 'admin')).returns(mock(:code => '200'))
provider.request(:get, 'api/v2/example')
end

Expand All @@ -89,12 +89,12 @@
end

it 'passes data' do
consumer.expects(:request).with(:get, anything, anything, anything, 'test', anything).returns(mock(:code => '200'))
provider.request(:get, 'api/v2/example', {}, 'test')
consumer.expects(:request).with(:post, anything, anything, anything, 'test', anything).returns(mock(:code => '200'))
provider.request(:post, 'api/v2/example', {}, 'test')
end

it 'merges headers' do
consumer.expects(:request).with(:get, anything, anything, anything, anything, has_entries('test' => 'value', 'Accept' => 'application/json')).returns(mock(:code => '200'))
consumer.expects(:request).with(:get, anything, anything, anything, has_entries('test' => 'value', 'Accept' => 'application/json')).returns(mock(:code => '200'))
provider.request(:get, 'api/v2/example', {}, nil, {'test' => 'value'})
end

Expand Down

0 comments on commit 02f4ae0

Please sign in to comment.