grape 1.5.3 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +67 -0
- data/CONTRIBUTING.md +2 -1
- data/README.md +150 -21
- data/UPGRADING.md +61 -4
- data/grape.gemspec +5 -5
- data/lib/grape/api/instance.rb +14 -18
- data/lib/grape/api.rb +17 -12
- data/lib/grape/cookies.rb +2 -0
- 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 -19
- data/lib/grape/dsl/headers.rb +5 -2
- data/lib/grape/dsl/helpers.rb +7 -7
- data/lib/grape/dsl/inside_route.rb +43 -30
- data/lib/grape/dsl/middleware.rb +4 -6
- data/lib/grape/dsl/parameters.rb +8 -10
- data/lib/grape/dsl/request_response.rb +9 -8
- data/lib/grape/dsl/routing.rb +6 -4
- data/lib/grape/dsl/settings.rb +5 -7
- data/lib/grape/dsl/validations.rb +0 -15
- data/lib/grape/endpoint.rb +20 -35
- data/lib/grape/error_formatter/json.rb +9 -7
- data/lib/grape/error_formatter/xml.rb +2 -6
- data/lib/grape/exceptions/base.rb +2 -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 +1 -6
- data/lib/grape/formatter/json.rb +1 -0
- data/lib/grape/formatter/serializable_hash.rb +2 -1
- data/lib/grape/formatter/xml.rb +1 -0
- data/lib/grape/locale/en.yml +9 -8
- data/lib/grape/middleware/auth/dsl.rb +7 -2
- data/lib/grape/middleware/base.rb +3 -1
- data/lib/grape/middleware/error.rb +2 -2
- data/lib/grape/middleware/formatter.rb +4 -4
- data/lib/grape/middleware/stack.rb +2 -2
- data/lib/grape/middleware/versioner/accept_version_header.rb +3 -5
- data/lib/grape/middleware/versioner/header.rb +6 -4
- data/lib/grape/middleware/versioner/param.rb +1 -0
- data/lib/grape/middleware/versioner/parse_media_type_patch.rb +2 -1
- data/lib/grape/middleware/versioner/path.rb +2 -0
- data/lib/grape/path.rb +1 -0
- data/lib/grape/request.rb +3 -0
- data/lib/grape/router/pattern.rb +1 -1
- data/lib/grape/router/route.rb +2 -2
- data/lib/grape/router.rb +6 -0
- data/lib/grape/util/inheritable_setting.rb +1 -3
- data/lib/grape/util/json.rb +2 -0
- data/lib/grape/util/lazy_value.rb +3 -2
- data/lib/grape/util/strict_hash_configuration.rb +1 -1
- data/lib/grape/validations/attributes_doc.rb +58 -0
- data/lib/grape/validations/params_scope.rb +137 -78
- data/lib/grape/validations/types/array_coercer.rb +0 -2
- data/lib/grape/validations/types/custom_type_coercer.rb +1 -0
- data/lib/grape/validations/types/dry_type_coercer.rb +4 -8
- data/lib/grape/validations/types/json.rb +2 -1
- data/lib/grape/validations/types/primitive_coercer.rb +16 -8
- 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_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 +82 -70
- 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 -20
- 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/validations.rb +16 -6
- data/lib/grape/version.rb +1 -1
- data/lib/grape.rb +69 -29
- data/spec/grape/api/custom_validations_spec.rb +116 -45
- data/spec/grape/api/deeply_included_options_spec.rb +3 -5
- data/spec/grape/api/defines_boolean_in_params_spec.rb +2 -3
- 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 +2 -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 +1 -3
- 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 +8 -10
- data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +9 -17
- data/spec/grape/api/shared_helpers_spec.rb +0 -2
- data/spec/grape/api_remount_spec.rb +16 -16
- data/spec/grape/api_spec.rb +457 -231
- data/spec/grape/config_spec.rb +0 -2
- data/spec/grape/dsl/callbacks_spec.rb +2 -3
- data/spec/grape/dsl/configuration_spec.rb +0 -2
- data/spec/grape/dsl/desc_spec.rb +0 -2
- data/spec/grape/dsl/headers_spec.rb +39 -11
- data/spec/grape/dsl/helpers_spec.rb +3 -4
- data/spec/grape/dsl/inside_route_spec.rb +16 -16
- data/spec/grape/dsl/logger_spec.rb +15 -19
- data/spec/grape/dsl/middleware_spec.rb +2 -3
- data/spec/grape/dsl/parameters_spec.rb +2 -2
- data/spec/grape/dsl/request_response_spec.rb +7 -8
- data/spec/grape/dsl/routing_spec.rb +11 -10
- 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 +261 -16
- data/spec/grape/endpoint_spec.rb +86 -58
- data/spec/grape/entity_spec.rb +22 -23
- data/spec/grape/exceptions/base_spec.rb +16 -2
- data/spec/grape/exceptions/body_parse_errors_spec.rb +3 -2
- data/spec/grape/exceptions/invalid_accept_header_spec.rb +61 -24
- 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 +13 -11
- data/spec/grape/exceptions/validation_spec.rb +5 -5
- data/spec/grape/extensions/param_builders/hash_spec.rb +7 -9
- data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +8 -10
- data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +8 -10
- data/spec/grape/integration/global_namespace_function_spec.rb +0 -2
- data/spec/grape/integration/rack_sendfile_spec.rb +1 -3
- data/spec/grape/integration/rack_spec.rb +0 -2
- data/spec/grape/loading_spec.rb +8 -10
- data/spec/grape/middleware/auth/base_spec.rb +0 -1
- data/spec/grape/middleware/auth/dsl_spec.rb +15 -8
- data/spec/grape/middleware/auth/strategies_spec.rb +60 -22
- data/spec/grape/middleware/base_spec.rb +24 -17
- data/spec/grape/middleware/error_spec.rb +8 -3
- data/spec/grape/middleware/exception_spec.rb +111 -163
- data/spec/grape/middleware/formatter_spec.rb +27 -8
- data/spec/grape/middleware/globals_spec.rb +7 -6
- data/spec/grape/middleware/stack_spec.rb +14 -14
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +2 -3
- data/spec/grape/middleware/versioner/header_spec.rb +30 -15
- data/spec/grape/middleware/versioner/param_spec.rb +7 -3
- data/spec/grape/middleware/versioner/path_spec.rb +5 -3
- data/spec/grape/middleware/versioner_spec.rb +1 -3
- data/spec/grape/named_api_spec.rb +0 -2
- data/spec/grape/parser_spec.rb +4 -2
- data/spec/grape/path_spec.rb +52 -54
- data/spec/grape/presenters/presenter_spec.rb +7 -8
- data/spec/grape/request_spec.rb +6 -6
- data/spec/grape/util/inheritable_setting_spec.rb +7 -8
- data/spec/grape/util/inheritable_values_spec.rb +3 -3
- data/spec/grape/util/reverse_stackable_values_spec.rb +3 -2
- data/spec/grape/util/stackable_values_spec.rb +7 -6
- 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/attributes_iterator_spec.rb +0 -2
- data/spec/grape/validations/instance_behaivour_spec.rb +9 -12
- data/spec/grape/validations/multiple_attributes_iterator_spec.rb +1 -2
- data/spec/grape/validations/params_scope_spec.rb +361 -96
- data/spec/grape/validations/single_attribute_iterator_spec.rb +2 -3
- data/spec/grape/validations/types/array_coercer_spec.rb +0 -2
- data/spec/grape/validations/types/primitive_coercer_spec.rb +24 -9
- data/spec/grape/validations/types/set_coercer_spec.rb +0 -2
- data/spec/grape/validations/types_spec.rb +36 -10
- data/spec/grape/validations/validators/all_or_none_spec.rb +50 -58
- data/spec/grape/validations/validators/allow_blank_spec.rb +135 -141
- data/spec/grape/validations/validators/at_least_one_of_spec.rb +50 -58
- data/spec/grape/validations/validators/coerce_spec.rb +23 -24
- data/spec/grape/validations/validators/default_spec.rb +72 -80
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +71 -79
- data/spec/grape/validations/validators/except_values_spec.rb +3 -5
- data/spec/grape/validations/validators/mutual_exclusion_spec.rb +71 -79
- data/spec/grape/validations/validators/presence_spec.rb +16 -3
- data/spec/grape/validations/validators/regexp_spec.rb +25 -33
- data/spec/grape/validations/validators/same_as_spec.rb +14 -22
- data/spec/grape/validations/validators/values_spec.rb +182 -179
- data/spec/grape/validations_spec.rb +149 -80
- data/spec/integration/eager_load/eager_load_spec.rb +2 -2
- data/spec/integration/multi_json/json_spec.rb +1 -3
- data/spec/integration/multi_xml/xml_spec.rb +1 -3
- data/spec/shared/versioning_examples.rb +12 -9
- data/spec/spec_helper.rb +21 -6
- data/spec/support/basic_auth_encode_helpers.rb +1 -1
- metadata +126 -117
- data/lib/grape/validations/validators/all_or_none.rb +0 -15
- data/lib/grape/validations/validators/allow_blank.rb +0 -18
- data/lib/grape/validations/validators/as.rb +0 -16
- data/lib/grape/validations/validators/at_least_one_of.rb +0 -14
- data/lib/grape/validations/validators/coerce.rb +0 -91
- data/lib/grape/validations/validators/default.rb +0 -48
- data/lib/grape/validations/validators/exactly_one_of.rb +0 -16
- data/lib/grape/validations/validators/except_values.rb +0 -22
- data/lib/grape/validations/validators/mutual_exclusion.rb +0 -15
- data/lib/grape/validations/validators/presence.rb +0 -12
- data/lib/grape/validations/validators/regexp.rb +0 -13
- data/lib/grape/validations/validators/same_as.rb +0 -26
- data/lib/grape/validations/validators/values.rb +0 -83
- data/spec/support/eager_load.rb +0 -19
data/spec/grape/config_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 CallbacksSpec
|
@@ -12,7 +10,8 @@ module Grape
|
|
12
10
|
|
13
11
|
describe Callbacks do
|
14
12
|
subject { Class.new(CallbacksSpec::Dummy) }
|
15
|
-
|
13
|
+
|
14
|
+
let(:proc) { -> {} }
|
16
15
|
|
17
16
|
describe '.before' do
|
18
17
|
it 'adds a block to "before"' do
|
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 HeadersSpec
|
@@ -12,21 +10,51 @@ module Grape
|
|
12
10
|
describe Headers do
|
13
11
|
subject { HeadersSpec::Dummy.new }
|
14
12
|
|
15
|
-
|
16
|
-
|
13
|
+
let(:header_data) do
|
14
|
+
{ 'First Key' => 'First Value',
|
15
|
+
'Second Key' => 'Second Value' }
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'when headers are set' do
|
19
|
+
describe '#header' do
|
17
20
|
before do
|
18
|
-
subject.header
|
21
|
+
header_data.each { |k, v| subject.header(k, v) }
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'get' do
|
25
|
+
it 'returns a specifc value' do
|
26
|
+
expect(subject.header['First Key']).to eq 'First Value'
|
27
|
+
expect(subject.header['Second Key']).to eq 'Second Value'
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'returns all set headers' do
|
31
|
+
expect(subject.header).to eq header_data
|
32
|
+
expect(subject.headers).to eq header_data
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe 'set' do
|
37
|
+
it 'returns value' do
|
38
|
+
expect(subject.header('Third Key', 'Third Value'))
|
39
|
+
expect(subject.header['Third Key']).to eq 'Third Value'
|
40
|
+
end
|
19
41
|
end
|
20
42
|
|
21
|
-
|
22
|
-
|
23
|
-
|
43
|
+
describe 'delete' do
|
44
|
+
it 'deletes a header key-value pair' do
|
45
|
+
expect(subject.header('First Key')).to eq header_data['First Key']
|
46
|
+
expect(subject.header).not_to have_key('First Key')
|
47
|
+
end
|
24
48
|
end
|
25
49
|
end
|
50
|
+
end
|
26
51
|
|
27
|
-
|
28
|
-
|
29
|
-
|
52
|
+
context 'when no headers are set' do
|
53
|
+
describe '#header' do
|
54
|
+
it 'returns nil' do
|
55
|
+
expect(subject.header['First Key']).to be_nil
|
56
|
+
expect(subject.header('First Key')).to be_nil
|
57
|
+
end
|
30
58
|
end
|
31
59
|
end
|
32
60
|
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 HelpersSpec
|
@@ -34,6 +32,7 @@ module Grape
|
|
34
32
|
|
35
33
|
describe Helpers do
|
36
34
|
subject { Class.new(HelpersSpec::Dummy) }
|
35
|
+
|
37
36
|
let(:proc) do
|
38
37
|
lambda do |*|
|
39
38
|
def test
|
@@ -54,7 +53,7 @@ module Grape
|
|
54
53
|
it 'uses provided modules' do
|
55
54
|
mod = Module.new
|
56
55
|
|
57
|
-
expect(subject).to receive(:namespace_stackable).with(:helpers, kind_of(Grape::DSL::Helpers::BaseHelper)).and_call_original.
|
56
|
+
expect(subject).to receive(:namespace_stackable).with(:helpers, kind_of(Grape::DSL::Helpers::BaseHelper)).and_call_original.twice
|
58
57
|
expect(subject).to receive(:namespace_stackable).with(:helpers).and_call_original
|
59
58
|
subject.helpers(mod, &proc)
|
60
59
|
|
@@ -92,7 +91,7 @@ module Grape
|
|
92
91
|
use :requires_toggle_prm
|
93
92
|
end
|
94
93
|
end
|
95
|
-
end.
|
94
|
+
end.not_to raise_exception
|
96
95
|
end
|
97
96
|
end
|
98
97
|
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
|
@@ -43,6 +41,7 @@ describe Grape::Endpoint do
|
|
43
41
|
before do
|
44
42
|
catch(:error) { subject.error! 'Not Found', 404 }
|
45
43
|
end
|
44
|
+
|
46
45
|
it 'sets status' do
|
47
46
|
expect(subject.status).to eq 404
|
48
47
|
end
|
@@ -53,6 +52,7 @@ describe Grape::Endpoint do
|
|
53
52
|
subject.namespace_inheritable(:default_error_status, 500)
|
54
53
|
catch(:error) { subject.error! 'Unknown' }
|
55
54
|
end
|
55
|
+
|
56
56
|
it 'sets status to default_error_status' do
|
57
57
|
expect(subject.status).to eq 500
|
58
58
|
end
|
@@ -136,7 +136,7 @@ describe Grape::Endpoint do
|
|
136
136
|
end
|
137
137
|
|
138
138
|
it 'accepts unknown Integer status codes' do
|
139
|
-
expect { subject.status 210 }.
|
139
|
+
expect { subject.status 210 }.not_to raise_error
|
140
140
|
end
|
141
141
|
|
142
142
|
it 'raises error if status is not a integer or symbol' do
|
@@ -165,7 +165,7 @@ describe Grape::Endpoint do
|
|
165
165
|
end
|
166
166
|
|
167
167
|
it 'returns default' do
|
168
|
-
expect(subject.content_type).to
|
168
|
+
expect(subject.content_type).to be_nil
|
169
169
|
end
|
170
170
|
end
|
171
171
|
|
@@ -198,7 +198,7 @@ describe Grape::Endpoint do
|
|
198
198
|
end
|
199
199
|
|
200
200
|
it 'returns default' do
|
201
|
-
expect(subject.body).to
|
201
|
+
expect(subject.body).to be_nil
|
202
202
|
end
|
203
203
|
end
|
204
204
|
|
@@ -273,7 +273,7 @@ describe Grape::Endpoint do
|
|
273
273
|
end
|
274
274
|
|
275
275
|
it 'sends no deprecation warnings' do
|
276
|
-
expect(subject).
|
276
|
+
expect(subject).not_to receive(:warn)
|
277
277
|
|
278
278
|
subject.sendfile file_path
|
279
279
|
end
|
@@ -313,7 +313,7 @@ describe Grape::Endpoint do
|
|
313
313
|
end
|
314
314
|
|
315
315
|
it 'returns default' do
|
316
|
-
expect(subject.sendfile).to
|
316
|
+
expect(subject.sendfile).to be_nil
|
317
317
|
end
|
318
318
|
end
|
319
319
|
|
@@ -334,7 +334,7 @@ describe Grape::Endpoint do
|
|
334
334
|
end
|
335
335
|
|
336
336
|
it 'emits no deprecation warnings' do
|
337
|
-
expect(subject).
|
337
|
+
expect(subject).not_to receive(:warn)
|
338
338
|
|
339
339
|
subject.stream file_path
|
340
340
|
end
|
@@ -360,13 +360,13 @@ describe Grape::Endpoint do
|
|
360
360
|
it 'sets Content-Length header to nil' do
|
361
361
|
subject.stream file_path
|
362
362
|
|
363
|
-
expect(subject.header['Content-Length']).to
|
363
|
+
expect(subject.header['Content-Length']).to be_nil
|
364
364
|
end
|
365
365
|
|
366
366
|
it 'sets Transfer-Encoding header to nil' do
|
367
367
|
subject.stream file_path
|
368
368
|
|
369
|
-
expect(subject.header['Transfer-Encoding']).to
|
369
|
+
expect(subject.header['Transfer-Encoding']).to be_nil
|
370
370
|
end
|
371
371
|
end
|
372
372
|
|
@@ -384,7 +384,7 @@ describe Grape::Endpoint do
|
|
384
384
|
end
|
385
385
|
|
386
386
|
it 'emits no deprecation warnings' do
|
387
|
-
expect(subject).
|
387
|
+
expect(subject).not_to receive(:warn)
|
388
388
|
|
389
389
|
subject.stream stream_object
|
390
390
|
end
|
@@ -404,13 +404,13 @@ describe Grape::Endpoint do
|
|
404
404
|
it 'sets Content-Length header to nil' do
|
405
405
|
subject.stream stream_object
|
406
406
|
|
407
|
-
expect(subject.header['Content-Length']).to
|
407
|
+
expect(subject.header['Content-Length']).to be_nil
|
408
408
|
end
|
409
409
|
|
410
410
|
it 'sets Transfer-Encoding header to nil' do
|
411
411
|
subject.stream stream_object
|
412
412
|
|
413
|
-
expect(subject.header['Transfer-Encoding']).to
|
413
|
+
expect(subject.header['Transfer-Encoding']).to be_nil
|
414
414
|
end
|
415
415
|
end
|
416
416
|
|
@@ -424,8 +424,8 @@ describe Grape::Endpoint do
|
|
424
424
|
end
|
425
425
|
|
426
426
|
it 'returns default' do
|
427
|
-
expect(subject.stream).to
|
428
|
-
expect(subject.header['Cache-Control']).to
|
427
|
+
expect(subject.stream).to be_nil
|
428
|
+
expect(subject.header['Cache-Control']).to be_nil
|
429
429
|
end
|
430
430
|
end
|
431
431
|
|
@@ -1,28 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
describe Grape::DSL::Logger do
|
4
|
+
subject { Class.new(dummy_logger) }
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
class Dummy
|
9
|
-
extend Grape::DSL::Logger
|
10
|
-
end
|
6
|
+
let(:dummy_logger) do
|
7
|
+
Class.new do
|
8
|
+
extend Grape::DSL::Logger
|
11
9
|
end
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:logger) { instance_double(::Logger) }
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
describe '.logger' do
|
15
|
+
it 'sets a logger' do
|
16
|
+
subject.logger logger
|
17
|
+
expect(subject.logger).to eq logger
|
18
|
+
end
|
21
19
|
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
20
|
+
it 'returns a logger' do
|
21
|
+
expect(subject.logger(logger)).to eq logger
|
26
22
|
end
|
27
23
|
end
|
28
24
|
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 MiddlewareSpec
|
@@ -12,7 +10,8 @@ module Grape
|
|
12
10
|
|
13
11
|
describe Middleware do
|
14
12
|
subject { Class.new(MiddlewareSpec::Dummy) }
|
15
|
-
|
13
|
+
|
14
|
+
let(:proc) { -> {} }
|
16
15
|
let(:foo_middleware) { Class.new }
|
17
16
|
let(:bar_middleware) { Class.new }
|
18
17
|
|
@@ -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 ParametersSpec
|
@@ -40,6 +38,7 @@ module Grape
|
|
40
38
|
|
41
39
|
def extract_message_option(attrs)
|
42
40
|
return nil unless attrs.is_a?(Array)
|
41
|
+
|
43
42
|
opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
|
44
43
|
opts.key?(:message) && !opts[:message].nil? ? opts.delete(:message) : nil
|
45
44
|
end
|
@@ -54,6 +53,7 @@ module Grape
|
|
54
53
|
allow_message_expectations_on_nil
|
55
54
|
allow(subject.api).to receive(:namespace_stackable).with(:named_params)
|
56
55
|
end
|
56
|
+
|
57
57
|
let(:options) { { option: 'value' } }
|
58
58
|
let(:named_params) { { params_group: proc {} } }
|
59
59
|
|
@@ -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
|
@@ -20,6 +18,7 @@ module Grape
|
|
20
18
|
|
21
19
|
describe RequestResponse do
|
22
20
|
subject { Class.new(RequestResponseSpec::Dummy) }
|
21
|
+
|
23
22
|
let(:c_type) { 'application/json' }
|
24
23
|
let(:format) { 'txt' }
|
25
24
|
|
@@ -161,34 +160,34 @@ module Grape
|
|
161
160
|
|
162
161
|
describe 'list of exceptions is passed' do
|
163
162
|
it 'sets hash of exceptions as rescue handlers' do
|
164
|
-
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 })
|
165
164
|
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
|
166
165
|
subject.rescue_from StandardError
|
167
166
|
end
|
168
167
|
|
169
168
|
it 'rescues only base handlers if rescue_subclasses: false option is passed' do
|
170
|
-
expect(subject).to receive(:namespace_reverse_stackable).with(:base_only_rescue_handlers, StandardError => nil)
|
171
|
-
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 })
|
172
171
|
subject.rescue_from StandardError, rescue_subclasses: false
|
173
172
|
end
|
174
173
|
|
175
174
|
it 'sets given proc as rescue handler for each key in hash' do
|
176
175
|
rescue_handler_proc = proc {}
|
177
|
-
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 })
|
178
177
|
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
|
179
178
|
subject.rescue_from StandardError, rescue_handler_proc
|
180
179
|
end
|
181
180
|
|
182
181
|
it 'sets given block as rescue handler for each key in hash' do
|
183
182
|
rescue_handler_proc = proc {}
|
184
|
-
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 })
|
185
184
|
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
|
186
185
|
subject.rescue_from StandardError, &rescue_handler_proc
|
187
186
|
end
|
188
187
|
|
189
188
|
it 'sets a rescue handler declared through :with option for each key in hash' do
|
190
189
|
with_block = -> { 'hello' }
|
191
|
-
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) })
|
192
191
|
expect(subject).to receive(:namespace_stackable).with(:rescue_options, {})
|
193
192
|
subject.rescue_from StandardError, with: with_block
|
194
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
|
@@ -12,7 +10,8 @@ module Grape
|
|
12
10
|
|
13
11
|
describe Routing do
|
14
12
|
subject { Class.new(RoutingSpec::Dummy) }
|
15
|
-
|
13
|
+
|
14
|
+
let(:proc) { -> {} }
|
16
15
|
let(:options) { { a: :b } }
|
17
16
|
let(:path) { '/dummy' }
|
18
17
|
|
@@ -20,7 +19,7 @@ module Grape
|
|
20
19
|
it 'sets a version for route' do
|
21
20
|
version = 'v1'
|
22
21
|
expect(subject).to receive(:namespace_inheritable).with(:version, [version])
|
23
|
-
expect(subject).to receive(:namespace_inheritable).with(:version_options, using: :path)
|
22
|
+
expect(subject).to receive(:namespace_inheritable).with(:version_options, { using: :path })
|
24
23
|
expect(subject.version(version)).to eq(version)
|
25
24
|
end
|
26
25
|
end
|
@@ -109,7 +108,7 @@ module Grape
|
|
109
108
|
it 'does not duplicate identical endpoints' do
|
110
109
|
subject.route(:any)
|
111
110
|
expect { subject.route(:any) }
|
112
|
-
.
|
111
|
+
.not_to change(subject.endpoints, :count)
|
113
112
|
end
|
114
113
|
|
115
114
|
it 'generates correct endpoint options' do
|
@@ -233,21 +232,23 @@ module Grape
|
|
233
232
|
allow(subject).to receive(:prepare_routes).and_return(routes)
|
234
233
|
subject.routes
|
235
234
|
end
|
236
|
-
|
237
|
-
|
235
|
+
|
236
|
+
it 'does not call prepare_routes again' do
|
237
|
+
expect(subject).not_to receive(:prepare_routes)
|
238
238
|
expect(subject.routes).to eq routes
|
239
239
|
end
|
240
240
|
end
|
241
241
|
end
|
242
242
|
|
243
243
|
describe '.route_param' do
|
244
|
+
let!(:options) { { requirements: regex } }
|
245
|
+
let(:regex) { /(.*)/ }
|
246
|
+
|
244
247
|
it 'calls #namespace with given params' do
|
245
248
|
expect(subject).to receive(:namespace).with(':foo', {}).and_yield
|
246
249
|
subject.route_param('foo', {}, &proc {})
|
247
250
|
end
|
248
251
|
|
249
|
-
let(:regex) { /(.*)/ }
|
250
|
-
let!(:options) { { requirements: regex } }
|
251
252
|
it 'nests requirements option under param name' do
|
252
253
|
expect(subject).to receive(:namespace) do |_param, options|
|
253
254
|
expect(options[:requirements][:foo]).to eq regex
|
@@ -258,7 +259,7 @@ module Grape
|
|
258
259
|
it 'does not modify options parameter' do
|
259
260
|
allow(subject).to receive(:namespace)
|
260
261
|
expect { subject.route_param('foo', options, &proc {}) }
|
261
|
-
.
|
262
|
+
.not_to change { options }
|
262
263
|
end
|
263
264
|
end
|
264
265
|
|
@@ -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
|