grape 1.8.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +65 -1
  3. data/README.md +377 -334
  4. data/UPGRADING.md +231 -6
  5. data/grape.gemspec +6 -10
  6. data/lib/grape/api/instance.rb +13 -10
  7. data/lib/grape/api.rb +17 -8
  8. data/lib/grape/content_types.rb +0 -2
  9. data/lib/grape/cookies.rb +2 -1
  10. data/lib/grape/dry_types.rb +0 -2
  11. data/lib/grape/dsl/desc.rb +23 -21
  12. data/lib/grape/dsl/headers.rb +1 -1
  13. data/lib/grape/dsl/inside_route.rb +47 -22
  14. data/lib/grape/dsl/parameters.rb +4 -3
  15. data/lib/grape/dsl/routing.rb +20 -4
  16. data/lib/grape/dsl/validations.rb +13 -0
  17. data/lib/grape/endpoint.rb +15 -10
  18. data/lib/grape/{util/env.rb → env.rb} +0 -5
  19. data/lib/grape/error_formatter/txt.rb +11 -10
  20. data/lib/grape/exceptions/base.rb +3 -3
  21. data/lib/grape/exceptions/missing_group_type.rb +1 -1
  22. data/lib/grape/exceptions/unsupported_group_type.rb +1 -1
  23. data/lib/grape/exceptions/validation.rb +0 -2
  24. data/lib/grape/exceptions/validation_array_errors.rb +1 -0
  25. data/lib/grape/exceptions/validation_errors.rb +1 -3
  26. data/lib/grape/extensions/hash.rb +5 -1
  27. data/lib/grape/http/headers.rb +18 -24
  28. data/lib/grape/{util/json.rb → json.rb} +1 -3
  29. data/lib/grape/locale/en.yml +3 -0
  30. data/lib/grape/middleware/auth/base.rb +0 -2
  31. data/lib/grape/middleware/auth/dsl.rb +0 -2
  32. data/lib/grape/middleware/auth/strategies.rb +1 -2
  33. data/lib/grape/middleware/base.rb +0 -2
  34. data/lib/grape/middleware/error.rb +55 -50
  35. data/lib/grape/middleware/formatter.rb +21 -18
  36. data/lib/grape/middleware/globals.rb +1 -3
  37. data/lib/grape/middleware/stack.rb +2 -3
  38. data/lib/grape/middleware/versioner/accept_version_header.rb +0 -2
  39. data/lib/grape/middleware/versioner/header.rb +17 -163
  40. data/lib/grape/middleware/versioner/param.rb +2 -4
  41. data/lib/grape/middleware/versioner/path.rb +1 -3
  42. data/lib/grape/namespace.rb +3 -4
  43. data/lib/grape/path.rb +24 -29
  44. data/lib/grape/railtie.rb +9 -0
  45. data/lib/grape/request.rb +3 -5
  46. data/lib/grape/router/base_route.rb +39 -0
  47. data/lib/grape/router/greedy_route.rb +20 -0
  48. data/lib/grape/router/pattern.rb +39 -30
  49. data/lib/grape/router/route.rb +22 -59
  50. data/lib/grape/router.rb +30 -36
  51. data/lib/grape/util/accept/header.rb +19 -0
  52. data/lib/grape/util/accept_header_handler.rb +105 -0
  53. data/lib/grape/util/base_inheritable.rb +4 -4
  54. data/lib/grape/util/cache.rb +0 -3
  55. data/lib/grape/util/endpoint_configuration.rb +1 -1
  56. data/lib/grape/util/header.rb +13 -0
  57. data/lib/grape/util/inheritable_values.rb +0 -2
  58. data/lib/grape/util/lazy/block.rb +29 -0
  59. data/lib/grape/util/lazy/object.rb +45 -0
  60. data/lib/grape/util/lazy/value.rb +38 -0
  61. data/lib/grape/util/lazy/value_array.rb +21 -0
  62. data/lib/grape/util/lazy/value_enumerable.rb +34 -0
  63. data/lib/grape/util/lazy/value_hash.rb +21 -0
  64. data/lib/grape/util/media_type.rb +70 -0
  65. data/lib/grape/util/reverse_stackable_values.rb +1 -6
  66. data/lib/grape/util/stackable_values.rb +1 -6
  67. data/lib/grape/util/strict_hash_configuration.rb +3 -3
  68. data/lib/grape/validations/attributes_doc.rb +38 -36
  69. data/lib/grape/validations/contract_scope.rb +71 -0
  70. data/lib/grape/validations/params_scope.rb +10 -9
  71. data/lib/grape/validations/types/array_coercer.rb +0 -2
  72. data/lib/grape/validations/types/build_coercer.rb +69 -71
  73. data/lib/grape/validations/types/dry_type_coercer.rb +1 -11
  74. data/lib/grape/validations/types/json.rb +0 -2
  75. data/lib/grape/validations/types/primitive_coercer.rb +0 -2
  76. data/lib/grape/validations/types/set_coercer.rb +0 -3
  77. data/lib/grape/validations/types.rb +0 -3
  78. data/lib/grape/validations/validators/base.rb +2 -1
  79. data/lib/grape/validations/validators/default_validator.rb +5 -1
  80. data/lib/grape/validations/validators/length_validator.rb +42 -0
  81. data/lib/grape/validations/validators/values_validator.rb +8 -3
  82. data/lib/grape/validations.rb +3 -7
  83. data/lib/grape/version.rb +1 -1
  84. data/lib/grape/{util/xml.rb → xml.rb} +1 -1
  85. data/lib/grape.rb +38 -269
  86. metadata +33 -274
  87. data/lib/grape/eager_load.rb +0 -20
  88. data/lib/grape/middleware/versioner/parse_media_type_patch.rb +0 -24
  89. data/lib/grape/router/attribute_translator.rb +0 -63
  90. data/lib/grape/util/lazy_block.rb +0 -27
  91. data/lib/grape/util/lazy_object.rb +0 -43
  92. data/lib/grape/util/lazy_value.rb +0 -91
  93. data/spec/grape/api/custom_validations_spec.rb +0 -213
  94. data/spec/grape/api/deeply_included_options_spec.rb +0 -56
  95. data/spec/grape/api/defines_boolean_in_params_spec.rb +0 -38
  96. data/spec/grape/api/documentation_spec.rb +0 -59
  97. data/spec/grape/api/inherited_helpers_spec.rb +0 -114
  98. data/spec/grape/api/instance_spec.rb +0 -103
  99. data/spec/grape/api/invalid_format_spec.rb +0 -45
  100. data/spec/grape/api/namespace_parameters_in_route_spec.rb +0 -38
  101. data/spec/grape/api/nested_helpers_spec.rb +0 -50
  102. data/spec/grape/api/optional_parameters_in_route_spec.rb +0 -43
  103. data/spec/grape/api/parameters_modification_spec.rb +0 -41
  104. data/spec/grape/api/patch_method_helpers_spec.rb +0 -79
  105. data/spec/grape/api/recognize_path_spec.rb +0 -21
  106. data/spec/grape/api/required_parameters_in_route_spec.rb +0 -37
  107. data/spec/grape/api/required_parameters_with_invalid_method_spec.rb +0 -26
  108. data/spec/grape/api/routes_with_requirements_spec.rb +0 -59
  109. data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +0 -41
  110. data/spec/grape/api/shared_helpers_spec.rb +0 -36
  111. data/spec/grape/api_remount_spec.rb +0 -509
  112. data/spec/grape/api_spec.rb +0 -4356
  113. data/spec/grape/dsl/callbacks_spec.rb +0 -45
  114. data/spec/grape/dsl/desc_spec.rb +0 -98
  115. data/spec/grape/dsl/headers_spec.rb +0 -62
  116. data/spec/grape/dsl/helpers_spec.rb +0 -100
  117. data/spec/grape/dsl/inside_route_spec.rb +0 -531
  118. data/spec/grape/dsl/logger_spec.rb +0 -24
  119. data/spec/grape/dsl/middleware_spec.rb +0 -60
  120. data/spec/grape/dsl/parameters_spec.rb +0 -180
  121. data/spec/grape/dsl/request_response_spec.rb +0 -225
  122. data/spec/grape/dsl/routing_spec.rb +0 -275
  123. data/spec/grape/dsl/settings_spec.rb +0 -261
  124. data/spec/grape/dsl/validations_spec.rb +0 -55
  125. data/spec/grape/endpoint/declared_spec.rb +0 -846
  126. data/spec/grape/endpoint_spec.rb +0 -1085
  127. data/spec/grape/entity_spec.rb +0 -336
  128. data/spec/grape/exceptions/base_spec.rb +0 -81
  129. data/spec/grape/exceptions/body_parse_errors_spec.rb +0 -185
  130. data/spec/grape/exceptions/invalid_accept_header_spec.rb +0 -358
  131. data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -15
  132. data/spec/grape/exceptions/invalid_response_spec.rb +0 -11
  133. data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -15
  134. data/spec/grape/exceptions/missing_group_type_spec.rb +0 -17
  135. data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -17
  136. data/spec/grape/exceptions/missing_option_spec.rb +0 -15
  137. data/spec/grape/exceptions/unknown_options_spec.rb +0 -15
  138. data/spec/grape/exceptions/unknown_validator_spec.rb +0 -15
  139. data/spec/grape/exceptions/unsupported_group_type_spec.rb +0 -19
  140. data/spec/grape/exceptions/validation_errors_spec.rb +0 -92
  141. data/spec/grape/exceptions/validation_spec.rb +0 -19
  142. data/spec/grape/extensions/param_builders/hash_spec.rb +0 -83
  143. data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +0 -105
  144. data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +0 -79
  145. data/spec/grape/grape_spec.rb +0 -9
  146. data/spec/grape/integration/global_namespace_function_spec.rb +0 -29
  147. data/spec/grape/integration/rack_sendfile_spec.rb +0 -48
  148. data/spec/grape/integration/rack_spec.rb +0 -51
  149. data/spec/grape/loading_spec.rb +0 -44
  150. data/spec/grape/middleware/auth/base_spec.rb +0 -31
  151. data/spec/grape/middleware/auth/dsl_spec.rb +0 -60
  152. data/spec/grape/middleware/auth/strategies_spec.rb +0 -120
  153. data/spec/grape/middleware/base_spec.rb +0 -221
  154. data/spec/grape/middleware/error_spec.rb +0 -85
  155. data/spec/grape/middleware/exception_spec.rb +0 -294
  156. data/spec/grape/middleware/formatter_spec.rb +0 -461
  157. data/spec/grape/middleware/globals_spec.rb +0 -30
  158. data/spec/grape/middleware/stack_spec.rb +0 -155
  159. data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -122
  160. data/spec/grape/middleware/versioner/header_spec.rb +0 -345
  161. data/spec/grape/middleware/versioner/param_spec.rb +0 -171
  162. data/spec/grape/middleware/versioner/path_spec.rb +0 -62
  163. data/spec/grape/middleware/versioner_spec.rb +0 -21
  164. data/spec/grape/named_api_spec.rb +0 -19
  165. data/spec/grape/parser_spec.rb +0 -86
  166. data/spec/grape/path_spec.rb +0 -252
  167. data/spec/grape/presenters/presenter_spec.rb +0 -71
  168. data/spec/grape/request_spec.rb +0 -126
  169. data/spec/grape/util/inheritable_setting_spec.rb +0 -242
  170. data/spec/grape/util/inheritable_values_spec.rb +0 -79
  171. data/spec/grape/util/reverse_stackable_values_spec.rb +0 -134
  172. data/spec/grape/util/stackable_values_spec.rb +0 -128
  173. data/spec/grape/util/strict_hash_configuration_spec.rb +0 -38
  174. data/spec/grape/validations/attributes_doc_spec.rb +0 -153
  175. data/spec/grape/validations/instance_behaivour_spec.rb +0 -43
  176. data/spec/grape/validations/multiple_attributes_iterator_spec.rb +0 -38
  177. data/spec/grape/validations/params_scope_spec.rb +0 -1420
  178. data/spec/grape/validations/single_attribute_iterator_spec.rb +0 -56
  179. data/spec/grape/validations/types/array_coercer_spec.rb +0 -33
  180. data/spec/grape/validations/types/primitive_coercer_spec.rb +0 -150
  181. data/spec/grape/validations/types/set_coercer_spec.rb +0 -32
  182. data/spec/grape/validations/types_spec.rb +0 -111
  183. data/spec/grape/validations/validators/all_or_none_spec.rb +0 -162
  184. data/spec/grape/validations/validators/allow_blank_spec.rb +0 -575
  185. data/spec/grape/validations/validators/at_least_one_of_spec.rb +0 -205
  186. data/spec/grape/validations/validators/base_spec.rb +0 -38
  187. data/spec/grape/validations/validators/coerce_spec.rb +0 -1261
  188. data/spec/grape/validations/validators/default_spec.rb +0 -463
  189. data/spec/grape/validations/validators/exactly_one_of_spec.rb +0 -233
  190. data/spec/grape/validations/validators/except_values_spec.rb +0 -192
  191. data/spec/grape/validations/validators/mutual_exclusion_spec.rb +0 -214
  192. data/spec/grape/validations/validators/presence_spec.rb +0 -315
  193. data/spec/grape/validations/validators/regexp_spec.rb +0 -161
  194. data/spec/grape/validations/validators/same_as_spec.rb +0 -57
  195. data/spec/grape/validations/validators/values_spec.rb +0 -733
  196. data/spec/grape/validations/validators/zh-CN.yml +0 -10
  197. data/spec/grape/validations_spec.rb +0 -2030
  198. data/spec/integration/eager_load/eager_load_spec.rb +0 -15
  199. data/spec/integration/multi_json/json_spec.rb +0 -7
  200. data/spec/integration/multi_xml/xml_spec.rb +0 -7
  201. data/spec/shared/deprecated_class_examples.rb +0 -16
  202. data/spec/shared/versioning_examples.rb +0 -215
  203. data/spec/spec_helper.rb +0 -52
  204. data/spec/support/basic_auth_encode_helpers.rb +0 -11
  205. data/spec/support/chunks.rb +0 -14
  206. data/spec/support/content_type_helpers.rb +0 -15
  207. data/spec/support/endpoint_faker.rb +0 -25
  208. data/spec/support/file_streamer.rb +0 -13
  209. data/spec/support/integer_helpers.rb +0 -13
  210. data/spec/support/versioned_helpers.rb +0 -55
@@ -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
@@ -1,37 +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' do
13
- [params[:id], params[:ext]].compact.join('/')
14
- end
15
-
16
- put ':something_id' do
17
- params[:something_id]
18
- end
19
- end
20
- end
21
-
22
- context 'get' do
23
- it 'responds' do
24
- get '/api/foo'
25
- expect(last_response.status).to eq 200
26
- expect(last_response.body).to eq 'foo'
27
- end
28
- end
29
-
30
- context 'put' do
31
- it 'responds' do
32
- put '/api/foo'
33
- expect(last_response.status).to eq 200
34
- expect(last_response.body).to eq 'foo'
35
- end
36
- end
37
- end
@@ -1,26 +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
- params do
13
- requires :id, desc: 'Identifier.'
14
- end
15
- get ':id' do
16
- end
17
- end
18
- end
19
-
20
- context 'post' do
21
- it '405' do
22
- post '/something'
23
- expect(last_response.status).to eq 405
24
- end
25
- end
26
- end
@@ -1,59 +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
- context 'get' do
11
- it 'routes to a namespace param with dots' do
12
- subject.namespace ':ns_with_dots', requirements: { ns_with_dots: %r{[^/]+} } do
13
- get '/' do
14
- params[:ns_with_dots]
15
- end
16
- end
17
-
18
- get '/test.id.with.dots'
19
- expect(last_response.status).to eq 200
20
- expect(last_response.body).to eq 'test.id.with.dots'
21
- end
22
-
23
- it 'routes to a path with multiple params with dots' do
24
- subject.get ':id_with_dots/:another_id_with_dots', requirements: { id_with_dots: %r{[^/]+},
25
- another_id_with_dots: %r{[^/]+} } do
26
- "#{params[:id_with_dots]}/#{params[:another_id_with_dots]}"
27
- end
28
-
29
- get '/test.id/test2.id'
30
- expect(last_response.status).to eq 200
31
- expect(last_response.body).to eq 'test.id/test2.id'
32
- end
33
-
34
- it 'routes to namespace and path params with dots, with overridden requirements' do
35
- subject.namespace ':ns_with_dots', requirements: { ns_with_dots: %r{[^/]+} } do
36
- get ':another_id_with_dots', requirements: { ns_with_dots: %r{[^/]+},
37
- another_id_with_dots: %r{[^/]+} } do
38
- "#{params[:ns_with_dots]}/#{params[:another_id_with_dots]}"
39
- end
40
- end
41
-
42
- get '/test.id/test2.id'
43
- expect(last_response.status).to eq 200
44
- expect(last_response.body).to eq 'test.id/test2.id'
45
- end
46
-
47
- it 'routes to namespace and path params with dots, with merged requirements' do
48
- subject.namespace ':ns_with_dots', requirements: { ns_with_dots: %r{[^/]+} } do
49
- get ':another_id_with_dots', requirements: { another_id_with_dots: %r{[^/]+} } do
50
- "#{params[:ns_with_dots]}/#{params[:another_id_with_dots]}"
51
- end
52
- end
53
-
54
- get '/test.id/test2.id'
55
- expect(last_response.status).to eq 200
56
- expect(last_response.body).to eq 'test.id/test2.id'
57
- end
58
- end
59
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::API::Helpers do
4
- let(:app) do
5
- Class.new(Grape::API) do
6
- helpers Module.new do
7
- extend Grape::API::Helpers
8
-
9
- params :drink do
10
- optional :beer
11
- optional :wine
12
- exactly_one_of :beer, :wine
13
- end
14
- end
15
- format :json
16
-
17
- params do
18
- requires :orderType, type: String, values: %w[food drink]
19
- given orderType: ->(val) { val == 'food' } do
20
- optional :pasta
21
- optional :pizza
22
- exactly_one_of :pasta, :pizza
23
- end
24
- given orderType: ->(val) { val == 'drink' } do
25
- use :drink
26
- end
27
- end
28
- get do
29
- declared(params, include_missing: true)
30
- end
31
- end
32
- end
33
-
34
- it 'defines parameters' do
35
- get '/', orderType: 'food', pizza: 'mista'
36
- expect(last_response.status).to eq 200
37
- expect(last_response.body).to eq({ orderType: 'food',
38
- pasta: nil, pizza: 'mista',
39
- beer: nil, wine: nil }.to_json)
40
- end
41
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::API::Helpers do
4
- subject do
5
- shared_params = Module.new do
6
- extend Grape::API::Helpers
7
-
8
- params :pagination do
9
- optional :page, type: Integer
10
- optional :size, type: Integer
11
- end
12
- end
13
-
14
- Class.new(Grape::API) do
15
- helpers shared_params
16
- format :json
17
-
18
- params do
19
- use :pagination
20
- end
21
- get do
22
- declared(params, include_missing: true)
23
- end
24
- end
25
- end
26
-
27
- def app
28
- subject
29
- end
30
-
31
- it 'defines parameters' do
32
- get '/'
33
- expect(last_response.status).to eq 200
34
- expect(last_response.body).to eq({ page: nil, size: nil }.to_json)
35
- end
36
- end