grape 1.7.1 → 2.0.0
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 +37 -1
- data/CONTRIBUTING.md +1 -1
- data/README.md +30 -25
- data/UPGRADING.md +35 -0
- data/grape.gemspec +3 -6
- data/lib/grape/api.rb +2 -2
- data/lib/grape/content_types.rb +2 -8
- data/lib/grape/dsl/desc.rb +1 -1
- data/lib/grape/dsl/inside_route.rb +11 -11
- data/lib/grape/dsl/request_response.rb +2 -1
- data/lib/grape/dsl/settings.rb +2 -6
- data/lib/grape/endpoint.rb +28 -18
- data/lib/grape/error_formatter/base.rb +1 -1
- data/lib/grape/exceptions/base.rb +2 -2
- data/lib/grape/exceptions/missing_group_type.rb +1 -6
- data/lib/grape/exceptions/unsupported_group_type.rb +1 -6
- data/lib/grape/exceptions/validation_errors.rb +1 -6
- data/lib/grape/extensions/active_support/hash_with_indifferent_access.rb +3 -3
- data/lib/grape/extensions/hash.rb +4 -7
- data/lib/grape/extensions/hashie/mash.rb +3 -3
- data/lib/grape/formatter/serializable_hash.rb +7 -7
- data/lib/grape/http/headers.rb +12 -2
- data/lib/grape/middleware/auth/base.rb +1 -1
- data/lib/grape/middleware/auth/strategies.rb +1 -2
- data/lib/grape/middleware/error.rb +5 -5
- data/lib/grape/middleware/formatter.rb +6 -6
- data/lib/grape/middleware/versioner/header.rb +11 -19
- data/lib/grape/railtie.rb +9 -0
- data/lib/grape/request.rb +8 -2
- data/lib/grape/router/route.rb +1 -3
- data/lib/grape/util/lazy_value.rb +3 -11
- data/lib/grape/util/strict_hash_configuration.rb +3 -4
- data/lib/grape/validations/multiple_attributes_iterator.rb +1 -1
- data/lib/grape/validations/params_scope.rb +8 -2
- data/lib/grape/validations/single_attribute_iterator.rb +3 -1
- data/lib/grape/validations/types/custom_type_coercer.rb +2 -16
- data/lib/grape/validations/validators/base.rb +9 -20
- data/lib/grape/validations/validators/default_validator.rb +2 -20
- data/lib/grape/validations/validators/multiple_params_base.rb +4 -8
- data/lib/grape/validations/validators/values_validator.rb +14 -5
- data/lib/grape/version.rb +1 -1
- data/lib/grape.rb +26 -5
- metadata +13 -253
- data/lib/grape/config.rb +0 -34
- data/lib/grape/extensions/deep_mergeable_hash.rb +0 -21
- data/lib/grape/extensions/deep_symbolize_hash.rb +0 -32
- data/spec/grape/api/custom_validations_spec.rb +0 -256
- 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 -473
- data/spec/grape/api_spec.rb +0 -4347
- data/spec/grape/config_spec.rb +0 -17
- data/spec/grape/dsl/callbacks_spec.rb +0 -45
- data/spec/grape/dsl/desc_spec.rb +0 -101
- 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 -535
- 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 -206
- 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 -145
- 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 -21
- 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 -23
- 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/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 -136
- 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 -40
- data/spec/grape/validations/params_scope_spec.rb +0 -1420
- data/spec/grape/validations/single_attribute_iterator_spec.rb +0 -57
- 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/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 -696
- data/spec/grape/validations/validators/zh-CN.yml +0 -10
- data/spec/grape/validations_spec.rb +0 -2029
- 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/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,56 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module DeeplyIncludedOptionsSpec
|
4
|
-
module Defaults
|
5
|
-
extend ActiveSupport::Concern
|
6
|
-
included do
|
7
|
-
format :json
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
module Admin
|
12
|
-
module Defaults
|
13
|
-
extend ActiveSupport::Concern
|
14
|
-
include DeeplyIncludedOptionsSpec::Defaults
|
15
|
-
end
|
16
|
-
|
17
|
-
class Users < Grape::API
|
18
|
-
include DeeplyIncludedOptionsSpec::Admin::Defaults
|
19
|
-
|
20
|
-
resource :users do
|
21
|
-
get do
|
22
|
-
status 200
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
class Main < Grape::API
|
29
|
-
mount DeeplyIncludedOptionsSpec::Admin::Users
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe Grape::API do
|
34
|
-
subject { DeeplyIncludedOptionsSpec::Main }
|
35
|
-
|
36
|
-
def app
|
37
|
-
subject
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'works for unspecified format' do
|
41
|
-
get '/users'
|
42
|
-
expect(last_response.status).to be 200
|
43
|
-
expect(last_response.content_type).to eql 'application/json'
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'works for specified format' do
|
47
|
-
get '/users.json'
|
48
|
-
expect(last_response.status).to be 200
|
49
|
-
expect(last_response.content_type).to eql 'application/json'
|
50
|
-
end
|
51
|
-
|
52
|
-
it "doesn't work for format different than specified" do
|
53
|
-
get '/users.txt'
|
54
|
-
expect(last_response.status).to be 404
|
55
|
-
end
|
56
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe Grape::API::Instance do
|
4
|
-
describe 'boolean constant' do
|
5
|
-
module DefinesBooleanInstanceSpec
|
6
|
-
class API < Grape::API
|
7
|
-
params do
|
8
|
-
requires :message, type: Boolean
|
9
|
-
end
|
10
|
-
post :echo do
|
11
|
-
{ class: params[:message].class.name, value: params[:message] }
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def app
|
17
|
-
DefinesBooleanInstanceSpec::API
|
18
|
-
end
|
19
|
-
|
20
|
-
let(:expected_body) do
|
21
|
-
{ class: 'TrueClass', value: true }.to_s
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'sets Boolean as a type' do
|
25
|
-
post '/echo?message=true'
|
26
|
-
expect(last_response.status).to eq(201)
|
27
|
-
expect(last_response.body).to eq expected_body
|
28
|
-
end
|
29
|
-
|
30
|
-
context 'Params endpoint type' do
|
31
|
-
subject { DefinesBooleanInstanceSpec::API.new.router.map['POST'].first.options[:params]['message'][:type] }
|
32
|
-
|
33
|
-
it 'params type is a boolean' do
|
34
|
-
expect(subject).to eq 'Grape::API::Boolean'
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,59 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Grape::API do
|
6
|
-
subject { Class.new(described_class) }
|
7
|
-
|
8
|
-
let(:app) { subject }
|
9
|
-
|
10
|
-
context 'an endpoint with documentation' do
|
11
|
-
it 'documents parameters' do
|
12
|
-
subject.params do
|
13
|
-
requires 'price', type: Float, desc: 'Sales price'
|
14
|
-
end
|
15
|
-
subject.get '/'
|
16
|
-
|
17
|
-
expect(subject.routes.first.params['price']).to eq(required: true,
|
18
|
-
type: 'Float',
|
19
|
-
desc: 'Sales price')
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'allows documentation with a hash' do
|
23
|
-
documentation = { example: 'Joe' }
|
24
|
-
|
25
|
-
subject.params do
|
26
|
-
requires 'first_name', documentation: documentation
|
27
|
-
end
|
28
|
-
subject.get '/'
|
29
|
-
|
30
|
-
expect(subject.routes.first.params['first_name'][:documentation]).to eq(documentation)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'an endpoint without documentation' do
|
35
|
-
before do
|
36
|
-
subject.do_not_document!
|
37
|
-
|
38
|
-
subject.params do
|
39
|
-
requires :city, type: String, desc: 'Should be ignored'
|
40
|
-
optional :postal_code, type: Integer
|
41
|
-
end
|
42
|
-
subject.post '/' do
|
43
|
-
declared(params).to_json
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'does not document parameters for the endpoint' do
|
48
|
-
expect(subject.routes.first.params).to eq({})
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'still declares params internally' do
|
52
|
-
data = { city: 'Berlin', postal_code: 10_115 }
|
53
|
-
|
54
|
-
post '/', data
|
55
|
-
|
56
|
-
expect(last_response.body).to eq(data.to_json)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
@@ -1,114 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe Grape::API::Helpers do
|
4
|
-
let(:user) { 'Miguel Caneo' }
|
5
|
-
let(:id) { '42' }
|
6
|
-
|
7
|
-
module InheritedHelpersSpec
|
8
|
-
class SuperClass < Grape::API
|
9
|
-
helpers do
|
10
|
-
params(:superclass_params) { requires :id, type: String }
|
11
|
-
|
12
|
-
def current_user
|
13
|
-
params[:user]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
class OverriddenSubClass < SuperClass
|
19
|
-
params { use :superclass_params }
|
20
|
-
|
21
|
-
helpers do
|
22
|
-
def current_user
|
23
|
-
"#{params[:user]} with id"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
get 'resource' do
|
28
|
-
"#{current_user}: #{params['id']}"
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
class SubClass < SuperClass
|
33
|
-
params { use :superclass_params }
|
34
|
-
|
35
|
-
get 'resource' do
|
36
|
-
"#{current_user}: #{params['id']}"
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
class Example < SubClass
|
41
|
-
params { use :superclass_params }
|
42
|
-
|
43
|
-
get 'resource' do
|
44
|
-
"#{current_user}: #{params['id']}"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
context 'non overriding subclass' do
|
50
|
-
subject { InheritedHelpersSpec::SubClass }
|
51
|
-
|
52
|
-
def app
|
53
|
-
subject
|
54
|
-
end
|
55
|
-
|
56
|
-
context 'given expected params' do
|
57
|
-
it 'inherits helpers from a superclass' do
|
58
|
-
get '/resource', id: id, user: user
|
59
|
-
expect(last_response.body).to eq("#{user}: #{id}")
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
context 'with lack of expected params' do
|
64
|
-
it 'returns missing error' do
|
65
|
-
get '/resource'
|
66
|
-
expect(last_response.body).to eq('id is missing')
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
context 'overriding subclass' do
|
72
|
-
subject { InheritedHelpersSpec::OverriddenSubClass }
|
73
|
-
|
74
|
-
def app
|
75
|
-
subject
|
76
|
-
end
|
77
|
-
|
78
|
-
context 'given expected params' do
|
79
|
-
it 'overrides helpers from a superclass' do
|
80
|
-
get '/resource', id: id, user: user
|
81
|
-
expect(last_response.body).to eq("#{user} with id: #{id}")
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
context 'with lack of expected params' do
|
86
|
-
it 'returns missing error' do
|
87
|
-
get '/resource'
|
88
|
-
expect(last_response.body).to eq('id is missing')
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
context 'example subclass' do
|
94
|
-
subject { InheritedHelpersSpec::Example }
|
95
|
-
|
96
|
-
def app
|
97
|
-
subject
|
98
|
-
end
|
99
|
-
|
100
|
-
context 'given expected params' do
|
101
|
-
it 'inherits helpers from a superclass' do
|
102
|
-
get '/resource', id: id, user: user
|
103
|
-
expect(last_response.body).to eq("#{user}: #{id}")
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
context 'with lack of expected params' do
|
108
|
-
it 'returns missing error' do
|
109
|
-
get '/resource'
|
110
|
-
expect(last_response.body).to eq('id is missing')
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
@@ -1,103 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'shared/versioning_examples'
|
4
|
-
|
5
|
-
describe Grape::API::Instance do
|
6
|
-
subject(:an_instance) do
|
7
|
-
Class.new(Grape::API::Instance) do
|
8
|
-
namespace :some_namespace do
|
9
|
-
get 'some_endpoint' do
|
10
|
-
'success'
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
let(:root_api) do
|
17
|
-
to_mount = an_instance
|
18
|
-
Class.new(Grape::API) do
|
19
|
-
mount to_mount
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def app
|
24
|
-
root_api
|
25
|
-
end
|
26
|
-
|
27
|
-
context 'when an instance is mounted on the root' do
|
28
|
-
it 'can call the instance endpoint' do
|
29
|
-
get '/some_namespace/some_endpoint'
|
30
|
-
expect(last_response.body).to eq 'success'
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'when an instance is the root' do
|
35
|
-
let(:root_api) do
|
36
|
-
to_mount = an_instance
|
37
|
-
Class.new(Grape::API::Instance) do
|
38
|
-
mount to_mount
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'can call the instance endpoint' do
|
43
|
-
get '/some_namespace/some_endpoint'
|
44
|
-
expect(last_response.body).to eq 'success'
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
context 'top level setting' do
|
49
|
-
it 'does not inherit settings from the superclass (Grape::API::Instance)' do
|
50
|
-
expect(an_instance.top_level_setting.parent).to be_nil
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
context 'with multiple moutes' do
|
55
|
-
let(:first) do
|
56
|
-
Class.new(Grape::API::Instance) do
|
57
|
-
namespace(:some_namespace) do
|
58
|
-
route :any, '*path' do
|
59
|
-
error!('Not found! (1)', 404)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
let(:second) do
|
65
|
-
Class.new(Grape::API::Instance) do
|
66
|
-
namespace(:another_namespace) do
|
67
|
-
route :any, '*path' do
|
68
|
-
error!('Not found! (2)', 404)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
let(:root_api) do
|
74
|
-
first_instance = first
|
75
|
-
second_instance = second
|
76
|
-
Class.new(Grape::API) do
|
77
|
-
mount first_instance
|
78
|
-
mount first_instance
|
79
|
-
mount second_instance
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'does not raise a FrozenError on first instance' do
|
84
|
-
expect { patch '/some_namespace/anything' }.not_to \
|
85
|
-
raise_error
|
86
|
-
end
|
87
|
-
|
88
|
-
it 'responds the correct body at the first instance' do
|
89
|
-
patch '/some_namespace/anything'
|
90
|
-
expect(last_response.body).to eq 'Not found! (1)'
|
91
|
-
end
|
92
|
-
|
93
|
-
it 'does not raise a FrozenError on second instance' do
|
94
|
-
expect { get '/another_namespace/other' }.not_to \
|
95
|
-
raise_error
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'responds the correct body at the second instance' do
|
99
|
-
get '/another_namespace/foobar'
|
100
|
-
expect(last_response.body).to eq 'Not found! (2)'
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe Grape::Endpoint do
|
4
|
-
subject { Class.new(Grape::API) }
|
5
|
-
|
6
|
-
def app
|
7
|
-
subject
|
8
|
-
end
|
9
|
-
|
10
|
-
before do
|
11
|
-
subject.namespace do
|
12
|
-
format :json
|
13
|
-
content_type :json, 'application/json'
|
14
|
-
params do
|
15
|
-
requires :id, desc: 'Identifier.'
|
16
|
-
end
|
17
|
-
get ':id' do
|
18
|
-
{
|
19
|
-
id: params[:id],
|
20
|
-
format: params[:format]
|
21
|
-
}
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
context 'get' do
|
27
|
-
it 'no format' do
|
28
|
-
get '/foo'
|
29
|
-
expect(last_response.status).to eq 200
|
30
|
-
expect(last_response.body).to eq(::Grape::Json.dump(id: 'foo', format: nil))
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'json format' do
|
34
|
-
get '/foo.json'
|
35
|
-
expect(last_response.status).to eq 200
|
36
|
-
expect(last_response.body).to eq(::Grape::Json.dump(id: 'foo', format: 'json'))
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'invalid format' do
|
40
|
-
get '/foo.invalid'
|
41
|
-
expect(last_response.status).to eq 200
|
42
|
-
expect(last_response.body).to eq(::Grape::Json.dump(id: 'foo', format: 'invalid'))
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe Grape::Endpoint do
|
4
|
-
subject { Class.new(Grape::API) }
|
5
|
-
|
6
|
-
def app
|
7
|
-
subject
|
8
|
-
end
|
9
|
-
|
10
|
-
before do
|
11
|
-
subject.namespace :me do
|
12
|
-
namespace :pending do
|
13
|
-
get '/' do
|
14
|
-
'banana'
|
15
|
-
end
|
16
|
-
end
|
17
|
-
put ':id' do
|
18
|
-
params[:id]
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
context 'get' do
|
24
|
-
it 'responds without ext' do
|
25
|
-
get '/me/pending'
|
26
|
-
expect(last_response.status).to eq 200
|
27
|
-
expect(last_response.body).to eq 'banana'
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
context 'put' do
|
32
|
-
it 'responds' do
|
33
|
-
put '/me/foo'
|
34
|
-
expect(last_response.status).to eq 200
|
35
|
-
expect(last_response.body).to eq 'foo'
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe Grape::API::Helpers do
|
4
|
-
module NestedHelpersSpec
|
5
|
-
module HelperMethods
|
6
|
-
extend Grape::API::Helpers
|
7
|
-
def current_user
|
8
|
-
@current_user ||= params[:current_user]
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
class Nested < Grape::API
|
13
|
-
resource :level1 do
|
14
|
-
helpers HelperMethods
|
15
|
-
|
16
|
-
get do
|
17
|
-
current_user
|
18
|
-
end
|
19
|
-
|
20
|
-
resource :level2 do
|
21
|
-
get do
|
22
|
-
current_user
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
class Main < Grape::API
|
29
|
-
mount Nested
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
subject do
|
34
|
-
NestedHelpersSpec::Main
|
35
|
-
end
|
36
|
-
|
37
|
-
def app
|
38
|
-
subject
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'can access helpers from a mounted resource' do
|
42
|
-
get '/level1', current_user: 'hello'
|
43
|
-
expect(last_response.body).to eq('hello')
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'can access helpers from a mounted resource in a nested resource' do
|
47
|
-
get '/level1/level2', current_user: 'world'
|
48
|
-
expect(last_response.body).to eq('world')
|
49
|
-
end
|
50
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe Grape::Endpoint do
|
4
|
-
subject { Class.new(Grape::API) }
|
5
|
-
|
6
|
-
def app
|
7
|
-
subject
|
8
|
-
end
|
9
|
-
|
10
|
-
before do
|
11
|
-
subject.namespace :api do
|
12
|
-
get ':id(/:ext)' do
|
13
|
-
[params[:id], params[:ext]].compact.join('/')
|
14
|
-
end
|
15
|
-
|
16
|
-
put ':id' do
|
17
|
-
params[:id]
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
context 'get' do
|
23
|
-
it 'responds without ext' do
|
24
|
-
get '/api/foo'
|
25
|
-
expect(last_response.status).to eq 200
|
26
|
-
expect(last_response.body).to eq 'foo'
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'responds with ext' do
|
30
|
-
get '/api/foo/bar'
|
31
|
-
expect(last_response.status).to eq 200
|
32
|
-
expect(last_response.body).to eq 'foo/bar'
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
context 'put' do
|
37
|
-
it 'responds' do
|
38
|
-
put '/api/foo'
|
39
|
-
expect(last_response.status).to eq 200
|
40
|
-
expect(last_response.body).to eq 'foo'
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe Grape::Endpoint do
|
4
|
-
subject { Class.new(Grape::API) }
|
5
|
-
|
6
|
-
def app
|
7
|
-
subject
|
8
|
-
end
|
9
|
-
|
10
|
-
before do
|
11
|
-
subject.namespace :test do
|
12
|
-
params do
|
13
|
-
optional :foo, default: +'-abcdef'
|
14
|
-
end
|
15
|
-
get do
|
16
|
-
params[:foo].slice!(0)
|
17
|
-
params[:foo]
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
context 'when route modifies param value' do
|
23
|
-
it 'param default should not change' do
|
24
|
-
get '/test'
|
25
|
-
expect(last_response.status).to eq 200
|
26
|
-
expect(last_response.body).to eq 'abcdef'
|
27
|
-
|
28
|
-
get '/test'
|
29
|
-
expect(last_response.status).to eq 200
|
30
|
-
expect(last_response.body).to eq 'abcdef'
|
31
|
-
|
32
|
-
get '/test?foo=-123456'
|
33
|
-
expect(last_response.status).to eq 200
|
34
|
-
expect(last_response.body).to eq '123456'
|
35
|
-
|
36
|
-
get '/test'
|
37
|
-
expect(last_response.status).to eq 200
|
38
|
-
expect(last_response.body).to eq 'abcdef'
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
@@ -1,79 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe Grape::API::Helpers do
|
4
|
-
module PatchHelpersSpec
|
5
|
-
class PatchPublic < Grape::API
|
6
|
-
format :json
|
7
|
-
version 'public-v1', using: :header, vendor: 'grape'
|
8
|
-
|
9
|
-
get do
|
10
|
-
{ ok: 'public' }
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
module AuthMethods
|
15
|
-
def authenticate!; end
|
16
|
-
end
|
17
|
-
|
18
|
-
class PatchPrivate < Grape::API
|
19
|
-
format :json
|
20
|
-
version 'private-v1', using: :header, vendor: 'grape'
|
21
|
-
|
22
|
-
helpers AuthMethods
|
23
|
-
|
24
|
-
before do
|
25
|
-
authenticate!
|
26
|
-
end
|
27
|
-
|
28
|
-
get do
|
29
|
-
{ ok: 'private' }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
class Main < Grape::API
|
34
|
-
mount PatchPublic
|
35
|
-
mount PatchPrivate
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def app
|
40
|
-
PatchHelpersSpec::Main
|
41
|
-
end
|
42
|
-
|
43
|
-
context 'patch' do
|
44
|
-
it 'public' do
|
45
|
-
patch '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-public-v1+json'
|
46
|
-
expect(last_response.status).to eq 405
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'private' do
|
50
|
-
patch '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-private-v1+json'
|
51
|
-
expect(last_response.status).to eq 405
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'default' do
|
55
|
-
patch '/'
|
56
|
-
expect(last_response.status).to eq 405
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
context 'default' do
|
61
|
-
it 'public' do
|
62
|
-
get '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-public-v1+json'
|
63
|
-
expect(last_response.status).to eq 200
|
64
|
-
expect(last_response.body).to eq({ ok: 'public' }.to_json)
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'private' do
|
68
|
-
get '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-private-v1+json'
|
69
|
-
expect(last_response.status).to eq 200
|
70
|
-
expect(last_response.body).to eq({ ok: 'private' }.to_json)
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'default' do
|
74
|
-
get '/'
|
75
|
-
expect(last_response.status).to eq 200
|
76
|
-
expect(last_response.body).to eq({ ok: 'public' }.to_json)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe Grape::API do
|
4
|
-
describe '.recognize_path' do
|
5
|
-
subject { Class.new(described_class) }
|
6
|
-
|
7
|
-
it 'fetches endpoint by given path' do
|
8
|
-
subject.get('/foo/:id') {}
|
9
|
-
subject.get('/bar/:id') {}
|
10
|
-
subject.get('/baz/:id') {}
|
11
|
-
|
12
|
-
actual = subject.recognize_path('/bar/1234').routes[0].origin
|
13
|
-
expect(actual).to eq('/bar/:id')
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'returns nil if given path does not match with registered routes' do
|
17
|
-
subject.get {}
|
18
|
-
expect(subject.recognize_path('/bar/1234')).to be_nil
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|