Skip to content

Commit 8a8f98c

Browse files
authored
Merge pull request #1657 from dblock/subject-spec
Cleanup exception_spec to use subject blocks.
2 parents 3c0be5e + 9d150f7 commit 8a8f98c

File tree

1 file changed

+127
-75
lines changed

1 file changed

+127
-75
lines changed

spec/grape/middleware/exception_spec.rb

Lines changed: 127 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -50,113 +50,146 @@ def call(_env)
5050
end
5151
end
5252

53-
attr_reader :app
53+
def app
54+
subject
55+
end
5456

55-
it 'does not trap errors by default' do
56-
@app ||= Rack::Builder.app do
57-
use Spec::Support::EndpointFaker
58-
use Grape::Middleware::Error
59-
run ExceptionSpec::ExceptionApp
57+
context 'with defaults' do
58+
subject do
59+
Rack::Builder.app do
60+
use Spec::Support::EndpointFaker
61+
use Grape::Middleware::Error
62+
run ExceptionSpec::ExceptionApp
63+
end
64+
end
65+
it 'does not trap errors by default' do
66+
expect { get '/' }.to raise_error(RuntimeError, 'rain!')
6067
end
61-
expect { get '/' }.to raise_error(RuntimeError, 'rain!')
6268
end
6369

64-
context 'with rescue_all set to true' do
65-
it 'sets the message appropriately' do
66-
@app ||= Rack::Builder.app do
70+
context 'with rescue_all' do
71+
subject do
72+
Rack::Builder.app do
6773
use Spec::Support::EndpointFaker
6874
use Grape::Middleware::Error, rescue_all: true
6975
run ExceptionSpec::ExceptionApp
7076
end
77+
end
78+
it 'sets the message appropriately' do
7179
get '/'
7280
expect(last_response.body).to eq('rain!')
7381
end
74-
7582
it 'defaults to a 500 status' do
76-
@app ||= Rack::Builder.app do
77-
use Spec::Support::EndpointFaker
78-
use Grape::Middleware::Error, rescue_all: true
79-
run ExceptionSpec::ExceptionApp
80-
end
8183
get '/'
8284
expect(last_response.status).to eq(500)
8385
end
86+
end
8487

85-
it 'is possible to specify a different default status code' do
86-
@app ||= Rack::Builder.app do
88+
context do
89+
subject do
90+
Rack::Builder.app do
8791
use Spec::Support::EndpointFaker
8892
use Grape::Middleware::Error, rescue_all: true, default_status: 500
8993
run ExceptionSpec::ExceptionApp
9094
end
95+
end
96+
it 'is possible to specify a different default status code' do
9197
get '/'
9298
expect(last_response.status).to eq(500)
9399
end
100+
end
94101

95-
it 'is possible to return errors in json format' do
96-
@app ||= Rack::Builder.app do
102+
context do
103+
subject do
104+
Rack::Builder.app do
97105
use Spec::Support::EndpointFaker
98106
use Grape::Middleware::Error, rescue_all: true, format: :json
99107
run ExceptionSpec::ExceptionApp
100108
end
109+
end
110+
it 'is possible to return errors in json format' do
101111
get '/'
102112
expect(last_response.body).to eq('{"error":"rain!"}')
103113
end
114+
end
104115

105-
it 'is possible to return hash errors in json format' do
106-
@app ||= Rack::Builder.app do
116+
context do
117+
subject do
118+
Rack::Builder.app do
107119
use Spec::Support::EndpointFaker
108120
use Grape::Middleware::Error, rescue_all: true, format: :json
109121
run ExceptionSpec::ErrorHashApp
110122
end
123+
end
124+
it 'is possible to return hash errors in json format' do
111125
get '/'
112126
expect(['{"error":"rain!","detail":"missing widget"}',
113127
'{"detail":"missing widget","error":"rain!"}']).to include(last_response.body)
114128
end
129+
end
115130

116-
it 'is possible to return errors in jsonapi format' do
117-
@app ||= Rack::Builder.app do
131+
context do
132+
subject do
133+
Rack::Builder.app do
118134
use Spec::Support::EndpointFaker
119135
use Grape::Middleware::Error, rescue_all: true, format: :jsonapi
120136
run ExceptionSpec::ExceptionApp
121137
end
138+
end
139+
it 'is possible to return errors in jsonapi format' do
122140
get '/'
123141
expect(last_response.body).to eq('{"error":"rain!"}')
124142
end
143+
end
125144

126-
it 'is possible to return hash errors in jsonapi format' do
127-
@app ||= Rack::Builder.app do
145+
context do
146+
subject do
147+
Rack::Builder.app do
128148
use Spec::Support::EndpointFaker
129149
use Grape::Middleware::Error, rescue_all: true, format: :jsonapi
130150
run ExceptionSpec::ErrorHashApp
131151
end
152+
end
153+
154+
it 'is possible to return hash errors in jsonapi format' do
132155
get '/'
133156
expect(['{"error":"rain!","detail":"missing widget"}',
134157
'{"detail":"missing widget","error":"rain!"}']).to include(last_response.body)
135158
end
159+
end
136160

137-
it 'is possible to return errors in xml format' do
138-
@app ||= Rack::Builder.app do
161+
context do
162+
subject do
163+
Rack::Builder.app do
139164
use Spec::Support::EndpointFaker
140165
use Grape::Middleware::Error, rescue_all: true, format: :xml
141166
run ExceptionSpec::ExceptionApp
142167
end
168+
end
169+
it 'is possible to return errors in xml format' do
143170
get '/'
144171
expect(last_response.body).to eq("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<error>\n <message>rain!</message>\n</error>\n")
145172
end
173+
end
146174

147-
it 'is possible to return hash errors in xml format' do
148-
@app ||= Rack::Builder.app do
175+
context do
176+
subject do
177+
Rack::Builder.app do
149178
use Spec::Support::EndpointFaker
150179
use Grape::Middleware::Error, rescue_all: true, format: :xml
151180
run ExceptionSpec::ErrorHashApp
152181
end
182+
end
183+
it 'is possible to return hash errors in xml format' do
153184
get '/'
154185
expect(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<error>\n <detail>missing widget</detail>\n <error>rain!</error>\n</error>\n",
155186
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<error>\n <error>rain!</error>\n <detail>missing widget</detail>\n</error>\n"]).to include(last_response.body)
156187
end
188+
end
157189

158-
it 'is possible to specify a custom formatter' do
159-
@app ||= Rack::Builder.app do
190+
context do
191+
subject do
192+
Rack::Builder.app do
160193
use Spec::Support::EndpointFaker
161194
use Grape::Middleware::Error,
162195
rescue_all: true,
@@ -168,71 +201,90 @@ def call(_env)
168201
}
169202
run ExceptionSpec::ExceptionApp
170203
end
204+
end
205+
it 'is possible to specify a custom formatter' do
171206
get '/'
172207
expect(last_response.body).to eq('{:custom_formatter=>"rain!"}')
173208
end
209+
end
174210

175-
it 'does not trap regular error! codes' do
176-
@app ||= Rack::Builder.app do
211+
context do
212+
subject do
213+
Rack::Builder.app do
177214
use Spec::Support::EndpointFaker
178215
use Grape::Middleware::Error
179216
run ExceptionSpec::AccessDeniedApp
180217
end
218+
end
219+
it 'does not trap regular error! codes' do
181220
get '/'
182221
expect(last_response.status).to eq(401)
183222
end
223+
end
184224

185-
it 'responds to custom Grape exceptions appropriately' do
186-
@app ||= Rack::Builder.app do
225+
context do
226+
subject do
227+
Rack::Builder.app do
187228
use Spec::Support::EndpointFaker
188229
use Grape::Middleware::Error, rescue_all: false
189230
run ExceptionSpec::CustomErrorApp
190231
end
191-
232+
end
233+
it 'responds to custom Grape exceptions appropriately' do
192234
get '/'
193235
expect(last_response.status).to eq(400)
194236
expect(last_response.body).to eq('failed validation')
195237
end
238+
end
196239

197-
context 'with rescue_options :backtrace and :exception set to true' do
198-
it 'is possible to return the backtrace and the original exception in json format' do
199-
@app ||= Rack::Builder.app do
200-
use Spec::Support::EndpointFaker
201-
use Grape::Middleware::Error,
202-
rescue_all: true,
203-
format: :json,
204-
rescue_options: { backtrace: true, original_exception: true }
205-
run ExceptionSpec::ExceptionApp
206-
end
207-
get '/'
208-
expect(last_response.body).to include('error', 'rain!', 'backtrace', 'original_exception', 'RuntimeError')
209-
end
210-
211-
it 'is possible to return the backtrace and the original exception in xml format' do
212-
@app ||= Rack::Builder.app do
213-
use Spec::Support::EndpointFaker
214-
use Grape::Middleware::Error,
215-
rescue_all: true,
216-
format: :xml,
217-
rescue_options: { backtrace: true, original_exception: true }
218-
run ExceptionSpec::ExceptionApp
219-
end
220-
get '/'
221-
expect(last_response.body).to include('error', 'rain!', 'backtrace', 'original-exception', 'RuntimeError')
222-
end
223-
224-
it 'is possible to return the backtrace and the original exception in txt format' do
225-
@app ||= Rack::Builder.app do
226-
use Spec::Support::EndpointFaker
227-
use Grape::Middleware::Error,
228-
rescue_all: true,
229-
format: :txt,
230-
rescue_options: { backtrace: true, original_exception: true }
231-
run ExceptionSpec::ExceptionApp
232-
end
233-
get '/'
234-
expect(last_response.body).to include('error', 'rain!', 'backtrace', 'original exception', 'RuntimeError')
240+
context 'with rescue_options :backtrace and :exception set to true' do
241+
subject do
242+
Rack::Builder.app do
243+
use Spec::Support::EndpointFaker
244+
use Grape::Middleware::Error,
245+
rescue_all: true,
246+
format: :json,
247+
rescue_options: { backtrace: true, original_exception: true }
248+
run ExceptionSpec::ExceptionApp
235249
end
236250
end
251+
it 'is possible to return the backtrace and the original exception in json format' do
252+
get '/'
253+
expect(last_response.body).to include('error', 'rain!', 'backtrace', 'original_exception', 'RuntimeError')
254+
end
255+
end
256+
257+
context do
258+
subject do
259+
Rack::Builder.app do
260+
use Spec::Support::EndpointFaker
261+
use Grape::Middleware::Error,
262+
rescue_all: true,
263+
format: :xml,
264+
rescue_options: { backtrace: true, original_exception: true }
265+
run ExceptionSpec::ExceptionApp
266+
end
267+
end
268+
it 'is possible to return the backtrace and the original exception in xml format' do
269+
get '/'
270+
expect(last_response.body).to include('error', 'rain!', 'backtrace', 'original-exception', 'RuntimeError')
271+
end
272+
end
273+
274+
context do
275+
subject do
276+
Rack::Builder.app do
277+
use Spec::Support::EndpointFaker
278+
use Grape::Middleware::Error,
279+
rescue_all: true,
280+
format: :txt,
281+
rescue_options: { backtrace: true, original_exception: true }
282+
run ExceptionSpec::ExceptionApp
283+
end
284+
end
285+
it 'is possible to return the backtrace and the original exception in txt format' do
286+
get '/'
287+
expect(last_response.body).to include('error', 'rain!', 'backtrace', 'original exception', 'RuntimeError')
288+
end
237289
end
238290
end

0 commit comments

Comments
 (0)