-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmount_and_helpers_order_spec.rb
210 lines (171 loc) · 4.75 KB
/
mount_and_helpers_order_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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# frozen_string_literal: true
describe Grape::API do
describe 'rescue_from' do
context 'when the API is mounted AFTER defining the class rescue_from handler' do
let(:api_rescue_from) do
Class.new(Grape::API) do
rescue_from :all do
error!({ type: 'all' }, 404)
end
get do
{ count: 1 / 0 }
end
end
end
let(:main_rescue_from_after) do
context = self
Class.new(Grape::API) do
rescue_from ZeroDivisionError do
error!({ type: 'zero' }, 500)
end
mount context.api_rescue_from
end
end
def app
main_rescue_from_after
end
it 'is rescued by the rescue_from ZeroDivisionError handler from Main class' do
get '/'
expect(last_response.status).to eq(500)
expect(last_response.body).to eq({ type: 'zero' }.to_json)
end
end
context 'when the API is mounted BEFORE defining the class rescue_from handler' do
let(:api_rescue_from) do
Class.new(Grape::API) do
rescue_from :all do
error!({ type: 'all' }, 404)
end
get do
{ count: 1 / 0 }
end
end
end
let(:main_rescue_from_before) do
context = self
Class.new(Grape::API) do
mount context.api_rescue_from
rescue_from ZeroDivisionError do
error!({ type: 'zero' }, 500)
end
end
end
def app
main_rescue_from_before
end
it 'is rescued by the rescue_from ZeroDivisionError handler from Main class' do
get '/'
expect(last_response.status).to eq(500)
expect(last_response.body).to eq({ type: 'zero' }.to_json)
end
end
end
describe 'before' do
context 'when the API is mounted AFTER defining the before helper' do
let(:api_before_handler) do
Class.new(Grape::API) do
get do
{ count: @count }.to_json
end
end
end
let(:main_before_handler_after) do
context = self
Class.new(Grape::API) do
before do
@count = 1
end
mount context.api_before_handler
end
end
def app
main_before_handler_after
end
it 'is able to access the variables defined in the before helper' do
get '/'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq({ count: 1 }.to_json)
end
end
context 'when the API is mounted BEFORE defining the before helper' do
let(:api_before_handler) do
Class.new(Grape::API) do
get do
{ count: @count }.to_json
end
end
end
let(:main_before_handler_before) do
context = self
Class.new(Grape::API) do
mount context.api_before_handler
before do
@count = 1
end
end
end
def app
main_before_handler_before
end
it 'is able to access the variables defined in the before helper' do
get '/'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq({ count: 1 }.to_json)
end
end
end
describe 'after' do
context 'when the API is mounted AFTER defining the after handler' do
let(:api_after_handler) do
Class.new(Grape::API) do
get do
{ count: 1 }.to_json
end
end
end
let(:main_after_handler_after) do
context = self
Class.new(Grape::API) do
after do
error!({ type: 'after' }, 500)
end
mount context.api_after_handler
end
end
def app
main_after_handler_after
end
it 'is able to access the variables defined in the after helper' do
get '/'
expect(last_response.status).to eq(500)
expect(last_response.body).to eq({ type: 'after' }.to_json)
end
end
context 'when the API is mounted BEFORE defining the after helper' do
let(:api_after_handler) do
Class.new(Grape::API) do
get do
{ count: 1 }.to_json
end
end
end
let(:main_after_handler_before) do
context = self
Class.new(Grape::API) do
mount context.api_after_handler
after do
error!({ type: 'after' }, 500)
end
end
end
def app
main_after_handler_before
end
it 'is able to access the variables defined in the after helper' do
get '/'
expect(last_response.status).to eq(500)
expect(last_response.body).to eq({ type: 'after' }.to_json)
end
end
end
end