From 9f88757bdfc55e18ff636cd12d96d1be3836f067 Mon Sep 17 00:00:00 2001 From: Wilmar van Heerden Date: Thu, 4 Jul 2019 12:07:14 +0200 Subject: [PATCH] Add headers option on Http download strategy - 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 Co-authored-by: Nico du Plessis --- CHANGELOG.md | 5 +++- lib/cocoapods-downloader/http.rb | 9 +++++- lib/cocoapods-downloader/remote_file.rb | 6 +++- spec/http_spec.rb | 38 +++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e0dc98..12027f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/cocoapods-downloader/http.rb b/lib/cocoapods-downloader/http.rb index c3a65fc..4ba7f0e 100644 --- a/lib/cocoapods-downloader/http.rb +++ b/lib/cocoapods-downloader/http.rb @@ -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 diff --git a/lib/cocoapods-downloader/remote_file.rb b/lib/cocoapods-downloader/remote_file.rb index e4ae88f..2721cc3 100644 --- a/lib/cocoapods-downloader/remote_file.rb +++ b/lib/cocoapods-downloader/remote_file.rb @@ -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 @@ -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. diff --git a/spec/http_spec.rb b/spec/http_spec.rb index 0456275..c785a68 100644 --- a/spec/http_spec.rb +++ b/spec/http_spec.rb @@ -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