Use version from each of the accept headers, not only first one

This commit is contained in:
Piotr Sarnacki 2013-02-18 17:58:45 +01:00
parent 5f91706e64
commit 5345ef818e
2 changed files with 31 additions and 1 deletions

View File

@ -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

View File

@ -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