This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
/
json_spec.rb
115 lines (99 loc) · 3.18 KB
/
json_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require 'backports'
require_relative 'spec_helper'
require_relative 'okjson'
shared_examples_for "a json encoder" do |lib, const|
before do
begin
require lib if lib
@encoder = eval(const)
rescue LoadError
pending "unable to load #{lib}"
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
describe Sinatra::JSON do
def mock_app(&block)
super do
helpers Sinatra::JSON
class_eval(&block)
end
end
def results_in(obj)
OkJson.decode(get('/').body).should == obj
end
it "encodes objects to json out of the box" do
mock_app { get('/') { json :foo => [1, 'bar'] } }
results_in 'foo' => [1, 'bar']
end
it "sets the content type to 'application/json'" do
mock_app { get('/') { json({}) } }
get('/')["Content-Type"].should include("application/json")
end
it "allows overriding content type with :content_type" do
mock_app { get('/') { json({}, :content_type => "foo/bar") } }
get('/')["Content-Type"].should == "foo/bar"
end
it "accepts shorthands for :content_type" do
mock_app { get('/') { json({}, :content_type => :js) } }
get('/')["Content-Type"].should == "application/javascript;charset=utf-8"
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) }}
get('/').body.should == '42'
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) }}
get('/').body.should == '42'
end
it 'sends :encoder as method call if it is a Symbol' do
mock_app { get('/') { json(42, :encoder => :inspect) }}
get('/').body.should == '42'
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
get('/').body.should == '42'
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
get('/').body.should == '42'
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
get('/').body.should == '42'
end
describe('Yajl') { it_should_behave_like "a json encoder", "yajl", "Yajl::Encoder" }
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" }
end