Merge pull request #97 from travis-ci/core-gh1555-pretty-print-json

Pretty print JSON data
This commit is contained in:
Konstantin Haase 2013-12-06 05:27:55 -08:00
commit fc06e3bff9
2 changed files with 45 additions and 2 deletions

View File

@ -10,7 +10,13 @@ class Travis::Api::App
def respond_with(resource, options = {})
result = respond(resource, options)
result = result.to_json if result && response.content_type =~ /application\/json/
if result && response.content_type =~ /application\/json/
if !params[:pretty].nil? && (params[:pretty].downcase == 'true' || params[:pretty].to_i > 0)
result = JSON.pretty_generate(result)
else
result = result.to_json
end
end
halt result || 404
end

View File

@ -1,5 +1,42 @@
require 'spec_helper'
describe Travis::Api::App::Endpoint::Builds do
it 'has to be tested'
include Travis::Testing::Stubs
it 'works with default options' do
get('/repos.json', {}).should be_ok
end
context '/repos.json is requested' do
before :each do
@plain_response_body = get('/repos.json').body
end
context 'when `pretty=true` is given' do
it 'prints pretty formatted data' do
response = get('/repos.json?pretty=true')
response.should be_ok
response.body.should_not eq(@plain_response_body)
response.body.should match(/\n/)
end
end
context 'when `pretty=1` is given' do
it 'prints pretty formatted data' do
response = get('/repos.json?pretty=1')
response.should be_ok
response.body.should_not eq(@plain_response_body)
response.body.should match(/\n/)
end
end
context 'when `pretty=bogus` is given' do
it 'prints plain-formatted data' do
response = get('/repos.json?pretty=bogus')
response.should be_ok
response.body.should eq(@plain_response_body)
end
end
end
end