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

Allow headers to be provided to fetch_url #499

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions lib/dragonfly/job/fetch_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ def uri
args.first
end

def arguments
args[1] || {}
end

def headers
arguments[:headers] || {}
end

def url
@url ||= uri =~ /\A\w+:[^\d]/ ? uri : "http://#{uri}"
end
Expand Down Expand Up @@ -71,6 +79,7 @@ def get(url)
http.use_ssl = true if url.scheme == 'https'

request = Net::HTTP::Get.new(url.request_uri)
headers.each { |key, value| request[key] = value }

if url.user || url.password
request.basic_auth(url.user, url.password)
Expand Down
9 changes: 9 additions & 0 deletions spec/dragonfly/job/fetch_url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
job.data.should == 'basic auth'
end

it 'should allow headers to be set' do
stub_request(:get, 'http://headers.com').
with(headers: { "Authorization" => "Bearer ABC123" }).
to_return(:body => 'header result!')

job.fetch_url!('http://headers.com', headers: { "Authorization" => "Bearer ABC123" })
job.data.should == "header result!"
end

[
"place.com",
"http://place.com",
Expand Down