Skip to content

Commit 7a2e002

Browse files
committed
Add support for headers parameter in ruby provider
1 parent d0f775f commit 7a2e002

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

lib/puppet/provider/archive/ruby.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ def download(location, filepath)
214214
filepath,
215215
username: resource[:username],
216216
password: resource[:password],
217+
headers: resource[:headers],
217218
cookie: resource[:cookie],
218219
proxy_server: resource[:proxy_server],
219220
proxy_type: resource[:proxy_type],

lib/puppet_x/bodeco/util.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class HTTP
7070
def initialize(_url, options)
7171
@username = options[:username]
7272
@password = options[:password]
73+
@headers = options[:headers]
7374
@cookie = options[:cookie]
7475
@insecure = options[:insecure]
7576

@@ -84,9 +85,11 @@ def initialize(_url, options)
8485
end
8586

8687
def generate_request(uri)
87-
header = @cookie && { 'Cookie' => @cookie }
88+
headers = {}
89+
headers.merge! @headers.to_h { |hdr| hdr.split(':', 2).map(&:strip) } if @headers
90+
headers['Cookie'] = @cookie if @cookie
8891

89-
request = Net::HTTP::Get.new(uri.request_uri, header)
92+
request = Net::HTTP::Get.new(uri.request_uri, headers)
9093
request.basic_auth(@username, @password) if @username && @password
9194
request
9295
end

spec/unit/puppet/provider/archive/ruby_spec.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,73 @@
150150
expect { provider.transfer_download(name) }.to raise_error(Puppet::Error, %r{Download file checksum mismatch})
151151
end
152152
end
153+
154+
describe 'header specified' do
155+
let(:source_location) { 'http://example.com' }
156+
let(:resource_properties) do
157+
{
158+
name: name,
159+
source: source_location,
160+
headers: ['Authorization: OAuth 123ABC']
161+
}
162+
end
163+
164+
it 'downloads with header' do
165+
http_dbl = double
166+
expect(http_dbl).to receive(:request) do |request|
167+
expect(request['Authorization']).to eq('OAuth 123ABC')
168+
end
169+
expect(Net::HTTP).to receive(:start).and_yield(http_dbl)
170+
171+
provider.download(source_location, name)
172+
end
173+
end
174+
175+
describe 'multiple headers specified' do
176+
let(:source_location) { 'http://example.com' }
177+
let(:resource_properties) do
178+
{
179+
name: name,
180+
source: source_location,
181+
headers: ['Authorization: OAuth 123ABC', 'Accept: application/json']
182+
}
183+
end
184+
185+
it 'downloads with headers' do
186+
http_dbl = double
187+
expect(http_dbl).to receive(:request) do |request|
188+
expect(request['Authorization']).to eq('OAuth 123ABC')
189+
expect(request['Accept']).to eq('application/json')
190+
end
191+
expect(Net::HTTP).to receive(:start).and_yield(http_dbl)
192+
193+
provider.download(source_location, name)
194+
end
195+
end
196+
197+
describe 'headers and cookie specified' do
198+
let(:source_location) { 'http://example.com' }
199+
let(:resource_properties) do
200+
{
201+
name: name,
202+
source: source_location,
203+
headers: ['Authorization: OAuth 123ABC', 'Accept: application/json'],
204+
cookie: 'something'
205+
}
206+
end
207+
208+
it 'downloads with headers and cookie' do
209+
http_dbl = double
210+
expect(http_dbl).to receive(:request) do |request|
211+
expect(request['Authorization']).to eq('OAuth 123ABC')
212+
expect(request['Accept']).to eq('application/json')
213+
expect(request['Cookie']).to eq('something')
214+
end
215+
expect(Net::HTTP).to receive(:start).and_yield(http_dbl)
216+
217+
provider.download(source_location, name)
218+
end
219+
end
153220
end
154221
end
155222
# rubocop:enable RSpec/MultipleMemoizedHelpers

0 commit comments

Comments
 (0)