Skip to content

Commit

Permalink
add x-robots-tag header to search endpoints (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackBlackLight authored Sep 11, 2024
1 parent a5af3ea commit 8aaec91
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/api/v1/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ class Search < Grape::API
content_type :json, 'application/json'
default_format :json

before do
header['X-Robots-Tag'] = 'none'
end

params do
optional :search_type, coerce: Symbol, default: :keyword, values: V1::Helpers::Solr::SEARCH_TYPES,
desc: 'type of search to be conducted, in most cases a keyword search should be sufficient'
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CatalogController < ApplicationController

# rubocop:disable Rails/LexicallyScopedActionFilter
before_action :record_view_stats, only: :show
before_action :set_xrobots_tag_to_none, only: :index
# rubocop:enable Rails/LexicallyScopedActionFilter

class LazyFeatureQueryFacet < Hash
Expand Down Expand Up @@ -352,6 +353,10 @@ def home

private

def set_xrobots_tag_to_none
response.headers['X-Robots-Tag'] = 'none'
end

def record_view_stats
record_stats(params['id'], Statistic::VIEW)
end
Expand Down
5 changes: 5 additions & 0 deletions spec/api/v1/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
'error' => 'search_type does not have a valid value'
)
end

it 'returns x-robots-tag none in headers' do
get '/api/v1/search?search_type=title'
expect(response.headers['X-Robots-Tag']).to eq('none')
end
end

context 'applies sort order' do
Expand Down
12 changes: 12 additions & 0 deletions spec/requests/catalog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,17 @@
get '/search?per_page=Bad+Value&sort=Published+Latest'
expect(response.status).to eq(400)
end

it 'returns x-robots-tag none in headers' do
get '/search?per_page=20&sort=Published+Latest'
expect(response.headers['X-Robots-Tag']).to eq('none')
end
end

describe '/show' do
it 'does not return x-robots-tag none in headers' do
get '/doi/10.7916/ALICE'
expect(response.headers['X-Robots-Tag']).to be_nil
end
end
end

0 comments on commit 8aaec91

Please sign in to comment.