Handle chunked=true param in Accept header

This commit is contained in:
Piotr Sarnacki 2013-02-18 16:58:06 +01:00
parent 0e6757080e
commit 36783e6359
2 changed files with 42 additions and 2 deletions

View File

@ -4,7 +4,7 @@ class Travis::Api::App
include Helpers::Accept
def apply?
super && !resource.is_a?(String) && !resource.nil?
super && !resource.is_a?(String) && !resource.nil? && accepts_log?
end
def apply
@ -13,13 +13,28 @@ class Travis::Api::App
private
def accepts_log?
return true unless resource.is_a?(Log)
chunked = accept_params[:chunked]
chunked ? !resource.aggregated_at : true
end
def result
builder ? builder.new(resource, request.params).data : resource
builder ? builder.new(resource, params).data : resource
end
def builder
@builder ||= Travis::Api.builder(resource, { :version => accept_version }.merge(options))
end
def accept_params
(options[:accept].params || {}).symbolize_keys
end
def params
(request.params || {}).merge(accept_params)
end
end
end
end

View File

@ -49,5 +49,30 @@ describe 'Jobs' do
response.headers['Location'].should == "https://s3.amazonaws.com/archive.travis-ci.org/jobs/#{job.id}/log.txt"
end
end
context 'with chunked log requested' do
it 'responds with 406 when log is already aggregated' do
job.log.update_attributes(aggregated_at: Time.now)
headers = { 'HTTP_ACCEPT' => 'application/vnd.travis-ci.2+json; chunked=true' }
response = get "/jobs/#{job.id}/log", {}, headers
response.status.should == 406
end
it 'responds with chunks instead of full log' do
job.log.parts << Log::Part.new(content: 'foo', number: 1, final: false)
job.log.parts << Log::Part.new(content: 'bar', number: 2, final: true)
headers = { 'HTTP_ACCEPT' => 'application/vnd.travis-ci.2+json; chunked=true' }
response = get "/jobs/#{job.id}/log", {}, headers
response.should deliver_json_for(job.log, version: 'v2', params: { chunked: true})
end
it 'responds with full log if chunks are not available and full log is accepted' do
job.log.update_attributes(aggregated_at: Time.now)
headers = { 'HTTP_ACCEPT' => 'application/vnd.travis-ci.2+json; chunked=true, application/vnd.travis-ci.2+json' }
response = get "/jobs/#{job.id}/log", {}, headers
response.should deliver_json_for(job.log, version: 'v2')
end
end
end
end