-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathold_grape_ams_spec.rb
More file actions
159 lines (141 loc) · 3.84 KB
/
Copy pathold_grape_ams_spec.rb
File metadata and controls
159 lines (141 loc) · 3.84 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require 'spec_helper'
require 'support/models/user'
require 'support/models/blog_post'
require 'support/serializers/user_serializer'
require 'support/serializers/blog_post_serializer'
require 'grape-active_model_serializers'
describe Grape::ActiveModelSerializers do
let(:app) { Class.new(Grape::API) }
subject { last_response.body }
before do
ActiveModelSerializers.config.adapter = :json
app.format :json
app.formatter :json, Grape::Formatter::ActiveModelSerializers
end
it 'should respond with proper content-type' do
app.get('/home/users', serializer: UserSerializer) do
User.new
end
get('/home/users')
expect(last_response.headers['Content-Type']).to eql 'application/json'
end
context 'serializer is set to nil' do
before do
app.get('/home', serializer: nil) do
{ user: { first_name: 'JR', last_name: 'HE' } }
end
end
it 'uses the built in grape serializer' do
get('/home')
expect(subject).to eq(
'{"user":{"first_name":"JR","last_name":"HE"}}'
)
end
end
context "serializer isn't set" do
before do
app.get('/home') do
User.new(
first_name: 'JR',
last_name: 'HE',
email: 'contact@jrhe.co.uk'
)
end
end
it 'infers the serializer' do
get '/home'
expect(subject).to eq(
'{"user":{"first_name":"JR","last_name":"HE"}}'
)
end
end
it 'serializes arrays of objects' do
app.get('/users') do
user = User.new(
first_name: 'JR',
last_name: 'HE',
email: 'contact@jrhe.co.uk'
)
[user, user]
end
get '/users'
expect(subject).to eq(
'{"users":[' \
'{"first_name":"JR","last_name":"HE"},' \
'{"first_name":"JR","last_name":"HE"}' \
']}'
)
end
context 'models with compound names' do
it "generates the proper 'root' node for individual objects" do
app.get('/home') do
BlogPost.new(title: 'Grape AM::S Rocks!', body: 'Really, it does.')
end
get '/home'
expect(subject).to eq(
'{"blog_post":' \
'{"title":"Grape AM::S Rocks!","body":"Really, it does."}' \
'}'
)
end
it "generates the proper 'root' node for serialized arrays" do
app.get('/blog_posts') do
blog_post = BlogPost.new(
title: 'Grape AM::S Rocks!',
body: 'Really, it does.'
)
[blog_post, blog_post]
end
get '/blog_posts'
expect(subject).to eq(
'{"blog_posts":[' \
'{"title":"Grape AM::S Rocks!","body":"Really, it does."},' \
'{"title":"Grape AM::S Rocks!","body":"Really, it does."}' \
']}'
)
end
end
it 'uses namespace options when provided' do
app.namespace :admin, serializer: UserSerializer do
get('/jeff') do
User.new(first_name: 'Jeff')
end
end
get '/admin/jeff'
expect(subject).to eq(
'{"user":{"first_name":"Jeff","last_name":null}}'
)
end
context 'route is in a namespace' do
it 'uses the name of the closest namespace for the root' do
app.namespace :admin do
get('/jeff') do
user = User.new(first_name: 'Jeff')
[user, user]
end
end
get '/admin/jeff'
expect(subject).to eq(
'{"admin":[' \
'{"first_name":"Jeff","last_name":null},' \
'{"first_name":"Jeff","last_name":null}' \
']}'
)
end
end
context 'route is not in a namespace' do
it 'uses the of the route for the root' do
app.get('/people') do
user = User.new(first_name: 'Jeff')
[user, user]
end
get '/people'
expect(subject).to eq(
'{"people":[' \
'{"first_name":"Jeff","last_name":null},' \
'{"first_name":"Jeff","last_name":null}' \
']}'
)
end
end
end