grape 1.6.0 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -0
- data/CONTRIBUTING.md +1 -0
- data/README.md +9 -1
- data/lib/grape/api.rb +12 -0
- data/lib/grape/dry_types.rb +12 -0
- data/lib/grape/dsl/headers.rb +5 -2
- data/lib/grape/dsl/helpers.rb +1 -1
- data/lib/grape/middleware/auth/dsl.rb +7 -1
- data/lib/grape/middleware/base.rb +1 -1
- data/lib/grape/util/json.rb +2 -0
- data/lib/grape/util/strict_hash_configuration.rb +1 -1
- data/lib/grape/validations/types/array_coercer.rb +0 -2
- data/lib/grape/validations/types/dry_type_coercer.rb +1 -10
- data/lib/grape/validations/types/json.rb +0 -2
- data/lib/grape/validations/types/primitive_coercer.rb +5 -7
- data/lib/grape/validations/types/set_coercer.rb +0 -3
- data/lib/grape/validations/types.rb +83 -9
- data/lib/grape/validations/validators/all_or_none_of_validator.rb +16 -0
- data/lib/grape/validations/validators/allow_blank_validator.rb +20 -0
- data/lib/grape/validations/validators/as_validator.rb +14 -0
- data/lib/grape/validations/validators/at_least_one_of_validator.rb +15 -0
- data/lib/grape/validations/validators/base.rb +73 -71
- data/lib/grape/validations/validators/coerce_validator.rb +75 -0
- data/lib/grape/validations/validators/default_validator.rb +51 -0
- data/lib/grape/validations/validators/exactly_one_of_validator.rb +17 -0
- data/lib/grape/validations/validators/except_values_validator.rb +24 -0
- data/lib/grape/validations/validators/multiple_params_base.rb +24 -22
- data/lib/grape/validations/validators/mutual_exclusion_validator.rb +16 -0
- data/lib/grape/validations/validators/presence_validator.rb +15 -0
- data/lib/grape/validations/validators/regexp_validator.rb +16 -0
- data/lib/grape/validations/validators/same_as_validator.rb +29 -0
- data/lib/grape/validations/validators/values_validator.rb +88 -0
- data/lib/grape/version.rb +1 -1
- data/lib/grape.rb +59 -24
- data/spec/grape/api/custom_validations_spec.rb +77 -46
- data/spec/grape/api/deeply_included_options_spec.rb +3 -3
- data/spec/grape/api/defines_boolean_in_params_spec.rb +2 -1
- data/spec/grape/api/invalid_format_spec.rb +2 -0
- data/spec/grape/api/recognize_path_spec.rb +1 -1
- data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +9 -15
- data/spec/grape/api_remount_spec.rb +16 -15
- data/spec/grape/api_spec.rb +317 -193
- data/spec/grape/dsl/callbacks_spec.rb +1 -0
- data/spec/grape/dsl/headers_spec.rb +39 -9
- data/spec/grape/dsl/helpers_spec.rb +3 -2
- data/spec/grape/dsl/inside_route_spec.rb +6 -4
- data/spec/grape/dsl/logger_spec.rb +16 -18
- data/spec/grape/dsl/middleware_spec.rb +1 -0
- data/spec/grape/dsl/parameters_spec.rb +1 -0
- data/spec/grape/dsl/request_response_spec.rb +1 -0
- data/spec/grape/dsl/routing_spec.rb +9 -6
- data/spec/grape/endpoint/declared_spec.rb +12 -12
- data/spec/grape/endpoint_spec.rb +59 -50
- data/spec/grape/entity_spec.rb +13 -13
- data/spec/grape/exceptions/body_parse_errors_spec.rb +3 -0
- data/spec/grape/exceptions/invalid_accept_header_spec.rb +61 -22
- data/spec/grape/exceptions/validation_errors_spec.rb +13 -10
- data/spec/grape/exceptions/validation_spec.rb +5 -3
- data/spec/grape/extensions/param_builders/hash_spec.rb +7 -7
- data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +8 -8
- data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +8 -8
- data/spec/grape/integration/rack_sendfile_spec.rb +1 -1
- data/spec/grape/loading_spec.rb +8 -8
- data/spec/grape/middleware/auth/dsl_spec.rb +14 -5
- data/spec/grape/middleware/auth/strategies_spec.rb +60 -20
- data/spec/grape/middleware/base_spec.rb +24 -15
- data/spec/grape/middleware/error_spec.rb +1 -0
- data/spec/grape/middleware/exception_spec.rb +111 -161
- data/spec/grape/middleware/formatter_spec.rb +25 -4
- data/spec/grape/middleware/globals_spec.rb +7 -4
- data/spec/grape/middleware/stack_spec.rb +11 -11
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +2 -1
- data/spec/grape/middleware/versioner/header_spec.rb +14 -13
- data/spec/grape/middleware/versioner/param_spec.rb +7 -1
- data/spec/grape/middleware/versioner/path_spec.rb +5 -1
- data/spec/grape/middleware/versioner_spec.rb +1 -1
- data/spec/grape/parser_spec.rb +4 -0
- data/spec/grape/path_spec.rb +52 -52
- data/spec/grape/presenters/presenter_spec.rb +7 -6
- data/spec/grape/request_spec.rb +6 -4
- data/spec/grape/util/inheritable_setting_spec.rb +7 -7
- data/spec/grape/util/inheritable_values_spec.rb +3 -2
- data/spec/grape/util/reverse_stackable_values_spec.rb +3 -1
- data/spec/grape/util/stackable_values_spec.rb +7 -5
- data/spec/grape/validations/instance_behaivour_spec.rb +9 -10
- data/spec/grape/validations/multiple_attributes_iterator_spec.rb +1 -0
- data/spec/grape/validations/params_scope_spec.rb +9 -7
- data/spec/grape/validations/single_attribute_iterator_spec.rb +1 -0
- data/spec/grape/validations/types/primitive_coercer_spec.rb +2 -2
- data/spec/grape/validations/types_spec.rb +8 -8
- data/spec/grape/validations/validators/all_or_none_spec.rb +50 -56
- data/spec/grape/validations/validators/allow_blank_spec.rb +136 -140
- data/spec/grape/validations/validators/at_least_one_of_spec.rb +50 -56
- data/spec/grape/validations/validators/coerce_spec.rb +10 -12
- data/spec/grape/validations/validators/default_spec.rb +72 -78
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +71 -77
- data/spec/grape/validations/validators/except_values_spec.rb +1 -1
- data/spec/grape/validations/validators/mutual_exclusion_spec.rb +71 -77
- data/spec/grape/validations/validators/presence_spec.rb +16 -1
- data/spec/grape/validations/validators/regexp_spec.rb +25 -31
- data/spec/grape/validations/validators/same_as_spec.rb +14 -20
- data/spec/grape/validations/validators/values_spec.rb +172 -171
- data/spec/grape/validations_spec.rb +45 -16
- data/spec/integration/eager_load/eager_load_spec.rb +2 -2
- data/spec/integration/multi_json/json_spec.rb +1 -1
- data/spec/integration/multi_xml/xml_spec.rb +1 -1
- data/spec/shared/versioning_examples.rb +10 -7
- data/spec/spec_helper.rb +11 -1
- metadata +116 -116
- data/lib/grape/validations/types/build_coercer.rb +0 -94
- data/lib/grape/validations/validators/all_or_none.rb +0 -16
- data/lib/grape/validations/validators/allow_blank.rb +0 -18
- data/lib/grape/validations/validators/as.rb +0 -12
- data/lib/grape/validations/validators/at_least_one_of.rb +0 -15
- data/lib/grape/validations/validators/coerce.rb +0 -87
- data/lib/grape/validations/validators/default.rb +0 -49
- data/lib/grape/validations/validators/exactly_one_of.rb +0 -17
- data/lib/grape/validations/validators/except_values.rb +0 -22
- data/lib/grape/validations/validators/mutual_exclusion.rb +0 -16
- data/lib/grape/validations/validators/presence.rb +0 -13
- data/lib/grape/validations/validators/regexp.rb +0 -14
- data/lib/grape/validations/validators/same_as.rb +0 -27
- data/lib/grape/validations/validators/values.rb +0 -86
@@ -7,6 +7,7 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
7
7
|
it 'does return with status 200' do
|
8
8
|
expect(last_response.status).to eq 200
|
9
9
|
end
|
10
|
+
|
10
11
|
it 'does return the expected result' do
|
11
12
|
expect(last_response.body).to eq('beer received')
|
12
13
|
end
|
@@ -20,6 +21,7 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
20
21
|
it 'does not include the X-Cascade=pass header' do
|
21
22
|
expect(last_response.headers['X-Cascade']).to be_nil
|
22
23
|
end
|
24
|
+
|
23
25
|
it 'does not accept the request' do
|
24
26
|
expect(last_response.status).to eq 406
|
25
27
|
end
|
@@ -28,6 +30,7 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
28
30
|
it 'does not include the X-Cascade=pass header' do
|
29
31
|
expect(last_response.headers['X-Cascade']).to be_nil
|
30
32
|
end
|
33
|
+
|
31
34
|
it 'does show rescue handler processing' do
|
32
35
|
expect(last_response.status).to eq 400
|
33
36
|
expect(last_response.body).to eq('message was processed')
|
@@ -36,6 +39,7 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
36
39
|
|
37
40
|
context 'API with cascade=false and rescue_from :all handler' do
|
38
41
|
subject { Class.new(Grape::API) }
|
42
|
+
|
39
43
|
before do
|
40
44
|
subject.version 'v99', using: :header, vendor: 'vendorname', format: :json, cascade: false
|
41
45
|
subject.rescue_from :all do |e|
|
@@ -52,7 +56,8 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
52
56
|
|
53
57
|
context 'that received a request with correct vendor and version' do
|
54
58
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v99' }
|
55
|
-
|
59
|
+
|
60
|
+
it_behaves_like 'a valid request'
|
56
61
|
end
|
57
62
|
|
58
63
|
context 'that receives' do
|
@@ -61,13 +66,15 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
61
66
|
get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.invalidvendor-v99',
|
62
67
|
'CONTENT_TYPE' => 'application/json'
|
63
68
|
end
|
64
|
-
|
69
|
+
|
70
|
+
it_behaves_like 'a rescued request'
|
65
71
|
end
|
66
72
|
end
|
67
73
|
end
|
68
74
|
|
69
75
|
context 'API with cascade=false and without a rescue handler' do
|
70
76
|
subject { Class.new(Grape::API) }
|
77
|
+
|
71
78
|
before do
|
72
79
|
subject.version 'v99', using: :header, vendor: 'vendorname', format: :json, cascade: false
|
73
80
|
subject.get '/beer' do
|
@@ -81,23 +88,28 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
81
88
|
|
82
89
|
context 'that received a request with correct vendor and version' do
|
83
90
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v99' }
|
84
|
-
|
91
|
+
|
92
|
+
it_behaves_like 'a valid request'
|
85
93
|
end
|
86
94
|
|
87
95
|
context 'that receives' do
|
88
96
|
context 'an invalid version in the request' do
|
89
97
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v77' }
|
90
|
-
|
98
|
+
|
99
|
+
it_behaves_like 'a not-cascaded request'
|
91
100
|
end
|
101
|
+
|
92
102
|
context 'an invalid vendor in the request' do
|
93
103
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.invalidvendor-v99' }
|
94
|
-
|
104
|
+
|
105
|
+
it_behaves_like 'a not-cascaded request'
|
95
106
|
end
|
96
107
|
end
|
97
108
|
end
|
98
109
|
|
99
110
|
context 'API with cascade=false and with rescue_from :all handler and http_codes' do
|
100
111
|
subject { Class.new(Grape::API) }
|
112
|
+
|
101
113
|
before do
|
102
114
|
subject.version 'v99', using: :header, vendor: 'vendorname', format: :json, cascade: false
|
103
115
|
subject.rescue_from :all do |e|
|
@@ -119,7 +131,8 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
119
131
|
|
120
132
|
context 'that received a request with correct vendor and version' do
|
121
133
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v99' }
|
122
|
-
|
134
|
+
|
135
|
+
it_behaves_like 'a valid request'
|
123
136
|
end
|
124
137
|
|
125
138
|
context 'that receives' do
|
@@ -128,13 +141,15 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
128
141
|
get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.invalidvendor-v99',
|
129
142
|
'CONTENT_TYPE' => 'application/json'
|
130
143
|
end
|
131
|
-
|
144
|
+
|
145
|
+
it_behaves_like 'a rescued request'
|
132
146
|
end
|
133
147
|
end
|
134
148
|
end
|
135
149
|
|
136
150
|
context 'API with cascade=false, http_codes but without a rescue handler' do
|
137
151
|
subject { Class.new(Grape::API) }
|
152
|
+
|
138
153
|
before do
|
139
154
|
subject.version 'v99', using: :header, vendor: 'vendorname', format: :json, cascade: false
|
140
155
|
subject.desc 'Get beer' do
|
@@ -153,23 +168,28 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
153
168
|
|
154
169
|
context 'that received a request with correct vendor and version' do
|
155
170
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v99' }
|
156
|
-
|
171
|
+
|
172
|
+
it_behaves_like 'a valid request'
|
157
173
|
end
|
158
174
|
|
159
175
|
context 'that receives' do
|
160
176
|
context 'an invalid version in the request' do
|
161
177
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v77' }
|
162
|
-
|
178
|
+
|
179
|
+
it_behaves_like 'a not-cascaded request'
|
163
180
|
end
|
181
|
+
|
164
182
|
context 'an invalid vendor in the request' do
|
165
183
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.invalidvendor-v99' }
|
166
|
-
|
184
|
+
|
185
|
+
it_behaves_like 'a not-cascaded request'
|
167
186
|
end
|
168
187
|
end
|
169
188
|
end
|
170
189
|
|
171
190
|
context 'API with cascade=true and rescue_from :all handler' do
|
172
191
|
subject { Class.new(Grape::API) }
|
192
|
+
|
173
193
|
before do
|
174
194
|
subject.version 'v99', using: :header, vendor: 'vendorname', format: :json, cascade: true
|
175
195
|
subject.rescue_from :all do |e|
|
@@ -186,7 +206,8 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
186
206
|
|
187
207
|
context 'that received a request with correct vendor and version' do
|
188
208
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v99' }
|
189
|
-
|
209
|
+
|
210
|
+
it_behaves_like 'a valid request'
|
190
211
|
end
|
191
212
|
|
192
213
|
context 'that receives' do
|
@@ -195,20 +216,24 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
195
216
|
get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v77',
|
196
217
|
'CONTENT_TYPE' => 'application/json'
|
197
218
|
end
|
198
|
-
|
219
|
+
|
220
|
+
it_behaves_like 'a cascaded request'
|
199
221
|
end
|
222
|
+
|
200
223
|
context 'an invalid vendor in the request' do
|
201
224
|
before do
|
202
225
|
get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.invalidvendor-v99',
|
203
226
|
'CONTENT_TYPE' => 'application/json'
|
204
227
|
end
|
205
|
-
|
228
|
+
|
229
|
+
it_behaves_like 'a cascaded request'
|
206
230
|
end
|
207
231
|
end
|
208
232
|
end
|
209
233
|
|
210
234
|
context 'API with cascade=true and without a rescue handler' do
|
211
235
|
subject { Class.new(Grape::API) }
|
236
|
+
|
212
237
|
before do
|
213
238
|
subject.version 'v99', using: :header, vendor: 'vendorname', format: :json, cascade: true
|
214
239
|
subject.get '/beer' do
|
@@ -222,23 +247,28 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
222
247
|
|
223
248
|
context 'that received a request with correct vendor and version' do
|
224
249
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v99' }
|
225
|
-
|
250
|
+
|
251
|
+
it_behaves_like 'a valid request'
|
226
252
|
end
|
227
253
|
|
228
254
|
context 'that receives' do
|
229
255
|
context 'an invalid version in the request' do
|
230
256
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v77' }
|
231
|
-
|
257
|
+
|
258
|
+
it_behaves_like 'a cascaded request'
|
232
259
|
end
|
260
|
+
|
233
261
|
context 'an invalid vendor in the request' do
|
234
262
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.invalidvendor-v99' }
|
235
|
-
|
263
|
+
|
264
|
+
it_behaves_like 'a cascaded request'
|
236
265
|
end
|
237
266
|
end
|
238
267
|
end
|
239
268
|
|
240
269
|
context 'API with cascade=true and with rescue_from :all handler and http_codes' do
|
241
270
|
subject { Class.new(Grape::API) }
|
271
|
+
|
242
272
|
before do
|
243
273
|
subject.version 'v99', using: :header, vendor: 'vendorname', format: :json, cascade: true
|
244
274
|
subject.rescue_from :all do |e|
|
@@ -260,7 +290,8 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
260
290
|
|
261
291
|
context 'that received a request with correct vendor and version' do
|
262
292
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v99' }
|
263
|
-
|
293
|
+
|
294
|
+
it_behaves_like 'a valid request'
|
264
295
|
end
|
265
296
|
|
266
297
|
context 'that receives' do
|
@@ -269,20 +300,24 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
269
300
|
get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v77',
|
270
301
|
'CONTENT_TYPE' => 'application/json'
|
271
302
|
end
|
272
|
-
|
303
|
+
|
304
|
+
it_behaves_like 'a cascaded request'
|
273
305
|
end
|
306
|
+
|
274
307
|
context 'an invalid vendor in the request' do
|
275
308
|
before do
|
276
309
|
get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.invalidvendor-v99',
|
277
310
|
'CONTENT_TYPE' => 'application/json'
|
278
311
|
end
|
279
|
-
|
312
|
+
|
313
|
+
it_behaves_like 'a cascaded request'
|
280
314
|
end
|
281
315
|
end
|
282
316
|
end
|
283
317
|
|
284
318
|
context 'API with cascade=true, http_codes but without a rescue handler' do
|
285
319
|
subject { Class.new(Grape::API) }
|
320
|
+
|
286
321
|
before do
|
287
322
|
subject.version 'v99', using: :header, vendor: 'vendorname', format: :json, cascade: true
|
288
323
|
subject.desc 'Get beer' do
|
@@ -301,17 +336,21 @@ describe Grape::Exceptions::InvalidAcceptHeader do
|
|
301
336
|
|
302
337
|
context 'that received a request with correct vendor and version' do
|
303
338
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v99' }
|
304
|
-
|
339
|
+
|
340
|
+
it_behaves_like 'a valid request'
|
305
341
|
end
|
306
342
|
|
307
343
|
context 'that receives' do
|
308
344
|
context 'an invalid version in the request' do
|
309
345
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.vendorname-v77' }
|
310
|
-
|
346
|
+
|
347
|
+
it_behaves_like 'a cascaded request'
|
311
348
|
end
|
349
|
+
|
312
350
|
context 'an invalid vendor in the request' do
|
313
351
|
before { get '/beer', {}, 'HTTP_ACCEPT' => 'application/vnd.invalidvendor-v99' }
|
314
|
-
|
352
|
+
|
353
|
+
it_behaves_like 'a cascaded request'
|
315
354
|
end
|
316
355
|
end
|
317
356
|
end
|
@@ -5,30 +5,31 @@ require 'ostruct'
|
|
5
5
|
|
6
6
|
describe Grape::Exceptions::ValidationErrors do
|
7
7
|
let(:validation_message) { 'FooBar is invalid' }
|
8
|
-
let(:validation_error) {
|
8
|
+
let(:validation_error) { instance_double Grape::Exceptions::Validation, params: [validation_message], message: '' }
|
9
9
|
|
10
10
|
context 'initialize' do
|
11
|
+
subject do
|
12
|
+
described_class.new(errors: [validation_error], headers: headers)
|
13
|
+
end
|
14
|
+
|
11
15
|
let(:headers) do
|
12
16
|
{
|
13
17
|
'A-Header-Key' => 'A-Header-Value'
|
14
18
|
}
|
15
19
|
end
|
16
20
|
|
17
|
-
|
18
|
-
described_class.new(errors: [validation_error], headers: headers)
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'should assign headers through base class' do
|
21
|
+
it 'assigns headers through base class' do
|
22
22
|
expect(subject.headers).to eq(headers)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
context 'message' do
|
27
27
|
context 'is not repeated' do
|
28
|
+
subject(:message) { error.message.split(',').map(&:strip) }
|
29
|
+
|
28
30
|
let(:error) do
|
29
31
|
described_class.new(errors: [validation_error, validation_error])
|
30
32
|
end
|
31
|
-
subject(:message) { error.message.split(',').map(&:strip) }
|
32
33
|
|
33
34
|
it { expect(message).to include validation_message }
|
34
35
|
it { expect(message.size).to eq 1 }
|
@@ -37,9 +38,10 @@ describe Grape::Exceptions::ValidationErrors do
|
|
37
38
|
|
38
39
|
describe '#full_messages' do
|
39
40
|
context 'with errors' do
|
41
|
+
subject { described_class.new(errors: [validation_error_1, validation_error_2]).full_messages }
|
42
|
+
|
40
43
|
let(:validation_error_1) { Grape::Exceptions::Validation.new(params: ['id'], message: :presence) }
|
41
44
|
let(:validation_error_2) { Grape::Exceptions::Validation.new(params: ['name'], message: :presence) }
|
42
|
-
subject { described_class.new(errors: [validation_error_1, validation_error_2]).full_messages }
|
43
45
|
|
44
46
|
it 'returns an array with each errors full message' do
|
45
47
|
expect(subject).to contain_exactly('id is missing', 'name is missing')
|
@@ -47,9 +49,10 @@ describe Grape::Exceptions::ValidationErrors do
|
|
47
49
|
end
|
48
50
|
|
49
51
|
context 'when attributes is an array of symbols' do
|
50
|
-
let(:validation_error) { Grape::Exceptions::Validation.new(params: [:admin_field], message: 'Can not set admin-only field') }
|
51
52
|
subject { described_class.new(errors: [validation_error]).full_messages }
|
52
53
|
|
54
|
+
let(:validation_error) { Grape::Exceptions::Validation.new(params: [:admin_field], message: 'Can not set admin-only field') }
|
55
|
+
|
53
56
|
it 'returns an array with an error full message' do
|
54
57
|
expect(subject.first).to eq('admin_field Can not set admin-only field')
|
55
58
|
end
|
@@ -65,7 +68,7 @@ describe Grape::Exceptions::ValidationErrors do
|
|
65
68
|
|
66
69
|
it 'can return structured json with separate fields' do
|
67
70
|
subject.format :json
|
68
|
-
subject.rescue_from
|
71
|
+
subject.rescue_from described_class do |e|
|
69
72
|
error!(e, 400)
|
70
73
|
end
|
71
74
|
subject.params do
|
@@ -4,16 +4,18 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
describe Grape::Exceptions::Validation do
|
6
6
|
it 'fails when params are missing' do
|
7
|
-
expect {
|
7
|
+
expect { described_class.new(message: 'presence') }.to raise_error(ArgumentError, /missing keyword:.+?params/)
|
8
8
|
end
|
9
|
+
|
9
10
|
context 'when message is a symbol' do
|
10
11
|
it 'stores message_key' do
|
11
|
-
expect(
|
12
|
+
expect(described_class.new(params: ['id'], message: :presence).message_key).to eq(:presence)
|
12
13
|
end
|
13
14
|
end
|
15
|
+
|
14
16
|
context 'when message is a String' do
|
15
17
|
it 'does not store the message_key' do
|
16
|
-
expect(
|
18
|
+
expect(described_class.new(params: ['id'], message: 'presence').message_key).to eq(nil)
|
17
19
|
end
|
18
20
|
end
|
19
21
|
end
|
@@ -10,10 +10,10 @@ describe Grape::Extensions::Hash::ParamBuilder do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
describe 'in an endpoint' do
|
13
|
-
|
13
|
+
describe '#params' do
|
14
14
|
before do
|
15
15
|
subject.params do
|
16
|
-
build_with Grape::Extensions::Hash::ParamBuilder
|
16
|
+
build_with Grape::Extensions::Hash::ParamBuilder # rubocop:disable RSpec/DescribedClass
|
17
17
|
end
|
18
18
|
|
19
19
|
subject.get do
|
@@ -21,7 +21,7 @@ describe Grape::Extensions::Hash::ParamBuilder do
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
it '
|
24
|
+
it 'is of type Hash' do
|
25
25
|
get '/'
|
26
26
|
expect(last_response.status).to eq(200)
|
27
27
|
expect(last_response.body).to eq('Hash')
|
@@ -31,17 +31,17 @@ describe Grape::Extensions::Hash::ParamBuilder do
|
|
31
31
|
|
32
32
|
describe 'in an api' do
|
33
33
|
before do
|
34
|
-
subject.send(:include, Grape::Extensions::Hash::ParamBuilder)
|
34
|
+
subject.send(:include, Grape::Extensions::Hash::ParamBuilder) # rubocop:disable RSpec/DescribedClass
|
35
35
|
end
|
36
36
|
|
37
|
-
|
37
|
+
describe '#params' do
|
38
38
|
before do
|
39
39
|
subject.get do
|
40
40
|
params.class
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
it '
|
44
|
+
it 'is Hash' do
|
45
45
|
get '/'
|
46
46
|
expect(last_response.status).to eq(200)
|
47
47
|
expect(last_response.body).to eq('Hash')
|
@@ -69,7 +69,7 @@ describe Grape::Extensions::Hash::ParamBuilder do
|
|
69
69
|
|
70
70
|
it 'symbolizes the params' do
|
71
71
|
subject.params do
|
72
|
-
build_with Grape::Extensions::Hash::ParamBuilder
|
72
|
+
build_with Grape::Extensions::Hash::ParamBuilder # rubocop:disable RSpec/DescribedClass
|
73
73
|
requires :a, type: String
|
74
74
|
end
|
75
75
|
|
@@ -10,10 +10,10 @@ describe Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuild
|
|
10
10
|
end
|
11
11
|
|
12
12
|
describe 'in an endpoint' do
|
13
|
-
|
13
|
+
describe '#params' do
|
14
14
|
before do
|
15
15
|
subject.params do
|
16
|
-
build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder
|
16
|
+
build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder # rubocop:disable RSpec/DescribedClass
|
17
17
|
end
|
18
18
|
|
19
19
|
subject.get do
|
@@ -21,7 +21,7 @@ describe Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuild
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
it '
|
24
|
+
it 'is of type Hash' do
|
25
25
|
get '/'
|
26
26
|
expect(last_response.status).to eq(200)
|
27
27
|
expect(last_response.body).to eq('ActiveSupport::HashWithIndifferentAccess')
|
@@ -31,10 +31,10 @@ describe Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuild
|
|
31
31
|
|
32
32
|
describe 'in an api' do
|
33
33
|
before do
|
34
|
-
subject.send(:include, Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder)
|
34
|
+
subject.send(:include, Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder) # rubocop:disable RSpec/DescribedClass
|
35
35
|
end
|
36
36
|
|
37
|
-
|
37
|
+
describe '#params' do
|
38
38
|
before do
|
39
39
|
subject.get do
|
40
40
|
params.class
|
@@ -49,7 +49,7 @@ describe Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuild
|
|
49
49
|
|
50
50
|
it 'parses sub hash params' do
|
51
51
|
subject.params do
|
52
|
-
build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder
|
52
|
+
build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder # rubocop:disable RSpec/DescribedClass
|
53
53
|
|
54
54
|
optional :a, type: Hash do
|
55
55
|
optional :b, type: Hash do
|
@@ -70,7 +70,7 @@ describe Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuild
|
|
70
70
|
|
71
71
|
it 'params are indifferent to symbol or string keys' do
|
72
72
|
subject.params do
|
73
|
-
build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder
|
73
|
+
build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder # rubocop:disable RSpec/DescribedClass
|
74
74
|
optional :a, type: Hash do
|
75
75
|
optional :b, type: Hash do
|
76
76
|
optional :c, type: String
|
@@ -90,7 +90,7 @@ describe Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuild
|
|
90
90
|
|
91
91
|
it 'responds to string keys' do
|
92
92
|
subject.params do
|
93
|
-
build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder
|
93
|
+
build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder # rubocop:disable RSpec/DescribedClass
|
94
94
|
requires :a, type: String
|
95
95
|
end
|
96
96
|
|
@@ -10,10 +10,10 @@ describe Grape::Extensions::Hashie::Mash::ParamBuilder do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
describe 'in an endpoint' do
|
13
|
-
|
13
|
+
describe '#params' do
|
14
14
|
before do
|
15
15
|
subject.params do
|
16
|
-
build_with Grape::Extensions::Hashie::Mash::ParamBuilder
|
16
|
+
build_with Grape::Extensions::Hashie::Mash::ParamBuilder # rubocop:disable RSpec/DescribedClass
|
17
17
|
end
|
18
18
|
|
19
19
|
subject.get do
|
@@ -21,7 +21,7 @@ describe Grape::Extensions::Hashie::Mash::ParamBuilder do
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
it '
|
24
|
+
it 'is of type Hashie::Mash' do
|
25
25
|
get '/'
|
26
26
|
expect(last_response.status).to eq(200)
|
27
27
|
expect(last_response.body).to eq('Hashie::Mash')
|
@@ -31,17 +31,17 @@ describe Grape::Extensions::Hashie::Mash::ParamBuilder do
|
|
31
31
|
|
32
32
|
describe 'in an api' do
|
33
33
|
before do
|
34
|
-
subject.send(:include, Grape::Extensions::Hashie::Mash::ParamBuilder)
|
34
|
+
subject.send(:include, Grape::Extensions::Hashie::Mash::ParamBuilder) # rubocop:disable RSpec/DescribedClass
|
35
35
|
end
|
36
36
|
|
37
|
-
|
37
|
+
describe '#params' do
|
38
38
|
before do
|
39
39
|
subject.get do
|
40
40
|
params.class
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
it '
|
44
|
+
it 'is Hashie::Mash' do
|
45
45
|
get '/'
|
46
46
|
expect(last_response.status).to eq(200)
|
47
47
|
expect(last_response.body).to eq('Hashie::Mash')
|
@@ -57,7 +57,7 @@ describe Grape::Extensions::Hashie::Mash::ParamBuilder do
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
it '
|
60
|
+
it 'is Hashie::Mash' do
|
61
61
|
get '/foo'
|
62
62
|
expect(last_response.status).to eq(200)
|
63
63
|
expect(last_response.body).to eq('Hashie::Mash')
|
@@ -66,7 +66,7 @@ describe Grape::Extensions::Hashie::Mash::ParamBuilder do
|
|
66
66
|
|
67
67
|
it 'is indifferent to key or symbol access' do
|
68
68
|
subject.params do
|
69
|
-
build_with Grape::Extensions::Hashie::Mash::ParamBuilder
|
69
|
+
build_with Grape::Extensions::Hashie::Mash::ParamBuilder # rubocop:disable RSpec/DescribedClass
|
70
70
|
requires :a, type: String
|
71
71
|
end
|
72
72
|
subject.get '/foo' do
|
data/spec/grape/loading_spec.rb
CHANGED
@@ -3,6 +3,14 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Grape::API do
|
6
|
+
subject do
|
7
|
+
CombinedApi = combined_api
|
8
|
+
Class.new(Grape::API) do
|
9
|
+
format :json
|
10
|
+
mount CombinedApi => '/'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
6
14
|
let(:jobs_api) do
|
7
15
|
Class.new(Grape::API) do
|
8
16
|
namespace :one do
|
@@ -26,14 +34,6 @@ describe Grape::API do
|
|
26
34
|
end
|
27
35
|
end
|
28
36
|
|
29
|
-
subject do
|
30
|
-
CombinedApi = combined_api
|
31
|
-
Class.new(Grape::API) do
|
32
|
-
format :json
|
33
|
-
mount CombinedApi => '/'
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
37
|
def app
|
38
38
|
subject
|
39
39
|
end
|
@@ -16,7 +16,7 @@ describe Grape::Middleware::Auth::DSL do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
describe '.auth' do
|
19
|
-
it '
|
19
|
+
it 'sets auth parameters' do
|
20
20
|
expect(subject.base_instance).to receive(:use).with(Grape::Middleware::Auth::Base, settings)
|
21
21
|
|
22
22
|
subject.auth :http_digest, realm: settings[:realm], opaque: settings[:opaque], &settings[:proc]
|
@@ -38,16 +38,25 @@ describe Grape::Middleware::Auth::DSL do
|
|
38
38
|
end
|
39
39
|
|
40
40
|
describe '.http_basic' do
|
41
|
-
it '
|
41
|
+
it 'sets auth parameters' do
|
42
42
|
subject.http_basic realm: 'my_realm', &settings[:proc]
|
43
43
|
expect(subject.auth).to eq(realm: 'my_realm', type: :http_basic, proc: block)
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
describe '.http_digest' do
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
context 'when realm is a hash' do
|
49
|
+
it 'sets auth parameters' do
|
50
|
+
subject.http_digest realm: { realm: 'my_realm', opaque: 'my_opaque' }, &settings[:proc]
|
51
|
+
expect(subject.auth).to eq(realm: { realm: 'my_realm', opaque: 'my_opaque' }, type: :http_digest, proc: block)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when realm is not hash' do
|
56
|
+
it 'sets auth parameters' do
|
57
|
+
subject.http_digest realm: 'my_realm', opaque: 'my_opaque', &settings[:proc]
|
58
|
+
expect(subject.auth).to eq(realm: 'my_realm', type: :http_digest, proc: block, opaque: 'my_opaque')
|
59
|
+
end
|
51
60
|
end
|
52
61
|
end
|
53
62
|
end
|