Skip to content

Commit a0f95ad

Browse files
committed
Add conditional accept header support.
1 parent c9231d7 commit a0f95ad

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

lib/feedkit/request.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def initialize(url, on_redirect: nil, auto_inflate: true, username: nil, passwor
2727
end
2828

2929
def download
30-
if @url.respond_to?(:host) && curl_hosts.include?(@url.host)
30+
if curl_host?
3131
return Curl.download(@parsed_url.url)
3232
end
3333

@@ -81,10 +81,11 @@ def client
8181
def headers
8282
Hash.new.tap do |hash|
8383
hash[:user_agent] = @user_agent || "Feedbin"
84-
hash[:accept_encoding] = "gzip, deflate" if @auto_inflate
85-
hash[:if_none_match] = @etag unless @etag.nil?
86-
hash[:if_modified_since] = @last_modified unless @last_modified.nil?
87-
hash[:authorization] = basic_auth unless basic_auth.nil?
84+
hash[:accept_encoding] = "gzip, deflate" if @auto_inflate
85+
hash[:accept] = "application/xml" if accept_header_host?
86+
hash[:if_none_match] = @etag unless @etag.nil?
87+
hash[:if_modified_since] = @last_modified unless @last_modified.nil?
88+
hash[:authorization] = basic_auth unless basic_auth.nil?
8889
end
8990
end
9091

@@ -152,8 +153,14 @@ def request_error!(exception)
152153
end
153154
end
154155

155-
def curl_hosts
156-
ENV["FEEDKIT_CURL_HOSTS"]&.split(",") || []
156+
def curl_host?
157+
hosts = ENV["FEEDKIT_CURL_HOSTS"]&.split(",") || []
158+
@url.respond_to?(:host) && hosts.include?(@url.host)
159+
end
160+
161+
def accept_header_host?
162+
hosts = ENV["FEEDKIT_ACCEPT_HOSTS"]&.split(",") || []
163+
@url.respond_to?(:host) && hosts.include?(@url.host)
157164
end
158165
end
159166
end

test/feedkit/request_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,20 @@ def test_should_allow_setting_auto_inflate
232232

233233
assert_requested(:get, without_auto_inflate) { |request| request.headers["Accept-Encoding"] == nil }
234234
end
235+
236+
def test_should_use_accept_header
237+
ENV["FEEDKIT_ACCEPT_HOSTS"] = "www.example.com"
238+
with_accept_url = "http://www.example.com"
239+
stub_request(:any, with_accept_url)
240+
241+
::Feedkit::Request.download(with_accept_url)
242+
243+
assert_requested :get, with_accept_url, headers: {"Accept" => "application/xml"}
244+
245+
without_accept_url = "http://www.example2.com"
246+
stub_request(:any, without_accept_url)
247+
::Feedkit::Request.download(without_accept_url)
248+
249+
assert_requested(:get, without_accept_url) { |request| request.headers["Accept"] == nil }
250+
end
235251
end

0 commit comments

Comments
 (0)