diff --git a/.rubocop.yml b/.rubocop.yml index 34b5a194..16acdda8 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -64,6 +64,9 @@ Style/StringLiterals: RSpec/MultipleMemoizedHelpers: Enabled: false +RSpec/MessageChain: + Enabled: false + Layout/EmptyLinesAroundAttributeAccessor: Enabled: true Layout/SpaceAroundMethodCallOperator: diff --git a/app/models/iiif_image.rb b/app/models/iiif_image.rb index 20004c94..372623a9 100644 --- a/app/models/iiif_image.rb +++ b/app/models/iiif_image.rb @@ -19,7 +19,10 @@ def initialize(stacks_file:, transformation:, base_uri: Settings.imageserver.bas def response with_retries max_tries: 3, rescue: [HTTP::ConnectionError] do benchmark "Fetch #{image_url}" do - HTTP.timeout(connect: 15).use({ normalize_uri: { normalizer: lambda(&:itself) } }).get(image_url) + HTTP.timeout(connect: 15) + .headers(user_agent: "#{HTTP::Request::USER_AGENT} (#{Settings.user_agent})") + .use({ normalize_uri: { normalizer: lambda(&:itself) } }) + .get(image_url) end end end diff --git a/app/services/iiif_metadata_service.rb b/app/services/iiif_metadata_service.rb index 5d26c91c..cfe18211 100644 --- a/app/services/iiif_metadata_service.rb +++ b/app/services/iiif_metadata_service.rb @@ -47,7 +47,9 @@ def retrieve handle_response( # Disable url normalization as an upstream bug in addressable causes issues for `+` # https://github.com/sporkmonger/addressable/issues/386 - HTTP.use({ normalize_uri: { normalizer: lambda(&:itself) } }).get(@url) + HTTP.timeout(connect: 15) + .headers(user_agent: "#{HTTP::Request::USER_AGENT} (#{Settings.user_agent})") + .use({ normalize_uri: { normalizer: lambda(&:itself) } }).get(@url) ) end end diff --git a/config/settings.yml b/config/settings.yml index 8f6b591f..34dd3aa8 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -62,3 +62,5 @@ cors: token: default_expiry_time: <%= 1.hour %> + +user_agent: 'stacks.stanford.edu' diff --git a/spec/models/projection_spec.rb b/spec/models/projection_spec.rb index 01b3900b..608ee0cd 100644 --- a/spec/models/projection_spec.rb +++ b/spec/models/projection_spec.rb @@ -110,7 +110,7 @@ let(:options) { { size: 'max', region: 'full' } } it 'allows the user to see the full-resolution image' do - allow(HTTP).to receive(:use).and_return(http_client) + allow(HTTP).to receive_message_chain(:timeout, :headers, :use).and_return(http_client) allow(http_client).to receive(:get).and_return(double(body: nil)) subject.response expect(http_client).to have_received(:get).with(%r{/full/max/0/default.jpg}) @@ -121,7 +121,7 @@ let(:options) { { size: '!850,700', region: 'full' } } it 'returns original size when requested dimensions are larger' do - allow(HTTP).to receive(:use).and_return(http_client) + allow(HTTP).to receive_message_chain(:timeout, :headers, :use).and_return(http_client) allow(http_client).to receive(:get).and_return(double(body: nil)) subject.response expect(http_client).to have_received(:get).with(%r{/full/!800,600/0/default.jpg}) @@ -138,7 +138,7 @@ let(:options) { { size: 'max', region: 'full' } } it 'limits users to a thumbnail' do - allow(HTTP).to receive(:use) + allow(HTTP).to receive_message_chain(:timeout, :headers, :use) .and_return(http_client) allow(http_client).to receive(:get).and_return(double(body: nil)) subject.response @@ -150,7 +150,7 @@ let(:options) { { size: '!100,100', region: 'full' } } it 'limits users to a thumbnail' do - allow(HTTP).to receive(:use) + allow(HTTP).to receive_message_chain(:timeout, :headers, :use) .and_return(http_client) allow(http_client).to receive(:get).and_return(double(body: nil)) subject.response @@ -162,7 +162,7 @@ let(:options) { { size: '!800,880', region: 'full' } } it 'limits users to a thumbnail' do - allow(HTTP).to receive(:use) + allow(HTTP).to receive_message_chain(:timeout, :headers, :use) .and_return(http_client) allow(http_client).to receive(:get).and_return(double(body: nil)) subject.response @@ -174,7 +174,7 @@ let(:options) { { size: '100,100', region: 'square' } } it 'limits users to a thumbnail' do - allow(HTTP).to receive(:use) + allow(HTTP).to receive_message_chain(:timeout, :headers, :use) .and_return(http_client) allow(http_client).to receive(:get).and_return(double(body: nil)) subject.response diff --git a/spec/requests/iiif_auth_request_spec.rb b/spec/requests/iiif_auth_request_spec.rb index 221fc6ae..ff1e06d3 100644 --- a/spec/requests/iiif_auth_request_spec.rb +++ b/spec/requests/iiif_auth_request_spec.rb @@ -27,7 +27,7 @@ describe "#show" do before do allow_any_instance_of(Projection).to receive(:valid?).and_return(true) - allow(HTTP).to receive(:use).and_return(http_client) + allow(HTTP).to receive_message_chain(:timeout, :headers, :use).and_return(http_client) allow(http_client).to receive(:get).and_return(instance_double(HTTP::Response, status: 200, body: StringIO.new)) allow_any_instance_of(IiifController).to receive(:current_user).and_return(current_user) allow_any_instance_of(IiifController).to receive(:current_image).and_return(current_image) diff --git a/spec/services/iiif_metadata_service_spec.rb b/spec/services/iiif_metadata_service_spec.rb index a8a1c655..c7910c1d 100644 --- a/spec/services/iiif_metadata_service_spec.rb +++ b/spec/services/iiif_metadata_service_spec.rb @@ -22,7 +22,7 @@ let(:response) { instance_double(HTTP::Response, code: 200, body: json) } before do - allow(HTTP).to receive(:use).and_return(http_client) + allow(HTTP).to receive_message_chain(:timeout, :headers, :use).and_return(http_client) allow(http_client).to receive(:get) .with("https://sul-imageserver-uat.stanford.edu/cantaloupe/iiif/2/#{image_server_path(druid, file_name)}/info.json") .and_return(response) @@ -57,7 +57,7 @@ let(:empty_json) { '' } let(:bad_response) { instance_double(HTTP::Response, code: 200, body: empty_json) } before do - allow(HTTP).to receive(:use) + allow(HTTP).to receive_message_chain(:timeout, :headers, :use) .and_return(http_client) allow(http_client).to receive(:get) .with("https://sul-imageserver-uat.stanford.edu/cantaloupe/iiif/2/#{image_server_path(druid, file_name)}/info.json")