diff --git a/fetcher/lib/fetcher/worker.rb b/fetcher/lib/fetcher/worker.rb index 248ce90..9d531bf 100644 --- a/fetcher/lib/fetcher/worker.rb +++ b/fetcher/lib/fetcher/worker.rb @@ -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 @@ -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) @@ -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 diff --git a/fetcher/test/test_get.rb b/fetcher/test/test_get.rb index aab0481..ee73eab 100644 --- a/fetcher/test/test_get.rb +++ b/fetcher/test/test_get.rb @@ -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