From 65b1e6b988a9e0dcfb0fdf3a3079e74c4219db25 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Sat, 16 Feb 2013 05:03:51 +0100 Subject: [PATCH] Respond with 406 if we can't return requested formats --- lib/travis/api/app/helpers/respond_with.rb | 4 +++- spec/integration/formats_handling_spec.rb | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/travis/api/app/helpers/respond_with.rb b/lib/travis/api/app/helpers/respond_with.rb index 565f0172..a5b19204 100644 --- a/lib/travis/api/app/helpers/respond_with.rb +++ b/lib/travis/api/app/helpers/respond_with.rb @@ -24,12 +24,14 @@ class Travis::Api::App def respond(resource, options) resource = apply_service_responder(resource, options) - acceptable_formats.find do |accept| + response = acceptable_formats.find do |accept| responders(resource, options).find do |const| responder = const.new(self, resource, options.dup.merge(accept: accept)) responder.apply if responder.apply? end end + + response || (resource ? error(406) : error(404)) end def apply_service_responder(resource, options) diff --git a/spec/integration/formats_handling_spec.rb b/spec/integration/formats_handling_spec.rb index ede76766..efda70b5 100644 --- a/spec/integration/formats_handling_spec.rb +++ b/spec/integration/formats_handling_spec.rb @@ -24,4 +24,9 @@ describe 'App' do response = get '/foo', {}, 'HTTP_ACCEPT' => 'image/jpeg, application/json' response.content_type.should =~ /^application\/json/ end + + it 'responds with 406 if server can\'t use any mime type' do + response = get '/foo/hash', {}, 'HTTP_ACCEPT' => 'text/plain, image/jpeg' + response.status.should == 406 + end end