-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy patherror_spec.rb
77 lines (65 loc) · 1.69 KB
/
error_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
# frozen_string_literal: true
describe Grape::Middleware::Error do
let(:error_entity) do
Class.new(Grape::Entity) do
expose :code
expose :static
def static
'static text'
end
end
end
let(:err_app) do
Class.new do
class << self
attr_accessor :error, :format
def call(_env)
throw :error, error
end
end
end
end
let(:options) { { default_message: 'Aww, hamburgers.' } }
let(:app) do
opts = options
context = self
Rack::Builder.app do
use Spec::Support::EndpointFaker
use Grape::Middleware::Error, opts # rubocop:disable RSpec/DescribedClass
run context.err_app
end
end
it 'sets the status code appropriately' do
err_app.error = { status: 410 }
get '/'
expect(last_response.status).to eq(410)
end
it 'sets the status code based on the rack util status code symbol' do
err_app.error = { status: :gone }
get '/'
expect(last_response.status).to eq(410)
end
it 'sets the error message appropriately' do
err_app.error = { message: 'Awesome stuff.' }
get '/'
expect(last_response.body).to eq('Awesome stuff.')
end
it 'defaults to a 500 status' do
err_app.error = {}
get '/'
expect(last_response).to be_server_error
end
it 'has a default message' do
err_app.error = {}
get '/'
expect(last_response.body).to eq('Aww, hamburgers.')
end
context 'with http code' do
let(:options) { { default_message: 'Aww, hamburgers.' } }
it 'adds the status code if wanted' do
err_app.error = { message: { code: 200 } }
get '/'
expect(last_response.body).to eq({ code: 200 }.to_json)
end
end
end