grape 0.5.0 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (215) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1223 -164
  3. data/CONTRIBUTING.md +148 -0
  4. data/LICENSE +1 -1
  5. data/README.md +3531 -675
  6. data/UPGRADING.md +1851 -0
  7. data/grape.gemspec +26 -32
  8. data/grape.png +0 -0
  9. data/lib/grape/api/instance.rb +194 -0
  10. data/lib/grape/api.rb +110 -485
  11. data/lib/grape/content_types.rb +28 -0
  12. data/lib/grape/cookies.rb +32 -25
  13. data/lib/grape/declared_params_handler.rb +118 -0
  14. data/lib/grape/dry_types.rb +54 -0
  15. data/lib/grape/dsl/callbacks.rb +19 -0
  16. data/lib/grape/dsl/declared.rb +35 -0
  17. data/lib/grape/dsl/desc.rb +65 -0
  18. data/lib/grape/dsl/headers.rb +21 -0
  19. data/lib/grape/dsl/helpers.rb +103 -0
  20. data/lib/grape/dsl/inside_route.rb +293 -0
  21. data/lib/grape/dsl/logger.rb +19 -0
  22. data/lib/grape/dsl/middleware.rb +36 -0
  23. data/lib/grape/dsl/parameters.rb +239 -0
  24. data/lib/grape/dsl/request_response.rb +168 -0
  25. data/lib/grape/dsl/routing.rb +288 -0
  26. data/lib/grape/dsl/settings.rb +67 -0
  27. data/lib/grape/dsl/validations.rb +51 -0
  28. data/lib/grape/endpoint.rb +287 -386
  29. data/lib/grape/env.rb +20 -0
  30. data/lib/grape/error_formatter/base.rb +56 -19
  31. data/lib/grape/error_formatter/json.rb +18 -7
  32. data/lib/grape/error_formatter/serializable_hash.rb +7 -0
  33. data/lib/grape/error_formatter/txt.rb +14 -11
  34. data/lib/grape/error_formatter/xml.rb +5 -11
  35. data/lib/grape/error_formatter.rb +15 -0
  36. data/lib/grape/exceptions/base.rb +31 -47
  37. data/lib/grape/exceptions/incompatible_option_values.rb +11 -0
  38. data/lib/grape/exceptions/invalid_accept_header.rb +11 -0
  39. data/lib/grape/exceptions/invalid_formatter.rb +3 -5
  40. data/lib/grape/exceptions/invalid_message_body.rb +11 -0
  41. data/lib/grape/exceptions/invalid_response.rb +11 -0
  42. data/lib/grape/exceptions/invalid_version_header.rb +11 -0
  43. data/lib/grape/exceptions/invalid_versioner_option.rb +3 -6
  44. data/lib/grape/exceptions/invalid_with_option_for_represent.rb +3 -7
  45. data/lib/grape/exceptions/method_not_allowed.rb +11 -0
  46. data/lib/grape/exceptions/missing_group_type.rb +11 -0
  47. data/lib/grape/exceptions/missing_mime_type.rb +3 -6
  48. data/lib/grape/exceptions/missing_vendor_option.rb +3 -5
  49. data/lib/grape/exceptions/request_error.rb +11 -0
  50. data/lib/grape/exceptions/unknown_auth_strategy.rb +11 -0
  51. data/lib/grape/exceptions/unknown_parameter.rb +11 -0
  52. data/lib/grape/exceptions/unknown_params_builder.rb +11 -0
  53. data/lib/grape/exceptions/unknown_validator.rb +3 -4
  54. data/lib/grape/exceptions/unsupported_group_type.rb +11 -0
  55. data/lib/grape/exceptions/validation.rb +20 -8
  56. data/lib/grape/exceptions/validation_array_errors.rb +14 -0
  57. data/lib/grape/exceptions/validation_errors.rb +59 -0
  58. data/lib/grape/formatter/base.rb +9 -26
  59. data/lib/grape/formatter/json.rb +6 -7
  60. data/lib/grape/formatter/serializable_hash.rb +24 -20
  61. data/lib/grape/formatter/txt.rb +5 -7
  62. data/lib/grape/formatter/xml.rb +6 -7
  63. data/lib/grape/formatter.rb +17 -0
  64. data/lib/grape/json.rb +10 -0
  65. data/lib/grape/locale/en.yml +47 -20
  66. data/lib/grape/middleware/auth/base.rb +25 -0
  67. data/lib/grape/middleware/auth/dsl.rb +38 -0
  68. data/lib/grape/middleware/auth/strategies.rb +25 -0
  69. data/lib/grape/middleware/auth/strategy_info.rb +15 -0
  70. data/lib/grape/middleware/base.rb +73 -19
  71. data/lib/grape/middleware/error.rb +104 -44
  72. data/lib/grape/middleware/filter.rb +5 -3
  73. data/lib/grape/middleware/formatter.rb +115 -105
  74. data/lib/grape/middleware/globals.rb +14 -0
  75. data/lib/grape/middleware/stack.rb +110 -0
  76. data/lib/grape/middleware/versioner/accept_version_header.rb +11 -40
  77. data/lib/grape/middleware/versioner/base.rb +76 -0
  78. data/lib/grape/middleware/versioner/header.rb +61 -78
  79. data/lib/grape/middleware/versioner/param.rb +9 -21
  80. data/lib/grape/middleware/versioner/path.rb +16 -31
  81. data/lib/grape/middleware/versioner.rb +10 -15
  82. data/lib/grape/namespace.rb +39 -10
  83. data/lib/grape/params_builder/base.rb +20 -0
  84. data/lib/grape/params_builder/hash.rb +11 -0
  85. data/lib/grape/params_builder/hash_with_indifferent_access.rb +11 -0
  86. data/lib/grape/params_builder/hashie_mash.rb +11 -0
  87. data/lib/grape/params_builder.rb +15 -0
  88. data/lib/grape/parser/base.rb +9 -24
  89. data/lib/grape/parser/json.rb +8 -7
  90. data/lib/grape/parser/xml.rb +8 -7
  91. data/lib/grape/parser.rb +15 -0
  92. data/lib/grape/path.rb +76 -0
  93. data/lib/grape/presenters/presenter.rb +11 -0
  94. data/lib/grape/railtie.rb +9 -0
  95. data/lib/grape/request.rb +182 -0
  96. data/lib/grape/router/base_route.rb +48 -0
  97. data/lib/grape/router/greedy_route.rb +26 -0
  98. data/lib/grape/router/pattern.rb +67 -0
  99. data/lib/grape/router/route.rb +58 -0
  100. data/lib/grape/router.rb +167 -0
  101. data/lib/grape/serve_stream/file_body.rb +36 -0
  102. data/lib/grape/serve_stream/sendfile_response.rb +21 -0
  103. data/lib/grape/serve_stream/stream_response.rb +23 -0
  104. data/lib/grape/util/api_description.rb +58 -0
  105. data/lib/grape/util/base_inheritable.rb +46 -0
  106. data/lib/grape/util/cache.rb +18 -0
  107. data/lib/grape/util/deep_freeze.rb +35 -0
  108. data/lib/grape/util/endpoint_configuration.rb +8 -0
  109. data/lib/grape/util/header.rb +13 -0
  110. data/lib/grape/util/inheritable_setting.rb +107 -0
  111. data/lib/grape/util/inheritable_values.rb +29 -0
  112. data/lib/grape/util/lazy/block.rb +29 -0
  113. data/lib/grape/util/lazy/value.rb +38 -0
  114. data/lib/grape/util/lazy/value_array.rb +21 -0
  115. data/lib/grape/util/lazy/value_enumerable.rb +34 -0
  116. data/lib/grape/util/lazy/value_hash.rb +21 -0
  117. data/lib/grape/util/media_type.rb +70 -0
  118. data/lib/grape/util/registry.rb +27 -0
  119. data/lib/grape/util/reverse_stackable_values.rb +15 -0
  120. data/lib/grape/util/stackable_values.rb +36 -0
  121. data/lib/grape/util/translation.rb +42 -0
  122. data/lib/grape/validations/attributes_iterator.rb +77 -0
  123. data/lib/grape/validations/contract_scope.rb +28 -0
  124. data/lib/grape/validations/multiple_attributes_iterator.rb +13 -0
  125. data/lib/grape/validations/param_scope_tracker.rb +57 -0
  126. data/lib/grape/validations/params_documentation.rb +50 -0
  127. data/lib/grape/validations/params_scope.rb +529 -0
  128. data/lib/grape/validations/single_attribute_iterator.rb +26 -0
  129. data/lib/grape/validations/types/array_coercer.rb +60 -0
  130. data/lib/grape/validations/types/custom_type_coercer.rb +164 -0
  131. data/lib/grape/validations/types/custom_type_collection_coercer.rb +56 -0
  132. data/lib/grape/validations/types/dry_type_coercer.rb +59 -0
  133. data/lib/grape/validations/types/file.rb +31 -0
  134. data/lib/grape/validations/types/invalid_value.rb +17 -0
  135. data/lib/grape/validations/types/json.rb +71 -0
  136. data/lib/grape/validations/types/multiple_type_coercer.rb +57 -0
  137. data/lib/grape/validations/types/primitive_coercer.rb +46 -0
  138. data/lib/grape/validations/types/set_coercer.rb +35 -0
  139. data/lib/grape/validations/types/variant_collection_coercer.rb +51 -0
  140. data/lib/grape/validations/types.rb +198 -0
  141. data/lib/grape/validations/validators/all_or_none_of_validator.rb +19 -0
  142. data/lib/grape/validations/validators/allow_blank_validator.rb +25 -0
  143. data/lib/grape/validations/validators/as_validator.rb +14 -0
  144. data/lib/grape/validations/validators/at_least_one_of_validator.rb +18 -0
  145. data/lib/grape/validations/validators/base.rb +158 -0
  146. data/lib/grape/validations/validators/coerce_validator.rb +55 -0
  147. data/lib/grape/validations/validators/contract_scope_validator.rb +45 -0
  148. data/lib/grape/validations/validators/default_validator.rb +31 -0
  149. data/lib/grape/validations/validators/exactly_one_of_validator.rb +24 -0
  150. data/lib/grape/validations/validators/except_values_validator.rb +33 -0
  151. data/lib/grape/validations/validators/length_validator.rb +48 -0
  152. data/lib/grape/validations/validators/multiple_params_base.rb +34 -0
  153. data/lib/grape/validations/validators/mutually_exclusive_validator.rb +18 -0
  154. data/lib/grape/validations/validators/presence_validator.rb +17 -0
  155. data/lib/grape/validations/validators/regexp_validator.rb +24 -0
  156. data/lib/grape/validations/validators/same_as_validator.rb +20 -0
  157. data/lib/grape/validations/validators/values_validator.rb +68 -0
  158. data/lib/grape/validations.rb +10 -226
  159. data/lib/grape/version.rb +4 -1
  160. data/lib/grape/xml.rb +10 -0
  161. data/lib/grape.rb +79 -77
  162. metadata +160 -306
  163. data/.gitignore +0 -38
  164. data/.rspec +0 -2
  165. data/.travis.yml +0 -9
  166. data/.yardopts +0 -2
  167. data/Gemfile +0 -19
  168. data/Guardfile +0 -15
  169. data/Rakefile +0 -64
  170. data/lib/grape/exceptions/missing_option.rb +0 -13
  171. data/lib/grape/exceptions/unknown_options.rb +0 -14
  172. data/lib/grape/http/request.rb +0 -28
  173. data/lib/grape/middleware/auth/basic.rb +0 -30
  174. data/lib/grape/middleware/auth/digest.rb +0 -30
  175. data/lib/grape/middleware/auth/oauth2.rb +0 -72
  176. data/lib/grape/route.rb +0 -23
  177. data/lib/grape/util/content_types.rb +0 -17
  178. data/lib/grape/util/deep_merge.rb +0 -23
  179. data/lib/grape/util/hash_stack.rb +0 -120
  180. data/lib/grape/validations/coerce.rb +0 -62
  181. data/lib/grape/validations/default.rb +0 -24
  182. data/lib/grape/validations/presence.rb +0 -12
  183. data/lib/grape/validations/regexp.rb +0 -14
  184. data/spec/grape/api_spec.rb +0 -2129
  185. data/spec/grape/endpoint_spec.rb +0 -605
  186. data/spec/grape/entity_spec.rb +0 -281
  187. data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -18
  188. data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -18
  189. data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -24
  190. data/spec/grape/exceptions/missing_option_spec.rb +0 -18
  191. data/spec/grape/exceptions/unknown_options_spec.rb +0 -18
  192. data/spec/grape/exceptions/unknown_validator_spec.rb +0 -18
  193. data/spec/grape/middleware/auth/basic_spec.rb +0 -31
  194. data/spec/grape/middleware/auth/digest_spec.rb +0 -47
  195. data/spec/grape/middleware/auth/oauth2_spec.rb +0 -90
  196. data/spec/grape/middleware/base_spec.rb +0 -63
  197. data/spec/grape/middleware/error_spec.rb +0 -49
  198. data/spec/grape/middleware/exception_spec.rb +0 -164
  199. data/spec/grape/middleware/formatter_spec.rb +0 -244
  200. data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -121
  201. data/spec/grape/middleware/versioner/header_spec.rb +0 -277
  202. data/spec/grape/middleware/versioner/param_spec.rb +0 -58
  203. data/spec/grape/middleware/versioner/path_spec.rb +0 -44
  204. data/spec/grape/middleware/versioner_spec.rb +0 -22
  205. data/spec/grape/util/hash_stack_spec.rb +0 -133
  206. data/spec/grape/validations/coerce_spec.rb +0 -143
  207. data/spec/grape/validations/default_spec.rb +0 -67
  208. data/spec/grape/validations/presence_spec.rb +0 -144
  209. data/spec/grape/validations/regexp_spec.rb +0 -33
  210. data/spec/grape/validations/zh-CN.yml +0 -9
  211. data/spec/grape/validations_spec.rb +0 -217
  212. data/spec/shared/versioning_examples.rb +0 -121
  213. data/spec/spec_helper.rb +0 -25
  214. data/spec/support/basic_auth_encode_helpers.rb +0 -4
  215. data/spec/support/versioned_helpers.rb +0 -48
@@ -1,164 +0,0 @@
1
- require 'spec_helper'
2
- require 'active_support/core_ext/hash'
3
-
4
- describe Grape::Middleware::Error do
5
-
6
- # raises a text exception
7
- class ExceptionApp
8
- class << self
9
- def call(env)
10
- raise "rain!"
11
- end
12
- end
13
- end
14
-
15
- # raises a hash error
16
- class ErrorHashApp
17
- class << self
18
- def error!(message, status=403)
19
- throw :error, :message => { :error => message, :detail => "missing widget" }, :status => status
20
- end
21
- def call(env)
22
- error!("rain!", 401)
23
- end
24
- end
25
- end
26
-
27
- # raises an error!
28
- class AccessDeniedApp
29
- class << self
30
- def error!(message, status=403)
31
- throw :error, :message => message, :status => status
32
- end
33
- def call(env)
34
- error!("Access Denied", 401)
35
- end
36
- end
37
- end
38
-
39
- # raises a custom error
40
- class CustomError < Grape::Exceptions::Base; end
41
- class CustomErrorApp
42
- class << self
43
- def call(env)
44
- raise CustomError, :status => 400, :message => 'failed validation'
45
- end
46
- end
47
- end
48
-
49
- def app
50
- @app
51
- end
52
-
53
- it 'does not trap errors by default' do
54
- @app ||= Rack::Builder.app do
55
- use Grape::Middleware::Error
56
- run ExceptionApp
57
- end
58
- lambda { get '/' }.should raise_error
59
- end
60
-
61
- context 'with rescue_all set to true' do
62
- it 'sets the message appropriately' do
63
- @app ||= Rack::Builder.app do
64
- use Grape::Middleware::Error, :rescue_all => true
65
- run ExceptionApp
66
- end
67
- get '/'
68
- last_response.body.should == "rain!"
69
- end
70
-
71
- it 'defaults to a 403 status' do
72
- @app ||= Rack::Builder.app do
73
- use Grape::Middleware::Error, :rescue_all => true
74
- run ExceptionApp
75
- end
76
- get '/'
77
- last_response.status.should == 403
78
- end
79
-
80
- it 'is possible to specify a different default status code' do
81
- @app ||= Rack::Builder.app do
82
- use Grape::Middleware::Error, :rescue_all => true, :default_status => 500
83
- run ExceptionApp
84
- end
85
- get '/'
86
- last_response.status.should == 500
87
- end
88
-
89
- it 'is possible to return errors in json format' do
90
- @app ||= Rack::Builder.app do
91
- use Grape::Middleware::Error, :rescue_all => true, :format => :json
92
- run ExceptionApp
93
- end
94
- get '/'
95
- last_response.body.should == '{"error":"rain!"}'
96
- end
97
-
98
- it 'is possible to return hash errors in json format' do
99
- @app ||= Rack::Builder.app do
100
- use Grape::Middleware::Error, :rescue_all => true, :format => :json
101
- run ErrorHashApp
102
- end
103
- get '/'
104
- ['{"error":"rain!","detail":"missing widget"}',
105
- '{"detail":"missing widget","error":"rain!"}'].should include(last_response.body)
106
- end
107
-
108
- it 'is possible to return errors in xml format' do
109
- @app ||= Rack::Builder.app do
110
- use Grape::Middleware::Error, :rescue_all => true, :format => :xml
111
- run ExceptionApp
112
- end
113
- get '/'
114
- last_response.body.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<error>\n <message>rain!</message>\n</error>\n"
115
- end
116
-
117
- it 'is possible to return hash errors in xml format' do
118
- @app ||= Rack::Builder.app do
119
- use Grape::Middleware::Error, :rescue_all => true, :format => :xml
120
- run ErrorHashApp
121
- end
122
- get '/'
123
- ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<error>\n <detail>missing widget</detail>\n <error>rain!</error>\n</error>\n",
124
- "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<error>\n <error>rain!</error>\n <detail>missing widget</detail>\n</error>\n"].should include(last_response.body)
125
- end
126
-
127
- it 'is possible to specify a custom formatter' do
128
- @app ||= Rack::Builder.app do
129
- use Grape::Middleware::Error,
130
- :rescue_all => true,
131
- :format => :custom,
132
- :error_formatters => {
133
- :custom => lambda { |message, backtrace, options, env|
134
- { :custom_formatter => message }.inspect
135
- }
136
- }
137
- run ExceptionApp
138
- end
139
- get '/'
140
- last_response.body.should == '{:custom_formatter=>"rain!"}'
141
- end
142
-
143
- it 'does not trap regular error! codes' do
144
- @app ||= Rack::Builder.app do
145
- use Grape::Middleware::Error
146
- run AccessDeniedApp
147
- end
148
- get '/'
149
- last_response.status.should == 401
150
- end
151
-
152
- it 'responds to custom Grape exceptions appropriately' do
153
- @app ||= Rack::Builder.app do
154
- use Grape::Middleware::Error, :rescue_all => false
155
- run CustomErrorApp
156
- end
157
-
158
- get '/'
159
- last_response.status.should == 400
160
- last_response.body.should == 'failed validation'
161
- end
162
-
163
- end
164
- end
@@ -1,244 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Grape::Middleware::Formatter do
4
- subject{ Grape::Middleware::Formatter.new(app) }
5
- before{ subject.stub!(:dup).and_return(subject) }
6
-
7
- let(:app){ lambda{|env| [200, {}, [@body || { "foo" => "bar" }]]} }
8
-
9
- context 'serialization' do
10
- it 'looks at the bodies for possibly serializable data' do
11
- @body = {"abc" => "def"}
12
- status, headers, bodies = *subject.call({'PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json'})
13
- bodies.each{|b| b.should == MultiJson.dump(@body) }
14
- end
15
-
16
- it 'calls #to_json since default format is json' do
17
- @body = ['foo']
18
- @body.instance_eval do
19
- def to_json
20
- "\"bar\""
21
- end
22
- end
23
-
24
- subject.call({'PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json'}).last.each{|b| b.should == '"bar"'}
25
- end
26
-
27
- it 'calls #to_xml if the content type is xml' do
28
- @body = "string"
29
- @body.instance_eval do
30
- def to_xml
31
- "<bar/>"
32
- end
33
- end
34
-
35
- subject.call({'PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json'}).last.each{|b| b.should == '<bar/>'}
36
- end
37
- end
38
-
39
- context 'error handling' do
40
- let(:formatter) { stub(:formatter) }
41
- before do
42
- Grape::Formatter::Base.stub(:formatter_for) { formatter }
43
- end
44
-
45
- it 'rescues formatter-specific exceptions' do
46
- formatter.stub(:call) { raise Grape::Exceptions::InvalidFormatter.new(String, 'xml') }
47
-
48
- expect {
49
- catch(:error){subject.call({'PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json'})}
50
- }.to_not raise_error
51
- end
52
-
53
- it 'does not rescue other exceptions' do
54
- formatter.stub(:call) { raise StandardError }
55
-
56
- expect {
57
- catch(:error){subject.call({'PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json'})}
58
- }.to raise_error
59
- end
60
- end
61
-
62
- context 'detection' do
63
-
64
- it 'uses the extension if one is provided' do
65
- subject.call({'PATH_INFO' => '/info.xml'})
66
- subject.env['api.format'].should == :xml
67
- subject.call({'PATH_INFO' => '/info.json'})
68
- subject.env['api.format'].should == :json
69
- end
70
-
71
- it 'uses the format parameter if one is provided' do
72
- subject.call({'PATH_INFO' => '/info','QUERY_STRING' => 'format=json'})
73
- subject.env['api.format'].should == :json
74
- subject.call({'PATH_INFO' => '/info','QUERY_STRING' => 'format=xml'})
75
- subject.env['api.format'].should == :xml
76
- end
77
-
78
- it 'uses the default format if none is provided' do
79
- subject.call({'PATH_INFO' => '/info'})
80
- subject.env['api.format'].should == :txt
81
- end
82
-
83
- it 'uses the requested format if provided in headers' do
84
- subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json'})
85
- subject.env['api.format'].should == :json
86
- end
87
-
88
- it 'uses the file extension format if provided before headers' do
89
- subject.call({'PATH_INFO' => '/info.txt', 'HTTP_ACCEPT' => 'application/json'})
90
- subject.env['api.format'].should == :txt
91
- end
92
- end
93
-
94
- context 'accept header detection' do
95
- it 'detects from the Accept header' do
96
- subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/xml'})
97
- subject.env['api.format'].should == :xml
98
- end
99
-
100
- it 'looks for case-indifferent headers' do
101
- subject.call({'PATH_INFO' => '/info', 'http_accept' => 'application/xml'})
102
- subject.env['api.format'].should == :xml
103
- end
104
-
105
- it 'uses quality rankings to determine formats' do
106
- subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json; q=0.3,application/xml; q=1.0'})
107
- subject.env['api.format'].should == :xml
108
- subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json; q=1.0,application/xml; q=0.3'})
109
- subject.env['api.format'].should == :json
110
- end
111
-
112
- it 'handles quality rankings mixed with nothing' do
113
- subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json,application/xml; q=1.0'})
114
- subject.env['api.format'].should == :xml
115
- end
116
-
117
- it 'parses headers with other attributes' do
118
- subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json; abc=2.3; q=1.0,application/xml; q=0.7'})
119
- subject.env['api.format'].should == :json
120
- end
121
-
122
- it 'parses headers with vendor and api version' do
123
- subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test-v1+xml'})
124
- subject.env['api.format'].should == :xml
125
- end
126
-
127
- it 'parses headers with symbols as hash keys' do
128
- subject.call({'PATH_INFO' => '/info', 'http_accept' => 'application/xml', :system_time => '091293'})
129
- subject.env[:system_time].should == '091293'
130
- end
131
- end
132
-
133
- context 'content-type' do
134
- it 'is set for json' do
135
- _, headers, _ = subject.call({'PATH_INFO' => '/info.json'})
136
- headers['Content-type'].should == 'application/json'
137
- end
138
- it 'is set for xml' do
139
- _, headers, _ = subject.call({'PATH_INFO' => '/info.xml'})
140
- headers['Content-type'].should == 'application/xml'
141
- end
142
- it 'is set for txt' do
143
- _, headers, _ = subject.call({'PATH_INFO' => '/info.txt'})
144
- headers['Content-type'].should == 'text/plain'
145
- end
146
- it 'is set for custom' do
147
- subject.options[:content_types] = {}
148
- subject.options[:content_types][:custom] = 'application/x-custom'
149
- _, headers, _ = subject.call({'PATH_INFO' => '/info.custom'})
150
- headers['Content-type'].should == 'application/x-custom'
151
- end
152
- end
153
-
154
- context 'format' do
155
- it 'uses custom formatter' do
156
- subject.options[:content_types] = {}
157
- subject.options[:content_types][:custom] = "don't care"
158
- subject.options[:formatters][:custom] = lambda { |obj, env| 'CUSTOM FORMAT' }
159
- _, _, body = subject.call({'PATH_INFO' => '/info.custom'})
160
- body.body.should == ['CUSTOM FORMAT']
161
- end
162
- it 'uses default json formatter' do
163
- @body = ['blah']
164
- _, _, body = subject.call({'PATH_INFO' => '/info.json'})
165
- body.body.should == ['["blah"]']
166
- end
167
- it 'uses custom json formatter' do
168
- subject.options[:formatters][:json] = lambda { |obj, env| 'CUSTOM JSON FORMAT' }
169
- _, _, body = subject.call({'PATH_INFO' => '/info.json'})
170
- body.body.should == ['CUSTOM JSON FORMAT']
171
- end
172
- end
173
-
174
- context 'input' do
175
- [ "POST", "PATCH", "PUT" ].each do |method|
176
- [ "application/json", "application/json; charset=utf-8" ].each do |content_type|
177
- context content_type do
178
- it 'parses the body from #{method} and copies values into rack.request.form_hash' do
179
- io = StringIO.new('{"is_boolean":true,"string":"thing"}')
180
- subject.call({
181
- 'PATH_INFO' => '/info',
182
- 'REQUEST_METHOD' => method,
183
- 'CONTENT_TYPE' => content_type,
184
- 'rack.input' => io,
185
- 'CONTENT_LENGTH' => io.length
186
- })
187
- subject.env['rack.request.form_hash']['is_boolean'].should be_true
188
- subject.env['rack.request.form_hash']['string'].should == 'thing'
189
- end
190
- end
191
- end
192
- it "parses the chunked body from #{method} and copies values into rack.request.from_hash" do
193
- io = StringIO.new('{"is_boolean":true,"string":"thing"}')
194
- subject.call({
195
- 'PATH_INFO' => '/infol',
196
- 'REQUEST_METHOD' => method,
197
- 'CONTENT_TYPE' => 'application/json',
198
- 'rack.input' => io,
199
- 'HTTP_TRANSFER_ENCODING' => 'chunked'
200
- })
201
- subject.env['rack.request.form_hash']['is_boolean'].should be_true
202
- subject.env['rack.request.form_hash']['string'].should == 'thing'
203
- end
204
- it "rewinds IO" do
205
- io = StringIO.new('{"is_boolean":true,"string":"thing"}')
206
- io.read
207
- subject.call({
208
- 'PATH_INFO' => '/infol',
209
- 'REQUEST_METHOD' => method,
210
- 'CONTENT_TYPE' => 'application/json',
211
- 'rack.input' => io,
212
- 'HTTP_TRANSFER_ENCODING' => 'chunked'
213
- })
214
- subject.env['rack.request.form_hash']['is_boolean'].should be_true
215
- subject.env['rack.request.form_hash']['string'].should == 'thing'
216
- end
217
- it 'parses the body from an xml #{method} and copies values into rack.request.from_hash' do
218
- io = StringIO.new('<thing><name>Test</name></thing>')
219
- subject.call({
220
- 'PATH_INFO' => '/info.xml',
221
- 'REQUEST_METHOD' => method,
222
- 'CONTENT_TYPE' => 'application/xml',
223
- 'rack.input' => io,
224
- 'CONTENT_LENGTH' => io.length
225
- })
226
- subject.env['rack.request.form_hash']['thing']['name'].should == 'Test'
227
- end
228
- [ Rack::Request::FORM_DATA_MEDIA_TYPES, Rack::Request::PARSEABLE_DATA_MEDIA_TYPES ].flatten.each do |content_type|
229
- it "ignores #{content_type}" do
230
- io = StringIO.new('name=Other+Test+Thing')
231
- subject.call({
232
- 'PATH_INFO' => '/info',
233
- 'REQUEST_METHOD' => method,
234
- 'CONTENT_TYPE' => content_type,
235
- 'rack.input' => io,
236
- 'CONTENT_LENGTH' => io.length
237
- })
238
- subject.env['rack.request.form_hash'].should be_nil
239
- end
240
- end
241
- end
242
- end
243
-
244
- end
@@ -1,121 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Grape::Middleware::Versioner::AcceptVersionHeader do
4
- let(:app) { lambda{|env| [200, env, env]} }
5
- subject { Grape::Middleware::Versioner::AcceptVersionHeader.new(app, @options || {}) }
6
-
7
- before do
8
- @options = {
9
- :version_options => {
10
- :using => :accept_version_header
11
- },
12
- }
13
- end
14
-
15
- context 'api.version' do
16
- before do
17
- @options[:versions] = ['v1']
18
- end
19
-
20
- it 'is set' do
21
- status, _, env = subject.call('HTTP_ACCEPT_VERSION' => 'v1')
22
- env['api.version'].should eql 'v1'
23
- status.should == 200
24
- end
25
-
26
- it 'is set if format provided' do
27
- status, _, env = subject.call('HTTP_ACCEPT_VERSION' => 'v1')
28
- env['api.version'].should eql 'v1'
29
- status.should == 200
30
- end
31
-
32
- it 'fails with 406 Not Acceptable if version is not supported' do
33
- expect {
34
- env = subject.call('HTTP_ACCEPT_VERSION' => 'v2').last
35
- }.to throw_symbol(
36
- :error,
37
- :status => 406,
38
- :headers => {'X-Cascade' => 'pass'},
39
- :message => 'The requested version is not supported.'
40
- )
41
- end
42
- end
43
-
44
- it 'succeeds if :strict is not set' do
45
- subject.call('HTTP_ACCEPT_VERSION' => '').first.should == 200
46
- subject.call({}).first.should == 200
47
- end
48
-
49
- it 'succeeds if :strict is set to false' do
50
- @options[:version_options][:strict] = false
51
- subject.call('HTTP_ACCEPT_VERSION' => '').first.should == 200
52
- subject.call({}).first.should == 200
53
- end
54
-
55
- context 'when :strict is set' do
56
- before do
57
- @options[:versions] = ['v1']
58
- @options[:version_options][:strict] = true
59
- end
60
-
61
- it 'fails with 406 Not Acceptable if header is not set' do
62
- expect {
63
- env = subject.call({}).last
64
- }.to throw_symbol(
65
- :error,
66
- :status => 406,
67
- :headers => {'X-Cascade' => 'pass'},
68
- :message => 'Accept-Version header must be set.'
69
- )
70
- end
71
-
72
- it 'fails with 406 Not Acceptable if header is empty' do
73
- expect {
74
- env = subject.call('HTTP_ACCEPT_VERSION' => '').last
75
- }.to throw_symbol(
76
- :error,
77
- :status => 406,
78
- :headers => {'X-Cascade' => 'pass'},
79
- :message => 'Accept-Version header must be set.'
80
- )
81
- end
82
-
83
- it 'succeeds if proper header is set' do
84
- subject.call('HTTP_ACCEPT_VERSION' => 'v1').first.should == 200
85
- end
86
- end
87
-
88
- context 'when :strict and :cascade=>false are set' do
89
- before do
90
- @options[:versions] = ['v1']
91
- @options[:version_options][:strict] = true
92
- @options[:version_options][:cascade] = false
93
- end
94
-
95
- it 'fails with 406 Not Acceptable if header is not set' do
96
- expect {
97
- env = subject.call({}).last
98
- }.to throw_symbol(
99
- :error,
100
- :status => 406,
101
- :headers => {},
102
- :message => 'Accept-Version header must be set.'
103
- )
104
- end
105
-
106
- it 'fails with 406 Not Acceptable if header is empty' do
107
- expect {
108
- env = subject.call('HTTP_ACCEPT_VERSION' => '').last
109
- }.to throw_symbol(
110
- :error,
111
- :status => 406,
112
- :headers => {},
113
- :message => 'Accept-Version header must be set.'
114
- )
115
- end
116
-
117
- it 'succeeds if proper header is set' do
118
- subject.call('HTTP_ACCEPT_VERSION' => 'v1').first.should == 200
119
- end
120
- end
121
- end