grape 1.8.0 → 4.0.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 +517 -1
- data/CONTRIBUTING.md +19 -11
- data/README.md +841 -545
- data/UPGRADING.md +1553 -7
- data/grape.gemspec +11 -14
- data/lib/grape/api/instance.rb +77 -160
- data/lib/grape/api.rb +75 -107
- data/lib/grape/content_types.rb +67 -9
- data/lib/grape/cookies.rb +28 -25
- data/lib/grape/declared_params_handler.rb +116 -0
- data/lib/grape/dry_types.rb +48 -6
- data/lib/grape/dsl/callbacks.rb +8 -58
- data/lib/grape/dsl/declared.rb +35 -0
- data/lib/grape/dsl/desc.rb +25 -63
- data/lib/grape/dsl/entity.rb +106 -0
- data/lib/grape/dsl/headers.rb +2 -2
- data/lib/grape/dsl/helpers.rb +83 -64
- data/lib/grape/dsl/inside_route.rb +69 -296
- data/lib/grape/dsl/logger.rb +4 -9
- data/lib/grape/dsl/middleware.rb +22 -40
- data/lib/grape/dsl/parameters.rb +88 -94
- data/lib/grape/dsl/request_response.rb +143 -147
- data/lib/grape/dsl/rescue_options.rb +25 -0
- data/lib/grape/dsl/routing.rb +333 -191
- data/lib/grape/dsl/settings.rb +29 -135
- data/lib/grape/dsl/validations.rb +39 -32
- data/lib/grape/dsl/version_options.rb +23 -0
- data/lib/grape/endpoint/options.rb +25 -0
- data/lib/grape/endpoint.rb +319 -269
- data/lib/grape/{util/env.rb → env.rb} +8 -8
- data/lib/grape/error_formatter/base.rb +57 -21
- data/lib/grape/error_formatter/json.rb +13 -15
- data/lib/grape/error_formatter/serializable_hash.rb +7 -0
- data/lib/grape/error_formatter/txt.rb +12 -18
- data/lib/grape/error_formatter/xml.rb +3 -13
- data/lib/grape/error_formatter.rb +9 -25
- data/lib/grape/exceptions/base.rb +22 -58
- data/lib/grape/exceptions/error_response.rb +48 -0
- data/lib/grape/exceptions/incompatible_option_values.rb +1 -1
- data/lib/grape/exceptions/internal_server_error.rb +16 -0
- data/lib/grape/exceptions/invalid_accept_header.rb +1 -1
- data/lib/grape/exceptions/invalid_formatter.rb +1 -1
- data/lib/grape/exceptions/invalid_message_body.rb +1 -1
- data/lib/grape/exceptions/invalid_version_header.rb +1 -1
- data/lib/grape/exceptions/invalid_versioner_option.rb +1 -1
- data/lib/grape/exceptions/method_not_allowed.rb +1 -1
- data/lib/grape/exceptions/missing_group_type.rb +0 -2
- data/lib/grape/exceptions/missing_mime_type.rb +1 -1
- data/lib/grape/exceptions/request_error.rb +11 -0
- data/lib/grape/exceptions/unknown_auth_strategy.rb +11 -0
- data/lib/grape/exceptions/unknown_error_formatter.rb +11 -0
- data/lib/grape/exceptions/unknown_parameter.rb +1 -1
- data/lib/grape/exceptions/unknown_params_builder.rb +11 -0
- data/lib/grape/exceptions/unknown_validator.rb +1 -1
- data/lib/grape/exceptions/unsupported_group_type.rb +0 -2
- data/lib/grape/exceptions/validation.rb +28 -11
- data/lib/grape/exceptions/validation_array_errors.rb +5 -0
- data/lib/grape/exceptions/validation_errors.rb +22 -26
- data/lib/grape/formatter/base.rb +16 -0
- data/lib/grape/formatter/json.rb +5 -6
- data/lib/grape/formatter/serializable_hash.rb +7 -10
- data/lib/grape/formatter/txt.rb +3 -5
- data/lib/grape/formatter/xml.rb +4 -6
- data/lib/grape/formatter.rb +7 -25
- data/lib/grape/json.rb +46 -0
- data/lib/grape/locale/en.yml +44 -42
- data/lib/grape/middleware/auth/base.rb +11 -33
- data/lib/grape/middleware/auth/dsl.rb +12 -34
- data/lib/grape/middleware/auth/strategies.rb +1 -2
- data/lib/grape/middleware/base.rb +56 -33
- data/lib/grape/middleware/error.rb +290 -91
- data/lib/grape/middleware/formatter.rb +164 -106
- data/lib/grape/middleware/precomputed_content_types.rb +51 -0
- data/lib/grape/middleware/stack.rb +38 -40
- data/lib/grape/middleware/versioner/accept_version_header.rb +6 -33
- data/lib/grape/middleware/versioner/base.rb +66 -0
- data/lib/grape/middleware/versioner/header.rb +44 -129
- data/lib/grape/middleware/versioner/param.rb +4 -25
- data/lib/grape/middleware/versioner/path.rb +48 -25
- data/lib/grape/middleware/versioner.rb +7 -14
- data/lib/grape/mountable.rb +22 -0
- data/lib/grape/namespace.rb +21 -14
- data/lib/grape/params_builder/base.rb +20 -0
- data/lib/grape/params_builder/hash.rb +11 -0
- data/lib/grape/params_builder/hash_with_indifferent_access.rb +11 -0
- data/lib/grape/params_builder/hashie_mash.rb +11 -0
- data/lib/grape/params_builder.rb +15 -0
- data/lib/grape/parser/base.rb +16 -0
- data/lib/grape/parser/json.rb +6 -8
- data/lib/grape/parser/xml.rb +6 -8
- data/lib/grape/parser.rb +5 -23
- data/lib/grape/path.rb +8 -94
- data/lib/grape/precompiled_json.rb +50 -0
- data/lib/grape/railtie.rb +9 -0
- data/lib/grape/request.rb +200 -26
- data/lib/grape/router/base_route.rb +85 -0
- data/lib/grape/router/greedy_route.rb +30 -0
- data/lib/grape/router/mustermann_pattern.rb +44 -0
- data/lib/grape/router/pattern/path.rb +78 -0
- data/lib/grape/router/pattern.rb +77 -35
- data/lib/grape/router/route.rb +79 -60
- data/lib/grape/router.rb +128 -104
- data/lib/grape/serve_stream/file_body.rb +7 -0
- data/lib/grape/serve_stream/sendfile_response.rb +3 -5
- data/lib/grape/serve_stream/stream_response.rb +7 -0
- data/lib/grape/testing.rb +33 -0
- data/lib/grape/util/api_description.rb +67 -0
- data/lib/grape/util/cache.rb +22 -5
- data/lib/grape/util/deep_freeze.rb +34 -0
- data/lib/grape/util/endpoint_configuration.rb +1 -1
- data/lib/grape/util/freeze_on_new.rb +20 -0
- data/lib/grape/util/header.rb +13 -0
- data/lib/grape/util/inheritable_setting.rb +777 -39
- data/lib/grape/util/lazy/base.rb +16 -0
- data/lib/grape/util/lazy/block.rb +22 -0
- data/lib/grape/util/lazy/value.rb +31 -0
- data/lib/grape/util/lazy/value_array.rb +21 -0
- data/lib/grape/util/lazy/value_enumerable.rb +31 -0
- data/lib/grape/util/lazy/value_hash.rb +21 -0
- data/lib/grape/util/media_type.rb +74 -0
- data/lib/grape/util/path_normalizer.rb +37 -0
- data/lib/grape/util/registry.rb +37 -0
- data/lib/grape/util/shadowed_rescue_handlers.rb +49 -0
- data/lib/grape/util/stackable_values.rb +40 -16
- data/lib/grape/util/translation.rb +42 -0
- data/lib/grape/validations/attributes_iterator.rb +61 -28
- data/lib/grape/validations/coerce_options.rb +21 -0
- data/lib/grape/validations/contract_scope.rb +29 -0
- data/lib/grape/validations/multiple_attributes_iterator.rb +1 -1
- data/lib/grape/validations/oneof_collector.rb +35 -0
- data/lib/grape/validations/param_scope_tracker.rb +62 -0
- data/lib/grape/validations/params_documentation.rb +52 -0
- data/lib/grape/validations/params_scope.rb +223 -306
- data/lib/grape/validations/shared_options.rb +19 -0
- data/lib/grape/validations/single_attribute_iterator.rb +6 -4
- data/lib/grape/validations/types/array_coercer.rb +7 -12
- data/lib/grape/validations/types/custom_type_coercer.rb +47 -85
- data/lib/grape/validations/types/custom_type_collection_coercer.rb +1 -1
- data/lib/grape/validations/types/dry_type_coercer.rb +17 -28
- data/lib/grape/validations/types/json.rb +1 -5
- data/lib/grape/validations/types/multiple_type_coercer.rb +5 -3
- data/lib/grape/validations/types/primitive_coercer.rb +14 -35
- data/lib/grape/validations/types/set_coercer.rb +1 -4
- data/lib/grape/validations/types/variant_collection_coercer.rb +16 -3
- data/lib/grape/validations/types.rb +29 -54
- data/lib/grape/validations/validations_spec.rb +164 -0
- data/lib/grape/validations/validators/all_or_none_of_validator.rb +6 -3
- data/lib/grape/validations/validators/allow_blank_validator.rb +10 -5
- data/lib/grape/validations/validators/at_least_one_of_validator.rb +5 -2
- data/lib/grape/validations/validators/base.rb +118 -37
- data/lib/grape/validations/validators/coerce_validator.rb +26 -38
- data/lib/grape/validations/validators/contract_scope_validator.rb +46 -0
- data/lib/grape/validations/validators/default_validator.rb +13 -16
- data/lib/grape/validations/validators/exactly_one_of_validator.rb +10 -3
- data/lib/grape/validations/validators/except_values_validator.rb +15 -5
- data/lib/grape/validations/validators/length_validator.rb +50 -0
- data/lib/grape/validations/validators/multiple_params_base.rb +12 -9
- data/lib/grape/validations/validators/{mutual_exclusion_validator.rb → mutually_exclusive_validator.rb} +4 -2
- data/lib/grape/validations/validators/oneof_validator.rb +51 -0
- data/lib/grape/validations/validators/presence_validator.rb +4 -2
- data/lib/grape/validations/validators/regexp_validator.rb +11 -3
- data/lib/grape/validations/validators/same_as_validator.rb +7 -15
- data/lib/grape/validations/validators/values_validator.rb +36 -65
- data/lib/grape/validations.rb +8 -21
- data/lib/grape/version.rb +1 -2
- data/lib/grape/xml.rb +17 -0
- data/lib/grape.rb +96 -288
- metadata +83 -294
- data/lib/grape/api/helpers.rb +0 -9
- data/lib/grape/dsl/api.rb +0 -19
- data/lib/grape/dsl/configuration.rb +0 -15
- data/lib/grape/eager_load.rb +0 -20
- data/lib/grape/exceptions/empty_message_body.rb +0 -11
- data/lib/grape/exceptions/missing_option.rb +0 -11
- data/lib/grape/exceptions/too_many_multipart_files.rb +0 -11
- data/lib/grape/exceptions/unknown_options.rb +0 -11
- data/lib/grape/extensions/active_support/hash_with_indifferent_access.rb +0 -27
- data/lib/grape/extensions/hash.rb +0 -22
- data/lib/grape/extensions/hashie/mash.rb +0 -26
- data/lib/grape/http/headers.rb +0 -61
- data/lib/grape/middleware/globals.rb +0 -16
- data/lib/grape/middleware/helpers.rb +0 -12
- data/lib/grape/middleware/versioner/parse_media_type_patch.rb +0 -24
- data/lib/grape/router/attribute_translator.rb +0 -63
- data/lib/grape/types/invalid_value.rb +0 -8
- data/lib/grape/util/base_inheritable.rb +0 -43
- data/lib/grape/util/inheritable_values.rb +0 -31
- data/lib/grape/util/json.rb +0 -12
- data/lib/grape/util/lazy_block.rb +0 -27
- data/lib/grape/util/lazy_object.rb +0 -43
- data/lib/grape/util/lazy_value.rb +0 -91
- data/lib/grape/util/registrable.rb +0 -15
- data/lib/grape/util/reverse_stackable_values.rb +0 -20
- data/lib/grape/util/strict_hash_configuration.rb +0 -108
- data/lib/grape/util/xml.rb +0 -10
- data/lib/grape/validations/attributes_doc.rb +0 -58
- data/lib/grape/validations/types/build_coercer.rb +0 -94
- data/lib/grape/validations/validator_factory.rb +0 -15
- data/spec/grape/api/custom_validations_spec.rb +0 -213
- data/spec/grape/api/deeply_included_options_spec.rb +0 -56
- data/spec/grape/api/defines_boolean_in_params_spec.rb +0 -38
- data/spec/grape/api/documentation_spec.rb +0 -59
- data/spec/grape/api/inherited_helpers_spec.rb +0 -114
- data/spec/grape/api/instance_spec.rb +0 -103
- data/spec/grape/api/invalid_format_spec.rb +0 -45
- data/spec/grape/api/namespace_parameters_in_route_spec.rb +0 -38
- data/spec/grape/api/nested_helpers_spec.rb +0 -50
- data/spec/grape/api/optional_parameters_in_route_spec.rb +0 -43
- data/spec/grape/api/parameters_modification_spec.rb +0 -41
- data/spec/grape/api/patch_method_helpers_spec.rb +0 -79
- data/spec/grape/api/recognize_path_spec.rb +0 -21
- data/spec/grape/api/required_parameters_in_route_spec.rb +0 -37
- data/spec/grape/api/required_parameters_with_invalid_method_spec.rb +0 -26
- data/spec/grape/api/routes_with_requirements_spec.rb +0 -59
- data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +0 -41
- data/spec/grape/api/shared_helpers_spec.rb +0 -36
- data/spec/grape/api_remount_spec.rb +0 -509
- data/spec/grape/api_spec.rb +0 -4356
- data/spec/grape/dsl/callbacks_spec.rb +0 -45
- data/spec/grape/dsl/desc_spec.rb +0 -98
- data/spec/grape/dsl/headers_spec.rb +0 -62
- data/spec/grape/dsl/helpers_spec.rb +0 -100
- data/spec/grape/dsl/inside_route_spec.rb +0 -531
- data/spec/grape/dsl/logger_spec.rb +0 -24
- data/spec/grape/dsl/middleware_spec.rb +0 -60
- data/spec/grape/dsl/parameters_spec.rb +0 -180
- data/spec/grape/dsl/request_response_spec.rb +0 -225
- data/spec/grape/dsl/routing_spec.rb +0 -275
- data/spec/grape/dsl/settings_spec.rb +0 -261
- data/spec/grape/dsl/validations_spec.rb +0 -55
- data/spec/grape/endpoint/declared_spec.rb +0 -846
- data/spec/grape/endpoint_spec.rb +0 -1085
- data/spec/grape/entity_spec.rb +0 -336
- data/spec/grape/exceptions/base_spec.rb +0 -81
- data/spec/grape/exceptions/body_parse_errors_spec.rb +0 -185
- data/spec/grape/exceptions/invalid_accept_header_spec.rb +0 -358
- data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -15
- data/spec/grape/exceptions/invalid_response_spec.rb +0 -11
- data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -15
- data/spec/grape/exceptions/missing_group_type_spec.rb +0 -17
- data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -17
- data/spec/grape/exceptions/missing_option_spec.rb +0 -15
- data/spec/grape/exceptions/unknown_options_spec.rb +0 -15
- data/spec/grape/exceptions/unknown_validator_spec.rb +0 -15
- data/spec/grape/exceptions/unsupported_group_type_spec.rb +0 -19
- data/spec/grape/exceptions/validation_errors_spec.rb +0 -92
- data/spec/grape/exceptions/validation_spec.rb +0 -19
- data/spec/grape/extensions/param_builders/hash_spec.rb +0 -83
- data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +0 -105
- data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +0 -79
- data/spec/grape/grape_spec.rb +0 -9
- data/spec/grape/integration/global_namespace_function_spec.rb +0 -29
- data/spec/grape/integration/rack_sendfile_spec.rb +0 -48
- data/spec/grape/integration/rack_spec.rb +0 -51
- data/spec/grape/loading_spec.rb +0 -44
- data/spec/grape/middleware/auth/base_spec.rb +0 -31
- data/spec/grape/middleware/auth/dsl_spec.rb +0 -60
- data/spec/grape/middleware/auth/strategies_spec.rb +0 -120
- data/spec/grape/middleware/base_spec.rb +0 -221
- data/spec/grape/middleware/error_spec.rb +0 -85
- data/spec/grape/middleware/exception_spec.rb +0 -294
- data/spec/grape/middleware/formatter_spec.rb +0 -461
- data/spec/grape/middleware/globals_spec.rb +0 -30
- data/spec/grape/middleware/stack_spec.rb +0 -155
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -122
- data/spec/grape/middleware/versioner/header_spec.rb +0 -345
- data/spec/grape/middleware/versioner/param_spec.rb +0 -171
- data/spec/grape/middleware/versioner/path_spec.rb +0 -62
- data/spec/grape/middleware/versioner_spec.rb +0 -21
- data/spec/grape/named_api_spec.rb +0 -19
- data/spec/grape/parser_spec.rb +0 -86
- data/spec/grape/path_spec.rb +0 -252
- data/spec/grape/presenters/presenter_spec.rb +0 -71
- data/spec/grape/request_spec.rb +0 -126
- data/spec/grape/util/inheritable_setting_spec.rb +0 -242
- data/spec/grape/util/inheritable_values_spec.rb +0 -79
- data/spec/grape/util/reverse_stackable_values_spec.rb +0 -134
- data/spec/grape/util/stackable_values_spec.rb +0 -128
- data/spec/grape/util/strict_hash_configuration_spec.rb +0 -38
- data/spec/grape/validations/attributes_doc_spec.rb +0 -153
- data/spec/grape/validations/instance_behaivour_spec.rb +0 -43
- data/spec/grape/validations/multiple_attributes_iterator_spec.rb +0 -38
- data/spec/grape/validations/params_scope_spec.rb +0 -1420
- data/spec/grape/validations/single_attribute_iterator_spec.rb +0 -56
- data/spec/grape/validations/types/array_coercer_spec.rb +0 -33
- data/spec/grape/validations/types/primitive_coercer_spec.rb +0 -150
- data/spec/grape/validations/types/set_coercer_spec.rb +0 -32
- data/spec/grape/validations/types_spec.rb +0 -111
- data/spec/grape/validations/validators/all_or_none_spec.rb +0 -162
- data/spec/grape/validations/validators/allow_blank_spec.rb +0 -575
- data/spec/grape/validations/validators/at_least_one_of_spec.rb +0 -205
- data/spec/grape/validations/validators/base_spec.rb +0 -38
- data/spec/grape/validations/validators/coerce_spec.rb +0 -1261
- data/spec/grape/validations/validators/default_spec.rb +0 -463
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +0 -233
- data/spec/grape/validations/validators/except_values_spec.rb +0 -192
- data/spec/grape/validations/validators/mutual_exclusion_spec.rb +0 -214
- data/spec/grape/validations/validators/presence_spec.rb +0 -315
- data/spec/grape/validations/validators/regexp_spec.rb +0 -161
- data/spec/grape/validations/validators/same_as_spec.rb +0 -57
- data/spec/grape/validations/validators/values_spec.rb +0 -733
- data/spec/grape/validations/validators/zh-CN.yml +0 -10
- data/spec/grape/validations_spec.rb +0 -2030
- data/spec/integration/eager_load/eager_load_spec.rb +0 -15
- data/spec/integration/multi_json/json_spec.rb +0 -7
- data/spec/integration/multi_xml/xml_spec.rb +0 -7
- data/spec/shared/deprecated_class_examples.rb +0 -16
- data/spec/shared/versioning_examples.rb +0 -215
- data/spec/spec_helper.rb +0 -52
- data/spec/support/basic_auth_encode_helpers.rb +0 -11
- data/spec/support/chunks.rb +0 -14
- data/spec/support/content_type_helpers.rb +0 -15
- data/spec/support/endpoint_faker.rb +0 -25
- data/spec/support/file_streamer.rb +0 -13
- data/spec/support/integer_helpers.rb +0 -13
- data/spec/support/versioned_helpers.rb +0 -55
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
describe Grape::Middleware::Base do
|
|
4
|
-
subject { described_class.new(blank_app) }
|
|
5
|
-
|
|
6
|
-
let(:blank_app) { ->(_) { [200, {}, 'Hi there.'] } }
|
|
7
|
-
|
|
8
|
-
before do
|
|
9
|
-
# Keep it one object for testing.
|
|
10
|
-
allow(subject).to receive(:dup).and_return(subject)
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
it 'has the app as an accessor' do
|
|
14
|
-
expect(subject.app).to eq(blank_app)
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
it 'calls through to the app' do
|
|
18
|
-
expect(subject.call({})).to eq([200, {}, 'Hi there.'])
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
context 'callbacks' do
|
|
22
|
-
after { subject.call!({}) }
|
|
23
|
-
|
|
24
|
-
it 'calls #before' do
|
|
25
|
-
expect(subject).to receive(:before)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
it 'calls #after' do
|
|
29
|
-
expect(subject).to receive(:after)
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
context 'callbacks on error' do
|
|
34
|
-
let(:blank_app) { ->(_) { raise StandardError } }
|
|
35
|
-
|
|
36
|
-
it 'calls #after' do
|
|
37
|
-
expect(subject).to receive(:after)
|
|
38
|
-
expect { subject.call({}) }.to raise_error(StandardError)
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
context 'after callback' do
|
|
43
|
-
before do
|
|
44
|
-
allow(subject).to receive(:after).and_return([200, {}, 'Hello from after callback'])
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
it 'overwrites application response' do
|
|
48
|
-
expect(subject.call!({}).last).to eq('Hello from after callback')
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
context 'after callback with errors' do
|
|
53
|
-
it 'does not overwrite the application response' do
|
|
54
|
-
expect(subject.call({})).to eq([200, {}, 'Hi there.'])
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
context 'with patched warnings' do
|
|
58
|
-
before do
|
|
59
|
-
@warnings = warnings = []
|
|
60
|
-
allow_any_instance_of(described_class).to receive(:warn) { |m| warnings << m }
|
|
61
|
-
allow(subject).to receive(:after).and_raise(StandardError)
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
it 'does show a warning' do
|
|
65
|
-
expect { subject.call({}) }.to raise_error(StandardError)
|
|
66
|
-
expect(@warnings).not_to be_empty
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
it 'is able to access the response' do
|
|
72
|
-
subject.call({})
|
|
73
|
-
expect(subject.response).to be_a(Rack::Response)
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
describe '#response' do
|
|
77
|
-
subject do
|
|
78
|
-
described_class.new(response)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
before { subject.call({}) }
|
|
82
|
-
|
|
83
|
-
context 'when Array' do
|
|
84
|
-
let(:rack_response) { Rack::Response.new('test', 204, abc: 1) }
|
|
85
|
-
let(:response) { ->(_) { [204, { abc: 1 }, 'test'] } }
|
|
86
|
-
|
|
87
|
-
it 'status' do
|
|
88
|
-
expect(subject.response.status).to eq(204)
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
it 'body' do
|
|
92
|
-
expect(subject.response.body).to eq(['test'])
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
it 'header' do
|
|
96
|
-
expect(subject.response.header).to have_key(:abc)
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
it 'returns the memoized Rack::Response instance' do
|
|
100
|
-
allow(Rack::Response).to receive(:new).and_return(rack_response)
|
|
101
|
-
expect(subject.response).to eq(rack_response)
|
|
102
|
-
end
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
context 'when Rack::Response' do
|
|
106
|
-
let(:rack_response) { Rack::Response.new('test', 204, abc: 1) }
|
|
107
|
-
let(:response) { ->(_) { rack_response } }
|
|
108
|
-
|
|
109
|
-
it 'status' do
|
|
110
|
-
expect(subject.response.status).to eq(204)
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
it 'body' do
|
|
114
|
-
expect(subject.response.body).to eq(['test'])
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
it 'header' do
|
|
118
|
-
expect(subject.response.header).to have_key(:abc)
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
it 'returns the memoized Rack::Response instance' do
|
|
122
|
-
expect(subject.response).to eq(rack_response)
|
|
123
|
-
end
|
|
124
|
-
end
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
describe '#context' do
|
|
128
|
-
subject { described_class.new(blank_app) }
|
|
129
|
-
|
|
130
|
-
it 'allows access to response context' do
|
|
131
|
-
subject.call(Grape::Env::API_ENDPOINT => { header: 'some header' })
|
|
132
|
-
expect(subject.context).to eq(header: 'some header')
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
context 'options' do
|
|
137
|
-
it 'persists options passed at initialization' do
|
|
138
|
-
expect(described_class.new(blank_app, abc: true).options[:abc]).to be true
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
context 'defaults' do
|
|
142
|
-
module BaseSpec
|
|
143
|
-
class ExampleWare < Grape::Middleware::Base
|
|
144
|
-
def default_options
|
|
145
|
-
{ monkey: true }
|
|
146
|
-
end
|
|
147
|
-
end
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
it 'persists the default options' do
|
|
151
|
-
expect(BaseSpec::ExampleWare.new(blank_app).options[:monkey]).to be true
|
|
152
|
-
end
|
|
153
|
-
|
|
154
|
-
it 'overrides default options when provided' do
|
|
155
|
-
expect(BaseSpec::ExampleWare.new(blank_app, monkey: false).options[:monkey]).to be false
|
|
156
|
-
end
|
|
157
|
-
end
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
context 'header' do
|
|
161
|
-
module HeaderSpec
|
|
162
|
-
class ExampleWare < Grape::Middleware::Base
|
|
163
|
-
def before
|
|
164
|
-
header 'X-Test-Before', 'Hi'
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
def after
|
|
168
|
-
header 'X-Test-After', 'Bye'
|
|
169
|
-
nil
|
|
170
|
-
end
|
|
171
|
-
end
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
def app
|
|
175
|
-
Rack::Builder.app do
|
|
176
|
-
use HeaderSpec::ExampleWare
|
|
177
|
-
run ->(_) { [200, {}, ['Yeah']] }
|
|
178
|
-
end
|
|
179
|
-
end
|
|
180
|
-
|
|
181
|
-
it 'is able to set a header' do
|
|
182
|
-
get '/'
|
|
183
|
-
expect(last_response.headers['X-Test-Before']).to eq('Hi')
|
|
184
|
-
expect(last_response.headers['X-Test-After']).to eq('Bye')
|
|
185
|
-
end
|
|
186
|
-
end
|
|
187
|
-
|
|
188
|
-
context 'header overwrite' do
|
|
189
|
-
module HeaderOverwritingSpec
|
|
190
|
-
class ExampleWare < Grape::Middleware::Base
|
|
191
|
-
def before
|
|
192
|
-
header 'X-Test-Overwriting', 'Hi'
|
|
193
|
-
end
|
|
194
|
-
|
|
195
|
-
def after
|
|
196
|
-
header 'X-Test-Overwriting', 'Bye'
|
|
197
|
-
nil
|
|
198
|
-
end
|
|
199
|
-
end
|
|
200
|
-
|
|
201
|
-
class API < Grape::API
|
|
202
|
-
get('/') do
|
|
203
|
-
header 'X-Test-Overwriting', 'Yeah'
|
|
204
|
-
'Hello'
|
|
205
|
-
end
|
|
206
|
-
end
|
|
207
|
-
end
|
|
208
|
-
|
|
209
|
-
def app
|
|
210
|
-
Rack::Builder.app do
|
|
211
|
-
use HeaderOverwritingSpec::ExampleWare
|
|
212
|
-
run HeaderOverwritingSpec::API.new
|
|
213
|
-
end
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
it 'overwrites header by after headers' do
|
|
217
|
-
get '/'
|
|
218
|
-
expect(last_response.headers['X-Test-Overwriting']).to eq('Bye')
|
|
219
|
-
end
|
|
220
|
-
end
|
|
221
|
-
end
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'grape-entity'
|
|
4
|
-
|
|
5
|
-
describe Grape::Middleware::Error do
|
|
6
|
-
module ErrorSpec
|
|
7
|
-
class ErrorEntity < Grape::Entity
|
|
8
|
-
expose :code
|
|
9
|
-
expose :static
|
|
10
|
-
|
|
11
|
-
def static
|
|
12
|
-
'static text'
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
class ErrApp
|
|
17
|
-
class << self
|
|
18
|
-
attr_accessor :error, :format
|
|
19
|
-
|
|
20
|
-
def call(_env)
|
|
21
|
-
throw :error, error
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def app
|
|
28
|
-
opts = options
|
|
29
|
-
Rack::Builder.app do
|
|
30
|
-
use Spec::Support::EndpointFaker
|
|
31
|
-
use Grape::Middleware::Error, **opts
|
|
32
|
-
run ErrorSpec::ErrApp
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
let(:options) { { default_message: 'Aww, hamburgers.' } }
|
|
37
|
-
|
|
38
|
-
it 'sets the status code appropriately' do
|
|
39
|
-
ErrorSpec::ErrApp.error = { status: 410 }
|
|
40
|
-
get '/'
|
|
41
|
-
expect(last_response.status).to eq(410)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
it 'sets the status code based on the rack util status code symbol' do
|
|
45
|
-
ErrorSpec::ErrApp.error = { status: :gone }
|
|
46
|
-
get '/'
|
|
47
|
-
expect(last_response.status).to eq(410)
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
it 'sets the error message appropriately' do
|
|
51
|
-
ErrorSpec::ErrApp.error = { message: 'Awesome stuff.' }
|
|
52
|
-
get '/'
|
|
53
|
-
expect(last_response.body).to eq('Awesome stuff.')
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
it 'defaults to a 500 status' do
|
|
57
|
-
ErrorSpec::ErrApp.error = {}
|
|
58
|
-
get '/'
|
|
59
|
-
expect(last_response.status).to eq(500)
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
it 'has a default message' do
|
|
63
|
-
ErrorSpec::ErrApp.error = {}
|
|
64
|
-
get '/'
|
|
65
|
-
expect(last_response.body).to eq('Aww, hamburgers.')
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
context 'with http code' do
|
|
69
|
-
let(:options) { { default_message: 'Aww, hamburgers.' } }
|
|
70
|
-
|
|
71
|
-
it 'adds the status code if wanted' do
|
|
72
|
-
ErrorSpec::ErrApp.error = { message: { code: 200 } }
|
|
73
|
-
get '/'
|
|
74
|
-
|
|
75
|
-
expect(last_response.body).to eq({ code: 200 }.to_json)
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
it 'presents an error message' do
|
|
79
|
-
ErrorSpec::ErrApp.error = { message: { code: 200, with: ErrorSpec::ErrorEntity } }
|
|
80
|
-
get '/'
|
|
81
|
-
|
|
82
|
-
expect(last_response.body).to eq({ code: 200, static: 'static text' }.to_json)
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
end
|
|
@@ -1,294 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
describe Grape::Middleware::Error do
|
|
4
|
-
let(:exception_app) do
|
|
5
|
-
Class.new do
|
|
6
|
-
class << self
|
|
7
|
-
def call(_env)
|
|
8
|
-
raise 'rain!'
|
|
9
|
-
end
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
let(:other_exception_app) do
|
|
15
|
-
Class.new do
|
|
16
|
-
class << self
|
|
17
|
-
def call(_env)
|
|
18
|
-
raise NotImplementedError, 'snow!'
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
let(:custom_error_app) do
|
|
25
|
-
Class.new do
|
|
26
|
-
class << self
|
|
27
|
-
class CustomError < Grape::Exceptions::Base; end
|
|
28
|
-
|
|
29
|
-
def call(_env)
|
|
30
|
-
raise CustomError.new(status: 400, message: 'failed validation')
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
let(:error_hash_app) do
|
|
37
|
-
Class.new do
|
|
38
|
-
class << self
|
|
39
|
-
def error!(message, status)
|
|
40
|
-
throw :error, message: { error: message, detail: 'missing widget' }, status: status
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def call(_env)
|
|
44
|
-
error!('rain!', 401)
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
let(:access_denied_app) do
|
|
51
|
-
Class.new do
|
|
52
|
-
class << self
|
|
53
|
-
def error!(message, status)
|
|
54
|
-
throw :error, message: message, status: status
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def call(_env)
|
|
58
|
-
error!('Access Denied', 401)
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
let(:app) do
|
|
65
|
-
builder = Rack::Builder.new
|
|
66
|
-
builder.use Spec::Support::EndpointFaker
|
|
67
|
-
if options.any?
|
|
68
|
-
builder.use described_class, options
|
|
69
|
-
else
|
|
70
|
-
builder.use described_class
|
|
71
|
-
end
|
|
72
|
-
builder.run running_app
|
|
73
|
-
builder.to_app
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
context 'with defaults' do
|
|
77
|
-
let(:running_app) { exception_app }
|
|
78
|
-
let(:options) { {} }
|
|
79
|
-
|
|
80
|
-
it 'does not trap errors by default' do
|
|
81
|
-
expect { get '/' }.to raise_error(RuntimeError, 'rain!')
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
context 'with rescue_all' do
|
|
86
|
-
context 'StandardError exception' do
|
|
87
|
-
let(:running_app) { exception_app }
|
|
88
|
-
let(:options) { { rescue_all: true } }
|
|
89
|
-
|
|
90
|
-
it 'sets the message appropriately' do
|
|
91
|
-
get '/'
|
|
92
|
-
expect(last_response.body).to eq('rain!')
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
it 'defaults to a 500 status' do
|
|
96
|
-
get '/'
|
|
97
|
-
expect(last_response.status).to eq(500)
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
context 'Non-StandardError exception' do
|
|
102
|
-
let(:running_app) { other_exception_app }
|
|
103
|
-
let(:options) { { rescue_all: true } }
|
|
104
|
-
|
|
105
|
-
it 'does not trap errors other than StandardError' do
|
|
106
|
-
expect { get '/' }.to raise_error(NotImplementedError, 'snow!')
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
context 'Non-StandardError exception with a provided rescue handler' do
|
|
112
|
-
context 'default error response' do
|
|
113
|
-
let(:running_app) { other_exception_app }
|
|
114
|
-
let(:options) { { rescue_handlers: { NotImplementedError => nil } } }
|
|
115
|
-
|
|
116
|
-
it 'rescues the exception using the default handler' do
|
|
117
|
-
get '/'
|
|
118
|
-
expect(last_response.body).to eq('snow!')
|
|
119
|
-
end
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
context 'custom error response' do
|
|
123
|
-
let(:running_app) { other_exception_app }
|
|
124
|
-
let(:options) { { rescue_handlers: { NotImplementedError => -> { Rack::Response.new('rescued', 200, {}) } } } }
|
|
125
|
-
|
|
126
|
-
it 'rescues the exception using the provided handler' do
|
|
127
|
-
get '/'
|
|
128
|
-
expect(last_response.body).to eq('rescued')
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
context do
|
|
134
|
-
let(:running_app) { exception_app }
|
|
135
|
-
let(:options) { { rescue_all: true, default_status: 500 } }
|
|
136
|
-
|
|
137
|
-
it 'is possible to specify a different default status code' do
|
|
138
|
-
get '/'
|
|
139
|
-
expect(last_response.status).to eq(500)
|
|
140
|
-
end
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
context do
|
|
144
|
-
let(:running_app) { exception_app }
|
|
145
|
-
let(:options) { { rescue_all: true, format: :json } }
|
|
146
|
-
|
|
147
|
-
it 'is possible to return errors in json format' do
|
|
148
|
-
get '/'
|
|
149
|
-
expect(last_response.body).to eq('{"error":"rain!"}')
|
|
150
|
-
end
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
context do
|
|
154
|
-
let(:running_app) { error_hash_app }
|
|
155
|
-
let(:options) { { rescue_all: true, format: :json } }
|
|
156
|
-
|
|
157
|
-
it 'is possible to return hash errors in json format' do
|
|
158
|
-
get '/'
|
|
159
|
-
expect(['{"error":"rain!","detail":"missing widget"}',
|
|
160
|
-
'{"detail":"missing widget","error":"rain!"}']).to include(last_response.body)
|
|
161
|
-
end
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
context do
|
|
165
|
-
let(:running_app) { exception_app }
|
|
166
|
-
let(:options) { { rescue_all: true, format: :jsonapi } }
|
|
167
|
-
|
|
168
|
-
it 'is possible to return errors in jsonapi format' do
|
|
169
|
-
get '/'
|
|
170
|
-
expect(last_response.body).to eq('{"error":"rain!"}')
|
|
171
|
-
end
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
context do
|
|
175
|
-
let(:running_app) { error_hash_app }
|
|
176
|
-
let(:options) { { rescue_all: true, format: :jsonapi } }
|
|
177
|
-
|
|
178
|
-
it 'is possible to return hash errors in jsonapi format' do
|
|
179
|
-
get '/'
|
|
180
|
-
expect(['{"error":"rain!","detail":"missing widget"}',
|
|
181
|
-
'{"detail":"missing widget","error":"rain!"}']).to include(last_response.body)
|
|
182
|
-
end
|
|
183
|
-
end
|
|
184
|
-
|
|
185
|
-
context do
|
|
186
|
-
let(:running_app) { exception_app }
|
|
187
|
-
let(:options) { { rescue_all: true, format: :xml } }
|
|
188
|
-
|
|
189
|
-
it 'is possible to return errors in xml format' do
|
|
190
|
-
get '/'
|
|
191
|
-
expect(last_response.body).to eq("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<error>\n <message>rain!</message>\n</error>\n")
|
|
192
|
-
end
|
|
193
|
-
end
|
|
194
|
-
|
|
195
|
-
context do
|
|
196
|
-
let(:running_app) { error_hash_app }
|
|
197
|
-
let(:options) { { rescue_all: true, format: :xml } }
|
|
198
|
-
|
|
199
|
-
it 'is possible to return hash errors in xml format' do
|
|
200
|
-
get '/'
|
|
201
|
-
expect(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<error>\n <detail>missing widget</detail>\n <error>rain!</error>\n</error>\n",
|
|
202
|
-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<error>\n <error>rain!</error>\n <detail>missing widget</detail>\n</error>\n"]).to include(last_response.body)
|
|
203
|
-
end
|
|
204
|
-
end
|
|
205
|
-
|
|
206
|
-
context do
|
|
207
|
-
let(:running_app) { exception_app }
|
|
208
|
-
let(:options) do
|
|
209
|
-
{
|
|
210
|
-
rescue_all: true,
|
|
211
|
-
format: :custom,
|
|
212
|
-
error_formatters: {
|
|
213
|
-
custom: lambda do |message, _backtrace, _options, _env, _original_exception|
|
|
214
|
-
{ custom_formatter: message }.inspect
|
|
215
|
-
end
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
end
|
|
219
|
-
|
|
220
|
-
it 'is possible to specify a custom formatter' do
|
|
221
|
-
get '/'
|
|
222
|
-
expect(last_response.body).to eq('{:custom_formatter=>"rain!"}')
|
|
223
|
-
end
|
|
224
|
-
end
|
|
225
|
-
|
|
226
|
-
context do
|
|
227
|
-
let(:running_app) { access_denied_app }
|
|
228
|
-
let(:options) { {} }
|
|
229
|
-
|
|
230
|
-
it 'does not trap regular error! codes' do
|
|
231
|
-
get '/'
|
|
232
|
-
expect(last_response.status).to eq(401)
|
|
233
|
-
end
|
|
234
|
-
end
|
|
235
|
-
|
|
236
|
-
context do
|
|
237
|
-
let(:running_app) { custom_error_app }
|
|
238
|
-
let(:options) { { rescue_all: false } }
|
|
239
|
-
|
|
240
|
-
it 'responds to custom Grape exceptions appropriately' do
|
|
241
|
-
get '/'
|
|
242
|
-
expect(last_response.status).to eq(400)
|
|
243
|
-
expect(last_response.body).to eq('failed validation')
|
|
244
|
-
end
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
context 'with rescue_options :backtrace and :exception set to true' do
|
|
248
|
-
let(:running_app) { exception_app }
|
|
249
|
-
let(:options) do
|
|
250
|
-
{
|
|
251
|
-
rescue_all: true,
|
|
252
|
-
format: :json,
|
|
253
|
-
rescue_options: { backtrace: true, original_exception: true }
|
|
254
|
-
}
|
|
255
|
-
end
|
|
256
|
-
|
|
257
|
-
it 'is possible to return the backtrace and the original exception in json format' do
|
|
258
|
-
get '/'
|
|
259
|
-
expect(last_response.body).to include('error', 'rain!', 'backtrace', 'original_exception', 'RuntimeError')
|
|
260
|
-
end
|
|
261
|
-
end
|
|
262
|
-
|
|
263
|
-
context do
|
|
264
|
-
let(:running_app) { exception_app }
|
|
265
|
-
let(:options) do
|
|
266
|
-
{
|
|
267
|
-
rescue_all: true,
|
|
268
|
-
format: :xml,
|
|
269
|
-
rescue_options: { backtrace: true, original_exception: true }
|
|
270
|
-
}
|
|
271
|
-
end
|
|
272
|
-
|
|
273
|
-
it 'is possible to return the backtrace and the original exception in xml format' do
|
|
274
|
-
get '/'
|
|
275
|
-
expect(last_response.body).to include('error', 'rain!', 'backtrace', 'original-exception', 'RuntimeError')
|
|
276
|
-
end
|
|
277
|
-
end
|
|
278
|
-
|
|
279
|
-
context do
|
|
280
|
-
let(:running_app) { exception_app }
|
|
281
|
-
let(:options) do
|
|
282
|
-
{
|
|
283
|
-
rescue_all: true,
|
|
284
|
-
format: :txt,
|
|
285
|
-
rescue_options: { backtrace: true, original_exception: true }
|
|
286
|
-
}
|
|
287
|
-
end
|
|
288
|
-
|
|
289
|
-
it 'is possible to return the backtrace and the original exception in txt format' do
|
|
290
|
-
get '/'
|
|
291
|
-
expect(last_response.body).to include('error', 'rain!', 'backtrace', 'original exception', 'RuntimeError')
|
|
292
|
-
end
|
|
293
|
-
end
|
|
294
|
-
end
|