From 5345ef818ec2a150fc788d6204111974514b9db3 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Mon, 18 Feb 2013 17:58:45 +0100 Subject: [PATCH] Use version from each of the accept headers, not only first one --- lib/travis/api/app/responders/json.rb | 6 +++++- spec/integration/version_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 spec/integration/version_spec.rb diff --git a/lib/travis/api/app/responders/json.rb b/lib/travis/api/app/responders/json.rb index ef8377b0..464be510 100644 --- a/lib/travis/api/app/responders/json.rb +++ b/lib/travis/api/app/responders/json.rb @@ -25,13 +25,17 @@ class Travis::Api::App end def builder - @builder ||= Travis::Api.builder(resource, { :version => accept_version }.merge(options)) + @builder ||= Travis::Api.builder(resource, { :version => version }.merge(options)) end def accept_params (options[:accept].params || {}).symbolize_keys end + def version + options[:accept].version || Travis::Api::App::Helpers::Accept::DEFAULT_VERSION + end + def params (request.params || {}).merge(accept_params) end diff --git a/spec/integration/version_spec.rb b/spec/integration/version_spec.rb new file mode 100644 index 00000000..818a0dbd --- /dev/null +++ b/spec/integration/version_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe 'App' do + before do + add_endpoint '/foo' do + get '/' do + respond_with foo: 'bar' + end + end + end + + it 'uses version from current accept header' do + Travis::Api.expects(:builder).with { |r, options| options[:version] == 'v1' } + + Travis::Api::App::Responders::Json.any_instance.stubs(:apply?). + returns(false).then.returns(true) + + response = get '/foo', {}, 'HTTP_ACCEPT' => 'application/json; version=2, application/json; version=1' + response.content_type.should == 'application/json;charset=utf-8' + end + + it 'uses v1 by default' do + Travis::Api.expects(:builder).with { |r, options| options[:version] == 'v1' } + get '/foo', {}, 'HTTP_ACCEPT' => 'application/json' + end +end