grape 1.1.0 → 1.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (306) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +370 -44
  3. data/CONTRIBUTING.md +32 -1
  4. data/LICENSE +1 -1
  5. data/README.md +683 -87
  6. data/UPGRADING.md +481 -17
  7. data/grape.gemspec +15 -4
  8. data/lib/grape/api/helpers.rb +2 -0
  9. data/lib/grape/api/instance.rb +279 -0
  10. data/lib/grape/api.rb +144 -176
  11. data/lib/grape/config.rb +34 -0
  12. data/lib/grape/content_types.rb +34 -0
  13. data/lib/grape/cookies.rb +4 -0
  14. data/lib/grape/dry_types.rb +12 -0
  15. data/lib/grape/dsl/api.rb +1 -1
  16. data/lib/grape/dsl/callbacks.rb +21 -1
  17. data/lib/grape/dsl/configuration.rb +1 -1
  18. data/lib/grape/dsl/desc.rb +41 -23
  19. data/lib/grape/dsl/headers.rb +7 -2
  20. data/lib/grape/dsl/helpers.rb +10 -7
  21. data/lib/grape/dsl/inside_route.rb +118 -62
  22. data/lib/grape/dsl/logger.rb +2 -0
  23. data/lib/grape/dsl/middleware.rb +11 -4
  24. data/lib/grape/dsl/parameters.rb +33 -19
  25. data/lib/grape/dsl/request_response.rb +12 -9
  26. data/lib/grape/dsl/routing.rb +22 -13
  27. data/lib/grape/dsl/settings.rb +10 -6
  28. data/lib/grape/dsl/validations.rb +19 -14
  29. data/lib/grape/eager_load.rb +20 -0
  30. data/lib/grape/endpoint.rb +67 -58
  31. data/lib/grape/error_formatter/base.rb +2 -0
  32. data/lib/grape/error_formatter/json.rb +11 -7
  33. data/lib/grape/error_formatter/txt.rb +2 -0
  34. data/lib/grape/error_formatter/xml.rb +4 -6
  35. data/lib/grape/error_formatter.rb +4 -2
  36. data/lib/grape/exceptions/base.rb +23 -16
  37. data/lib/grape/exceptions/empty_message_body.rb +11 -0
  38. data/lib/grape/exceptions/incompatible_option_values.rb +2 -0
  39. data/lib/grape/exceptions/invalid_accept_header.rb +2 -0
  40. data/lib/grape/exceptions/invalid_formatter.rb +2 -0
  41. data/lib/grape/exceptions/invalid_message_body.rb +2 -0
  42. data/lib/grape/exceptions/invalid_response.rb +11 -0
  43. data/lib/grape/exceptions/invalid_version_header.rb +2 -0
  44. data/lib/grape/exceptions/invalid_versioner_option.rb +2 -0
  45. data/lib/grape/exceptions/invalid_with_option_for_represent.rb +2 -0
  46. data/lib/grape/exceptions/method_not_allowed.rb +2 -0
  47. data/lib/grape/exceptions/missing_group_type.rb +10 -1
  48. data/lib/grape/exceptions/missing_mime_type.rb +2 -0
  49. data/lib/grape/exceptions/missing_option.rb +2 -0
  50. data/lib/grape/exceptions/missing_vendor_option.rb +2 -0
  51. data/lib/grape/exceptions/too_many_multipart_files.rb +11 -0
  52. data/lib/grape/exceptions/unknown_options.rb +2 -0
  53. data/lib/grape/exceptions/unknown_parameter.rb +2 -0
  54. data/lib/grape/exceptions/unknown_validator.rb +2 -0
  55. data/lib/grape/exceptions/unsupported_group_type.rb +10 -1
  56. data/lib/grape/exceptions/validation.rb +5 -8
  57. data/lib/grape/exceptions/validation_array_errors.rb +2 -0
  58. data/lib/grape/exceptions/validation_errors.rb +16 -13
  59. data/lib/grape/extensions/active_support/hash_with_indifferent_access.rb +4 -3
  60. data/lib/grape/extensions/deep_mergeable_hash.rb +2 -0
  61. data/lib/grape/extensions/deep_symbolize_hash.rb +2 -0
  62. data/lib/grape/extensions/hash.rb +2 -0
  63. data/lib/grape/extensions/hashie/mash.rb +2 -0
  64. data/lib/grape/formatter/json.rb +3 -0
  65. data/lib/grape/formatter/serializable_hash.rb +4 -1
  66. data/lib/grape/formatter/txt.rb +2 -0
  67. data/lib/grape/formatter/xml.rb +3 -0
  68. data/lib/grape/formatter.rb +5 -3
  69. data/lib/grape/http/headers.rb +50 -18
  70. data/lib/grape/locale/en.yml +11 -8
  71. data/lib/grape/middleware/auth/base.rb +7 -7
  72. data/lib/grape/middleware/auth/dsl.rb +9 -2
  73. data/lib/grape/middleware/auth/strategies.rb +2 -0
  74. data/lib/grape/middleware/auth/strategy_info.rb +2 -0
  75. data/lib/grape/middleware/base.rb +13 -8
  76. data/lib/grape/middleware/error.rb +22 -17
  77. data/lib/grape/middleware/filter.rb +2 -0
  78. data/lib/grape/middleware/formatter.rb +12 -10
  79. data/lib/grape/middleware/globals.rb +2 -0
  80. data/lib/grape/middleware/helpers.rb +12 -0
  81. data/lib/grape/middleware/stack.rb +16 -6
  82. data/lib/grape/middleware/versioner/accept_version_header.rb +5 -5
  83. data/lib/grape/middleware/versioner/header.rb +13 -9
  84. data/lib/grape/middleware/versioner/param.rb +4 -1
  85. data/lib/grape/middleware/versioner/parse_media_type_patch.rb +5 -1
  86. data/lib/grape/middleware/versioner/path.rb +5 -1
  87. data/lib/grape/middleware/versioner.rb +2 -0
  88. data/lib/grape/namespace.rb +14 -2
  89. data/lib/grape/parser/json.rb +3 -1
  90. data/lib/grape/parser/xml.rb +3 -1
  91. data/lib/grape/parser.rb +4 -2
  92. data/lib/grape/path.rb +16 -3
  93. data/lib/grape/presenters/presenter.rb +2 -0
  94. data/lib/grape/request.rb +21 -9
  95. data/lib/grape/router/attribute_translator.rb +41 -8
  96. data/lib/grape/router/pattern.rb +21 -17
  97. data/lib/grape/router/route.rb +15 -29
  98. data/lib/grape/router.rb +36 -29
  99. data/lib/grape/{serve_file → serve_stream}/file_body.rb +3 -1
  100. data/lib/grape/{serve_file → serve_stream}/sendfile_response.rb +3 -1
  101. data/lib/grape/{serve_file/file_response.rb → serve_stream/stream_response.rb} +10 -8
  102. data/lib/grape/types/invalid_value.rb +8 -0
  103. data/lib/grape/util/base_inheritable.rb +43 -0
  104. data/lib/grape/util/cache.rb +20 -0
  105. data/lib/grape/util/endpoint_configuration.rb +8 -0
  106. data/lib/grape/util/env.rb +19 -17
  107. data/lib/grape/util/inheritable_setting.rb +3 -3
  108. data/lib/grape/util/inheritable_values.rb +7 -25
  109. data/lib/grape/util/json.rb +4 -0
  110. data/lib/grape/util/lazy_block.rb +27 -0
  111. data/lib/grape/util/lazy_object.rb +43 -0
  112. data/lib/grape/util/lazy_value.rb +99 -0
  113. data/lib/grape/util/registrable.rb +2 -0
  114. data/lib/grape/util/reverse_stackable_values.rb +10 -35
  115. data/lib/grape/util/stackable_values.rb +21 -34
  116. data/lib/grape/util/strict_hash_configuration.rb +3 -1
  117. data/lib/grape/util/xml.rb +2 -0
  118. data/lib/grape/validations/attributes_doc.rb +58 -0
  119. data/lib/grape/validations/attributes_iterator.rb +16 -6
  120. data/lib/grape/validations/multiple_attributes_iterator.rb +13 -0
  121. data/lib/grape/validations/params_scope.rb +174 -94
  122. data/lib/grape/validations/single_attribute_iterator.rb +24 -0
  123. data/lib/grape/validations/types/array_coercer.rb +63 -0
  124. data/lib/grape/validations/types/build_coercer.rb +47 -49
  125. data/lib/grape/validations/types/custom_type_coercer.rb +30 -51
  126. data/lib/grape/validations/types/custom_type_collection_coercer.rb +10 -25
  127. data/lib/grape/validations/types/dry_type_coercer.rb +72 -0
  128. data/lib/grape/validations/types/file.rb +22 -18
  129. data/lib/grape/validations/types/invalid_value.rb +17 -0
  130. data/lib/grape/validations/types/json.rb +47 -39
  131. data/lib/grape/validations/types/multiple_type_coercer.rb +14 -33
  132. data/lib/grape/validations/types/primitive_coercer.rb +75 -0
  133. data/lib/grape/validations/types/set_coercer.rb +38 -0
  134. data/lib/grape/validations/types/variant_collection_coercer.rb +5 -13
  135. data/lib/grape/validations/types.rb +106 -63
  136. data/lib/grape/validations/validator_factory.rb +8 -11
  137. data/lib/grape/validations/validators/all_or_none_of_validator.rb +16 -0
  138. data/lib/grape/validations/validators/allow_blank_validator.rb +20 -0
  139. data/lib/grape/validations/validators/as_validator.rb +14 -0
  140. data/lib/grape/validations/validators/at_least_one_of_validator.rb +15 -0
  141. data/lib/grape/validations/validators/base.rb +84 -68
  142. data/lib/grape/validations/validators/coerce_validator.rb +75 -0
  143. data/lib/grape/validations/validators/default_validator.rb +51 -0
  144. data/lib/grape/validations/validators/exactly_one_of_validator.rb +17 -0
  145. data/lib/grape/validations/validators/except_values_validator.rb +24 -0
  146. data/lib/grape/validations/validators/multiple_params_base.rb +27 -16
  147. data/lib/grape/validations/validators/mutual_exclusion_validator.rb +16 -0
  148. data/lib/grape/validations/validators/presence_validator.rb +15 -0
  149. data/lib/grape/validations/validators/regexp_validator.rb +16 -0
  150. data/lib/grape/validations/validators/same_as_validator.rb +29 -0
  151. data/lib/grape/validations/validators/values_validator.rb +88 -0
  152. data/lib/grape/validations.rb +18 -6
  153. data/lib/grape/version.rb +3 -1
  154. data/lib/grape.rb +175 -94
  155. data/spec/grape/api/custom_validations_spec.rb +117 -44
  156. data/spec/grape/api/deeply_included_options_spec.rb +4 -4
  157. data/spec/grape/api/defines_boolean_in_params_spec.rb +38 -0
  158. data/spec/grape/api/documentation_spec.rb +59 -0
  159. data/spec/grape/api/inherited_helpers_spec.rb +1 -1
  160. data/spec/grape/api/instance_spec.rb +103 -0
  161. data/spec/grape/api/invalid_format_spec.rb +3 -1
  162. data/spec/grape/api/namespace_parameters_in_route_spec.rb +1 -1
  163. data/spec/grape/api/nested_helpers_spec.rb +1 -1
  164. data/spec/grape/api/optional_parameters_in_route_spec.rb +1 -1
  165. data/spec/grape/api/parameters_modification_spec.rb +2 -2
  166. data/spec/grape/api/patch_method_helpers_spec.rb +1 -1
  167. data/spec/grape/api/recognize_path_spec.rb +2 -2
  168. data/spec/grape/api/required_parameters_in_route_spec.rb +1 -1
  169. data/spec/grape/api/required_parameters_with_invalid_method_spec.rb +1 -1
  170. data/spec/grape/api/routes_with_requirements_spec.rb +59 -0
  171. data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +10 -16
  172. data/spec/grape/api/shared_helpers_spec.rb +1 -1
  173. data/spec/grape/api_remount_spec.rb +473 -0
  174. data/spec/grape/api_spec.rb +995 -231
  175. data/spec/grape/config_spec.rb +17 -0
  176. data/spec/grape/dsl/callbacks_spec.rb +3 -2
  177. data/spec/grape/dsl/desc_spec.rb +43 -17
  178. data/spec/grape/dsl/headers_spec.rb +40 -10
  179. data/spec/grape/dsl/helpers_spec.rb +6 -5
  180. data/spec/grape/dsl/inside_route_spec.rb +189 -38
  181. data/spec/grape/dsl/logger_spec.rb +17 -19
  182. data/spec/grape/dsl/middleware_spec.rb +11 -2
  183. data/spec/grape/dsl/parameters_spec.rb +3 -1
  184. data/spec/grape/dsl/request_response_spec.rb +8 -7
  185. data/spec/grape/dsl/routing_spec.rb +22 -9
  186. data/spec/grape/dsl/settings_spec.rb +1 -1
  187. data/spec/grape/dsl/validations_spec.rb +1 -16
  188. data/spec/grape/endpoint/declared_spec.rb +846 -0
  189. data/spec/grape/endpoint_spec.rb +136 -577
  190. data/spec/grape/entity_spec.rb +31 -24
  191. data/spec/grape/exceptions/base_spec.rb +81 -0
  192. data/spec/grape/exceptions/body_parse_errors_spec.rb +4 -1
  193. data/spec/grape/exceptions/invalid_accept_header_spec.rb +65 -23
  194. data/spec/grape/exceptions/invalid_formatter_spec.rb +1 -1
  195. data/spec/grape/exceptions/invalid_response_spec.rb +11 -0
  196. data/spec/grape/exceptions/invalid_versioner_option_spec.rb +2 -2
  197. data/spec/grape/exceptions/missing_group_type_spec.rb +21 -0
  198. data/spec/grape/exceptions/missing_mime_type_spec.rb +1 -1
  199. data/spec/grape/exceptions/missing_option_spec.rb +2 -2
  200. data/spec/grape/exceptions/unknown_options_spec.rb +1 -1
  201. data/spec/grape/exceptions/unknown_validator_spec.rb +1 -1
  202. data/spec/grape/exceptions/unsupported_group_type_spec.rb +23 -0
  203. data/spec/grape/exceptions/validation_errors_spec.rb +21 -15
  204. data/spec/grape/exceptions/validation_spec.rb +6 -4
  205. data/spec/grape/extensions/param_builders/hash_spec.rb +8 -8
  206. data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +9 -9
  207. data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +9 -9
  208. data/spec/grape/integration/global_namespace_function_spec.rb +2 -2
  209. data/spec/grape/integration/rack_sendfile_spec.rb +14 -10
  210. data/spec/grape/integration/rack_spec.rb +25 -8
  211. data/spec/grape/loading_spec.rb +9 -9
  212. data/spec/grape/middleware/auth/base_spec.rb +2 -1
  213. data/spec/grape/middleware/auth/dsl_spec.rb +19 -10
  214. data/spec/grape/middleware/auth/strategies_spec.rb +62 -22
  215. data/spec/grape/middleware/base_spec.rb +36 -17
  216. data/spec/grape/middleware/error_spec.rb +11 -4
  217. data/spec/grape/middleware/exception_spec.rb +112 -162
  218. data/spec/grape/middleware/formatter_spec.rb +65 -29
  219. data/spec/grape/middleware/globals_spec.rb +8 -5
  220. data/spec/grape/middleware/stack_spec.rb +25 -13
  221. data/spec/grape/middleware/versioner/accept_version_header_spec.rb +3 -2
  222. data/spec/grape/middleware/versioner/header_spec.rb +37 -14
  223. data/spec/grape/middleware/versioner/param_spec.rb +8 -2
  224. data/spec/grape/middleware/versioner/path_spec.rb +6 -2
  225. data/spec/grape/middleware/versioner_spec.rb +2 -2
  226. data/spec/grape/named_api_spec.rb +19 -0
  227. data/spec/grape/parser_spec.rb +10 -6
  228. data/spec/grape/path_spec.rb +53 -53
  229. data/spec/grape/presenters/presenter_spec.rb +8 -7
  230. data/spec/grape/request_spec.rb +29 -3
  231. data/spec/grape/util/inheritable_setting_spec.rb +9 -8
  232. data/spec/grape/util/inheritable_values_spec.rb +5 -3
  233. data/spec/grape/util/reverse_stackable_values_spec.rb +5 -2
  234. data/spec/grape/util/stackable_values_spec.rb +10 -7
  235. data/spec/grape/util/strict_hash_configuration_spec.rb +2 -1
  236. data/spec/grape/validations/attributes_doc_spec.rb +153 -0
  237. data/spec/grape/validations/instance_behaivour_spec.rb +13 -14
  238. data/spec/grape/validations/multiple_attributes_iterator_spec.rb +40 -0
  239. data/spec/grape/validations/params_scope_spec.rb +568 -99
  240. data/spec/grape/validations/single_attribute_iterator_spec.rb +57 -0
  241. data/spec/grape/validations/types/array_coercer_spec.rb +33 -0
  242. data/spec/grape/validations/types/primitive_coercer_spec.rb +150 -0
  243. data/spec/grape/validations/types/set_coercer_spec.rb +32 -0
  244. data/spec/grape/validations/types_spec.rb +44 -45
  245. data/spec/grape/validations/validators/all_or_none_spec.rb +134 -32
  246. data/spec/grape/validations/validators/allow_blank_spec.rb +137 -141
  247. data/spec/grape/validations/validators/at_least_one_of_spec.rb +169 -31
  248. data/spec/grape/validations/validators/coerce_spec.rb +491 -151
  249. data/spec/grape/validations/validators/default_spec.rb +242 -78
  250. data/spec/grape/validations/validators/exactly_one_of_spec.rb +198 -40
  251. data/spec/grape/validations/validators/except_values_spec.rb +6 -5
  252. data/spec/grape/validations/validators/mutual_exclusion_spec.rb +181 -30
  253. data/spec/grape/validations/validators/presence_spec.rb +45 -2
  254. data/spec/grape/validations/validators/regexp_spec.rb +27 -33
  255. data/spec/grape/validations/validators/same_as_spec.rb +57 -0
  256. data/spec/grape/validations/validators/values_spec.rb +227 -180
  257. data/spec/grape/validations_spec.rb +502 -72
  258. data/spec/integration/eager_load/eager_load_spec.rb +15 -0
  259. data/spec/integration/multi_json/json_spec.rb +2 -2
  260. data/spec/integration/multi_xml/xml_spec.rb +2 -2
  261. data/spec/shared/versioning_examples.rb +34 -29
  262. data/spec/spec_helper.rb +31 -5
  263. data/spec/support/basic_auth_encode_helpers.rb +3 -1
  264. data/spec/support/chunks.rb +14 -0
  265. data/spec/support/content_type_helpers.rb +2 -0
  266. data/spec/support/endpoint_faker.rb +2 -0
  267. data/spec/support/file_streamer.rb +2 -0
  268. data/spec/support/integer_helpers.rb +2 -0
  269. data/spec/support/versioned_helpers.rb +8 -8
  270. metadata +111 -61
  271. data/Appraisals +0 -32
  272. data/Dangerfile +0 -2
  273. data/Gemfile +0 -33
  274. data/Gemfile.lock +0 -231
  275. data/Guardfile +0 -10
  276. data/RELEASING.md +0 -111
  277. data/Rakefile +0 -25
  278. data/benchmark/simple.rb +0 -27
  279. data/benchmark/simple_with_type_coercer.rb +0 -22
  280. data/gemfiles/multi_json.gemfile +0 -35
  281. data/gemfiles/multi_xml.gemfile +0 -35
  282. data/gemfiles/rack_1.5.2.gemfile +0 -35
  283. data/gemfiles/rack_edge.gemfile +0 -35
  284. data/gemfiles/rails_3.gemfile +0 -36
  285. data/gemfiles/rails_4.gemfile +0 -35
  286. data/gemfiles/rails_5.gemfile +0 -35
  287. data/gemfiles/rails_edge.gemfile +0 -35
  288. data/lib/grape/extensions/deep_hash_with_indifferent_access.rb +0 -18
  289. data/lib/grape/util/content_types.rb +0 -26
  290. data/lib/grape/validations/types/virtus_collection_patch.rb +0 -16
  291. data/lib/grape/validations/validators/all_or_none.rb +0 -20
  292. data/lib/grape/validations/validators/allow_blank.rb +0 -16
  293. data/lib/grape/validations/validators/as.rb +0 -15
  294. data/lib/grape/validations/validators/at_least_one_of.rb +0 -20
  295. data/lib/grape/validations/validators/coerce.rb +0 -74
  296. data/lib/grape/validations/validators/default.rb +0 -48
  297. data/lib/grape/validations/validators/exactly_one_of.rb +0 -29
  298. data/lib/grape/validations/validators/except_values.rb +0 -20
  299. data/lib/grape/validations/validators/mutual_exclusion.rb +0 -25
  300. data/lib/grape/validations/validators/presence.rb +0 -10
  301. data/lib/grape/validations/validators/regexp.rb +0 -11
  302. data/lib/grape/validations/validators/values.rb +0 -71
  303. data/pkg/grape-0.17.0.gem +0 -0
  304. data/pkg/grape-0.19.0.gem +0 -0
  305. data/spec/grape/dsl/configuration_spec.rb +0 -14
  306. data/spec/grape/validations/attributes_iterator_spec.rb +0 -4
@@ -1,7 +1,8 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
3
  describe Grape::Middleware::Formatter do
4
- subject { Grape::Middleware::Formatter.new(app) }
4
+ subject { described_class.new(app) }
5
+
5
6
  before { allow(subject).to receive(:dup).and_return(subject) }
6
7
 
7
8
  let(:body) { { 'foo' => 'bar' } }
@@ -9,53 +10,57 @@ describe Grape::Middleware::Formatter do
9
10
 
10
11
  context 'serialization' do
11
12
  let(:body) { { 'abc' => 'def' } }
13
+
12
14
  it 'looks at the bodies for possibly serializable data' do
13
15
  _, _, bodies = *subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json')
14
- bodies.each { |b| expect(b).to eq(::Grape::Json.dump(body)) }
16
+ bodies.each { |b| expect(b).to eq(::Grape::Json.dump(body)) } # rubocop:disable RSpec/IteratedExpectation
15
17
  end
16
18
 
17
19
  context 'default format' do
18
20
  let(:body) { ['foo'] }
21
+
19
22
  it 'calls #to_json since default format is json' do
20
23
  body.instance_eval do
21
- def to_json
24
+ def to_json(*_args)
22
25
  '"bar"'
23
26
  end
24
27
  end
25
28
 
26
- subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json').to_a.last.each { |b| expect(b).to eq('"bar"') }
29
+ subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json').to_a.last.each { |b| expect(b).to eq('"bar"') } # rubocop:disable RSpec/IteratedExpectation
27
30
  end
28
31
  end
29
32
 
30
33
  context 'jsonapi' do
31
34
  let(:body) { { 'foos' => [{ 'bar' => 'baz' }] } }
35
+
32
36
  it 'calls #to_json if the content type is jsonapi' do
33
37
  body.instance_eval do
34
- def to_json
38
+ def to_json(*_args)
35
39
  '{"foos":[{"bar":"baz"}] }'
36
40
  end
37
41
  end
38
42
 
39
- subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/vnd.api+json').to_a.last.each { |b| expect(b).to eq('{"foos":[{"bar":"baz"}] }') }
43
+ subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/vnd.api+json').to_a.last.each { |b| expect(b).to eq('{"foos":[{"bar":"baz"}] }') } # rubocop:disable RSpec/IteratedExpectation
40
44
  end
41
45
  end
42
46
 
43
47
  context 'xml' do
44
- let(:body) { 'string' }
48
+ let(:body) { +'string' }
49
+
45
50
  it 'calls #to_xml if the content type is xml' do
46
51
  body.instance_eval do
47
52
  def to_xml
48
53
  '<bar/>'
49
54
  end
50
55
  end
51
-
52
- subject.call('PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json').to_a.last.each { |b| expect(b).to eq('<bar/>') }
56
+ subject.call('PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json').to_a.last.each { |b| expect(b).to eq('<bar/>') } # rubocop:disable RSpec/IteratedExpectation
53
57
  end
54
58
  end
55
59
  end
56
60
 
57
61
  context 'error handling' do
58
62
  let(:formatter) { double(:formatter) }
63
+
59
64
  before do
60
65
  allow(Grape::Formatter).to receive(:formatter_for) { formatter }
61
66
  end
@@ -65,7 +70,7 @@ describe Grape::Middleware::Formatter do
65
70
 
66
71
  expect do
67
72
  catch(:error) { subject.call('PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json') }
68
- end.to_not raise_error
73
+ end.not_to raise_error
69
74
  end
70
75
 
71
76
  it 'does not rescue other exceptions' do
@@ -145,7 +150,7 @@ describe Grape::Middleware::Formatter do
145
150
  subject.options[:content_types][:custom] = 'application/vnd.test+json'
146
151
  end
147
152
 
148
- it 'it uses the custom type' do
153
+ it 'uses the custom type' do
149
154
  subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json')
150
155
  expect(subject.env['api.format']).to eq(:custom)
151
156
  end
@@ -162,26 +167,31 @@ describe Grape::Middleware::Formatter do
162
167
  _, headers, = subject.call('PATH_INFO' => '/info.json')
163
168
  expect(headers['Content-type']).to eq('application/json')
164
169
  end
170
+
165
171
  it 'is set for xml' do
166
172
  _, headers, = subject.call('PATH_INFO' => '/info.xml')
167
173
  expect(headers['Content-type']).to eq('application/xml')
168
174
  end
175
+
169
176
  it 'is set for txt' do
170
177
  _, headers, = subject.call('PATH_INFO' => '/info.txt')
171
178
  expect(headers['Content-type']).to eq('text/plain')
172
179
  end
180
+
173
181
  it 'is set for custom' do
174
182
  subject.options[:content_types] = {}
175
183
  subject.options[:content_types][:custom] = 'application/x-custom'
176
184
  _, headers, = subject.call('PATH_INFO' => '/info.custom')
177
185
  expect(headers['Content-type']).to eq('application/x-custom')
178
186
  end
187
+
179
188
  it 'is set for vendored with registered type' do
180
189
  subject.options[:content_types] = {}
181
190
  subject.options[:content_types][:custom] = 'application/vnd.test+json'
182
191
  _, headers, = subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json')
183
192
  expect(headers['Content-type']).to eq('application/vnd.test+json')
184
193
  end
194
+
185
195
  it 'is set to closest generic for custom vendored/versioned without registered type' do
186
196
  _, headers, = subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json')
187
197
  expect(headers['Content-type']).to eq('application/json')
@@ -194,26 +204,35 @@ describe Grape::Middleware::Formatter do
194
204
  subject.options[:content_types][:custom] = "don't care"
195
205
  subject.options[:formatters][:custom] = ->(_obj, _env) { 'CUSTOM FORMAT' }
196
206
  _, _, body = subject.call('PATH_INFO' => '/info.custom')
197
- expect(body.body).to eq(['CUSTOM FORMAT'])
207
+ expect(read_chunks(body)).to eq(['CUSTOM FORMAT'])
198
208
  end
209
+
199
210
  context 'default' do
200
211
  let(:body) { ['blah'] }
212
+
201
213
  it 'uses default json formatter' do
202
214
  _, _, body = subject.call('PATH_INFO' => '/info.json')
203
- expect(body.body).to eq(['["blah"]'])
215
+ expect(read_chunks(body)).to eq(['["blah"]'])
204
216
  end
205
217
  end
218
+
206
219
  it 'uses custom json formatter' do
207
220
  subject.options[:formatters][:json] = ->(_obj, _env) { 'CUSTOM JSON FORMAT' }
208
221
  _, _, body = subject.call('PATH_INFO' => '/info.json')
209
- expect(body.body).to eq(['CUSTOM JSON FORMAT'])
222
+ expect(read_chunks(body)).to eq(['CUSTOM JSON FORMAT'])
210
223
  end
211
224
  end
212
225
 
213
226
  context 'no content responses' do
214
227
  let(:no_content_response) { ->(status) { [status, {}, ['']] } }
215
228
 
216
- Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.each do |status|
229
+ STATUSES_WITHOUT_BODY = if Gem::Version.new(Rack.release) >= Gem::Version.new('2.1.0')
230
+ Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.keys
231
+ else
232
+ Rack::Utils::STATUS_WITH_NO_ENTITY_BODY
233
+ end
234
+
235
+ STATUSES_WITHOUT_BODY.each do |status|
217
236
  it "does not modify a #{status} response" do
218
237
  expected_response = no_content_response[status]
219
238
  allow(app).to receive(:call).and_return(expected_response)
@@ -223,6 +242,7 @@ describe Grape::Middleware::Formatter do
223
242
  end
224
243
 
225
244
  context 'input' do
245
+ content_types = ['application/json', 'application/json; charset=utf-8'].freeze
226
246
  %w[POST PATCH PUT DELETE].each do |method|
227
247
  context 'when body is not nil or empty' do
228
248
  context 'when Content-Type is supported' do
@@ -247,7 +267,7 @@ describe Grape::Middleware::Formatter do
247
267
  let(:content_type) { 'application/atom+xml' }
248
268
 
249
269
  it 'returns a 415 HTTP error status' do
250
- error = catch(:error) {
270
+ error = catch(:error) do
251
271
  subject.call(
252
272
  'PATH_INFO' => '/info',
253
273
  'REQUEST_METHOD' => method,
@@ -255,7 +275,7 @@ describe Grape::Middleware::Formatter do
255
275
  'rack.input' => io,
256
276
  'CONTENT_LENGTH' => io.length
257
277
  )
258
- }
278
+ end
259
279
  expect(error[:status]).to eq(415)
260
280
  expect(error[:message]).to eq("The provided content-type 'application/atom+xml' is not supported.")
261
281
  end
@@ -264,6 +284,7 @@ describe Grape::Middleware::Formatter do
264
284
 
265
285
  context 'when body is nil' do
266
286
  let(:io) { double }
287
+
267
288
  before do
268
289
  allow(io).to receive_message_chain(:rewind, :read).and_return(nil)
269
290
  end
@@ -282,6 +303,7 @@ describe Grape::Middleware::Formatter do
282
303
 
283
304
  context 'when body is empty' do
284
305
  let(:io) { double }
306
+
285
307
  before do
286
308
  allow(io).to receive_message_chain(:rewind, :read).and_return('')
287
309
  end
@@ -298,7 +320,7 @@ describe Grape::Middleware::Formatter do
298
320
  end
299
321
  end
300
322
 
301
- ['application/json', 'application/json; charset=utf-8'].each do |content_type|
323
+ content_types.each do |content_type|
302
324
  context content_type do
303
325
  it "parses the body from #{method} and copies values into rack.request.form_hash" do
304
326
  io = StringIO.new('{"is_boolean":true,"string":"thing"}')
@@ -326,6 +348,7 @@ describe Grape::Middleware::Formatter do
326
348
  expect(subject.env['rack.request.form_hash']['is_boolean']).to be true
327
349
  expect(subject.env['rack.request.form_hash']['string']).to eq('thing')
328
350
  end
351
+
329
352
  it 'rewinds IO' do
330
353
  io = StringIO.new('{"is_boolean":true,"string":"thing"}')
331
354
  io.read
@@ -339,6 +362,7 @@ describe Grape::Middleware::Formatter do
339
362
  expect(subject.env['rack.request.form_hash']['is_boolean']).to be true
340
363
  expect(subject.env['rack.request.form_hash']['string']).to eq('thing')
341
364
  end
365
+
342
366
  it "parses the body from an xml #{method} and copies values into rack.request.from_hash" do
343
367
  io = StringIO.new('<thing><name>Test</name></thing>')
344
368
  subject.call(
@@ -354,6 +378,7 @@ describe Grape::Middleware::Formatter do
354
378
  expect(subject.env['rack.request.form_hash']['thing']['name']['__content__']).to eq('Test')
355
379
  end
356
380
  end
381
+
357
382
  [Rack::Request::FORM_DATA_MEDIA_TYPES, Rack::Request::PARSEABLE_DATA_MEDIA_TYPES].flatten.each do |content_type|
358
383
  it "ignores #{content_type}" do
359
384
  io = StringIO.new('name=Other+Test+Thing')
@@ -371,12 +396,17 @@ describe Grape::Middleware::Formatter do
371
396
  end
372
397
 
373
398
  context 'send file' do
374
- let(:body) { Grape::ServeFile::FileResponse.new('file') }
375
- let(:app) { ->(_env) { [200, {}, body] } }
399
+ let(:file) { double(File) }
400
+ let(:file_body) { Grape::ServeStream::StreamResponse.new(file) }
401
+ let(:app) { ->(_env) { [200, {}, file_body] } }
376
402
 
377
- it 'returns Grape::Uril::SendFileReponse' do
403
+ it 'returns a file response' do
404
+ expect(file).to receive(:each).and_yield('data')
378
405
  env = { 'PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json' }
379
- expect(subject.call(env)).to be_a(Grape::ServeFile::SendfileResponse)
406
+ status, headers, body = subject.call(env)
407
+ expect(status).to be == 200
408
+ expect(headers).to be == { 'Content-Type' => 'application/json' }
409
+ expect(read_chunks(body)).to be == ['data']
380
410
  end
381
411
  end
382
412
 
@@ -387,27 +417,33 @@ describe Grape::Middleware::Formatter do
387
417
  end
388
418
  end
389
419
  let(:app) { ->(_env) { [200, {}, ['']] } }
420
+
390
421
  before do
391
422
  Grape::Formatter.register :invalid, InvalidFormatter
392
- Grape::ContentTypes::CONTENT_TYPES[:invalid] = 'application/x-invalid'
423
+ Grape::ContentTypes.register :invalid, 'application/x-invalid'
424
+ end
425
+
426
+ after do
427
+ Grape::ContentTypes.default_elements.delete(:invalid)
428
+ Grape::Formatter.default_elements.delete(:invalid)
393
429
  end
394
430
 
395
431
  it 'returns response by invalid formatter' do
396
432
  env = { 'PATH_INFO' => '/hello.invalid', 'HTTP_ACCEPT' => 'application/x-invalid' }
397
- _, _, bodies = *subject.call(env)
398
- expect(bodies.body.first).to eq({ message: 'invalid' }.to_json)
433
+ _, _, body = *subject.call(env)
434
+ expect(read_chunks(body).join).to eq({ message: 'invalid' }.to_json)
399
435
  end
400
436
  end
401
437
 
402
438
  context 'custom parser raises exception and rescue options are enabled for backtrace and original_exception' do
403
439
  it 'adds the backtrace and original_exception to the error output' do
404
- subject = Grape::Middleware::Formatter.new(
440
+ subject = described_class.new(
405
441
  app,
406
442
  rescue_options: { backtrace: true, original_exception: true },
407
443
  parsers: { json: ->(_object, _env) { raise StandardError, 'fail' } }
408
444
  )
409
445
  io = StringIO.new('{invalid}')
410
- error = catch(:error) {
446
+ error = catch(:error) do
411
447
  subject.call(
412
448
  'PATH_INFO' => '/info',
413
449
  'REQUEST_METHOD' => 'POST',
@@ -415,7 +451,7 @@ describe Grape::Middleware::Formatter do
415
451
  'rack.input' => io,
416
452
  'CONTENT_LENGTH' => io.length
417
453
  )
418
- }
454
+ end
419
455
 
420
456
  expect(error[:message]).to eq 'fail'
421
457
  expect(error[:backtrace].size).to be >= 1
@@ -1,7 +1,8 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
3
  describe Grape::Middleware::Globals do
4
- subject { Grape::Middleware::Globals.new(blank_app) }
4
+ subject { described_class.new(blank_app) }
5
+
5
6
  before { allow(subject).to receive(:dup).and_return(subject) }
6
7
 
7
8
  let(:blank_app) { ->(_env) { [200, {}, 'Hi there.'] } }
@@ -11,15 +12,17 @@ describe Grape::Middleware::Globals do
11
12
  end
12
13
 
13
14
  context 'environment' do
14
- it 'should set the grape.request environment' do
15
+ it 'sets the grape.request environment' do
15
16
  subject.call({})
16
17
  expect(subject.env['grape.request']).to be_a(Grape::Request)
17
18
  end
18
- it 'should set the grape.request.headers environment' do
19
+
20
+ it 'sets the grape.request.headers environment' do
19
21
  subject.call({})
20
22
  expect(subject.env['grape.request.headers']).to be_a(Hash)
21
23
  end
22
- it 'should set the grape.request.params environment' do
24
+
25
+ it 'sets the grape.request.params environment' do
23
26
  subject.call('QUERY_STRING' => 'test=1', 'rack.input' => StringIO.new)
24
27
  expect(subject.env['grape.request.params']).to be_a(Hash)
25
28
  end
@@ -1,21 +1,24 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
3
  describe Grape::Middleware::Stack do
4
4
  module StackSpec
5
5
  class FooMiddleware; end
6
+
6
7
  class BarMiddleware; end
8
+
7
9
  class BlockMiddleware
8
10
  attr_reader :block
11
+
9
12
  def initialize(&block)
10
13
  @block = block
11
14
  end
12
15
  end
13
16
  end
14
17
 
15
- let(:proc) { ->() {} }
16
- let(:others) { [[:use, StackSpec::BarMiddleware], [:insert_before, StackSpec::BarMiddleware, StackSpec::BlockMiddleware, proc]] }
18
+ subject { described_class.new }
17
19
 
18
- subject { Grape::Middleware::Stack.new }
20
+ let(:proc) { -> {} }
21
+ let(:others) { [[:use, StackSpec::BarMiddleware], [:insert_before, StackSpec::BarMiddleware, StackSpec::BlockMiddleware, proc]] }
19
22
 
20
23
  before do
21
24
  subject.use StackSpec::FooMiddleware
@@ -24,20 +27,20 @@ describe Grape::Middleware::Stack do
24
27
  describe '#use' do
25
28
  it 'pushes a middleware class onto the stack' do
26
29
  expect { subject.use StackSpec::BarMiddleware }
27
- .to change { subject.size }.by(1)
30
+ .to change(subject, :size).by(1)
28
31
  expect(subject.last).to eq(StackSpec::BarMiddleware)
29
32
  end
30
33
 
31
34
  it 'pushes a middleware class with arguments onto the stack' do
32
35
  expect { subject.use StackSpec::BarMiddleware, false, my_arg: 42 }
33
- .to change { subject.size }.by(1)
36
+ .to change(subject, :size).by(1)
34
37
  expect(subject.last).to eq(StackSpec::BarMiddleware)
35
38
  expect(subject.last.args).to eq([false, { my_arg: 42 }])
36
39
  end
37
40
 
38
41
  it 'pushes a middleware class with block arguments onto the stack' do
39
42
  expect { subject.use StackSpec::BlockMiddleware, &proc }
40
- .to change { subject.size }.by(1)
43
+ .to change(subject, :size).by(1)
41
44
  expect(subject.last).to eq(StackSpec::BlockMiddleware)
42
45
  expect(subject.last.args).to eq([])
43
46
  expect(subject.last.block).to eq(proc)
@@ -47,7 +50,7 @@ describe Grape::Middleware::Stack do
47
50
  describe '#insert' do
48
51
  it 'inserts a middleware class at the integer index' do
49
52
  expect { subject.insert 0, StackSpec::BarMiddleware }
50
- .to change { subject.size }.by(1)
53
+ .to change(subject, :size).by(1)
51
54
  expect(subject[0]).to eq(StackSpec::BarMiddleware)
52
55
  expect(subject[1]).to eq(StackSpec::FooMiddleware)
53
56
  end
@@ -56,7 +59,7 @@ describe Grape::Middleware::Stack do
56
59
  describe '#insert_before' do
57
60
  it 'inserts a middleware before another middleware class' do
58
61
  expect { subject.insert_before StackSpec::FooMiddleware, StackSpec::BarMiddleware }
59
- .to change { subject.size }.by(1)
62
+ .to change(subject, :size).by(1)
60
63
  expect(subject[0]).to eq(StackSpec::BarMiddleware)
61
64
  expect(subject[1]).to eq(StackSpec::FooMiddleware)
62
65
  end
@@ -65,7 +68,7 @@ describe Grape::Middleware::Stack do
65
68
  subject.use Class.new(StackSpec::BlockMiddleware)
66
69
 
67
70
  expect { subject.insert_before StackSpec::BlockMiddleware, StackSpec::BarMiddleware }
68
- .to change { subject.size }.by(1)
71
+ .to change(subject, :size).by(1)
69
72
 
70
73
  expect(subject[1]).to eq(StackSpec::BarMiddleware)
71
74
  expect(subject[2]).to eq(StackSpec::BlockMiddleware)
@@ -80,7 +83,7 @@ describe Grape::Middleware::Stack do
80
83
  describe '#insert_after' do
81
84
  it 'inserts a middleware after another middleware class' do
82
85
  expect { subject.insert_after StackSpec::FooMiddleware, StackSpec::BarMiddleware }
83
- .to change { subject.size }.by(1)
86
+ .to change(subject, :size).by(1)
84
87
  expect(subject[1]).to eq(StackSpec::BarMiddleware)
85
88
  expect(subject[0]).to eq(StackSpec::FooMiddleware)
86
89
  end
@@ -89,7 +92,7 @@ describe Grape::Middleware::Stack do
89
92
  subject.use Class.new(StackSpec::BlockMiddleware)
90
93
 
91
94
  expect { subject.insert_after StackSpec::BlockMiddleware, StackSpec::BarMiddleware }
92
- .to change { subject.size }.by(1)
95
+ .to change(subject, :size).by(1)
93
96
 
94
97
  expect(subject[1]).to eq(StackSpec::BlockMiddleware)
95
98
  expect(subject[2]).to eq(StackSpec::BarMiddleware)
@@ -104,11 +107,20 @@ describe Grape::Middleware::Stack do
104
107
  describe '#merge_with' do
105
108
  it 'applies a collection of operations and middlewares' do
106
109
  expect { subject.merge_with(others) }
107
- .to change { subject.size }.by(2)
110
+ .to change(subject, :size).by(2)
108
111
  expect(subject[0]).to eq(StackSpec::FooMiddleware)
109
112
  expect(subject[1]).to eq(StackSpec::BlockMiddleware)
110
113
  expect(subject[2]).to eq(StackSpec::BarMiddleware)
111
114
  end
115
+
116
+ context 'middleware spec with proc declaration exists' do
117
+ let(:middleware_spec_with_proc) { [:use, StackSpec::FooMiddleware, proc] }
118
+
119
+ it 'properly forwards spec arguments' do
120
+ expect(subject).to receive(:use).with(StackSpec::FooMiddleware)
121
+ subject.merge_with([middleware_spec_with_proc])
122
+ end
123
+ end
112
124
  end
113
125
 
114
126
  describe '#build' do
@@ -1,8 +1,9 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
3
  describe Grape::Middleware::Versioner::AcceptVersionHeader do
4
+ subject { described_class.new(app, **(@options || {})) }
5
+
4
6
  let(:app) { ->(env) { [200, env, env] } }
5
- subject { Grape::Middleware::Versioner::AcceptVersionHeader.new(app, @options || {}) }
6
7
 
7
8
  before do
8
9
  @options = {
@@ -1,8 +1,9 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
3
  describe Grape::Middleware::Versioner::Header do
4
+ subject { described_class.new(app, **(@options || {})) }
5
+
4
6
  let(:app) { ->(env) { [200, env, env] } }
5
- subject { Grape::Middleware::Versioner::Header.new(app, @options || {}) }
6
7
 
7
8
  before do
8
9
  @options = {
@@ -45,7 +46,7 @@ describe Grape::Middleware::Versioner::Header do
45
46
 
46
47
  it 'is nil if not provided' do
47
48
  status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor')
48
- expect(env['api.format']).to eql nil
49
+ expect(env['api.format']).to be_nil
49
50
  expect(status).to eq(200)
50
51
  end
51
52
 
@@ -63,7 +64,7 @@ describe Grape::Middleware::Versioner::Header do
63
64
 
64
65
  it 'is nil if not provided' do
65
66
  status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1')
66
- expect(env['api.format']).to eql nil
67
+ expect(env['api.format']).to be_nil
67
68
  expect(status).to eq(200)
68
69
  end
69
70
  end
@@ -88,7 +89,7 @@ describe Grape::Middleware::Versioner::Header do
88
89
  .to raise_exception do |exception|
89
90
  expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
90
91
  expect(exception.headers).to eql('X-Cascade' => 'pass')
91
- expect(exception.status).to eql 406
92
+ expect(exception.status).to be 406
92
93
  expect(exception.message).to include 'API vendor not found'
93
94
  end
94
95
  end
@@ -115,7 +116,7 @@ describe Grape::Middleware::Versioner::Header do
115
116
  .to raise_exception do |exception|
116
117
  expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
117
118
  expect(exception.headers).to eql('X-Cascade' => 'pass')
118
- expect(exception.status).to eql 406
119
+ expect(exception.status).to be 406
119
120
  expect(exception.message).to include('API vendor not found')
120
121
  end
121
122
  end
@@ -143,7 +144,7 @@ describe Grape::Middleware::Versioner::Header do
143
144
  expect { subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v2+json').last }.to raise_exception do |exception|
144
145
  expect(exception).to be_a(Grape::Exceptions::InvalidVersionHeader)
145
146
  expect(exception.headers).to eql('X-Cascade' => 'pass')
146
- expect(exception.status).to eql 406
147
+ expect(exception.status).to be 406
147
148
  expect(exception.message).to include('API version not found')
148
149
  end
149
150
  end
@@ -160,6 +161,12 @@ describe Grape::Middleware::Versioner::Header do
160
161
  expect(subject.call({}).first).to eq(200)
161
162
  end
162
163
 
164
+ it 'succeeds if :strict is set to false and given an invalid header' do
165
+ @options[:version_options][:strict] = false
166
+ expect(subject.call('HTTP_ACCEPT' => 'yaml').first).to eq(200)
167
+ expect(subject.call({}).first).to eq(200)
168
+ end
169
+
163
170
  context 'when :strict is set' do
164
171
  before do
165
172
  @options[:versions] = ['v1']
@@ -170,7 +177,7 @@ describe Grape::Middleware::Versioner::Header do
170
177
  expect { subject.call({}).last }.to raise_exception do |exception|
171
178
  expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
172
179
  expect(exception.headers).to eql('X-Cascade' => 'pass')
173
- expect(exception.status).to eql 406
180
+ expect(exception.status).to be 406
174
181
  expect(exception.message).to include('Accept header must be set.')
175
182
  end
176
183
  end
@@ -179,7 +186,7 @@ describe Grape::Middleware::Versioner::Header do
179
186
  expect { subject.call('HTTP_ACCEPT' => '').last }.to raise_exception do |exception|
180
187
  expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
181
188
  expect(exception.headers).to eql('X-Cascade' => 'pass')
182
- expect(exception.status).to eql 406
189
+ expect(exception.status).to be 406
183
190
  expect(exception.message).to include('Accept header must be set.')
184
191
  end
185
192
  end
@@ -200,7 +207,7 @@ describe Grape::Middleware::Versioner::Header do
200
207
  expect { subject.call({}).last }.to raise_exception do |exception|
201
208
  expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
202
209
  expect(exception.headers).to eql({})
203
- expect(exception.status).to eql 406
210
+ expect(exception.status).to be 406
204
211
  expect(exception.message).to include('Accept header must be set.')
205
212
  end
206
213
  end
@@ -210,7 +217,7 @@ describe Grape::Middleware::Versioner::Header do
210
217
  .to raise_exception do |exception|
211
218
  expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
212
219
  expect(exception.headers).to eql({})
213
- expect(exception.status).to eql 406
220
+ expect(exception.status).to be 406
214
221
  expect(exception.message).to include('API vendor or version not found.')
215
222
  end
216
223
  end
@@ -219,7 +226,7 @@ describe Grape::Middleware::Versioner::Header do
219
226
  expect { subject.call('HTTP_ACCEPT' => '').last }.to raise_exception do |exception|
220
227
  expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
221
228
  expect(exception.headers).to eql({})
222
- expect(exception.status).to eql 406
229
+ expect(exception.status).to be 406
223
230
  expect(exception.message).to include('Accept header must be set.')
224
231
  end
225
232
  end
@@ -229,7 +236,7 @@ describe Grape::Middleware::Versioner::Header do
229
236
  .to raise_exception do |exception|
230
237
  expect(exception).to be_a(Grape::Exceptions::InvalidAcceptHeader)
231
238
  expect(exception.headers).to eql({})
232
- expect(exception.status).to eql 406
239
+ expect(exception.status).to be 406
233
240
  expect(exception.message).to include('API vendor or version not found.')
234
241
  end
235
242
  end
@@ -256,7 +263,7 @@ describe Grape::Middleware::Versioner::Header do
256
263
  expect { subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v3+json') }.to raise_exception do |exception|
257
264
  expect(exception).to be_a(Grape::Exceptions::InvalidVersionHeader)
258
265
  expect(exception.headers).to eql('X-Cascade' => 'pass')
259
- expect(exception.status).to eql 406
266
+ expect(exception.status).to be 406
260
267
  expect(exception.message).to include('API version not found')
261
268
  end
262
269
  end
@@ -319,4 +326,20 @@ describe Grape::Middleware::Versioner::Header do
319
326
  end
320
327
  end
321
328
  end
329
+
330
+ context 'with missing vendor option' do
331
+ subject do
332
+ Class.new(Grape::API) do
333
+ version 'v1', using: :header
334
+ end
335
+ end
336
+
337
+ def app
338
+ subject
339
+ end
340
+
341
+ it 'fails' do
342
+ expect { versioned_get '/', 'v1', using: :header }.to raise_error Grape::Exceptions::MissingVendorOption
343
+ end
344
+ end
322
345
  end
@@ -1,9 +1,10 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
3
  describe Grape::Middleware::Versioner::Param do
4
+ subject { described_class.new(app, **options) }
5
+
4
6
  let(:app) { ->(env) { [200, env, env['api.version']] } }
5
7
  let(:options) { {} }
6
- subject { Grape::Middleware::Versioner::Param.new(app, options) }
7
8
 
8
9
  it 'sets the API version based on the default param (apiver)' do
9
10
  env = Rack::MockRequest.env_for('/awesome', params: { 'apiver' => 'v1' })
@@ -24,10 +25,12 @@ describe Grape::Middleware::Versioner::Param do
24
25
 
25
26
  context 'with specified parameter name' do
26
27
  let(:options) { { version_options: { parameter: 'v' } } }
28
+
27
29
  it 'sets the API version based on the custom parameter name' do
28
30
  env = Rack::MockRequest.env_for('/awesome', params: { 'v' => 'v1' })
29
31
  expect(subject.call(env)[1]['api.version']).to eq('v1')
30
32
  end
33
+
31
34
  it 'does not set the API version based on the default param' do
32
35
  env = Rack::MockRequest.env_for('/awesome', params: { 'apiver' => 'v1' })
33
36
  expect(subject.call(env)[1]['api.version']).to be_nil
@@ -36,10 +39,12 @@ describe Grape::Middleware::Versioner::Param do
36
39
 
37
40
  context 'with specified versions' do
38
41
  let(:options) { { versions: %w[v1 v2] } }
42
+
39
43
  it 'throws an error if a non-allowed version is specified' do
40
44
  env = Rack::MockRequest.env_for('/awesome', params: { 'apiver' => 'v3' })
41
45
  expect(catch(:error) { subject.call(env) }[:status]).to eq(404)
42
46
  end
47
+
43
48
  it 'allows versions that have been specified' do
44
49
  env = Rack::MockRequest.env_for('/awesome', params: { 'apiver' => 'v1' })
45
50
  expect(subject.call(env)[1]['api.version']).to eq('v1')
@@ -53,6 +58,7 @@ describe Grape::Middleware::Versioner::Param do
53
58
  version_options: { using: :header }
54
59
  }
55
60
  end
61
+
56
62
  it 'returns a 200 (matches the first version found)' do
57
63
  env = Rack::MockRequest.env_for('/awesome', params: {})
58
64
  expect(subject.call(env).first).to eq(200)