grape 1.1.0 → 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 +370 -44
- data/CONTRIBUTING.md +32 -1
- data/LICENSE +1 -1
- data/README.md +683 -87
- data/UPGRADING.md +481 -17
- data/grape.gemspec +15 -4
- data/lib/grape/api/helpers.rb +2 -0
- data/lib/grape/api/instance.rb +279 -0
- data/lib/grape/api.rb +144 -176
- data/lib/grape/config.rb +34 -0
- data/lib/grape/content_types.rb +34 -0
- data/lib/grape/cookies.rb +4 -0
- data/lib/grape/dry_types.rb +12 -0
- data/lib/grape/dsl/api.rb +1 -1
- data/lib/grape/dsl/callbacks.rb +21 -1
- data/lib/grape/dsl/configuration.rb +1 -1
- data/lib/grape/dsl/desc.rb +41 -23
- data/lib/grape/dsl/headers.rb +7 -2
- data/lib/grape/dsl/helpers.rb +10 -7
- data/lib/grape/dsl/inside_route.rb +118 -62
- data/lib/grape/dsl/logger.rb +2 -0
- data/lib/grape/dsl/middleware.rb +11 -4
- data/lib/grape/dsl/parameters.rb +33 -19
- data/lib/grape/dsl/request_response.rb +12 -9
- data/lib/grape/dsl/routing.rb +22 -13
- data/lib/grape/dsl/settings.rb +10 -6
- data/lib/grape/dsl/validations.rb +19 -14
- data/lib/grape/eager_load.rb +20 -0
- data/lib/grape/endpoint.rb +67 -58
- data/lib/grape/error_formatter/base.rb +2 -0
- data/lib/grape/error_formatter/json.rb +11 -7
- data/lib/grape/error_formatter/txt.rb +2 -0
- data/lib/grape/error_formatter/xml.rb +4 -6
- data/lib/grape/error_formatter.rb +4 -2
- data/lib/grape/exceptions/base.rb +23 -16
- data/lib/grape/exceptions/empty_message_body.rb +11 -0
- data/lib/grape/exceptions/incompatible_option_values.rb +2 -0
- data/lib/grape/exceptions/invalid_accept_header.rb +2 -0
- data/lib/grape/exceptions/invalid_formatter.rb +2 -0
- data/lib/grape/exceptions/invalid_message_body.rb +2 -0
- data/lib/grape/exceptions/invalid_response.rb +11 -0
- data/lib/grape/exceptions/invalid_version_header.rb +2 -0
- data/lib/grape/exceptions/invalid_versioner_option.rb +2 -0
- data/lib/grape/exceptions/invalid_with_option_for_represent.rb +2 -0
- data/lib/grape/exceptions/method_not_allowed.rb +2 -0
- data/lib/grape/exceptions/missing_group_type.rb +10 -1
- data/lib/grape/exceptions/missing_mime_type.rb +2 -0
- data/lib/grape/exceptions/missing_option.rb +2 -0
- data/lib/grape/exceptions/missing_vendor_option.rb +2 -0
- data/lib/grape/exceptions/too_many_multipart_files.rb +11 -0
- data/lib/grape/exceptions/unknown_options.rb +2 -0
- data/lib/grape/exceptions/unknown_parameter.rb +2 -0
- data/lib/grape/exceptions/unknown_validator.rb +2 -0
- data/lib/grape/exceptions/unsupported_group_type.rb +10 -1
- data/lib/grape/exceptions/validation.rb +5 -8
- data/lib/grape/exceptions/validation_array_errors.rb +2 -0
- data/lib/grape/exceptions/validation_errors.rb +16 -13
- data/lib/grape/extensions/active_support/hash_with_indifferent_access.rb +4 -3
- data/lib/grape/extensions/deep_mergeable_hash.rb +2 -0
- data/lib/grape/extensions/deep_symbolize_hash.rb +2 -0
- data/lib/grape/extensions/hash.rb +2 -0
- data/lib/grape/extensions/hashie/mash.rb +2 -0
- data/lib/grape/formatter/json.rb +3 -0
- data/lib/grape/formatter/serializable_hash.rb +4 -1
- data/lib/grape/formatter/txt.rb +2 -0
- data/lib/grape/formatter/xml.rb +3 -0
- data/lib/grape/formatter.rb +5 -3
- data/lib/grape/http/headers.rb +50 -18
- data/lib/grape/locale/en.yml +11 -8
- data/lib/grape/middleware/auth/base.rb +7 -7
- data/lib/grape/middleware/auth/dsl.rb +9 -2
- data/lib/grape/middleware/auth/strategies.rb +2 -0
- data/lib/grape/middleware/auth/strategy_info.rb +2 -0
- data/lib/grape/middleware/base.rb +13 -8
- data/lib/grape/middleware/error.rb +22 -17
- data/lib/grape/middleware/filter.rb +2 -0
- data/lib/grape/middleware/formatter.rb +12 -10
- data/lib/grape/middleware/globals.rb +2 -0
- data/lib/grape/middleware/helpers.rb +12 -0
- data/lib/grape/middleware/stack.rb +16 -6
- data/lib/grape/middleware/versioner/accept_version_header.rb +5 -5
- data/lib/grape/middleware/versioner/header.rb +13 -9
- data/lib/grape/middleware/versioner/param.rb +4 -1
- data/lib/grape/middleware/versioner/parse_media_type_patch.rb +5 -1
- data/lib/grape/middleware/versioner/path.rb +5 -1
- data/lib/grape/middleware/versioner.rb +2 -0
- data/lib/grape/namespace.rb +14 -2
- data/lib/grape/parser/json.rb +3 -1
- data/lib/grape/parser/xml.rb +3 -1
- data/lib/grape/parser.rb +4 -2
- data/lib/grape/path.rb +16 -3
- data/lib/grape/presenters/presenter.rb +2 -0
- data/lib/grape/request.rb +21 -9
- data/lib/grape/router/attribute_translator.rb +41 -8
- data/lib/grape/router/pattern.rb +21 -17
- data/lib/grape/router/route.rb +15 -29
- data/lib/grape/router.rb +36 -29
- data/lib/grape/{serve_file → serve_stream}/file_body.rb +3 -1
- data/lib/grape/{serve_file → serve_stream}/sendfile_response.rb +3 -1
- data/lib/grape/{serve_file/file_response.rb → serve_stream/stream_response.rb} +10 -8
- data/lib/grape/types/invalid_value.rb +8 -0
- data/lib/grape/util/base_inheritable.rb +43 -0
- data/lib/grape/util/cache.rb +20 -0
- data/lib/grape/util/endpoint_configuration.rb +8 -0
- data/lib/grape/util/env.rb +19 -17
- data/lib/grape/util/inheritable_setting.rb +3 -3
- data/lib/grape/util/inheritable_values.rb +7 -25
- data/lib/grape/util/json.rb +4 -0
- data/lib/grape/util/lazy_block.rb +27 -0
- data/lib/grape/util/lazy_object.rb +43 -0
- data/lib/grape/util/lazy_value.rb +99 -0
- data/lib/grape/util/registrable.rb +2 -0
- data/lib/grape/util/reverse_stackable_values.rb +10 -35
- data/lib/grape/util/stackable_values.rb +21 -34
- data/lib/grape/util/strict_hash_configuration.rb +3 -1
- data/lib/grape/util/xml.rb +2 -0
- data/lib/grape/validations/attributes_doc.rb +58 -0
- data/lib/grape/validations/attributes_iterator.rb +16 -6
- data/lib/grape/validations/multiple_attributes_iterator.rb +13 -0
- data/lib/grape/validations/params_scope.rb +174 -94
- data/lib/grape/validations/single_attribute_iterator.rb +24 -0
- data/lib/grape/validations/types/array_coercer.rb +63 -0
- data/lib/grape/validations/types/build_coercer.rb +47 -49
- data/lib/grape/validations/types/custom_type_coercer.rb +30 -51
- data/lib/grape/validations/types/custom_type_collection_coercer.rb +10 -25
- data/lib/grape/validations/types/dry_type_coercer.rb +72 -0
- data/lib/grape/validations/types/file.rb +22 -18
- data/lib/grape/validations/types/invalid_value.rb +17 -0
- data/lib/grape/validations/types/json.rb +47 -39
- data/lib/grape/validations/types/multiple_type_coercer.rb +14 -33
- data/lib/grape/validations/types/primitive_coercer.rb +75 -0
- data/lib/grape/validations/types/set_coercer.rb +38 -0
- data/lib/grape/validations/types/variant_collection_coercer.rb +5 -13
- data/lib/grape/validations/types.rb +106 -63
- data/lib/grape/validations/validator_factory.rb +8 -11
- 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 +84 -68
- 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 +27 -16
- 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 +18 -6
- data/lib/grape/version.rb +3 -1
- data/lib/grape.rb +175 -94
- data/spec/grape/api/custom_validations_spec.rb +117 -44
- data/spec/grape/api/deeply_included_options_spec.rb +4 -4
- data/spec/grape/api/defines_boolean_in_params_spec.rb +38 -0
- data/spec/grape/api/documentation_spec.rb +59 -0
- data/spec/grape/api/inherited_helpers_spec.rb +1 -1
- data/spec/grape/api/instance_spec.rb +103 -0
- data/spec/grape/api/invalid_format_spec.rb +3 -1
- data/spec/grape/api/namespace_parameters_in_route_spec.rb +1 -1
- data/spec/grape/api/nested_helpers_spec.rb +1 -1
- data/spec/grape/api/optional_parameters_in_route_spec.rb +1 -1
- data/spec/grape/api/parameters_modification_spec.rb +2 -2
- data/spec/grape/api/patch_method_helpers_spec.rb +1 -1
- data/spec/grape/api/recognize_path_spec.rb +2 -2
- data/spec/grape/api/required_parameters_in_route_spec.rb +1 -1
- data/spec/grape/api/required_parameters_with_invalid_method_spec.rb +1 -1
- data/spec/grape/api/routes_with_requirements_spec.rb +59 -0
- data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +10 -16
- data/spec/grape/api/shared_helpers_spec.rb +1 -1
- data/spec/grape/api_remount_spec.rb +473 -0
- data/spec/grape/api_spec.rb +995 -231
- data/spec/grape/config_spec.rb +17 -0
- data/spec/grape/dsl/callbacks_spec.rb +3 -2
- data/spec/grape/dsl/desc_spec.rb +43 -17
- data/spec/grape/dsl/headers_spec.rb +40 -10
- data/spec/grape/dsl/helpers_spec.rb +6 -5
- data/spec/grape/dsl/inside_route_spec.rb +189 -38
- data/spec/grape/dsl/logger_spec.rb +17 -19
- data/spec/grape/dsl/middleware_spec.rb +11 -2
- data/spec/grape/dsl/parameters_spec.rb +3 -1
- data/spec/grape/dsl/request_response_spec.rb +8 -7
- data/spec/grape/dsl/routing_spec.rb +22 -9
- data/spec/grape/dsl/settings_spec.rb +1 -1
- data/spec/grape/dsl/validations_spec.rb +1 -16
- data/spec/grape/endpoint/declared_spec.rb +846 -0
- data/spec/grape/endpoint_spec.rb +136 -577
- data/spec/grape/entity_spec.rb +31 -24
- data/spec/grape/exceptions/base_spec.rb +81 -0
- data/spec/grape/exceptions/body_parse_errors_spec.rb +4 -1
- data/spec/grape/exceptions/invalid_accept_header_spec.rb +65 -23
- data/spec/grape/exceptions/invalid_formatter_spec.rb +1 -1
- data/spec/grape/exceptions/invalid_response_spec.rb +11 -0
- data/spec/grape/exceptions/invalid_versioner_option_spec.rb +2 -2
- data/spec/grape/exceptions/missing_group_type_spec.rb +21 -0
- data/spec/grape/exceptions/missing_mime_type_spec.rb +1 -1
- data/spec/grape/exceptions/missing_option_spec.rb +2 -2
- data/spec/grape/exceptions/unknown_options_spec.rb +1 -1
- data/spec/grape/exceptions/unknown_validator_spec.rb +1 -1
- data/spec/grape/exceptions/unsupported_group_type_spec.rb +23 -0
- data/spec/grape/exceptions/validation_errors_spec.rb +21 -15
- data/spec/grape/exceptions/validation_spec.rb +6 -4
- data/spec/grape/extensions/param_builders/hash_spec.rb +8 -8
- data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +9 -9
- data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +9 -9
- data/spec/grape/integration/global_namespace_function_spec.rb +2 -2
- data/spec/grape/integration/rack_sendfile_spec.rb +14 -10
- data/spec/grape/integration/rack_spec.rb +25 -8
- data/spec/grape/loading_spec.rb +9 -9
- data/spec/grape/middleware/auth/base_spec.rb +2 -1
- data/spec/grape/middleware/auth/dsl_spec.rb +19 -10
- data/spec/grape/middleware/auth/strategies_spec.rb +62 -22
- data/spec/grape/middleware/base_spec.rb +36 -17
- data/spec/grape/middleware/error_spec.rb +11 -4
- data/spec/grape/middleware/exception_spec.rb +112 -162
- data/spec/grape/middleware/formatter_spec.rb +65 -29
- data/spec/grape/middleware/globals_spec.rb +8 -5
- data/spec/grape/middleware/stack_spec.rb +25 -13
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +3 -2
- data/spec/grape/middleware/versioner/header_spec.rb +37 -14
- data/spec/grape/middleware/versioner/param_spec.rb +8 -2
- data/spec/grape/middleware/versioner/path_spec.rb +6 -2
- data/spec/grape/middleware/versioner_spec.rb +2 -2
- data/spec/grape/named_api_spec.rb +19 -0
- data/spec/grape/parser_spec.rb +10 -6
- data/spec/grape/path_spec.rb +53 -53
- data/spec/grape/presenters/presenter_spec.rb +8 -7
- data/spec/grape/request_spec.rb +29 -3
- data/spec/grape/util/inheritable_setting_spec.rb +9 -8
- data/spec/grape/util/inheritable_values_spec.rb +5 -3
- data/spec/grape/util/reverse_stackable_values_spec.rb +5 -2
- data/spec/grape/util/stackable_values_spec.rb +10 -7
- data/spec/grape/util/strict_hash_configuration_spec.rb +2 -1
- data/spec/grape/validations/attributes_doc_spec.rb +153 -0
- data/spec/grape/validations/instance_behaivour_spec.rb +13 -14
- data/spec/grape/validations/multiple_attributes_iterator_spec.rb +40 -0
- data/spec/grape/validations/params_scope_spec.rb +568 -99
- data/spec/grape/validations/single_attribute_iterator_spec.rb +57 -0
- data/spec/grape/validations/types/array_coercer_spec.rb +33 -0
- data/spec/grape/validations/types/primitive_coercer_spec.rb +150 -0
- data/spec/grape/validations/types/set_coercer_spec.rb +32 -0
- data/spec/grape/validations/types_spec.rb +44 -45
- data/spec/grape/validations/validators/all_or_none_spec.rb +134 -32
- data/spec/grape/validations/validators/allow_blank_spec.rb +137 -141
- data/spec/grape/validations/validators/at_least_one_of_spec.rb +169 -31
- data/spec/grape/validations/validators/coerce_spec.rb +491 -151
- data/spec/grape/validations/validators/default_spec.rb +242 -78
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +198 -40
- data/spec/grape/validations/validators/except_values_spec.rb +6 -5
- data/spec/grape/validations/validators/mutual_exclusion_spec.rb +181 -30
- data/spec/grape/validations/validators/presence_spec.rb +45 -2
- data/spec/grape/validations/validators/regexp_spec.rb +27 -33
- data/spec/grape/validations/validators/same_as_spec.rb +57 -0
- data/spec/grape/validations/validators/values_spec.rb +227 -180
- data/spec/grape/validations_spec.rb +502 -72
- data/spec/integration/eager_load/eager_load_spec.rb +15 -0
- data/spec/integration/multi_json/json_spec.rb +2 -2
- data/spec/integration/multi_xml/xml_spec.rb +2 -2
- data/spec/shared/versioning_examples.rb +34 -29
- data/spec/spec_helper.rb +31 -5
- data/spec/support/basic_auth_encode_helpers.rb +3 -1
- data/spec/support/chunks.rb +14 -0
- data/spec/support/content_type_helpers.rb +2 -0
- data/spec/support/endpoint_faker.rb +2 -0
- data/spec/support/file_streamer.rb +2 -0
- data/spec/support/integer_helpers.rb +2 -0
- data/spec/support/versioned_helpers.rb +8 -8
- metadata +111 -61
- data/Appraisals +0 -32
- data/Dangerfile +0 -2
- data/Gemfile +0 -33
- data/Gemfile.lock +0 -231
- data/Guardfile +0 -10
- data/RELEASING.md +0 -111
- data/Rakefile +0 -25
- data/benchmark/simple.rb +0 -27
- data/benchmark/simple_with_type_coercer.rb +0 -22
- data/gemfiles/multi_json.gemfile +0 -35
- data/gemfiles/multi_xml.gemfile +0 -35
- data/gemfiles/rack_1.5.2.gemfile +0 -35
- data/gemfiles/rack_edge.gemfile +0 -35
- data/gemfiles/rails_3.gemfile +0 -36
- data/gemfiles/rails_4.gemfile +0 -35
- data/gemfiles/rails_5.gemfile +0 -35
- data/gemfiles/rails_edge.gemfile +0 -35
- data/lib/grape/extensions/deep_hash_with_indifferent_access.rb +0 -18
- data/lib/grape/util/content_types.rb +0 -26
- data/lib/grape/validations/types/virtus_collection_patch.rb +0 -16
- data/lib/grape/validations/validators/all_or_none.rb +0 -20
- data/lib/grape/validations/validators/allow_blank.rb +0 -16
- data/lib/grape/validations/validators/as.rb +0 -15
- data/lib/grape/validations/validators/at_least_one_of.rb +0 -20
- data/lib/grape/validations/validators/coerce.rb +0 -74
- data/lib/grape/validations/validators/default.rb +0 -48
- data/lib/grape/validations/validators/exactly_one_of.rb +0 -29
- data/lib/grape/validations/validators/except_values.rb +0 -20
- data/lib/grape/validations/validators/mutual_exclusion.rb +0 -25
- data/lib/grape/validations/validators/presence.rb +0 -10
- data/lib/grape/validations/validators/regexp.rb +0 -11
- data/lib/grape/validations/validators/values.rb +0 -71
- data/pkg/grape-0.17.0.gem +0 -0
- data/pkg/grape-0.19.0.gem +0 -0
- data/spec/grape/dsl/configuration_spec.rb +0 -14
- data/spec/grape/validations/attributes_iterator_spec.rb +0 -4
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe '.configure' do
|
4
|
+
before do
|
5
|
+
Grape.configure do |config|
|
6
|
+
config.param_builder = 42
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
after do
|
11
|
+
Grape.config.reset
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'is configured to the new value' do
|
15
|
+
expect(Grape.config.param_builder).to eq 42
|
16
|
+
end
|
17
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module DSL
|
@@ -10,7 +10,8 @@ module Grape
|
|
10
10
|
|
11
11
|
describe Callbacks do
|
12
12
|
subject { Class.new(CallbacksSpec::Dummy) }
|
13
|
-
|
13
|
+
|
14
|
+
let(:proc) { -> {} }
|
14
15
|
|
15
16
|
describe '.before' do
|
16
17
|
it 'adds a block to "before"' do
|
data/spec/grape/dsl/desc_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module DSL
|
@@ -21,36 +21,62 @@ module Grape
|
|
21
21
|
|
22
22
|
it 'can be set with a block' do
|
23
23
|
expected_options = {
|
24
|
+
summary: 'summary',
|
24
25
|
description: 'The description',
|
25
26
|
detail: 'more details',
|
26
27
|
params: { first: :param },
|
27
28
|
entity: Object,
|
29
|
+
default: { code: 400, message: 'Invalid' },
|
28
30
|
http_codes: [[401, 'Unauthorized', 'Entities::Error']],
|
29
31
|
named: 'My named route',
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
body_name: 'My body name',
|
33
|
+
headers: [
|
34
|
+
XAuthToken: {
|
35
|
+
description: 'Valdates your identity',
|
36
|
+
required: true
|
37
|
+
},
|
38
|
+
XOptionalHeader: {
|
39
|
+
description: 'Not really needed',
|
40
|
+
required: false
|
41
|
+
}
|
42
|
+
],
|
43
|
+
hidden: false,
|
44
|
+
deprecated: false,
|
45
|
+
is_array: true,
|
46
|
+
nickname: 'nickname',
|
47
|
+
produces: %w[array of mime_types],
|
48
|
+
consumes: %w[array of mime_types],
|
49
|
+
tags: %w[tag1 tag2],
|
50
|
+
security: %w[array of security schemes]
|
38
51
|
}
|
39
52
|
|
40
53
|
subject.desc 'The description' do
|
54
|
+
summary 'summary'
|
41
55
|
detail 'more details'
|
42
56
|
params(first: :param)
|
43
57
|
success Object
|
58
|
+
default code: 400, message: 'Invalid'
|
44
59
|
failure [[401, 'Unauthorized', 'Entities::Error']]
|
45
60
|
named 'My named route'
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
61
|
+
body_name 'My body name'
|
62
|
+
headers [
|
63
|
+
XAuthToken: {
|
64
|
+
description: 'Valdates your identity',
|
65
|
+
required: true
|
66
|
+
},
|
67
|
+
XOptionalHeader: {
|
68
|
+
description: 'Not really needed',
|
69
|
+
required: false
|
70
|
+
}
|
71
|
+
]
|
72
|
+
hidden false
|
73
|
+
deprecated false
|
74
|
+
is_array true
|
75
|
+
nickname 'nickname'
|
76
|
+
produces %w[array of mime_types]
|
77
|
+
consumes %w[array of mime_types]
|
78
|
+
tags %w[tag1 tag2]
|
79
|
+
security %w[array of security schemes]
|
54
80
|
end
|
55
81
|
|
56
82
|
expect(subject.namespace_setting(:description)).to eq(expected_options)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module DSL
|
@@ -10,21 +10,51 @@ module Grape
|
|
10
10
|
describe Headers do
|
11
11
|
subject { HeadersSpec::Dummy.new }
|
12
12
|
|
13
|
-
|
14
|
-
|
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
|
15
20
|
before do
|
16
|
-
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
|
17
34
|
end
|
18
35
|
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
41
|
+
end
|
42
|
+
|
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
|
22
48
|
end
|
23
49
|
end
|
50
|
+
end
|
24
51
|
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
28
58
|
end
|
29
59
|
end
|
30
60
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module DSL
|
@@ -32,6 +32,7 @@ module Grape
|
|
32
32
|
|
33
33
|
describe Helpers do
|
34
34
|
subject { Class.new(HelpersSpec::Dummy) }
|
35
|
+
|
35
36
|
let(:proc) do
|
36
37
|
lambda do |*|
|
37
38
|
def test
|
@@ -52,7 +53,7 @@ module Grape
|
|
52
53
|
it 'uses provided modules' do
|
53
54
|
mod = Module.new
|
54
55
|
|
55
|
-
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
|
56
57
|
expect(subject).to receive(:namespace_stackable).with(:helpers).and_call_original
|
57
58
|
subject.helpers(mod, &proc)
|
58
59
|
|
@@ -75,9 +76,9 @@ module Grape
|
|
75
76
|
end
|
76
77
|
|
77
78
|
context 'with an external file' do
|
78
|
-
it 'sets Boolean as a
|
79
|
+
it 'sets Boolean as a Grape::API::Boolean' do
|
79
80
|
subject.helpers BooleanParam
|
80
|
-
expect(subject.first_mod::Boolean).to eq
|
81
|
+
expect(subject.first_mod::Boolean).to eq Grape::API::Boolean
|
81
82
|
end
|
82
83
|
end
|
83
84
|
|
@@ -90,7 +91,7 @@ module Grape
|
|
90
91
|
use :requires_toggle_prm
|
91
92
|
end
|
92
93
|
end
|
93
|
-
end.
|
94
|
+
end.not_to raise_exception
|
94
95
|
end
|
95
96
|
end
|
96
97
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module DSL
|
@@ -23,7 +23,7 @@ describe Grape::Endpoint do
|
|
23
23
|
|
24
24
|
describe '#version' do
|
25
25
|
it 'defaults to nil' do
|
26
|
-
expect(subject.version).to
|
26
|
+
expect(subject.version).to be_nil
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'returns env[api.version]' do
|
@@ -41,6 +41,7 @@ describe Grape::Endpoint do
|
|
41
41
|
before do
|
42
42
|
catch(:error) { subject.error! 'Not Found', 404 }
|
43
43
|
end
|
44
|
+
|
44
45
|
it 'sets status' do
|
45
46
|
expect(subject.status).to eq 404
|
46
47
|
end
|
@@ -51,6 +52,7 @@ describe Grape::Endpoint do
|
|
51
52
|
subject.namespace_inheritable(:default_error_status, 500)
|
52
53
|
catch(:error) { subject.error! 'Unknown' }
|
53
54
|
end
|
55
|
+
|
54
56
|
it 'sets status to default_error_status' do
|
55
57
|
expect(subject.status).to eq 500
|
56
58
|
end
|
@@ -134,7 +136,7 @@ describe Grape::Endpoint do
|
|
134
136
|
end
|
135
137
|
|
136
138
|
it 'accepts unknown Integer status codes' do
|
137
|
-
expect { subject.status 210 }.
|
139
|
+
expect { subject.status 210 }.not_to raise_error
|
138
140
|
end
|
139
141
|
|
140
142
|
it 'raises error if status is not a integer or symbol' do
|
@@ -163,7 +165,7 @@ describe Grape::Endpoint do
|
|
163
165
|
end
|
164
166
|
|
165
167
|
it 'returns default' do
|
166
|
-
expect(subject.content_type).to
|
168
|
+
expect(subject.content_type).to be_nil
|
167
169
|
end
|
168
170
|
end
|
169
171
|
|
@@ -196,85 +198,234 @@ describe Grape::Endpoint do
|
|
196
198
|
end
|
197
199
|
|
198
200
|
it 'returns default' do
|
199
|
-
expect(subject.body).to
|
201
|
+
expect(subject.body).to be_nil
|
200
202
|
end
|
201
203
|
end
|
202
204
|
|
203
205
|
describe '#file' do
|
206
|
+
before do
|
207
|
+
allow(subject).to receive(:warn)
|
208
|
+
end
|
209
|
+
|
204
210
|
describe 'set' do
|
205
211
|
context 'as file path' do
|
206
212
|
let(:file_path) { '/some/file/path' }
|
207
213
|
|
208
|
-
|
209
|
-
|
210
|
-
Grape::ServeFile::FileResponse.new(file_body)
|
211
|
-
end
|
214
|
+
it 'emits a warning that this method is deprecated' do
|
215
|
+
expect(subject).to receive(:warn).with(/Use sendfile or stream/)
|
212
216
|
|
213
|
-
before do
|
214
217
|
subject.file file_path
|
215
218
|
end
|
216
219
|
|
217
|
-
it '
|
218
|
-
expect(subject
|
220
|
+
it 'forwards the call to sendfile' do
|
221
|
+
expect(subject).to receive(:sendfile).with(file_path)
|
222
|
+
|
223
|
+
subject.file file_path
|
219
224
|
end
|
220
225
|
end
|
221
226
|
|
222
227
|
context 'as object (backward compatibility)' do
|
223
|
-
let(:file_object) {
|
228
|
+
let(:file_object) { double('StreamerObject', each: nil) }
|
229
|
+
|
230
|
+
it 'emits a warning that this method is deprecated' do
|
231
|
+
expect(subject).to receive(:warn).with(/Use stream to use a Stream object/)
|
232
|
+
|
233
|
+
subject.file file_object
|
234
|
+
end
|
235
|
+
|
236
|
+
it 'forwards the call to stream' do
|
237
|
+
expect(subject).to receive(:stream).with(file_object)
|
238
|
+
|
239
|
+
subject.file file_object
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
describe 'get' do
|
245
|
+
it 'emits a warning that this method is deprecated' do
|
246
|
+
expect(subject).to receive(:warn).with(/Use sendfile or stream/)
|
247
|
+
|
248
|
+
subject.file
|
249
|
+
end
|
250
|
+
|
251
|
+
it 'fowards call to sendfile' do
|
252
|
+
expect(subject).to receive(:sendfile)
|
253
|
+
|
254
|
+
subject.file
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
describe '#sendfile' do
|
260
|
+
describe 'set' do
|
261
|
+
context 'as file path' do
|
262
|
+
let(:file_path) { '/some/file/path' }
|
224
263
|
|
225
264
|
let(:file_response) do
|
226
|
-
Grape::
|
265
|
+
file_body = Grape::ServeStream::FileBody.new(file_path)
|
266
|
+
Grape::ServeStream::StreamResponse.new(file_body)
|
227
267
|
end
|
228
268
|
|
229
269
|
before do
|
230
|
-
subject.
|
270
|
+
subject.header 'Cache-Control', 'cache'
|
271
|
+
subject.header 'Content-Length', 123
|
272
|
+
subject.header 'Transfer-Encoding', 'base64'
|
273
|
+
end
|
274
|
+
|
275
|
+
it 'sends no deprecation warnings' do
|
276
|
+
expect(subject).not_to receive(:warn)
|
277
|
+
|
278
|
+
subject.sendfile file_path
|
279
|
+
end
|
280
|
+
|
281
|
+
it 'returns value wrapped in StreamResponse' do
|
282
|
+
subject.sendfile file_path
|
283
|
+
|
284
|
+
expect(subject.sendfile).to eq file_response
|
285
|
+
end
|
286
|
+
|
287
|
+
it 'does not change the Cache-Control header' do
|
288
|
+
subject.sendfile file_path
|
289
|
+
|
290
|
+
expect(subject.header['Cache-Control']).to eq 'cache'
|
291
|
+
end
|
292
|
+
|
293
|
+
it 'does not change the Content-Length header' do
|
294
|
+
subject.sendfile file_path
|
295
|
+
|
296
|
+
expect(subject.header['Content-Length']).to eq 123
|
231
297
|
end
|
232
298
|
|
233
|
-
it '
|
234
|
-
|
299
|
+
it 'does not change the Transfer-Encoding header' do
|
300
|
+
subject.sendfile file_path
|
301
|
+
|
302
|
+
expect(subject.header['Transfer-Encoding']).to eq 'base64'
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
context 'as object' do
|
307
|
+
let(:file_object) { double('StreamerObject', each: nil) }
|
308
|
+
|
309
|
+
it 'raises an error that only a file path is supported' do
|
310
|
+
expect { subject.sendfile file_object }.to raise_error(ArgumentError, /Argument must be a file path/)
|
235
311
|
end
|
236
312
|
end
|
237
313
|
end
|
238
314
|
|
239
315
|
it 'returns default' do
|
240
|
-
expect(subject.
|
316
|
+
expect(subject.sendfile).to be_nil
|
241
317
|
end
|
242
318
|
end
|
243
319
|
|
244
320
|
describe '#stream' do
|
245
321
|
describe 'set' do
|
246
|
-
|
322
|
+
context 'as a file path' do
|
323
|
+
let(:file_path) { '/some/file/path' }
|
247
324
|
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
subject.stream file_object
|
253
|
-
end
|
325
|
+
let(:file_response) do
|
326
|
+
file_body = Grape::ServeStream::FileBody.new(file_path)
|
327
|
+
Grape::ServeStream::StreamResponse.new(file_body)
|
328
|
+
end
|
254
329
|
|
255
|
-
|
256
|
-
|
257
|
-
|
330
|
+
before do
|
331
|
+
subject.header 'Cache-Control', 'cache'
|
332
|
+
subject.header 'Content-Length', 123
|
333
|
+
subject.header 'Transfer-Encoding', 'base64'
|
334
|
+
end
|
258
335
|
|
259
|
-
|
260
|
-
|
261
|
-
|
336
|
+
it 'emits no deprecation warnings' do
|
337
|
+
expect(subject).not_to receive(:warn)
|
338
|
+
|
339
|
+
subject.stream file_path
|
340
|
+
end
|
341
|
+
|
342
|
+
it 'returns file body wrapped in StreamResponse' do
|
343
|
+
subject.stream file_path
|
344
|
+
|
345
|
+
expect(subject.stream).to eq file_response
|
346
|
+
end
|
347
|
+
|
348
|
+
it 'sets Cache-Control header to no-cache' do
|
349
|
+
subject.stream file_path
|
350
|
+
|
351
|
+
expect(subject.header['Cache-Control']).to eq 'no-cache'
|
352
|
+
end
|
353
|
+
|
354
|
+
it 'does not change Cache-Control header' do
|
355
|
+
subject.stream
|
356
|
+
|
357
|
+
expect(subject.header['Cache-Control']).to eq 'cache'
|
358
|
+
end
|
359
|
+
|
360
|
+
it 'sets Content-Length header to nil' do
|
361
|
+
subject.stream file_path
|
362
|
+
|
363
|
+
expect(subject.header['Content-Length']).to be_nil
|
364
|
+
end
|
262
365
|
|
263
|
-
|
264
|
-
|
366
|
+
it 'sets Transfer-Encoding header to nil' do
|
367
|
+
subject.stream file_path
|
368
|
+
|
369
|
+
expect(subject.header['Transfer-Encoding']).to be_nil
|
370
|
+
end
|
265
371
|
end
|
266
372
|
|
267
|
-
|
268
|
-
|
373
|
+
context 'as a stream object' do
|
374
|
+
let(:stream_object) { double('StreamerObject', each: nil) }
|
375
|
+
|
376
|
+
let(:stream_response) do
|
377
|
+
Grape::ServeStream::StreamResponse.new(stream_object)
|
378
|
+
end
|
379
|
+
|
380
|
+
before do
|
381
|
+
subject.header 'Cache-Control', 'cache'
|
382
|
+
subject.header 'Content-Length', 123
|
383
|
+
subject.header 'Transfer-Encoding', 'base64'
|
384
|
+
end
|
385
|
+
|
386
|
+
it 'emits no deprecation warnings' do
|
387
|
+
expect(subject).not_to receive(:warn)
|
388
|
+
|
389
|
+
subject.stream stream_object
|
390
|
+
end
|
391
|
+
|
392
|
+
it 'returns value wrapped in StreamResponse' do
|
393
|
+
subject.stream stream_object
|
394
|
+
|
395
|
+
expect(subject.stream).to eq stream_response
|
396
|
+
end
|
397
|
+
|
398
|
+
it 'sets Cache-Control header to no-cache' do
|
399
|
+
subject.stream stream_object
|
400
|
+
|
401
|
+
expect(subject.header['Cache-Control']).to eq 'no-cache'
|
402
|
+
end
|
403
|
+
|
404
|
+
it 'sets Content-Length header to nil' do
|
405
|
+
subject.stream stream_object
|
406
|
+
|
407
|
+
expect(subject.header['Content-Length']).to be_nil
|
408
|
+
end
|
409
|
+
|
410
|
+
it 'sets Transfer-Encoding header to nil' do
|
411
|
+
subject.stream stream_object
|
412
|
+
|
413
|
+
expect(subject.header['Transfer-Encoding']).to be_nil
|
414
|
+
end
|
269
415
|
end
|
270
416
|
|
271
|
-
|
272
|
-
|
417
|
+
context 'as a non-stream object' do
|
418
|
+
let(:non_stream_object) { double('NonStreamerObject') }
|
419
|
+
|
420
|
+
it 'raises an error that the object must implement :each' do
|
421
|
+
expect { subject.stream non_stream_object }.to raise_error(ArgumentError, /:each/)
|
422
|
+
end
|
273
423
|
end
|
274
424
|
end
|
275
425
|
|
276
426
|
it 'returns default' do
|
277
|
-
expect(subject.
|
427
|
+
expect(subject.stream).to be_nil
|
428
|
+
expect(subject.header['Cache-Control']).to be_nil
|
278
429
|
end
|
279
430
|
end
|
280
431
|
|
@@ -1,26 +1,24 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
describe Grape::DSL::Logger do
|
4
|
+
subject { Class.new(dummy_logger) }
|
5
|
+
|
6
|
+
let(:dummy_logger) do
|
7
|
+
Class.new do
|
8
|
+
extend Grape::DSL::Logger
|
9
9
|
end
|
10
|
-
|
11
|
-
subject { Class.new(LoggerSpec::Dummy) }
|
12
|
-
let(:logger) { double(:logger) }
|
10
|
+
end
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
let(:logger) { instance_double(::Logger) }
|
13
|
+
|
14
|
+
describe '.logger' do
|
15
|
+
it 'sets a logger' do
|
16
|
+
subject.logger logger
|
17
|
+
expect(subject.logger).to eq logger
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
20
|
+
it 'returns a logger' do
|
21
|
+
expect(subject.logger(logger)).to eq logger
|
24
22
|
end
|
25
23
|
end
|
26
24
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module DSL
|
@@ -10,7 +10,8 @@ module Grape
|
|
10
10
|
|
11
11
|
describe Middleware do
|
12
12
|
subject { Class.new(MiddlewareSpec::Dummy) }
|
13
|
-
|
13
|
+
|
14
|
+
let(:proc) { -> {} }
|
14
15
|
let(:foo_middleware) { Class.new }
|
15
16
|
let(:bar_middleware) { Class.new }
|
16
17
|
|
@@ -22,6 +23,14 @@ module Grape
|
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
26
|
+
describe '.insert' do
|
27
|
+
it 'adds a middleware with the right operation' do
|
28
|
+
expect(subject).to receive(:namespace_stackable).with(:middleware, [:insert, 0, :arg1, proc])
|
29
|
+
|
30
|
+
subject.insert 0, :arg1, &proc
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
25
34
|
describe '.insert_before' do
|
26
35
|
it 'adds a middleware with the right operation' do
|
27
36
|
expect(subject).to receive(:namespace_stackable).with(:middleware, [:insert_before, foo_middleware, :arg1, proc])
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Grape
|
4
4
|
module DSL
|
@@ -38,6 +38,7 @@ module Grape
|
|
38
38
|
|
39
39
|
def extract_message_option(attrs)
|
40
40
|
return nil unless attrs.is_a?(Array)
|
41
|
+
|
41
42
|
opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
|
42
43
|
opts.key?(:message) && !opts[:message].nil? ? opts.delete(:message) : nil
|
43
44
|
end
|
@@ -52,6 +53,7 @@ module Grape
|
|
52
53
|
allow_message_expectations_on_nil
|
53
54
|
allow(subject.api).to receive(:namespace_stackable).with(:named_params)
|
54
55
|
end
|
56
|
+
|
55
57
|
let(:options) { { option: 'value' } }
|
56
58
|
let(:named_params) { { params_group: proc {} } }
|
57
59
|
|