Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add x-robots-tag header to search endpoints #286

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading