grape 1.5.3 → 1.7.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 +92 -0
- data/CONTRIBUTING.md +32 -1
- data/README.md +176 -25
- data/UPGRADING.md +61 -4
- data/grape.gemspec +6 -6
- 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 +4 -20
- 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 +13 -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 +22 -37
- data/lib/grape/error_formatter/json.rb +9 -7
- data/lib/grape/error_formatter/xml.rb +2 -6
- data/lib/grape/exceptions/base.rb +3 -2
- data/lib/grape/exceptions/missing_group_type.rb +8 -1
- data/lib/grape/exceptions/too_many_multipart_files.rb +11 -0
- data/lib/grape/exceptions/unsupported_group_type.rb +8 -1
- data/lib/grape/exceptions/validation.rb +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 +3 -3
- 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 +4 -1
- data/lib/grape/router/attribute_translator.rb +1 -1
- 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/types/invalid_value.rb +8 -0
- data/lib/grape/util/cache.rb +1 -1
- 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 +138 -79
- 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/invalid_value.rb +0 -7
- 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 +77 -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 +462 -251
- data/spec/grape/config_spec.rb +0 -2
- data/spec/grape/dsl/callbacks_spec.rb +2 -3
- data/spec/grape/dsl/desc_spec.rb +2 -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 +88 -59
- 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 +64 -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 +6 -7
- 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 +28 -19
- 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 +33 -14
- 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/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 +201 -179
- data/spec/grape/validations_spec.rb +171 -79
- 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 +41 -29
- 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/grape/dsl/configuration_spec.rb +0 -16
- data/spec/grape/validations/attributes_iterator_spec.rb +0 -6
- data/spec/support/eager_load.rb +0 -19
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
describe Grape::Extensions::Hashie::Mash::ParamBuilder do
|
6
4
|
subject { Class.new(Grape::API) }
|
7
5
|
|
@@ -10,10 +8,10 @@ describe Grape::Extensions::Hashie::Mash::ParamBuilder do
|
|
10
8
|
end
|
11
9
|
|
12
10
|
describe 'in an endpoint' do
|
13
|
-
|
11
|
+
describe '#params' do
|
14
12
|
before do
|
15
13
|
subject.params do
|
16
|
-
build_with Grape::Extensions::Hashie::Mash::ParamBuilder
|
14
|
+
build_with Grape::Extensions::Hashie::Mash::ParamBuilder # rubocop:disable RSpec/DescribedClass
|
17
15
|
end
|
18
16
|
|
19
17
|
subject.get do
|
@@ -21,7 +19,7 @@ describe Grape::Extensions::Hashie::Mash::ParamBuilder do
|
|
21
19
|
end
|
22
20
|
end
|
23
21
|
|
24
|
-
it '
|
22
|
+
it 'is of type Hashie::Mash' do
|
25
23
|
get '/'
|
26
24
|
expect(last_response.status).to eq(200)
|
27
25
|
expect(last_response.body).to eq('Hashie::Mash')
|
@@ -31,17 +29,17 @@ describe Grape::Extensions::Hashie::Mash::ParamBuilder do
|
|
31
29
|
|
32
30
|
describe 'in an api' do
|
33
31
|
before do
|
34
|
-
subject.send(:include, Grape::Extensions::Hashie::Mash::ParamBuilder)
|
32
|
+
subject.send(:include, Grape::Extensions::Hashie::Mash::ParamBuilder) # rubocop:disable RSpec/DescribedClass
|
35
33
|
end
|
36
34
|
|
37
|
-
|
35
|
+
describe '#params' do
|
38
36
|
before do
|
39
37
|
subject.get do
|
40
38
|
params.class
|
41
39
|
end
|
42
40
|
end
|
43
41
|
|
44
|
-
it '
|
42
|
+
it 'is Hashie::Mash' do
|
45
43
|
get '/'
|
46
44
|
expect(last_response.status).to eq(200)
|
47
45
|
expect(last_response.body).to eq('Hashie::Mash')
|
@@ -57,7 +55,7 @@ describe Grape::Extensions::Hashie::Mash::ParamBuilder do
|
|
57
55
|
end
|
58
56
|
end
|
59
57
|
|
60
|
-
it '
|
58
|
+
it 'is Hashie::Mash' do
|
61
59
|
get '/foo'
|
62
60
|
expect(last_response.status).to eq(200)
|
63
61
|
expect(last_response.body).to eq('Hashie::Mash')
|
@@ -66,7 +64,7 @@ describe Grape::Extensions::Hashie::Mash::ParamBuilder do
|
|
66
64
|
|
67
65
|
it 'is indifferent to key or symbol access' do
|
68
66
|
subject.params do
|
69
|
-
build_with Grape::Extensions::Hashie::Mash::ParamBuilder
|
67
|
+
build_with Grape::Extensions::Hashie::Mash::ParamBuilder # rubocop:disable RSpec/DescribedClass
|
70
68
|
requires :a, type: String
|
71
69
|
end
|
72
70
|
subject.get '/foo' do
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
describe Rack::Sendfile do
|
6
4
|
subject do
|
7
5
|
content_object = file_object
|
@@ -44,7 +42,7 @@ describe Rack::Sendfile do
|
|
44
42
|
|
45
43
|
it 'not contains Sendfile headers' do
|
46
44
|
headers = subject[1]
|
47
|
-
expect(headers).
|
45
|
+
expect(headers).not_to include('X-Accel-Redirect')
|
48
46
|
end
|
49
47
|
end
|
50
48
|
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
describe Rack do
|
6
4
|
it 'correctly populates params from a Tempfile' do
|
7
5
|
input = Tempfile.new 'rubbish'
|
@@ -29,19 +27,20 @@ describe Rack do
|
|
29
27
|
end
|
30
28
|
|
31
29
|
context 'when the app is mounted' do
|
32
|
-
|
33
|
-
|
30
|
+
let(:ping_mount) do
|
31
|
+
Class.new(Grape::API) do
|
34
32
|
get 'ping'
|
35
33
|
end
|
36
34
|
end
|
37
35
|
|
38
|
-
let
|
39
|
-
app_to_mount =
|
40
|
-
Class.new(Grape::API) do
|
36
|
+
let(:app) do
|
37
|
+
app_to_mount = ping_mount
|
38
|
+
app = Class.new(Grape::API) do
|
41
39
|
namespace 'namespace' do
|
42
40
|
mount app_to_mount
|
43
41
|
end
|
44
42
|
end
|
43
|
+
Rack::Builder.new(app)
|
45
44
|
end
|
46
45
|
|
47
46
|
it 'finds the app on the namespace' do
|
data/spec/grape/loading_spec.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
describe Grape::API do
|
4
|
+
subject do
|
5
|
+
CombinedApi = combined_api
|
6
|
+
Class.new(Grape::API) do
|
7
|
+
format :json
|
8
|
+
mount CombinedApi => '/'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
6
12
|
let(:jobs_api) do
|
7
13
|
Class.new(Grape::API) do
|
8
14
|
namespace :one do
|
@@ -26,14 +32,6 @@ describe Grape::API do
|
|
26
32
|
end
|
27
33
|
end
|
28
34
|
|
29
|
-
subject do
|
30
|
-
CombinedApi = combined_api
|
31
|
-
Class.new(Grape::API) do
|
32
|
-
format :json
|
33
|
-
mount CombinedApi => '/'
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
35
|
def app
|
38
36
|
subject
|
39
37
|
end
|
@@ -1,11 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
describe Grape::Middleware::Auth::DSL do
|
6
4
|
subject { Class.new(Grape::API) }
|
7
5
|
|
8
|
-
let(:block) { ->
|
6
|
+
let(:block) { -> {} }
|
9
7
|
let(:settings) do
|
10
8
|
{
|
11
9
|
opaque: 'secret',
|
@@ -16,7 +14,7 @@ describe Grape::Middleware::Auth::DSL do
|
|
16
14
|
end
|
17
15
|
|
18
16
|
describe '.auth' do
|
19
|
-
it '
|
17
|
+
it 'sets auth parameters' do
|
20
18
|
expect(subject.base_instance).to receive(:use).with(Grape::Middleware::Auth::Base, settings)
|
21
19
|
|
22
20
|
subject.auth :http_digest, realm: settings[:realm], opaque: settings[:opaque], &settings[:proc]
|
@@ -38,16 +36,25 @@ describe Grape::Middleware::Auth::DSL do
|
|
38
36
|
end
|
39
37
|
|
40
38
|
describe '.http_basic' do
|
41
|
-
it '
|
39
|
+
it 'sets auth parameters' do
|
42
40
|
subject.http_basic realm: 'my_realm', &settings[:proc]
|
43
41
|
expect(subject.auth).to eq(realm: 'my_realm', type: :http_basic, proc: block)
|
44
42
|
end
|
45
43
|
end
|
46
44
|
|
47
45
|
describe '.http_digest' do
|
48
|
-
|
49
|
-
|
50
|
-
|
46
|
+
context 'when realm is a hash' do
|
47
|
+
it 'sets auth parameters' do
|
48
|
+
subject.http_digest realm: { realm: 'my_realm', opaque: 'my_opaque' }, &settings[:proc]
|
49
|
+
expect(subject.auth).to eq(realm: { realm: 'my_realm', opaque: 'my_opaque' }, type: :http_digest, proc: block)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'when realm is not hash' do
|
54
|
+
it 'sets auth parameters' do
|
55
|
+
subject.http_digest realm: 'my_realm', opaque: 'my_opaque', &settings[:proc]
|
56
|
+
expect(subject.auth).to eq(realm: 'my_realm', type: :http_digest, proc: block, opaque: 'my_opaque')
|
57
|
+
end
|
51
58
|
end
|
52
59
|
end
|
53
60
|
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
require 'base64'
|
6
4
|
|
7
5
|
describe Grape::Middleware::Auth::Strategies do
|
@@ -42,7 +40,17 @@ describe Grape::Middleware::Auth::Strategies do
|
|
42
40
|
end
|
43
41
|
|
44
42
|
module StrategiesSpec
|
45
|
-
class
|
43
|
+
class PasswordHashed < Grape::API
|
44
|
+
http_digest(realm: { realm: 'Test Api', opaque: 'secret', passwords_hashed: true }) do |username|
|
45
|
+
{ 'foo' => Digest::MD5.hexdigest(['foo', 'Test Api', 'bar'].join(':')) }[username]
|
46
|
+
end
|
47
|
+
|
48
|
+
get '/test' do
|
49
|
+
[{ hey: 'you' }, { there: 'bar' }, { foo: 'baz' }]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class PasswordIsNotHashed < Grape::API
|
46
54
|
http_digest(realm: 'Test Api', opaque: 'secret') do |username|
|
47
55
|
{ 'foo' => 'bar' }[username]
|
48
56
|
end
|
@@ -53,30 +61,60 @@ describe Grape::Middleware::Auth::Strategies do
|
|
53
61
|
end
|
54
62
|
end
|
55
63
|
|
56
|
-
|
57
|
-
|
58
|
-
|
64
|
+
context 'when password is hashed' do
|
65
|
+
def app
|
66
|
+
StrategiesSpec::PasswordHashed
|
67
|
+
end
|
59
68
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
69
|
+
it 'is a digest authentication challenge' do
|
70
|
+
get '/test'
|
71
|
+
expect(last_response).to be_challenge
|
72
|
+
end
|
64
73
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
74
|
+
it 'throws a 401 if no auth is given' do
|
75
|
+
get '/test'
|
76
|
+
expect(last_response.status).to eq(401)
|
77
|
+
end
|
69
78
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
79
|
+
it 'authenticates if given valid creds' do
|
80
|
+
digest_authorize 'foo', 'bar'
|
81
|
+
get '/test'
|
82
|
+
expect(last_response.status).to eq(200)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'throws a 401 if given invalid creds' do
|
86
|
+
digest_authorize 'bar', 'foo'
|
87
|
+
get '/test'
|
88
|
+
expect(last_response.status).to eq(401)
|
89
|
+
end
|
74
90
|
end
|
75
91
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
92
|
+
context 'when password is not hashed' do
|
93
|
+
def app
|
94
|
+
StrategiesSpec::PasswordIsNotHashed
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'is a digest authentication challenge' do
|
98
|
+
get '/test'
|
99
|
+
expect(last_response).to be_challenge
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'throws a 401 if no auth is given' do
|
103
|
+
get '/test'
|
104
|
+
expect(last_response.status).to eq(401)
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'authenticates if given valid creds' do
|
108
|
+
digest_authorize 'foo', 'bar'
|
109
|
+
get '/test'
|
110
|
+
expect(last_response.status).to eq(200)
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'throws a 401 if given invalid creds' do
|
114
|
+
digest_authorize 'bar', 'foo'
|
115
|
+
get '/test'
|
116
|
+
expect(last_response.status).to eq(401)
|
117
|
+
end
|
80
118
|
end
|
81
119
|
end
|
82
120
|
end
|
@@ -1,9 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
describe Grape::Middleware::Base do
|
6
|
-
subject {
|
4
|
+
subject { described_class.new(blank_app) }
|
5
|
+
|
7
6
|
let(:blank_app) { ->(_) { [200, {}, 'Hi there.'] } }
|
8
7
|
|
9
8
|
before do
|
@@ -20,6 +19,8 @@ describe Grape::Middleware::Base do
|
|
20
19
|
end
|
21
20
|
|
22
21
|
context 'callbacks' do
|
22
|
+
after { subject.call!({}) }
|
23
|
+
|
23
24
|
it 'calls #before' do
|
24
25
|
expect(subject).to receive(:before)
|
25
26
|
end
|
@@ -27,8 +28,6 @@ describe Grape::Middleware::Base do
|
|
27
28
|
it 'calls #after' do
|
28
29
|
expect(subject).to receive(:after)
|
29
30
|
end
|
30
|
-
|
31
|
-
after { subject.call!({}) }
|
32
31
|
end
|
33
32
|
|
34
33
|
context 'callbacks on error' do
|
@@ -58,7 +57,7 @@ describe Grape::Middleware::Base do
|
|
58
57
|
context 'with patched warnings' do
|
59
58
|
before do
|
60
59
|
@warnings = warnings = []
|
61
|
-
allow_any_instance_of(
|
60
|
+
allow_any_instance_of(described_class).to receive(:warn) { |m| warnings << m }
|
62
61
|
allow(subject).to receive(:after).and_raise(StandardError)
|
63
62
|
end
|
64
63
|
|
@@ -71,53 +70,63 @@ describe Grape::Middleware::Base do
|
|
71
70
|
|
72
71
|
it 'is able to access the response' do
|
73
72
|
subject.call({})
|
74
|
-
expect(subject.response).to
|
73
|
+
expect(subject.response).to be_a(Rack::Response)
|
75
74
|
end
|
76
75
|
|
77
76
|
describe '#response' do
|
78
|
-
subject
|
77
|
+
subject do
|
78
|
+
described_class.new(response)
|
79
|
+
end
|
79
80
|
|
80
|
-
|
81
|
+
before { subject.call({}) }
|
82
|
+
|
83
|
+
context 'when Array' do
|
84
|
+
let(:rack_response) { Rack::Response.new('test', 204, abc: 1) }
|
81
85
|
let(:response) { ->(_) { [204, { abc: 1 }, 'test'] } }
|
82
86
|
|
83
87
|
it 'status' do
|
84
|
-
subject.call({})
|
85
88
|
expect(subject.response.status).to eq(204)
|
86
89
|
end
|
87
90
|
|
88
91
|
it 'body' do
|
89
|
-
subject.call({})
|
90
92
|
expect(subject.response.body).to eq(['test'])
|
91
93
|
end
|
92
94
|
|
93
95
|
it 'header' do
|
94
|
-
subject.call({})
|
95
96
|
expect(subject.response.header).to have_key(:abc)
|
96
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
|
97
103
|
end
|
98
104
|
|
99
|
-
context Rack::Response do
|
100
|
-
let(:
|
105
|
+
context 'when Rack::Response' do
|
106
|
+
let(:rack_response) { Rack::Response.new('test', 204, abc: 1) }
|
107
|
+
let(:response) { ->(_) { rack_response } }
|
101
108
|
|
102
109
|
it 'status' do
|
103
|
-
subject.call({})
|
104
110
|
expect(subject.response.status).to eq(204)
|
105
111
|
end
|
106
112
|
|
107
113
|
it 'body' do
|
108
|
-
subject.call({})
|
109
114
|
expect(subject.response.body).to eq(['test'])
|
110
115
|
end
|
111
116
|
|
112
117
|
it 'header' do
|
113
|
-
subject.call({})
|
114
118
|
expect(subject.response.header).to have_key(:abc)
|
115
119
|
end
|
120
|
+
|
121
|
+
it 'returns the memoized Rack::Response instance' do
|
122
|
+
expect(subject.response).to eq(rack_response)
|
123
|
+
end
|
116
124
|
end
|
117
125
|
end
|
118
126
|
|
119
127
|
describe '#context' do
|
120
|
-
subject {
|
128
|
+
subject { described_class.new(blank_app) }
|
129
|
+
|
121
130
|
it 'allows access to response context' do
|
122
131
|
subject.call(Grape::Env::API_ENDPOINT => { header: 'some header' })
|
123
132
|
expect(subject.context).to eq(header: 'some header')
|
@@ -126,7 +135,7 @@ describe Grape::Middleware::Base do
|
|
126
135
|
|
127
136
|
context 'options' do
|
128
137
|
it 'persists options passed at initialization' do
|
129
|
-
expect(
|
138
|
+
expect(described_class.new(blank_app, abc: true).options[:abc]).to be true
|
130
139
|
end
|
131
140
|
|
132
141
|
context 'defaults' do
|
@@ -1,6 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
3
|
require 'grape-entity'
|
5
4
|
|
6
5
|
describe Grape::Middleware::Error do
|
@@ -16,8 +15,7 @@ describe Grape::Middleware::Error do
|
|
16
15
|
|
17
16
|
class ErrApp
|
18
17
|
class << self
|
19
|
-
attr_accessor :error
|
20
|
-
attr_accessor :format
|
18
|
+
attr_accessor :error, :format
|
21
19
|
|
22
20
|
def call(_env)
|
23
21
|
throw :error, error
|
@@ -43,6 +41,12 @@ describe Grape::Middleware::Error do
|
|
43
41
|
expect(last_response.status).to eq(410)
|
44
42
|
end
|
45
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
|
+
|
46
50
|
it 'sets the error message appropriately' do
|
47
51
|
ErrorSpec::ErrApp.error = { message: 'Awesome stuff.' }
|
48
52
|
get '/'
|
@@ -63,6 +67,7 @@ describe Grape::Middleware::Error do
|
|
63
67
|
|
64
68
|
context 'with http code' do
|
65
69
|
let(:options) { { default_message: 'Aww, hamburgers.' } }
|
70
|
+
|
66
71
|
it 'adds the status code if wanted' do
|
67
72
|
ErrorSpec::ErrApp.error = { message: { code: 200 } }
|
68
73
|
get '/'
|