From aea02869f2fc08dc41bcb370a18e28781780ec4b Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Tue, 20 Aug 2024 08:24:51 -0700 Subject: [PATCH] Add timeouts + user agents to faraday requests too --- app/models/cocina.rb | 6 +++++- app/services/metrics_service.rb | 7 +++++-- spec/controllers/object_controller_spec.rb | 17 ++++++++++------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/app/models/cocina.rb b/app/models/cocina.rb index bc6d12d0..e56c49bb 100644 --- a/app/models/cocina.rb +++ b/app/models/cocina.rb @@ -9,7 +9,11 @@ class Cocina def self.find(druid, version = :head) data = Rails.cache.fetch(metadata_cache_key(druid, version), expires_in: 10.minutes) do benchmark "Fetching public json for #{druid} version #{version}" do - response = Faraday.get(public_json_url(druid, version)) + connection = Faraday.new({ url: public_json_url(druid, version), + headers: { user_agent: Settings.user_agent }, + request: { open_timeout: 5 } }) + + response = connection.get raise Purl::Exception, response.status unless response.success? JSON.parse(response.body) diff --git a/app/services/metrics_service.rb b/app/services/metrics_service.rb index d6264d5f..0a7c7c2b 100644 --- a/app/services/metrics_service.rb +++ b/app/services/metrics_service.rb @@ -39,7 +39,10 @@ def event_data(name, properties) end def default_headers - { 'Content-Type': 'application/json' } + { + 'Content-Type': 'application/json', + 'User-Agent': Settings.user_agent + } end def post_json(url, data, headers) @@ -50,6 +53,6 @@ def post_json(url, data, headers) end def connection - @connection ||= Faraday.new(base_url) + @connection ||= Faraday.new({ url: base_url, request: { open_timeout: 5 } }) end end diff --git a/spec/controllers/object_controller_spec.rb b/spec/controllers/object_controller_spec.rb index c4cffa54..7aed97c8 100644 --- a/spec/controllers/object_controller_spec.rb +++ b/spec/controllers/object_controller_spec.rb @@ -9,13 +9,16 @@ allow_any_instance_of(StacksFile).to receive(:path).and_return(Rails.root + 'Gemfile') end + let(:connection) { instance_double(Faraday::Connection) } + describe '#show' do context 'when not logged in' do context "with an invalid druid" do let(:druid) { 'foo' } it 'returns a 404 Not Found' do - allow(Faraday).to receive(:get).with('https://purl.stanford.edu/foo.json').and_return( + allow(Faraday).to receive(:new).with(hash_including(url: 'https://purl.stanford.edu/foo.json')).and_return(connection) + allow(connection).to receive(:get).and_return( instance_double(Faraday::Response, status: 404, success?: false) ) get :show, params: { id: 'foo' } @@ -89,8 +92,8 @@ end before do - allow(Faraday).to receive(:get).with('https://purl.stanford.edu/fd063dh3727.json') - .and_return(instance_double(Faraday::Response, success?: true, body: json)) + allow(Faraday).to receive(:new).with(hash_including(url: 'https://purl.stanford.edu/fd063dh3727.json')).and_return(connection) + allow(connection).to receive(:get).and_return(instance_double(Faraday::Response, success?: true, body: json)) end it 'creates a zip' do @@ -145,8 +148,8 @@ end before do - allow(Faraday).to receive(:get).with('https://purl.stanford.edu/bb142ws0723.json') - .and_return(instance_double(Faraday::Response, success?: true, body: json)) + allow(Faraday).to receive(:new).with(hash_including(url: 'https://purl.stanford.edu/bb142ws0723.json')).and_return(connection) + allow(connection).to receive(:get).and_return(instance_double(Faraday::Response, success?: true, body: json)) end it 'redirects to login' do @@ -209,8 +212,8 @@ end before do - allow(Faraday).to receive(:get).with('https://purl.stanford.edu/bb142ws0723.json') - .and_return(instance_double(Faraday::Response, success?: true, body: json)) + allow(Faraday).to receive(:new).with(hash_including(url: 'https://purl.stanford.edu/bb142ws0723.json')).and_return(connection) + allow(connection).to receive(:get).and_return(instance_double(Faraday::Response, success?: true, body: json)) end it 'creates a zip' do