diff --git a/lib/travis/api/app/helpers/respond_with.rb b/lib/travis/api/app/helpers/respond_with.rb index a5b19204..19f4c4fd 100644 --- a/lib/travis/api/app/helpers/respond_with.rb +++ b/lib/travis/api/app/helpers/respond_with.rb @@ -10,8 +10,8 @@ class Travis::Api::App def respond_with(resource, options = {}) result = respond(resource, options) - result = result ? result.to_json : 404 - halt result + result = result.to_json if result && response.content_type =~ /application\/json/ + halt result || 404 end def body(value = nil, options = {}, &block) @@ -24,10 +24,18 @@ class Travis::Api::App def respond(resource, options) resource = apply_service_responder(resource, options) - response = acceptable_formats.find do |accept| + response = nil + 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? + response = responder.apply if responder.apply? + end + end + + if responders = options[:responders] + responders.each do |klass| + responder = klass.new(self, response, options) + response = responder.apply if responder.apply? end end diff --git a/lib/travis/api/app/responders/image.rb b/lib/travis/api/app/responders/image.rb index d80d28e4..c6c6a17d 100644 --- a/lib/travis/api/app/responders/image.rb +++ b/lib/travis/api/app/responders/image.rb @@ -8,7 +8,7 @@ module Travis::Api::App::Responders headers['Pragma'] = "no-cache" headers['Expires'] = Time.now.utc.httpdate headers['Content-Disposition'] = %(inline; filename="#{File.basename(filename)}") - halt send_file(filename, type: :png, last_modified: last_modified) + send_file(filename, type: :png, last_modified: last_modified) end private diff --git a/lib/travis/api/app/responders/json.rb b/lib/travis/api/app/responders/json.rb index 15388f6b..012b2be8 100644 --- a/lib/travis/api/app/responders/json.rb +++ b/lib/travis/api/app/responders/json.rb @@ -10,7 +10,7 @@ class Travis::Api::App def apply super - halt result.to_json if result + result end private diff --git a/lib/travis/api/app/responders/plain.rb b/lib/travis/api/app/responders/plain.rb index 9d045478..63d3d223 100644 --- a/lib/travis/api/app/responders/plain.rb +++ b/lib/travis/api/app/responders/plain.rb @@ -21,7 +21,7 @@ module Travis::Api::App::Responders headers['Content-Disposition'] = %(#{disposition}; filename="#{filename}") - halt(params[:deansi] ? clear_ansi(resource.content) : resource.content) + params[:deansi] ? clear_ansi(resource.content) : resource.content end private diff --git a/lib/travis/api/app/responders/xml.rb b/lib/travis/api/app/responders/xml.rb index b69911b1..836d5f20 100644 --- a/lib/travis/api/app/responders/xml.rb +++ b/lib/travis/api/app/responders/xml.rb @@ -22,7 +22,7 @@ module Travis::Api::App::Responders def apply super - halt TEMPLATE % data + TEMPLATE % data end private diff --git a/spec/integration/responders_spec.rb b/spec/integration/responders_spec.rb new file mode 100644 index 00000000..8624b486 --- /dev/null +++ b/spec/integration/responders_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe 'App' do + before do + FactoryGirl.create(:test, :number => '3.1', :queue => 'builds.common') + + responder = Class.new(Travis::Api::App::Responders::Base) do + def apply? + true + end + + def apply + resource[:extra] = 'moar!' + + resource + end + end + + add_endpoint '/foo' do + get '/hash' do + respond_with({ foo: 'bar' }, responders: [responder]) + end + end + end + + it '' do + response = get '/foo/hash', {}, 'HTTP_ACCEPT' => 'application/json' + JSON.parse(response.body).should == { 'foo' => 'bar', 'extra' => 'moar!' } + end +end diff --git a/spec/unit/responders/json_spec.rb b/spec/unit/responders/json_spec.rb index 37146d58..5b43bbc1 100644 --- a/spec/unit/responders/json_spec.rb +++ b/spec/unit/responders/json_spec.rb @@ -23,8 +23,7 @@ module Travis::Api::App::Responders let(:resource) { { foo: 'bar' } } it 'returns resource converted to_json' do - json.expects(:halt).with({ foo: 'bar' }.to_json) - json.apply + json.apply.should == { foo: 'bar' } end end @@ -46,8 +45,7 @@ module Travis::Api::App::Responders end it 'returns proper data converted to json' do - json.expects(:halt).with({ foo: 'bar' }.to_json) - json.apply + json.apply.should == { foo: 'bar' } end end end