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,83 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::Extensions::Hash::ParamBuilder do
4
- subject { Class.new(Grape::API) }
5
-
6
- def app
7
- subject
8
- end
9
-
10
- describe 'in an endpoint' do
11
- describe '#params' do
12
- before do
13
- subject.params do
14
- build_with Grape::Extensions::Hash::ParamBuilder # rubocop:disable RSpec/DescribedClass
15
- end
16
-
17
- subject.get do
18
- params.class
19
- end
20
- end
21
-
22
- it 'is of type Hash' do
23
- get '/'
24
- expect(last_response.status).to eq(200)
25
- expect(last_response.body).to eq('Hash')
26
- end
27
- end
28
- end
29
-
30
- describe 'in an api' do
31
- before do
32
- subject.send(:include, Grape::Extensions::Hash::ParamBuilder) # rubocop:disable RSpec/DescribedClass
33
- end
34
-
35
- describe '#params' do
36
- before do
37
- subject.get do
38
- params.class
39
- end
40
- end
41
-
42
- it 'is Hash' do
43
- get '/'
44
- expect(last_response.status).to eq(200)
45
- expect(last_response.body).to eq('Hash')
46
- end
47
- end
48
-
49
- it 'symbolizes params keys' do
50
- subject.params do
51
- optional :a, type: Hash do
52
- optional :b, type: Hash do
53
- optional :c, type: String
54
- end
55
- optional :d, type: Array
56
- end
57
- end
58
-
59
- subject.get '/foo' do
60
- [params[:a][:b][:c], params[:a][:d]]
61
- end
62
-
63
- get '/foo', 'a' => { b: { c: 'bar' }, 'd' => ['foo'] }
64
- expect(last_response.status).to eq(200)
65
- expect(last_response.body).to eq('["bar", ["foo"]]')
66
- end
67
-
68
- it 'symbolizes the params' do
69
- subject.params do
70
- build_with Grape::Extensions::Hash::ParamBuilder # rubocop:disable RSpec/DescribedClass
71
- requires :a, type: String
72
- end
73
-
74
- subject.get '/foo' do
75
- [params[:a], params['a']]
76
- end
77
-
78
- get '/foo', a: 'bar'
79
- expect(last_response.status).to eq(200)
80
- expect(last_response.body).to eq('["bar", nil]')
81
- end
82
- end
83
- end
@@ -1,105 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder do
4
- subject { Class.new(Grape::API) }
5
-
6
- def app
7
- subject
8
- end
9
-
10
- describe 'in an endpoint' do
11
- describe '#params' do
12
- before do
13
- subject.params do
14
- build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder # rubocop:disable RSpec/DescribedClass
15
- end
16
-
17
- subject.get do
18
- params.class
19
- end
20
- end
21
-
22
- it 'is of type Hash' do
23
- get '/'
24
- expect(last_response.status).to eq(200)
25
- expect(last_response.body).to eq('ActiveSupport::HashWithIndifferentAccess')
26
- end
27
- end
28
- end
29
-
30
- describe 'in an api' do
31
- before do
32
- subject.send(:include, Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder) # rubocop:disable RSpec/DescribedClass
33
- end
34
-
35
- describe '#params' do
36
- before do
37
- subject.get do
38
- params.class
39
- end
40
- end
41
-
42
- it 'is a Hash' do
43
- get '/'
44
- expect(last_response.status).to eq(200)
45
- expect(last_response.body).to eq('ActiveSupport::HashWithIndifferentAccess')
46
- end
47
-
48
- it 'parses sub hash params' do
49
- subject.params do
50
- build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder # rubocop:disable RSpec/DescribedClass
51
-
52
- optional :a, type: Hash do
53
- optional :b, type: Hash do
54
- optional :c, type: String
55
- end
56
- optional :d, type: Array
57
- end
58
- end
59
-
60
- subject.get '/foo' do
61
- [params[:a]['b'][:c], params['a'][:d]]
62
- end
63
-
64
- get '/foo', a: { b: { c: 'bar' }, d: ['foo'] }
65
- expect(last_response.status).to eq(200)
66
- expect(last_response.body).to eq('["bar", ["foo"]]')
67
- end
68
-
69
- it 'params are indifferent to symbol or string keys' do
70
- subject.params do
71
- build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder # rubocop:disable RSpec/DescribedClass
72
- optional :a, type: Hash do
73
- optional :b, type: Hash do
74
- optional :c, type: String
75
- end
76
- optional :d, type: Array
77
- end
78
- end
79
-
80
- subject.get '/foo' do
81
- [params[:a]['b'][:c], params['a'][:d]]
82
- end
83
-
84
- get '/foo', 'a' => { b: { c: 'bar' }, 'd' => ['foo'] }
85
- expect(last_response.status).to eq(200)
86
- expect(last_response.body).to eq('["bar", ["foo"]]')
87
- end
88
-
89
- it 'responds to string keys' do
90
- subject.params do
91
- build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder # rubocop:disable RSpec/DescribedClass
92
- requires :a, type: String
93
- end
94
-
95
- subject.get '/foo' do
96
- [params[:a], params['a']]
97
- end
98
-
99
- get '/foo', a: 'bar'
100
- expect(last_response.status).to eq(200)
101
- expect(last_response.body).to eq('["bar", "bar"]')
102
- end
103
- end
104
- end
105
- end
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::Extensions::Hashie::Mash::ParamBuilder do
4
- subject { Class.new(Grape::API) }
5
-
6
- def app
7
- subject
8
- end
9
-
10
- describe 'in an endpoint' do
11
- describe '#params' do
12
- before do
13
- subject.params do
14
- build_with Grape::Extensions::Hashie::Mash::ParamBuilder # rubocop:disable RSpec/DescribedClass
15
- end
16
-
17
- subject.get do
18
- params.class
19
- end
20
- end
21
-
22
- it 'is of type Hashie::Mash' do
23
- get '/'
24
- expect(last_response.status).to eq(200)
25
- expect(last_response.body).to eq('Hashie::Mash')
26
- end
27
- end
28
- end
29
-
30
- describe 'in an api' do
31
- before do
32
- subject.send(:include, Grape::Extensions::Hashie::Mash::ParamBuilder) # rubocop:disable RSpec/DescribedClass
33
- end
34
-
35
- describe '#params' do
36
- before do
37
- subject.get do
38
- params.class
39
- end
40
- end
41
-
42
- it 'is Hashie::Mash' do
43
- get '/'
44
- expect(last_response.status).to eq(200)
45
- expect(last_response.body).to eq('Hashie::Mash')
46
- end
47
- end
48
-
49
- context 'in a nested namespace api' do
50
- before do
51
- subject.namespace :foo do
52
- get do
53
- params.class
54
- end
55
- end
56
- end
57
-
58
- it 'is Hashie::Mash' do
59
- get '/foo'
60
- expect(last_response.status).to eq(200)
61
- expect(last_response.body).to eq('Hashie::Mash')
62
- end
63
- end
64
-
65
- it 'is indifferent to key or symbol access' do
66
- subject.params do
67
- build_with Grape::Extensions::Hashie::Mash::ParamBuilder # rubocop:disable RSpec/DescribedClass
68
- requires :a, type: String
69
- end
70
- subject.get '/foo' do
71
- [params[:a], params['a']]
72
- end
73
-
74
- get '/foo', a: 'bar'
75
- expect(last_response.status).to eq(200)
76
- expect(last_response.body).to eq('["bar", "bar"]')
77
- end
78
- end
79
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Grape do
4
- describe '.config' do
5
- subject { described_class.config }
6
-
7
- it { is_expected.to eq(param_builder: Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder) }
8
- end
9
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # see https://github.com/ruby-grape/grape/issues/1348
4
-
5
- def namespace
6
- raise
7
- end
8
-
9
- describe Grape::API do
10
- subject do
11
- Class.new(Grape::API) do
12
- format :json
13
- get do
14
- { ok: true }
15
- end
16
- end
17
- end
18
-
19
- def app
20
- subject
21
- end
22
-
23
- context 'with a global namespace function' do
24
- it 'works' do
25
- get '/'
26
- expect(last_response.status).to eq 200
27
- end
28
- end
29
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Rack::Sendfile do
4
- subject do
5
- content_object = file_object
6
- app = Class.new(Grape::API) do
7
- use Rack::Sendfile
8
- format :json
9
- get do
10
- if content_object.is_a?(String)
11
- sendfile content_object
12
- else
13
- stream content_object
14
- end
15
- end
16
- end
17
-
18
- options = {
19
- method: 'GET',
20
- 'HTTP_X_SENDFILE_TYPE' => 'X-Accel-Redirect',
21
- 'HTTP_X_ACCEL_MAPPING' => '/accel/mapping/=/replaced/'
22
- }
23
- env = Rack::MockRequest.env_for('/', options)
24
- app.call(env)
25
- end
26
-
27
- context 'when calling sendfile' do
28
- let(:file_object) do
29
- '/accel/mapping/some/path'
30
- end
31
-
32
- it 'contains Sendfile headers' do
33
- headers = subject[1]
34
- expect(headers).to include('X-Accel-Redirect')
35
- end
36
- end
37
-
38
- context 'when streaming non file content' do
39
- let(:file_object) do
40
- double(:file_object, each: nil)
41
- end
42
-
43
- it 'not contains Sendfile headers' do
44
- headers = subject[1]
45
- expect(headers).not_to include('X-Accel-Redirect')
46
- end
47
- end
48
- end
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Rack do
4
- it 'correctly populates params from a Tempfile' do
5
- input = Tempfile.new 'rubbish'
6
- begin
7
- app = Class.new(Grape::API) do
8
- format :json
9
- post do
10
- { params_keys: params.keys }
11
- end
12
- end
13
- input.write({ test: '123' * 10_000 }.to_json)
14
- input.rewind
15
- options = {
16
- input: input,
17
- method: 'POST',
18
- 'CONTENT_TYPE' => 'application/json'
19
- }
20
- env = Rack::MockRequest.env_for('/', options)
21
-
22
- expect(JSON.parse(read_chunks(app.call(env)[2]).join)['params_keys']).to match_array('test')
23
- ensure
24
- input.close
25
- input.unlink
26
- end
27
- end
28
-
29
- context 'when the app is mounted' do
30
- let(:ping_mount) do
31
- Class.new(Grape::API) do
32
- get 'ping'
33
- end
34
- end
35
-
36
- let(:app) do
37
- app_to_mount = ping_mount
38
- app = Class.new(Grape::API) do
39
- namespace 'namespace' do
40
- mount app_to_mount
41
- end
42
- end
43
- Rack::Builder.new(app)
44
- end
45
-
46
- it 'finds the app on the namespace' do
47
- get '/namespace/ping'
48
- expect(last_response.status).to eq 200
49
- end
50
- end
51
- end
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::API do
4
- subject do
5
- CombinedApi = combined_api
6
- Class.new(Grape::API) do
7
- format :json
8
- mount CombinedApi => '/'
9
- end
10
- end
11
-
12
- let(:jobs_api) do
13
- Class.new(Grape::API) do
14
- namespace :one do
15
- namespace :two do
16
- namespace :three do
17
- get :one do
18
- end
19
- get :two do
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end
26
-
27
- let(:combined_api) do
28
- JobsApi = jobs_api
29
- Class.new(Grape::API) do
30
- version :v1, using: :accept_version_header, cascade: true
31
- mount JobsApi
32
- end
33
- end
34
-
35
- def app
36
- subject
37
- end
38
-
39
- it 'execute first request in reasonable time' do
40
- started = Time.now
41
- get '/mount1/nested/test_method'
42
- expect(Time.now - started).to be < 5
43
- end
44
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'base64'
4
-
5
- describe Grape::Middleware::Auth::Base do
6
- subject do
7
- Class.new(Grape::API) do
8
- http_basic realm: 'my_realm' do |user, password|
9
- user && password && user == password
10
- end
11
- get '/authorized' do
12
- 'DONE'
13
- end
14
- end
15
- end
16
-
17
- def app
18
- subject
19
- end
20
-
21
- it 'authenticates if given valid creds' do
22
- get '/authorized', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('admin', 'admin')
23
- expect(last_response.status).to eq(200)
24
- expect(last_response.body).to eq('DONE')
25
- end
26
-
27
- it 'throws a 401 is wrong auth is given' do
28
- get '/authorized', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('admin', 'wrong')
29
- expect(last_response.status).to eq(401)
30
- end
31
- end
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::Middleware::Auth::DSL do
4
- subject { Class.new(Grape::API) }
5
-
6
- let(:block) { -> {} }
7
- let(:settings) do
8
- {
9
- opaque: 'secret',
10
- proc: block,
11
- realm: 'API Authorization',
12
- type: :http_digest
13
- }
14
- end
15
-
16
- describe '.auth' do
17
- it 'sets auth parameters' do
18
- expect(subject.base_instance).to receive(:use).with(Grape::Middleware::Auth::Base, settings)
19
-
20
- subject.auth :http_digest, realm: settings[:realm], opaque: settings[:opaque], &settings[:proc]
21
- expect(subject.auth).to eq(settings)
22
- end
23
-
24
- it 'can be called multiple times' do
25
- expect(subject.base_instance).to receive(:use).with(Grape::Middleware::Auth::Base, settings)
26
- expect(subject.base_instance).to receive(:use).with(Grape::Middleware::Auth::Base, settings.merge(realm: 'super_secret'))
27
-
28
- subject.auth :http_digest, realm: settings[:realm], opaque: settings[:opaque], &settings[:proc]
29
- first_settings = subject.auth
30
-
31
- subject.auth :http_digest, realm: 'super_secret', opaque: settings[:opaque], &settings[:proc]
32
-
33
- expect(subject.auth).to eq(settings.merge(realm: 'super_secret'))
34
- expect(subject.auth.object_id).not_to eq(first_settings.object_id)
35
- end
36
- end
37
-
38
- describe '.http_basic' do
39
- it 'sets auth parameters' do
40
- subject.http_basic realm: 'my_realm', &settings[:proc]
41
- expect(subject.auth).to eq(realm: 'my_realm', type: :http_basic, proc: block)
42
- end
43
- end
44
-
45
- describe '.http_digest' do
46
- context 'when realm is a hash' do
47
- it 'sets auth parameters' do
48
- subject.http_digest realm: { realm: 'my_realm', opaque: 'my_opaque' }, &settings[:proc]
49
- expect(subject.auth).to eq(realm: { realm: 'my_realm', opaque: 'my_opaque' }, type: :http_digest, proc: block)
50
- end
51
- end
52
-
53
- context 'when realm is not hash' do
54
- it 'sets auth parameters' do
55
- subject.http_digest realm: 'my_realm', opaque: 'my_opaque', &settings[:proc]
56
- expect(subject.auth).to eq(realm: 'my_realm', type: :http_digest, proc: block, opaque: 'my_opaque')
57
- end
58
- end
59
- end
60
- end
@@ -1,120 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'base64'
4
-
5
- describe Grape::Middleware::Auth::Strategies do
6
- context 'Basic Auth' do
7
- def app
8
- proc = ->(u, p) { u && p && u == p }
9
- Rack::Builder.new do |b|
10
- b.use Grape::Middleware::Error
11
- b.use(Grape::Middleware::Auth::Base, type: :http_basic, proc: proc)
12
- b.run ->(_env) { [200, {}, ['Hello there.']] }
13
- end
14
- end
15
-
16
- it 'throws a 401 if no auth is given' do
17
- @proc = -> { false }
18
- get '/whatever'
19
- expect(last_response.status).to eq(401)
20
- end
21
-
22
- it 'authenticates if given valid creds' do
23
- get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('admin', 'admin')
24
- expect(last_response.status).to eq(200)
25
- end
26
-
27
- it 'throws a 401 is wrong auth is given' do
28
- get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('admin', 'wrong')
29
- expect(last_response.status).to eq(401)
30
- end
31
- end
32
-
33
- context 'Digest MD5 Auth' do
34
- RSpec::Matchers.define :be_challenge do
35
- match do |actual_response|
36
- actual_response.status == 401 &&
37
- actual_response['WWW-Authenticate'].start_with?('Digest ') &&
38
- actual_response.body.empty?
39
- end
40
- end
41
-
42
- module StrategiesSpec
43
- class PasswordHashed < Grape::API
44
- http_digest(realm: { realm: 'Test Api', opaque: 'secret', passwords_hashed: true }) do |username|
45
- { 'foo' => Digest::MD5.hexdigest(['foo', 'Test Api', 'bar'].join(':')) }[username]
46
- end
47
-
48
- get '/test' do
49
- [{ hey: 'you' }, { there: 'bar' }, { foo: 'baz' }]
50
- end
51
- end
52
-
53
- class PasswordIsNotHashed < Grape::API
54
- http_digest(realm: 'Test Api', opaque: 'secret') do |username|
55
- { 'foo' => 'bar' }[username]
56
- end
57
-
58
- get '/test' do
59
- [{ hey: 'you' }, { there: 'bar' }, { foo: 'baz' }]
60
- end
61
- end
62
- end
63
-
64
- context 'when password is hashed' do
65
- def app
66
- StrategiesSpec::PasswordHashed
67
- end
68
-
69
- it 'is a digest authentication challenge' do
70
- get '/test'
71
- expect(last_response).to be_challenge
72
- end
73
-
74
- it 'throws a 401 if no auth is given' do
75
- get '/test'
76
- expect(last_response.status).to eq(401)
77
- end
78
-
79
- it 'authenticates if given valid creds' do
80
- digest_authorize 'foo', 'bar'
81
- get '/test'
82
- expect(last_response.status).to eq(200)
83
- end
84
-
85
- it 'throws a 401 if given invalid creds' do
86
- digest_authorize 'bar', 'foo'
87
- get '/test'
88
- expect(last_response.status).to eq(401)
89
- end
90
- end
91
-
92
- context 'when password is not hashed' do
93
- def app
94
- StrategiesSpec::PasswordIsNotHashed
95
- end
96
-
97
- it 'is a digest authentication challenge' do
98
- get '/test'
99
- expect(last_response).to be_challenge
100
- end
101
-
102
- it 'throws a 401 if no auth is given' do
103
- get '/test'
104
- expect(last_response.status).to eq(401)
105
- end
106
-
107
- it 'authenticates if given valid creds' do
108
- digest_authorize 'foo', 'bar'
109
- get '/test'
110
- expect(last_response.status).to eq(200)
111
- end
112
-
113
- it 'throws a 401 if given invalid creds' do
114
- digest_authorize 'bar', 'foo'
115
- get '/test'
116
- expect(last_response.status).to eq(401)
117
- end
118
- end
119
- end
120
- end