Project

General

Profile

« Previous | Next » 

Revision 02f4ae07

Added by Chris Duryee about 8 years ago

Fixes #14852 - do not pass in "data" param unless needed (#436)

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`.

View differences:

lib/puppet/provider/foreman_resource/rest_v3.rb
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?('/')
......
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
spec/unit/foreman_resource_rest_v3_spec.rb
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
......
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

Also available in: Unified diff