1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/sinatra-contrib/spec/json_spec.rb

116 lines
3.2 KiB
Ruby
Raw Normal View History

2013-02-12 15:28:34 -05:00
require 'multi_json'
2014-05-09 02:39:16 -04:00
require 'spec_helper'
require 'okjson'
2011-05-09 01:52:36 -04:00
RSpec.shared_examples_for "a json encoder" do |lib, const|
2011-05-09 01:52:36 -04:00
before do
begin
require lib if lib
@encoder = eval(const)
rescue LoadError
2016-05-10 10:08:54 -04:00
skip "unable to load #{lib}"
2011-05-09 01:52:36 -04:00
end
end
it "allows setting :encoder to #{const}" do
enc = @encoder
mock_app { get('/') { json({'foo' => 'bar'}, :encoder => enc) }}
results_in 'foo' => 'bar'
end
it "allows setting settings.json_encoder to #{const}" do
enc = @encoder
mock_app do
set :json_encoder, enc
get('/') { json 'foo' => 'bar' }
end
results_in 'foo' => 'bar'
end
end
RSpec.describe Sinatra::JSON do
2011-05-09 01:52:36 -04:00
def mock_app(&block)
super do
class_eval(&block)
end
end
def results_in(obj)
2016-05-10 10:08:54 -04:00
expect(OkJson.decode(get('/').body)).to eq(obj)
2011-05-09 01:52:36 -04:00
end
it "encodes objects to json out of the box" do
mock_app { get('/') { json :foo => [1, 'bar', nil] } }
results_in 'foo' => [1, 'bar', nil]
2011-05-09 01:52:36 -04:00
end
it "sets the content type to 'application/json'" do
mock_app { get('/') { json({}) } }
2016-05-10 10:08:54 -04:00
expect(get('/')["Content-Type"]).to include("application/json")
2011-05-09 01:52:36 -04:00
end
it "allows overriding content type with :content_type" do
mock_app { get('/') { json({}, :content_type => "foo/bar") } }
2016-05-10 10:08:54 -04:00
expect(get('/')["Content-Type"]).to eq("foo/bar")
2011-05-09 01:52:36 -04:00
end
it "accepts shorthands for :content_type" do
mock_app { get('/') { json({}, :content_type => :js) } }
2016-05-10 10:08:54 -04:00
expect(get('/')["Content-Type"]).to eq("application/javascript;charset=utf-8")
2011-05-09 01:52:36 -04:00
end
it 'calls generate on :encoder if available' do
enc = Object.new
def enc.generate(obj) obj.inspect end
mock_app { get('/') { json(42, :encoder => enc) }}
2016-05-10 10:08:54 -04:00
expect(get('/').body).to eq('42')
2011-05-09 01:52:36 -04:00
end
it 'calls encode on :encoder if available' do
enc = Object.new
def enc.encode(obj) obj.inspect end
mock_app { get('/') { json(42, :encoder => enc) }}
2016-05-10 10:08:54 -04:00
expect(get('/').body).to eq('42')
2011-05-09 01:52:36 -04:00
end
it 'sends :encoder as method call if it is a Symbol' do
mock_app { get('/') { json(42, :encoder => :inspect) }}
2016-05-10 10:08:54 -04:00
expect(get('/').body).to eq('42')
2011-05-09 01:52:36 -04:00
end
it 'calls generate on settings.json_encoder if available' do
enc = Object.new
def enc.generate(obj) obj.inspect end
mock_app do
set :json_encoder, enc
get('/') { json 42 }
end
2016-05-10 10:08:54 -04:00
expect(get('/').body).to eq('42')
2011-05-09 01:52:36 -04:00
end
it 'calls encode on settings.json_encode if available' do
enc = Object.new
def enc.encode(obj) obj.inspect end
mock_app do
set :json_encoder, enc
get('/') { json 42 }
end
2016-05-10 10:08:54 -04:00
expect(get('/').body).to eq('42')
2011-05-09 01:52:36 -04:00
end
it 'sends settings.json_encode as method call if it is a Symbol' do
mock_app do
set :json_encoder, :inspect
get('/') { json 42 }
end
2016-05-10 10:08:54 -04:00
expect(get('/').body).to eq('42')
2011-05-09 01:52:36 -04:00
end
describe('Yajl') { it_should_behave_like "a json encoder", "yajl", "Yajl::Encoder" } unless defined? JRUBY_VERSION
2011-05-09 01:52:36 -04:00
describe('JSON') { it_should_behave_like "a json encoder", "json", "::JSON" }
describe('OkJson') { it_should_behave_like "a json encoder", nil, "OkJson" }
describe('to_json') { it_should_behave_like "a json encoder", "json", ":to_json" }
describe('without') { it_should_behave_like "a json encoder", nil, "Sinatra::JSON" }
2011-05-09 01:52:36 -04:00
end