diff --git a/app/controllers/index_controller.rb b/app/controllers/index_controller.rb index 52b457384..0385997c3 100644 --- a/app/controllers/index_controller.rb +++ b/app/controllers/index_controller.rb @@ -11,10 +11,15 @@ def show doi = Doi.where(doi: params[:id], aasm_state: "findable").first fail ActiveRecord::RecordNotFound if doi.blank? + if request.format.symbol.nil? + redirect_to_doi(doi) + return + end + respond_to do |format| format.html do # forward to URL registered in handle system for no content negotiation - redirect_to doi.url, status: :see_other + redirect_to_doi(doi) end format.citation do # extract optional style and locale from header @@ -56,18 +61,23 @@ def show end rescue ActionController::UnknownFormat, ActionController::RoutingError # forward to URL registered in handle system for unrecognized format - redirect_to doi.url, status: :see_other, allow_other_host: true + redirect_to_doi(doi) end - def routing_error - fail ActiveRecord::RecordNotFound - end + private + def redirect_to_doi(doi) + redirect_to doi.url, status: :see_other, allow_other_host: true + end - def method_not_allowed - response.set_header("Allow", "POST") - render json: { - "message": "This endpoint only supports POST requests.", - }.to_json, - status: :method_not_allowed - end + def routing_error + fail ActiveRecord::RecordNotFound + end + + def method_not_allowed + response.set_header("Allow", "POST") + render json: { + "message": "This endpoint only supports POST requests.", + }.to_json, + status: :method_not_allowed + end end diff --git a/spec/requests/index_spec.rb b/spec/requests/index_spec.rb index f39f3e11b..0d612241f 100644 --- a/spec/requests/index_spec.rb +++ b/spec/requests/index_spec.rb @@ -329,6 +329,23 @@ end end + %w[ + */* + application/xml + application/rdf+xml + text/turtle + application/x-turtle + ].each do |accept| + context accept do + it "redirects to the DOI URL" do + get "/#{doi.doi}", nil, { "HTTP_ACCEPT" => accept } + + expect(last_response.status).to eq(303) + expect(last_response.headers["Location"]).to eq(doi.url) + end + end + end + context "missing content type" do it "returns the Doi" do get "/#{doi.doi}"