forked from ruby-grape/grape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbody_parse_errors_spec.rb
185 lines (158 loc) · 5.35 KB
/
body_parse_errors_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
# frozen_string_literal: true
describe Grape::Exceptions::ValidationErrors do
context 'api with rescue_from :all handler' do
subject { Class.new(Grape::API) }
before do
subject.rescue_from :all do |_e|
error! 'message was processed', 400
end
subject.params do
requires :beer
end
subject.post '/beer' do
'beer received'
end
end
def app
subject
end
context 'with content_type json' do
it 'can recover from failed body parsing' do
post '/beer', 'test', 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq 400
expect(last_response.body).to eq('message was processed')
end
end
context 'with content_type xml' do
it 'can recover from failed body parsing' do
post '/beer', 'test', 'CONTENT_TYPE' => 'application/xml'
expect(last_response.status).to eq 400
expect(last_response.body).to eq('message was processed')
end
end
context 'with content_type text' do
it 'can recover from failed body parsing' do
post '/beer', 'test', 'CONTENT_TYPE' => 'text/plain'
expect(last_response.status).to eq 400
expect(last_response.body).to eq('message was processed')
end
end
context 'with no specific content_type' do
it 'can recover from failed body parsing' do
post '/beer', 'test', {}
expect(last_response.status).to eq 400
expect(last_response.body).to eq('message was processed')
end
end
end
context 'api with rescue_from :grape_exceptions handler' do
subject { Class.new(Grape::API) }
before do
subject.rescue_from :all do |_e|
error! 'message was processed', 400
end
subject.rescue_from :grape_exceptions
subject.params do
requires :beer
end
subject.post '/beer' do
'beer received'
end
end
def app
subject
end
context 'with content_type json' do
it 'returns body parsing error message' do
post '/beer', 'test', 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq 400
expect(last_response.body).to include 'message body does not match declared format'
end
end
context 'with content_type xml' do
it 'returns body parsing error message' do
post '/beer', 'test', 'CONTENT_TYPE' => 'application/xml'
expect(last_response.status).to eq 400
expect(last_response.body).to include 'message body does not match declared format'
end
end
end
context 'api with rescue_from :grape_exceptions handler with block' do
subject { Class.new(Grape::API) }
before do
subject.rescue_from :grape_exceptions do |e|
error! "Custom Error Contents, Original Message: #{e.message}", 400
end
subject.params do
requires :beer
end
subject.post '/beer' do
'beer received'
end
end
def app
subject
end
context 'with content_type json' do
it 'returns body parsing error message' do
post '/beer', 'test', 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq 400
expect(last_response.body).to include 'message body does not match declared format'
expect(last_response.body).to include 'Custom Error Contents, Original Message'
end
end
context 'with content_type xml' do
it 'returns body parsing error message' do
post '/beer', 'test', 'CONTENT_TYPE' => 'application/xml'
expect(last_response.status).to eq 400
expect(last_response.body).to include 'message body does not match declared format'
expect(last_response.body).to include 'Custom Error Contents, Original Message'
end
end
end
context 'api without a rescue handler' do
subject { Class.new(Grape::API) }
before do
subject.params do
requires :beer
end
subject.post '/beer' do
'beer received'
end
end
def app
subject
end
context 'and with content_type json' do
it 'can recover from failed body parsing' do
post '/beer', 'test', 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq 400
expect(last_response.body).to include('message body does not match declared format')
expect(last_response.body).to include('application/json')
end
end
context 'with content_type xml' do
it 'can recover from failed body parsing' do
post '/beer', 'test', 'CONTENT_TYPE' => 'application/xml'
expect(last_response.status).to eq 400
expect(last_response.body).to include('message body does not match declared format')
expect(last_response.body).to include('application/xml')
end
end
context 'with content_type text' do
it 'can recover from failed body parsing' do
post '/beer', 'test', 'CONTENT_TYPE' => 'text/plain'
expect(last_response.status).to eq 400
expect(last_response.body).to eq('beer is missing')
end
end
context 'and with no specific content_type' do
it 'can recover from failed body parsing' do
post '/beer', 'test', {}
expect(last_response.status).to eq 400
# plain response with text/html
expect(last_response.body).to eq('beer is missing')
end
end
end
end