Skip to content
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
13 changes: 13 additions & 0 deletions fetcher/lib/fetcher/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def get_response( src )

redirect_limit = 6
response = nil
cookie_jar = nil

until false
raise ArgumentError, 'HTTP redirect too deep' if redirect_limit == 0
Expand All @@ -164,6 +165,7 @@ def get_response( src )
logger.debug "GET #{uri.request_uri} uri=#{uri}, redirect_limit=#{redirect_limit}"

headers = { 'User-Agent' => "fetcher gem v#{VERSION}" }
headers['Cookie'] = cookie_jar unless cookie_jar.nil?

if use_cache?
## check for existing cache entry in cache store (lookup by uri)
Expand Down Expand Up @@ -214,6 +216,17 @@ def get_response( src )
newuri = uri + response.header['location']
end
uri = newuri

cookie = response.get_fields('set-cookie')
if cookie.respond_to?('each')
cookies_array = Array.new
cookie.each { | cookie |
cookies_array.push(cookie.split('; ')[0])
}
cookie_jar = cookies_array.join('; ')
end

logger.debug "Set cookie: #{cookie_jar}"
else
puts "*** error - fetch HTTP - #{response.code} #{response.message}"
break # will return response
Expand Down
12 changes: 12 additions & 0 deletions fetcher/test/test_get.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,16 @@ def test_get_not_found
assert_equal '404', res.code
end

def test_get_cookie_redirect
url = 'https://link.springer.com/search.rss?search-within=Journal&facet-journal-id=10827'
worker = Fetcher::Worker.new
res = worker.get( url )
pp res

assert_equal '200', res.code # note: returned code is a string e.g. '200' not 200
assert_equal 'OK', res.message
assert_equal 'text/xml', res.content_type

end

end # class TestGet