-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DownloadStrategy and Cask::Headers for custom :headers support
accepts a :user_agent argument and/or multiple :cookies headers :user_agent => 'Netscape/1.0', :cookies => { :cookey => "r00t" } Also adds a standalone :fake_user_agent for a default UserAgent (Chrome) fake_user_agent References #1175, #958
- Loading branch information
1 parent
2441f71
commit 06be4b0
Showing
11 changed files
with
206 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'cgi' | ||
|
||
class Cask::DownloadStrategy < CurlDownloadStrategy | ||
attr_reader :headers, :url | ||
|
||
def initialize(cask, resource) | ||
@cask = cask | ||
@headers = cask.headers | ||
@url = resource.url | ||
super(cask.title, resource) | ||
end | ||
|
||
def _fetch | ||
curl(*curl_options) | ||
end | ||
|
||
private | ||
def default_options | ||
[url, '-C', downloaded_size, '-o', temporary_path] | ||
end | ||
|
||
def curl_options | ||
default_options.concat(headers.curl_options) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'cgi' | ||
|
||
class Cask::Headers | ||
attr_reader :headers | ||
|
||
def initialize(args) | ||
@headers = args | ||
create_methods_from_headers | ||
end | ||
|
||
def curl_options(options=[]) | ||
headers.map do |option, _| | ||
options.concat(send("#{option}_args")) if (!!send(option)) | ||
end | ||
options.flatten | ||
end | ||
|
||
private | ||
def create_methods_from_headers | ||
headers.map do |header, option| | ||
(class << self; self; end).class_eval do | ||
define_method(header.to_sym) { option } | ||
end | ||
end | ||
end | ||
|
||
def user_agent_args | ||
['-A', user_agent] | ||
end | ||
|
||
def cookies_args | ||
all_cookies = cookies.map do |name, value| | ||
"#{CGI.escape(name.to_s)}=#{CGI.escape(value.to_s)}" | ||
end | ||
['-b', all_cookies.join(';')] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'test_helper' | ||
require 'download_strategy' | ||
|
||
describe Cask::DownloadStrategy do | ||
class StubbedDownloadStrategy < Cask::DownloadStrategy | ||
def curl(*args) | ||
args | ||
end | ||
end | ||
|
||
before do | ||
@resource = Class.new | ||
@resource.stubs(:url).returns('example.com') | ||
@resource.stubs(:version).returns('3.1') | ||
end | ||
|
||
it 'provides user_agent options down to curl' do | ||
cask = Cask.load('with-headers') | ||
downloader = StubbedDownloadStrategy.new(cask, @resource) | ||
|
||
downloader._fetch.must_include '-A' | ||
downloader._fetch.must_include 'Mozilla/25.0.1' | ||
downloader.must_respond_to :headers | ||
end | ||
|
||
it 'provides :cookie options down to curl' do | ||
cask = Cask.load('with-cookies') | ||
downloader = StubbedDownloadStrategy.new(cask, @resource) | ||
|
||
downloader._fetch.must_include '-b' | ||
downloader._fetch.must_include 'cookie_key=cookie_value' | ||
downloader.must_respond_to :headers | ||
end | ||
|
||
it 'provides :fake_user_agent options down to curl' do | ||
cask = Cask.load('with-fake-user-agent') | ||
downloader = StubbedDownloadStrategy.new(cask, @resource) | ||
|
||
downloader._fetch.must_include '-A' | ||
downloader._fetch.must_include 'Chrome/32.0.1000.00' | ||
downloader.must_respond_to :headers | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'test_helper' | ||
|
||
describe Cask::Headers do | ||
describe '#headers' do | ||
it 'returns header options' do | ||
options = { :user_agent => 'Netscape/1.0.1', | ||
:cookies => { :cookey => 'nom_nom' } } | ||
instance = Cask::Headers.new(options) | ||
|
||
instance.headers.must_equal options | ||
instance.user_agent.must_equal options[:user_agent] | ||
instance.cookies.must_equal options[:cookies] | ||
instance.cookies[:cookey].must_equal options[:cookies][:cookey] | ||
end | ||
end | ||
|
||
describe '#user_agent' do | ||
it 'returns user_agent' do | ||
options = { user_agent: 'Mozilla/25.0.1' } | ||
instance = Cask::Headers.new(options) | ||
|
||
instance.headers.must_equal options | ||
instance.user_agent.must_equal options[:user_agent] | ||
end | ||
end | ||
|
||
describe '#cookies' do | ||
it 'returns cookies' do | ||
options = { cookies: { cookey: 'nom_nom' } } | ||
instance = Cask::Headers.new(options) | ||
|
||
instance.headers.must_equal options | ||
instance.cookies.must_equal options[:cookies] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class WithCookies < TestCask | ||
url 'http://example.com/file.dmg' | ||
headers :cookies => { :cookie_key => 'cookie_value' } | ||
homepage 'http://example.com/' | ||
version '3.2' | ||
sha1 '5a35b6d47150b60e0cf6892bfa7a37a9f9f30d76' | ||
link 'Mac.app' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class WithFakeUserAgent < TestCask | ||
url 'http://example.com/file.dmg' | ||
fake_user_agent | ||
homepage 'http://example.com/' | ||
version '3.2' | ||
sha1 '5a35b6d47150b60e0cf6892bfa7a37a9f9f30d76' | ||
link 'Mac.app' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class WithHeaders < TestCask | ||
url 'http://example.com/file.dmg' | ||
headers :user_agent => 'Mozilla/25.0.1' | ||
homepage 'http://example.com/' | ||
version '3.2' | ||
sha1 '5a35b6d47150b60e0cf6892bfa7a37a9f9f30d76' | ||
link 'Mac.app' | ||
end |