From f66156884166e4f52327b744d1a8303f87b8ac63 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Fri, 24 Jan 2014 21:42:04 -0800 Subject: [PATCH] Ensure we always respond with json (except the redirect at the root URI). closes #1739 --- .../app/controllers/application_controller.rb | 1 + .../api/app/controllers/static_controller.rb | 1 + .../test/integration/collections_api_test.rb | 21 +++++++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/services/api/app/controllers/application_controller.rb b/services/api/app/controllers/application_controller.rb index 7ffa38af93..fff52f5d62 100644 --- a/services/api/app/controllers/application_controller.rb +++ b/services/api/app/controllers/application_controller.rb @@ -1,6 +1,7 @@ class ApplicationController < ActionController::Base include CurrentApiClient + respond_to :json protect_from_forgery around_filter :thread_with_auth_info, :except => [:render_error, :render_not_found] diff --git a/services/api/app/controllers/static_controller.rb b/services/api/app/controllers/static_controller.rb index 77654990e0..ba69b8464f 100644 --- a/services/api/app/controllers/static_controller.rb +++ b/services/api/app/controllers/static_controller.rb @@ -1,4 +1,5 @@ class StaticController < ApplicationController + respond_to :json, :html skip_before_filter :find_object_by_uuid skip_before_filter :render_404_if_no_object diff --git a/services/api/test/integration/collections_api_test.rb b/services/api/test/integration/collections_api_test.rb index f2dfbee947..076b2342a2 100644 --- a/services/api/test/integration/collections_api_test.rb +++ b/services/api/test/integration/collections_api_test.rb @@ -3,11 +3,28 @@ require 'test_helper' class CollectionsApiTest < ActionDispatch::IntegrationTest fixtures :all + def jresponse + @jresponse ||= ActiveSupport::JSON.decode @response.body + end + test "should get index" do get "/arvados/v1/collections", {:format => :json}, {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:active).api_token}"} - @json_response ||= ActiveSupport::JSON.decode @response.body assert_response :success - assert_equal "arvados#collectionList", @json_response['kind'] + assert_equal "arvados#collectionList", jresponse['kind'] + end + + test "controller 404 response is json" do + get "/arvados/v1/thingsthatdonotexist", {:format => :xml}, {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:active).api_token}"} + assert_response 404 + assert_equal 1, jresponse['errors'].length + assert_equal true, jresponse['errors'][0].is_a?(String) + end + + test "object 404 response is json" do + get "/arvados/v1/groups/zzzzz-j7d0g-o5ba971173cup4f", {}, {'HTTP_AUTHORIZATION' => "OAuth2 #{api_client_authorizations(:active).api_token}"} + assert_response 404 + assert_equal 1, jresponse['errors'].length + assert_equal true, jresponse['errors'][0].is_a?(String) end end -- 2.30.2