Skip to content

Commit

Permalink
Add headers option on Http download strategy
Browse files Browse the repository at this point in the history
- Add an option :headers key for the caller to provide custom headers
- Add HTTP headers spec
- Add changelog entry for :headers option

Co-authored-by: Wilmar van Heerden <[email protected]>
Co-authored-by: Nico du Plessis <[email protected]>
  • Loading branch information
3 people committed Jul 11, 2019
1 parent a2df250 commit 9f88757
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

##### Enhancements

* None.
* Add `:headers` option to allow passing in custom headers to `cURL` when downloading source via the `:http` download strategy.
[Wilmar van Heerden](https://github.com/wilmarvh)
[cocoapods-downloader#89](https://github.com/CocoaPods/cocoapods-downloader/issues/89)
[#557](https://github.com/CocoaPods/Core/pull/557)

##### Bug Fixes

Expand Down
9 changes: 8 additions & 1 deletion lib/cocoapods-downloader/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ class Http < RemoteFile
executable :curl

def download_file(full_filename)
curl! '-f', '-L', '-o', full_filename, url, '--create-dirs', '--netrc-optional', '--retry', '2'
parameters = ['-f', '-L', '-o', full_filename, url, '--create-dirs', '--netrc-optional', '--retry', '2']

headers.each do |h|
parameters << '-H'
parameters << h
end unless headers.nil?

curl! parameters
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/cocoapods-downloader/remote_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Pod
module Downloader
class RemoteFile < Base
def self.options
[:type, :flatten, :sha1, :sha256]
[:type, :flatten, :sha1, :sha256, :headers]
end

class UnsupportedFileTypeError < StandardError; end
Expand Down Expand Up @@ -35,6 +35,10 @@ def type
end
end

def headers
options[:headers]
end

# @note The archive is flattened if it contains only one folder and its
# extension is either `tgz`, `tar`, `tbz` or the options specify
# it.
Expand Down
38 changes: 38 additions & 0 deletions spec/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,44 @@ module Downloader
new_options = Downloader.preprocess_options(options)
new_options.should == options
end

it 'passes the correct default parameters to cURL' do
options = { :http => "#{@fixtures_url}/lib.zip" }
downloader = Downloader.for_target(tmp_folder, options)
downloader.expects(:curl!).with(
all_of(
includes('-f'),
includes('-L'),
includes('-o'),
includes('--create-dirs'),
includes('--netrc-optional'),
includes('--retry'),
includes('2'),
),
)
should.raise DownloaderError do
downloader.download
end
end

it 'passes the HTTP headers to cURL' do
options = {
:http => "#{@fixtures_url}/lib.zip",
:headers => ['Accept: application/json', 'Authorization: Bearer'],
}
downloader = Downloader.for_target(tmp_folder, options)
downloader.expects(:curl!).with(
all_of(
includes('-H'),
includes('Accept: application/json'),
includes('-H'),
includes('Authorization: Bearer'),
),
)
should.raise DownloaderError do
downloader.download
end
end
end
end
end

0 comments on commit 9f88757

Please sign in to comment.