Skip to content

Commit 2f64cd4

Browse files
committed
Add support for headers parameter in ruby provider
1 parent d0f775f commit 2f64cd4

3 files changed

Lines changed: 82 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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,82 @@
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+
allow(http_dbl).to receive(:request) do |request|
167+
expect(request['Authorization']).to eq('OAuth 123ABC')
168+
end
169+
allow(Net::HTTP).to receive(:start).and_yield(http_dbl)
170+
171+
provider.download(source_location, name)
172+
173+
expect(Net::HTTP).to have_received(:start)
174+
expect(http_dbl).to have_received(:request)
175+
end
176+
end
177+
178+
describe 'multiple headers specified' do
179+
let(:source_location) { 'http://example.com' }
180+
let(:resource_properties) do
181+
{
182+
name: name,
183+
source: source_location,
184+
headers: ['Authorization: OAuth 123ABC', 'Accept: application/json']
185+
}
186+
end
187+
188+
it 'downloads with headers' do
189+
http_dbl = double
190+
allow(http_dbl).to receive(:request) do |request|
191+
expect(request['Authorization']).to eq('OAuth 123ABC')
192+
expect(request['Accept']).to eq('application/json')
193+
end
194+
allow(Net::HTTP).to receive(:start).and_yield(http_dbl)
195+
196+
provider.download(source_location, name)
197+
198+
expect(Net::HTTP).to have_received(:start)
199+
expect(http_dbl).to have_received(:request)
200+
end
201+
end
202+
203+
describe 'headers and cookie specified' do
204+
let(:source_location) { 'http://example.com' }
205+
let(:resource_properties) do
206+
{
207+
name: name,
208+
source: source_location,
209+
headers: ['Authorization: OAuth 123ABC', 'Accept: application/json'],
210+
cookie: 'something'
211+
}
212+
end
213+
214+
it 'downloads with headers and cookie' do
215+
http_dbl = double
216+
allow(http_dbl).to receive(:request) do |request|
217+
expect(request['Authorization']).to eq('OAuth 123ABC')
218+
expect(request['Accept']).to eq('application/json')
219+
expect(request['Cookie']).to eq('something')
220+
end
221+
allow(Net::HTTP).to receive(:start).and_yield(http_dbl)
222+
223+
provider.download(source_location, name)
224+
225+
expect(Net::HTTP).to have_received(:start)
226+
expect(http_dbl).to have_received(:request)
227+
end
228+
end
153229
end
154230
end
155231
# rubocop:enable RSpec/MultipleMemoizedHelpers

0 commit comments

Comments
 (0)