From 5143a1a6f8ab651fc5877b4a03c95642b49c8840 Mon Sep 17 00:00:00 2001 From: Emanuele Bardelli Date: Thu, 5 Aug 2021 15:11:08 -0400 Subject: [PATCH] Add support for redirect with cookie authentication In relation to feedreader/pluto#39, the request cookie gets saved for each redirect. This should support cookie based authentication. --- fetcher/lib/fetcher/worker.rb | 3 +++ fetcher/test/test_get.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/fetcher/lib/fetcher/worker.rb b/fetcher/lib/fetcher/worker.rb index 248ce90..e37aeeb 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 = 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 unless cookie.nil? if use_cache? ## check for existing cache entry in cache store (lookup by uri) @@ -214,6 +216,7 @@ def get_response( src ) newuri = uri + response.header['location'] end uri = newuri + cookie = response['Set-Cookie'] 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