grape 1.6.2 → 1.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +54 -1
- data/CONTRIBUTING.md +30 -0
- data/README.md +146 -23
- data/UPGRADING.md +15 -0
- data/grape.gemspec +2 -2
- data/lib/grape/api/instance.rb +1 -1
- data/lib/grape/dry_types.rb +12 -0
- data/lib/grape/dsl/api.rb +0 -2
- data/lib/grape/dsl/callbacks.rb +0 -2
- data/lib/grape/dsl/configuration.rb +0 -2
- data/lib/grape/dsl/desc.rb +2 -16
- data/lib/grape/dsl/helpers.rb +0 -2
- data/lib/grape/dsl/inside_route.rb +34 -30
- data/lib/grape/dsl/middleware.rb +0 -2
- data/lib/grape/dsl/parameters.rb +10 -7
- data/lib/grape/dsl/request_response.rb +1 -3
- data/lib/grape/dsl/routing.rb +4 -2
- data/lib/grape/dsl/settings.rb +0 -2
- data/lib/grape/dsl/validations.rb +0 -15
- data/lib/grape/endpoint.rb +2 -2
- data/lib/grape/error_formatter/json.rb +7 -1
- data/lib/grape/exceptions/base.rb +3 -2
- data/lib/grape/exceptions/missing_group_type.rb +8 -1
- data/lib/grape/exceptions/too_many_multipart_files.rb +11 -0
- data/lib/grape/exceptions/unsupported_group_type.rb +8 -1
- data/lib/grape/exceptions/validation.rb +0 -4
- data/lib/grape/locale/en.yml +9 -8
- data/lib/grape/middleware/auth/dsl.rb +0 -1
- data/lib/grape/middleware/error.rb +2 -2
- data/lib/grape/middleware/stack.rb +1 -1
- data/lib/grape/request.rb +3 -1
- data/lib/grape/router/attribute_translator.rb +1 -1
- data/lib/grape/types/invalid_value.rb +8 -0
- data/lib/grape/util/cache.rb +1 -1
- data/lib/grape/util/json.rb +2 -0
- data/lib/grape/validations/attributes_doc.rb +58 -0
- data/lib/grape/validations/params_scope.rb +67 -41
- data/lib/grape/validations/types/array_coercer.rb +0 -2
- data/lib/grape/validations/types/dry_type_coercer.rb +3 -7
- data/lib/grape/validations/types/invalid_value.rb +0 -7
- data/lib/grape/validations/types/primitive_coercer.rb +14 -6
- data/lib/grape/validations/types/set_coercer.rb +0 -2
- data/lib/grape/validations/types.rb +98 -30
- data/lib/grape/validations/validators/{all_or_none.rb → all_or_none_of_validator.rb} +0 -2
- data/lib/grape/validations/validators/{at_least_one_of.rb → at_least_one_of_validator.rb} +0 -2
- data/lib/grape/validations/validators/base.rb +7 -0
- data/lib/grape/validations/validators/{exactly_one_of.rb → exactly_one_of_validator.rb} +0 -2
- data/lib/grape/validations/validators/{mutual_exclusion.rb → mutual_exclusion_validator.rb} +0 -2
- data/lib/grape/validations.rb +16 -12
- data/lib/grape/version.rb +1 -1
- data/lib/grape.rb +74 -28
- data/spec/grape/api/custom_validations_spec.rb +41 -2
- data/spec/grape/api/deeply_included_options_spec.rb +0 -2
- data/spec/grape/api/defines_boolean_in_params_spec.rb +0 -2
- data/spec/grape/api/documentation_spec.rb +59 -0
- data/spec/grape/api/inherited_helpers_spec.rb +0 -2
- data/spec/grape/api/instance_spec.rb +0 -1
- data/spec/grape/api/invalid_format_spec.rb +0 -2
- data/spec/grape/api/namespace_parameters_in_route_spec.rb +0 -2
- data/spec/grape/api/nested_helpers_spec.rb +0 -2
- data/spec/grape/api/optional_parameters_in_route_spec.rb +0 -2
- data/spec/grape/api/parameters_modification_spec.rb +0 -2
- data/spec/grape/api/patch_method_helpers_spec.rb +0 -2
- data/spec/grape/api/recognize_path_spec.rb +0 -2
- data/spec/grape/api/required_parameters_in_route_spec.rb +0 -2
- data/spec/grape/api/required_parameters_with_invalid_method_spec.rb +0 -2
- data/spec/grape/api/routes_with_requirements_spec.rb +0 -2
- data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +0 -2
- data/spec/grape/api/shared_helpers_spec.rb +0 -2
- data/spec/grape/api_remount_spec.rb +0 -1
- data/spec/grape/api_spec.rb +23 -25
- data/spec/grape/config_spec.rb +0 -2
- data/spec/grape/dsl/callbacks_spec.rb +0 -2
- data/spec/grape/dsl/desc_spec.rb +2 -2
- data/spec/grape/dsl/headers_spec.rb +2 -4
- data/spec/grape/dsl/helpers_spec.rb +0 -2
- data/spec/grape/dsl/inside_route_spec.rb +10 -12
- data/spec/grape/dsl/logger_spec.rb +0 -2
- data/spec/grape/dsl/middleware_spec.rb +0 -2
- data/spec/grape/dsl/parameters_spec.rb +0 -2
- data/spec/grape/dsl/request_response_spec.rb +6 -8
- data/spec/grape/dsl/routing_spec.rb +1 -3
- data/spec/grape/dsl/settings_spec.rb +0 -2
- data/spec/grape/dsl/validations_spec.rb +0 -17
- data/spec/grape/endpoint/declared_spec.rb +2 -4
- data/spec/grape/endpoint_spec.rb +29 -9
- data/spec/grape/entity_spec.rb +0 -1
- data/spec/grape/exceptions/base_spec.rb +16 -2
- data/spec/grape/exceptions/body_parse_errors_spec.rb +0 -2
- data/spec/grape/exceptions/invalid_accept_header_spec.rb +3 -2
- data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -2
- data/spec/grape/exceptions/invalid_response_spec.rb +0 -2
- data/spec/grape/exceptions/invalid_versioner_option_spec.rb +1 -3
- data/spec/grape/exceptions/missing_group_type_spec.rb +21 -0
- data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -2
- data/spec/grape/exceptions/missing_option_spec.rb +1 -3
- data/spec/grape/exceptions/unknown_options_spec.rb +0 -2
- data/spec/grape/exceptions/unknown_validator_spec.rb +0 -2
- data/spec/grape/exceptions/unsupported_group_type_spec.rb +23 -0
- data/spec/grape/exceptions/validation_errors_spec.rb +0 -1
- data/spec/grape/exceptions/validation_spec.rb +1 -3
- data/spec/grape/extensions/param_builders/hash_spec.rb +0 -2
- data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +0 -2
- data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +0 -2
- data/spec/grape/integration/global_namespace_function_spec.rb +0 -2
- data/spec/grape/integration/rack_sendfile_spec.rb +0 -2
- data/spec/grape/integration/rack_spec.rb +6 -7
- data/spec/grape/loading_spec.rb +0 -2
- data/spec/grape/middleware/auth/base_spec.rb +0 -1
- data/spec/grape/middleware/auth/dsl_spec.rb +0 -2
- data/spec/grape/middleware/auth/strategies_spec.rb +0 -2
- data/spec/grape/middleware/base_spec.rb +7 -7
- data/spec/grape/middleware/error_spec.rb +6 -1
- data/spec/grape/middleware/exception_spec.rb +0 -2
- data/spec/grape/middleware/formatter_spec.rb +6 -8
- data/spec/grape/middleware/globals_spec.rb +0 -2
- data/spec/grape/middleware/stack_spec.rb +0 -2
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -2
- data/spec/grape/middleware/versioner/header_spec.rb +18 -4
- data/spec/grape/middleware/versioner/param_spec.rb +0 -2
- data/spec/grape/middleware/versioner/path_spec.rb +0 -2
- data/spec/grape/middleware/versioner_spec.rb +0 -2
- data/spec/grape/named_api_spec.rb +0 -2
- data/spec/grape/parser_spec.rb +0 -2
- data/spec/grape/path_spec.rb +0 -2
- data/spec/grape/presenters/presenter_spec.rb +0 -2
- data/spec/grape/request_spec.rb +0 -2
- data/spec/grape/util/inheritable_setting_spec.rb +0 -1
- data/spec/grape/util/inheritable_values_spec.rb +0 -1
- data/spec/grape/util/reverse_stackable_values_spec.rb +0 -1
- data/spec/grape/util/stackable_values_spec.rb +0 -1
- data/spec/grape/util/strict_hash_configuration_spec.rb +0 -1
- data/spec/grape/validations/attributes_doc_spec.rb +153 -0
- data/spec/grape/validations/instance_behaivour_spec.rb +0 -2
- data/spec/grape/validations/multiple_attributes_iterator_spec.rb +0 -2
- data/spec/grape/validations/params_scope_spec.rb +315 -86
- data/spec/grape/validations/single_attribute_iterator_spec.rb +0 -2
- data/spec/grape/validations/types/array_coercer_spec.rb +0 -2
- data/spec/grape/validations/types/primitive_coercer_spec.rb +20 -5
- data/spec/grape/validations/types/set_coercer_spec.rb +0 -2
- data/spec/grape/validations/types_spec.rb +28 -2
- data/spec/grape/validations/validators/all_or_none_spec.rb +0 -2
- data/spec/grape/validations/validators/allow_blank_spec.rb +0 -2
- data/spec/grape/validations/validators/at_least_one_of_spec.rb +0 -2
- data/spec/grape/validations/validators/coerce_spec.rb +0 -2
- data/spec/grape/validations/validators/default_spec.rb +0 -2
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +0 -2
- data/spec/grape/validations/validators/except_values_spec.rb +0 -2
- data/spec/grape/validations/validators/mutual_exclusion_spec.rb +0 -2
- data/spec/grape/validations/validators/presence_spec.rb +0 -2
- data/spec/grape/validations/validators/regexp_spec.rb +0 -2
- data/spec/grape/validations/validators/same_as_spec.rb +0 -2
- data/spec/grape/validations/validators/values_spec.rb +19 -2
- data/spec/grape/validations_spec.rb +78 -27
- data/spec/integration/multi_json/json_spec.rb +0 -2
- data/spec/integration/multi_xml/xml_spec.rb +0 -2
- data/spec/spec_helper.rb +9 -4
- metadata +134 -122
- data/spec/grape/dsl/configuration_spec.rb +0 -16
- data/spec/grape/validations/attributes_iterator_spec.rb +0 -6
- data/spec/support/eager_load.rb +0 -19
- /data/lib/grape/validations/validators/{allow_blank.rb → allow_blank_validator.rb} +0 -0
- /data/lib/grape/validations/validators/{as.rb → as_validator.rb} +0 -0
- /data/lib/grape/validations/validators/{coerce.rb → coerce_validator.rb} +0 -0
- /data/lib/grape/validations/validators/{default.rb → default_validator.rb} +0 -0
- /data/lib/grape/validations/validators/{except_values.rb → except_values_validator.rb} +0 -0
- /data/lib/grape/validations/validators/{presence.rb → presence_validator.rb} +0 -0
- /data/lib/grape/validations/validators/{regexp.rb → regexp_validator.rb} +0 -0
- /data/lib/grape/validations/validators/{same_as.rb → same_as_validator.rb} +0 -0
- /data/lib/grape/validations/validators/{values.rb → values_validator.rb} +0 -0
data/spec/grape/api_spec.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
3
|
require 'shared/versioning_examples'
|
5
4
|
|
6
5
|
describe Grape::API do
|
@@ -104,22 +103,6 @@ describe Grape::API do
|
|
104
103
|
}
|
105
104
|
end
|
106
105
|
end
|
107
|
-
|
108
|
-
# Behavior as defined by rfc2616 when no header is defined
|
109
|
-
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
110
|
-
describe 'no specified accept header' do
|
111
|
-
# subject.version 'v1', using: :header
|
112
|
-
# subject.get '/hello' do
|
113
|
-
# 'hello'
|
114
|
-
# end
|
115
|
-
|
116
|
-
# it 'routes' do
|
117
|
-
# get '/hello'
|
118
|
-
# last_response.status.should eql 200
|
119
|
-
# end
|
120
|
-
end
|
121
|
-
|
122
|
-
# pending 'routes if any media type is allowed'
|
123
106
|
end
|
124
107
|
|
125
108
|
describe '.version using accept_version_header' do
|
@@ -449,9 +432,10 @@ describe Grape::API do
|
|
449
432
|
expect(last_response.body).to eql 'hiya'
|
450
433
|
end
|
451
434
|
|
435
|
+
objects = ['string', :symbol, 1, -1.1, {}, [], true, false, nil].freeze
|
452
436
|
%i[put post].each do |verb|
|
453
437
|
context verb.to_s do
|
454
|
-
|
438
|
+
objects.each do |object|
|
455
439
|
it "allows a(n) #{object.class} json object in params" do
|
456
440
|
subject.format :json
|
457
441
|
subject.send(verb) do
|
@@ -1217,7 +1201,7 @@ describe Grape::API do
|
|
1217
1201
|
|
1218
1202
|
it 'does not set Cache-Control' do
|
1219
1203
|
get '/foo'
|
1220
|
-
expect(last_response.headers['Cache-Control']).to
|
1204
|
+
expect(last_response.headers['Cache-Control']).to be_nil
|
1221
1205
|
end
|
1222
1206
|
|
1223
1207
|
it 'sets content type for xml' do
|
@@ -1242,7 +1226,7 @@ describe Grape::API do
|
|
1242
1226
|
|
1243
1227
|
it 'returns raw data when content type binary' do
|
1244
1228
|
image_filename = 'grape.png'
|
1245
|
-
file = File.
|
1229
|
+
file = File.binread(image_filename)
|
1246
1230
|
subject.format :binary
|
1247
1231
|
subject.get('/binary_file') { File.binread(image_filename) }
|
1248
1232
|
get '/binary_file'
|
@@ -1274,7 +1258,7 @@ describe Grape::API do
|
|
1274
1258
|
get '/stream', {}, 'HTTP_VERSION' => 'HTTP/1.1', 'SERVER_PROTOCOL' => 'HTTP/1.1'
|
1275
1259
|
|
1276
1260
|
expect(last_response.headers['Content-Type']).to eq('text/plain')
|
1277
|
-
expect(last_response.headers['Content-Length']).to
|
1261
|
+
expect(last_response.headers['Content-Length']).to be_nil
|
1278
1262
|
expect(last_response.headers['Cache-Control']).to eq('no-cache')
|
1279
1263
|
expect(last_response.headers['Transfer-Encoding']).to eq('chunked')
|
1280
1264
|
|
@@ -1602,7 +1586,7 @@ describe Grape::API do
|
|
1602
1586
|
|
1603
1587
|
subject.get(:hello) { 'Hello, world.' }
|
1604
1588
|
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('allow', 'whatever')
|
1605
|
-
expect(basic_auth_context).to
|
1589
|
+
expect(basic_auth_context).to be_a(Grape::Endpoint)
|
1606
1590
|
end
|
1607
1591
|
|
1608
1592
|
it 'has access to helper methods' do
|
@@ -2292,7 +2276,7 @@ describe Grape::API do
|
|
2292
2276
|
subject.rescue_from :all, with: :not_exist_method
|
2293
2277
|
subject.get('/rescue_method') { raise StandardError }
|
2294
2278
|
|
2295
|
-
expect { get '/rescue_method' }.to raise_error(NoMethodError, /^undefined method
|
2279
|
+
expect { get '/rescue_method' }.to raise_error(NoMethodError, /^undefined method 'not_exist_method'/)
|
2296
2280
|
end
|
2297
2281
|
|
2298
2282
|
it 'correctly chooses exception handler if :all handler is specified' do
|
@@ -3068,7 +3052,7 @@ describe Grape::API do
|
|
3068
3052
|
expect(route.description).to eq('first method')
|
3069
3053
|
expect(route.route_foo).to be_nil
|
3070
3054
|
expect(route.params).to eq({})
|
3071
|
-
expect(route.options).to
|
3055
|
+
expect(route.options).to be_a(Hash)
|
3072
3056
|
end
|
3073
3057
|
|
3074
3058
|
it 'has params which does not include format and version as named captures' do
|
@@ -3726,7 +3710,7 @@ describe Grape::API do
|
|
3726
3710
|
it 'sets the instance' do
|
3727
3711
|
expect(subject.instance).to be_nil
|
3728
3712
|
subject.compile
|
3729
|
-
expect(subject.instance).to
|
3713
|
+
expect(subject.instance).to be_a(subject.base_instance)
|
3730
3714
|
end
|
3731
3715
|
end
|
3732
3716
|
|
@@ -4158,6 +4142,20 @@ describe Grape::API do
|
|
4158
4142
|
end
|
4159
4143
|
end
|
4160
4144
|
|
4145
|
+
context 'with non-UTF-8 characters in specified format' do
|
4146
|
+
it 'converts the characters' do
|
4147
|
+
subject.format :json
|
4148
|
+
subject.content_type :json, 'application/json'
|
4149
|
+
subject.get '/something' do
|
4150
|
+
'foo'
|
4151
|
+
end
|
4152
|
+
get '/something?format=%0A%0B%BF'
|
4153
|
+
expect(last_response.status).to eq(406)
|
4154
|
+
message = "The requested format '\n\u000b\357\277\275' is not supported."
|
4155
|
+
expect(last_response.body).to eq({ error: message }.to_json)
|
4156
|
+
end
|
4157
|
+
end
|
4158
|
+
|
4161
4159
|
context 'body' do
|
4162
4160
|
context 'false' do
|
4163
4161
|
before do
|
data/spec/grape/config_spec.rb
CHANGED
data/spec/grape/dsl/desc_spec.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
module Grape
|
6
4
|
module DSL
|
7
5
|
module DescSpec
|
@@ -28,6 +26,7 @@ module Grape
|
|
28
26
|
detail: 'more details',
|
29
27
|
params: { first: :param },
|
30
28
|
entity: Object,
|
29
|
+
default: { code: 400, message: 'Invalid' },
|
31
30
|
http_codes: [[401, 'Unauthorized', 'Entities::Error']],
|
32
31
|
named: 'My named route',
|
33
32
|
body_name: 'My body name',
|
@@ -56,6 +55,7 @@ module Grape
|
|
56
55
|
detail 'more details'
|
57
56
|
params(first: :param)
|
58
57
|
success Object
|
58
|
+
default code: 400, message: 'Invalid'
|
59
59
|
failure [[401, 'Unauthorized', 'Entities::Error']]
|
60
60
|
named 'My named route'
|
61
61
|
body_name 'My body name'
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
module Grape
|
6
4
|
module DSL
|
7
5
|
module HeadersSpec
|
@@ -54,8 +52,8 @@ module Grape
|
|
54
52
|
context 'when no headers are set' do
|
55
53
|
describe '#header' do
|
56
54
|
it 'returns nil' do
|
57
|
-
expect(subject.header['First Key']).to
|
58
|
-
expect(subject.header('First Key')).to
|
55
|
+
expect(subject.header['First Key']).to be_nil
|
56
|
+
expect(subject.header('First Key')).to be_nil
|
59
57
|
end
|
60
58
|
end
|
61
59
|
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
module Grape
|
6
4
|
module DSL
|
7
5
|
module InsideRouteSpec
|
@@ -25,7 +23,7 @@ describe Grape::Endpoint do
|
|
25
23
|
|
26
24
|
describe '#version' do
|
27
25
|
it 'defaults to nil' do
|
28
|
-
expect(subject.version).to
|
26
|
+
expect(subject.version).to be_nil
|
29
27
|
end
|
30
28
|
|
31
29
|
it 'returns env[api.version]' do
|
@@ -167,7 +165,7 @@ describe Grape::Endpoint do
|
|
167
165
|
end
|
168
166
|
|
169
167
|
it 'returns default' do
|
170
|
-
expect(subject.content_type).to
|
168
|
+
expect(subject.content_type).to be_nil
|
171
169
|
end
|
172
170
|
end
|
173
171
|
|
@@ -200,7 +198,7 @@ describe Grape::Endpoint do
|
|
200
198
|
end
|
201
199
|
|
202
200
|
it 'returns default' do
|
203
|
-
expect(subject.body).to
|
201
|
+
expect(subject.body).to be_nil
|
204
202
|
end
|
205
203
|
end
|
206
204
|
|
@@ -315,7 +313,7 @@ describe Grape::Endpoint do
|
|
315
313
|
end
|
316
314
|
|
317
315
|
it 'returns default' do
|
318
|
-
expect(subject.sendfile).to
|
316
|
+
expect(subject.sendfile).to be_nil
|
319
317
|
end
|
320
318
|
end
|
321
319
|
|
@@ -362,13 +360,13 @@ describe Grape::Endpoint do
|
|
362
360
|
it 'sets Content-Length header to nil' do
|
363
361
|
subject.stream file_path
|
364
362
|
|
365
|
-
expect(subject.header['Content-Length']).to
|
363
|
+
expect(subject.header['Content-Length']).to be_nil
|
366
364
|
end
|
367
365
|
|
368
366
|
it 'sets Transfer-Encoding header to nil' do
|
369
367
|
subject.stream file_path
|
370
368
|
|
371
|
-
expect(subject.header['Transfer-Encoding']).to
|
369
|
+
expect(subject.header['Transfer-Encoding']).to be_nil
|
372
370
|
end
|
373
371
|
end
|
374
372
|
|
@@ -406,13 +404,13 @@ describe Grape::Endpoint do
|
|
406
404
|
it 'sets Content-Length header to nil' do
|
407
405
|
subject.stream stream_object
|
408
406
|
|
409
|
-
expect(subject.header['Content-Length']).to
|
407
|
+
expect(subject.header['Content-Length']).to be_nil
|
410
408
|
end
|
411
409
|
|
412
410
|
it 'sets Transfer-Encoding header to nil' do
|
413
411
|
subject.stream stream_object
|
414
412
|
|
415
|
-
expect(subject.header['Transfer-Encoding']).to
|
413
|
+
expect(subject.header['Transfer-Encoding']).to be_nil
|
416
414
|
end
|
417
415
|
end
|
418
416
|
|
@@ -426,8 +424,8 @@ describe Grape::Endpoint do
|
|
426
424
|
end
|
427
425
|
|
428
426
|
it 'returns default' do
|
429
|
-
expect(subject.stream).to
|
430
|
-
expect(subject.header['Cache-Control']).to
|
427
|
+
expect(subject.stream).to be_nil
|
428
|
+
expect(subject.header['Cache-Control']).to be_nil
|
431
429
|
end
|
432
430
|
end
|
433
431
|
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
module Grape
|
6
4
|
module DSL
|
7
5
|
module RequestResponseSpec
|
@@ -162,34 +160,34 @@ module Grape
|
|
162
160
|
|
163
161
|
describe 'list of exceptions is passed' do
|
164
162
|
it 'sets hash of exceptions as rescue handlers' do
|
165
|
-
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, StandardError => nil)
|
163
|
+
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, { StandardError => nil })
|
166
164
|
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
|
167
165
|
subject.rescue_from StandardError
|
168
166
|
end
|
169
167
|
|
170
168
|
it 'rescues only base handlers if rescue_subclasses: false option is passed' do
|
171
|
-
expect(subject).to receive(:namespace_reverse_stackable).with(:base_only_rescue_handlers, StandardError => nil)
|
172
|
-
expect(subject).to receive(:namespace_stackable).with(:rescue_options, rescue_subclasses: false)
|
169
|
+
expect(subject).to receive(:namespace_reverse_stackable).with(:base_only_rescue_handlers, { StandardError => nil })
|
170
|
+
expect(subject).to receive(:namespace_stackable).with(:rescue_options, { rescue_subclasses: false })
|
173
171
|
subject.rescue_from StandardError, rescue_subclasses: false
|
174
172
|
end
|
175
173
|
|
176
174
|
it 'sets given proc as rescue handler for each key in hash' do
|
177
175
|
rescue_handler_proc = proc {}
|
178
|
-
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, StandardError => rescue_handler_proc)
|
176
|
+
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, { StandardError => rescue_handler_proc })
|
179
177
|
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
|
180
178
|
subject.rescue_from StandardError, rescue_handler_proc
|
181
179
|
end
|
182
180
|
|
183
181
|
it 'sets given block as rescue handler for each key in hash' do
|
184
182
|
rescue_handler_proc = proc {}
|
185
|
-
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, StandardError => rescue_handler_proc)
|
183
|
+
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, { StandardError => rescue_handler_proc })
|
186
184
|
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
|
187
185
|
subject.rescue_from StandardError, &rescue_handler_proc
|
188
186
|
end
|
189
187
|
|
190
188
|
it 'sets a rescue handler declared through :with option for each key in hash' do
|
191
189
|
with_block = -> { 'hello' }
|
192
|
-
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, StandardError => an_instance_of(Proc))
|
190
|
+
expect(subject).to receive(:namespace_reverse_stackable).with(:rescue_handlers, { StandardError => an_instance_of(Proc) })
|
193
191
|
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
|
194
192
|
subject.rescue_from StandardError, with: with_block
|
195
193
|
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
module Grape
|
6
4
|
module DSL
|
7
5
|
module RoutingSpec
|
@@ -21,7 +19,7 @@ module Grape
|
|
21
19
|
it 'sets a version for route' do
|
22
20
|
version = 'v1'
|
23
21
|
expect(subject).to receive(:namespace_inheritable).with(:version, [version])
|
24
|
-
expect(subject).to receive(:namespace_inheritable).with(:version_options, using: :path)
|
22
|
+
expect(subject).to receive(:namespace_inheritable).with(:version_options, { using: :path })
|
25
23
|
expect(subject.version(version)).to eq(version)
|
26
24
|
end
|
27
25
|
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
module Grape
|
6
4
|
module DSL
|
7
5
|
module ValidationsSpec
|
@@ -38,10 +36,6 @@ module Grape
|
|
38
36
|
expect(subject.namespace_stackable(:params)).to eq []
|
39
37
|
end
|
40
38
|
|
41
|
-
it 'resets documentation params' do
|
42
|
-
expect(subject.route_setting(:description)[:params]).to be_nil
|
43
|
-
end
|
44
|
-
|
45
39
|
it 'does not reset documentation description' do
|
46
40
|
expect(subject.route_setting(:description)[:description]).to eq 'lol'
|
47
41
|
end
|
@@ -56,17 +50,6 @@ module Grape
|
|
56
50
|
expect { subject.params { raise 'foo' } }.to raise_error RuntimeError, 'foo'
|
57
51
|
end
|
58
52
|
end
|
59
|
-
|
60
|
-
describe '.document_attribute' do
|
61
|
-
before do
|
62
|
-
subject.document_attribute([full_name: 'xxx'], foo: 'bar')
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'creates a param documentation' do
|
66
|
-
expect(subject.namespace_stackable(:params)).to eq(['xxx' => { foo: 'bar' }])
|
67
|
-
expect(subject.route_setting(:description)).to eq(params: { 'xxx' => { foo: 'bar' } })
|
68
|
-
end
|
69
|
-
end
|
70
53
|
end
|
71
54
|
end
|
72
55
|
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
describe Grape::Endpoint do
|
6
4
|
subject { Class.new(Grape::API) }
|
7
5
|
|
@@ -249,7 +247,7 @@ describe Grape::Endpoint do
|
|
249
247
|
end
|
250
248
|
get '/declared?first=one&other=two'
|
251
249
|
expect(last_response.status).to eq(200)
|
252
|
-
expect(JSON.parse(last_response.body).key?(:other)).to
|
250
|
+
expect(JSON.parse(last_response.body).key?(:other)).to be false
|
253
251
|
end
|
254
252
|
|
255
253
|
it 'stringifies if that option is passed' do
|
@@ -522,7 +520,7 @@ describe Grape::Endpoint do
|
|
522
520
|
json = JSON.parse(last_response.body, symbolize_names: true)
|
523
521
|
|
524
522
|
expect(json[:declared_params][:id]).to eq 123
|
525
|
-
expect(json[:declared_params_no_parent][:id]).to
|
523
|
+
expect(json[:declared_params_no_parent][:id]).to be_nil
|
526
524
|
end
|
527
525
|
end
|
528
526
|
|
data/spec/grape/endpoint_spec.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
describe Grape::Endpoint do
|
6
4
|
subject { Class.new(Grape::API) }
|
7
5
|
|
@@ -77,7 +75,7 @@ describe Grape::Endpoint do
|
|
77
75
|
it 'sets itself in the env upon call' do
|
78
76
|
subject.get('/') { 'Hello world.' }
|
79
77
|
get '/'
|
80
|
-
expect(last_request.env['api.endpoint']).to
|
78
|
+
expect(last_request.env['api.endpoint']).to be_a(described_class)
|
81
79
|
end
|
82
80
|
|
83
81
|
describe '#status' do
|
@@ -142,7 +140,8 @@ describe Grape::Endpoint do
|
|
142
140
|
get '/headers'
|
143
141
|
expect(JSON.parse(last_response.body)).to eq(
|
144
142
|
'Host' => 'example.org',
|
145
|
-
'Cookie' => ''
|
143
|
+
'Cookie' => '',
|
144
|
+
'Version' => 'HTTP/1.0'
|
146
145
|
)
|
147
146
|
end
|
148
147
|
|
@@ -215,10 +214,10 @@ describe Grape::Endpoint do
|
|
215
214
|
end
|
216
215
|
get '/test', {}, 'HTTP_COOKIE' => 'delete_this_cookie=1; and_this=2'
|
217
216
|
expect(last_response.body).to eq('3')
|
218
|
-
cookies = last_response.headers['Set-Cookie'].split("\n").
|
217
|
+
cookies = last_response.headers['Set-Cookie'].split("\n").to_h do |set_cookie|
|
219
218
|
cookie = CookieJar::Cookie.from_set_cookie 'http://localhost/test', set_cookie
|
220
219
|
[cookie.name, cookie]
|
221
|
-
end
|
220
|
+
end
|
222
221
|
expect(cookies.size).to eq(2)
|
223
222
|
%w[and_this delete_this_cookie].each do |cookie_name|
|
224
223
|
cookie = cookies[cookie_name]
|
@@ -239,10 +238,10 @@ describe Grape::Endpoint do
|
|
239
238
|
end
|
240
239
|
get('/test', {}, 'HTTP_COOKIE' => 'delete_this_cookie=1; and_this=2')
|
241
240
|
expect(last_response.body).to eq('3')
|
242
|
-
cookies = last_response.headers['Set-Cookie'].split("\n").
|
241
|
+
cookies = last_response.headers['Set-Cookie'].split("\n").to_h do |set_cookie|
|
243
242
|
cookie = CookieJar::Cookie.from_set_cookie 'http://localhost/test', set_cookie
|
244
243
|
[cookie.name, cookie]
|
245
|
-
end
|
244
|
+
end
|
246
245
|
expect(cookies.size).to eq(2)
|
247
246
|
%w[and_this delete_this_cookie].each do |cookie_name|
|
248
247
|
cookie = cookies[cookie_name]
|
@@ -434,7 +433,28 @@ describe Grape::Endpoint do
|
|
434
433
|
end
|
435
434
|
post '/upload', { file: '' }, 'CONTENT_TYPE' => 'multipart/form-data; boundary=foobar'
|
436
435
|
expect(last_response.status).to eq(400)
|
437
|
-
expect(last_response.body).to eq('
|
436
|
+
expect(last_response.body).to eq('file is invalid')
|
437
|
+
end
|
438
|
+
end
|
439
|
+
|
440
|
+
context 'when the limit on multipart files is exceeded' do
|
441
|
+
around do |example|
|
442
|
+
limit = Rack::Utils.multipart_part_limit
|
443
|
+
Rack::Utils.multipart_part_limit = 1
|
444
|
+
example.run
|
445
|
+
Rack::Utils.multipart_part_limit = limit
|
446
|
+
end
|
447
|
+
|
448
|
+
it 'returns a 413 if given too many multipart files' do
|
449
|
+
subject.params do
|
450
|
+
requires :file, type: Rack::Multipart::UploadedFile
|
451
|
+
end
|
452
|
+
subject.post '/upload' do
|
453
|
+
params[:file][:filename]
|
454
|
+
end
|
455
|
+
post '/upload', { file: Rack::Test::UploadedFile.new(__FILE__, 'text/plain'), extra: Rack::Test::UploadedFile.new(__FILE__, 'text/plain') }
|
456
|
+
expect(last_response.status).to eq(413)
|
457
|
+
expect(last_response.body).to eq("the number of uploaded files exceeded the system's configured limit (1)")
|
438
458
|
end
|
439
459
|
end
|
440
460
|
|