Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new CurlPostDownloadStrategy #3422

Merged
merged 1 commit into from
Mar 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/CASK_LANGUAGE_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ of key/value pairs appended to `url`:

| key | value |
| ------------------ | ----------- |
| `:using` | the symbol `:post` is the only legal value
| `:cookies` | a hash of cookies to be set in the download request
| `:referer` | a string holding the URL to set as referrer in the download request
| `:user_agent` | a string holding the user agent to set for the download request. Can also be set to the symbol `:fake`, which will use a generic Browser-like user agent string. we prefer `:fake` when the server does not require a specific user agent.
| `:data` | a hash of parameters to be set in the POST request

Example: [java.rb](../Casks/java.rb)

Expand Down
2 changes: 2 additions & 0 deletions lib/cask/download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def perform(force=false)
cask = @cask
if cask.url.using == :svn
downloader = Cask::SubversionDownloadStrategy.new(cask)
elsif cask.url.using == :post
downloader = Cask::CurlPostDownloadStrategy.new(cask)
else
downloader = Cask::CurlDownloadStrategy.new(cask)
end
Expand Down
19 changes: 19 additions & 0 deletions lib/cask/download_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ def referer_args
end
end

class Cask::CurlPostDownloadStrategy < Cask::CurlDownloadStrategy

def curl_args
super
default_curl_args.concat(post_args)
end

def post_args
if cask_url.data
# sort_by is for predictability between Ruby versions
cask_url.data.sort_by{ |key, value| key.to_s }.map do |key, value|
['-d', "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"]
end.flatten()
else
['-X', 'POST']
end
end
end

class Cask::SubversionDownloadStrategy < SubversionDownloadStrategy
include Cask::DownloadStrategy

Expand Down
3 changes: 2 additions & 1 deletion lib/cask/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Cask::URL
FAKE_USER_AGENT = 'Chrome/32.0.1000.00'

attr_reader :using, :revision, :trust_cert, :uri, :cookies, :referer
attr_reader :using, :revision, :trust_cert, :uri, :cookies, :referer, :data

extend Forwardable
def_delegators :uri, :path, :scheme, :to_s
Expand All @@ -16,6 +16,7 @@ def initialize(uri, options={})
@using = options[:using]
@revision = options[:revision]
@trust_cert = options[:trust_cert]
@data = options[:data]
end

def user_agent
Expand Down
45 changes: 45 additions & 0 deletions test/cask/download_strategy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,51 @@ def includes_args?(args, expected)
shutup { downloader.fetch }
end

describe Cask::CurlPostDownloadStrategy do
it 'properly assigns a name and Resource based on the cask' do
cask = Cask.load('basic-cask')

downloader = Cask::CurlPostDownloadStrategy.new(BasicCask)
downloader.name.must_equal 'basic-cask'
downloader.resource.name.must_equal 'basic-cask'
downloader.resource.url.must_equal cask.url.to_s
downloader.resource.version.to_s.must_equal cask.version
end

it 'adds curl args for post arguments' do
WithPostArgs = Class.new(Cask) do
url 'http://host/path/to/form.html', :data => {
:coo => 'kie',
:mon => 'ster'
}, :using => :post
version '1.2.3.4'
end

downloader = Cask::CurlPostDownloadStrategy.new(WithPostArgs)
downloader.temporary_path.stubs(:rename)
downloader.expects(:curl).with do |*args|
# includes_args tests set membership;
# repeated options don't pass the test
includes_args?(args, ['-d', 'coo=kie', 'mon=ster'])
end
shutup { downloader.fetch }
end

it 'adds curl args for post arguments with no :data' do
WithoutPostArgs = Class.new(Cask) do
url 'http://host/path/to/form.html', :using => :post
version '1.2.3.4'
end

downloader = Cask::CurlPostDownloadStrategy.new(WithoutPostArgs)
downloader.temporary_path.stubs(:rename)
downloader.expects(:curl).with do |*args|
includes_args?(args, ['-X', 'POST'])
end
shutup { downloader.fetch }
end
end

describe Cask::SubversionDownloadStrategy do
it 'recognizes the SVN download strategy' do
cask = Cask.load('svn-download-cask')
Expand Down