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,277 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Grape::Middleware::Versioner::Header do
4
- let(:app) { lambda{|env| [200, env, env]} }
5
- subject { Grape::Middleware::Versioner::Header.new(app, @options || {}) }
6
-
7
- before do
8
- @options = {
9
- :version_options => {
10
- :using => :header,
11
- :vendor => 'vendor',
12
- },
13
- }
14
- end
15
-
16
- context 'api.type and api.subtype' do
17
- it 'sets type and subtype to first choice of content type if no preference given' do
18
- status, _, env = subject.call('HTTP_ACCEPT' => '*/*')
19
- env['api.type'].should eql 'application'
20
- env['api.subtype'].should eql 'vnd.vendor+xml'
21
- status.should == 200
22
- end
23
-
24
- it 'sets preferred type' do
25
- status, _, env = subject.call('HTTP_ACCEPT' => 'application/*')
26
- env['api.type'].should eql 'application'
27
- env['api.subtype'].should eql 'vnd.vendor+xml'
28
- status.should == 200
29
- end
30
-
31
- it 'sets preferred type and subtype' do
32
- status, _, env = subject.call('HTTP_ACCEPT' => 'text/plain')
33
- env['api.type'].should eql 'text'
34
- env['api.subtype'].should eql 'plain'
35
- status.should == 200
36
- end
37
- end
38
-
39
- context 'api.format' do
40
- it 'is set' do
41
- status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor+json')
42
- env['api.format'].should eql 'json'
43
- status.should == 200
44
- end
45
-
46
- it 'is nil if not provided' do
47
- status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor')
48
- env['api.format'].should eql nil
49
- status.should == 200
50
- end
51
-
52
- [ 'v1', :v1 ].each do |version|
53
- context 'when version is set to #{version{' do
54
- before do
55
- @options[:versions] = [ version ]
56
- end
57
-
58
- it 'is set' do
59
- status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json')
60
- env['api.format'].should eql 'json'
61
- status.should == 200
62
- end
63
-
64
- it 'is nil if not provided' do
65
- status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1')
66
- env['api.format'].should eql nil
67
- status.should == 200
68
- end
69
- end
70
- end
71
- end
72
-
73
- context 'api.vendor' do
74
- it 'is set' do
75
- status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor')
76
- env['api.vendor'].should eql 'vendor'
77
- status.should == 200
78
- end
79
-
80
- it 'is set if format provided' do
81
- status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor+json')
82
- env['api.vendor'].should eql 'vendor'
83
- status.should == 200
84
- end
85
-
86
- it 'fails with 406 Not Acceptable if vendor is invalid' do
87
- expect {
88
- env = subject.call('HTTP_ACCEPT' => 'application/vnd.othervendor+json').last
89
- }.to throw_symbol(
90
- :error,
91
- :status => 406,
92
- :headers => {'X-Cascade' => 'pass'},
93
- :message => 'API vendor or version not found.'
94
- )
95
- end
96
-
97
- context 'when version is set' do
98
- before do
99
- @options[:versions] = ['v1']
100
- end
101
-
102
- it 'is set' do
103
- status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1')
104
- env['api.vendor'].should eql 'vendor'
105
- status.should == 200
106
- end
107
-
108
- it 'is set if format provided' do
109
- status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json')
110
- env['api.vendor'].should eql 'vendor'
111
- status.should == 200
112
- end
113
-
114
- it 'fails with 406 Not Acceptable if vendor is invalid' do
115
- expect {
116
- env = subject.call('HTTP_ACCEPT' => 'application/vnd.othervendor-v1+json').last
117
- }.to throw_symbol(
118
- :error,
119
- :status => 406,
120
- :headers => {'X-Cascade' => 'pass'},
121
- :message => 'API vendor or version not found.'
122
- )
123
- end
124
- end
125
- end
126
-
127
- context 'api.version' do
128
- before do
129
- @options[:versions] = ['v1']
130
- end
131
-
132
- it 'is set' do
133
- status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1')
134
- env['api.version'].should eql 'v1'
135
- status.should == 200
136
- end
137
-
138
- it 'is set if format provided' do
139
- status, _, env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json')
140
- env['api.version'].should eql 'v1'
141
- status.should == 200
142
- end
143
-
144
- it 'fails with 406 Not Acceptable if version is invalid' do
145
- expect {
146
- env = subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v2+json').last
147
- }.to throw_symbol(
148
- :error,
149
- :status => 406,
150
- :headers => {'X-Cascade' => 'pass'},
151
- :message => 'API vendor or version not found.'
152
- )
153
- end
154
- end
155
-
156
- it 'succeeds if :strict is not set' do
157
- subject.call('HTTP_ACCEPT' => '').first.should == 200
158
- subject.call({}).first.should == 200
159
- end
160
-
161
- it 'succeeds if :strict is set to false' do
162
- @options[:version_options][:strict] = false
163
- subject.call('HTTP_ACCEPT' => '').first.should == 200
164
- subject.call({}).first.should == 200
165
- end
166
-
167
- context 'when :strict is set' do
168
- before do
169
- @options[:versions] = ['v1']
170
- @options[:version_options][:strict] = true
171
- end
172
-
173
- it 'fails with 406 Not Acceptable if header is not set' do
174
- expect {
175
- env = subject.call({}).last
176
- }.to throw_symbol(
177
- :error,
178
- :status => 406,
179
- :headers => {'X-Cascade' => 'pass'},
180
- :message => 'Accept header must be set.'
181
- )
182
- end
183
-
184
- it 'fails with 406 Not Acceptable if header is empty' do
185
- expect {
186
- env = subject.call('HTTP_ACCEPT' => '').last
187
- }.to throw_symbol(
188
- :error,
189
- :status => 406,
190
- :headers => {'X-Cascade' => 'pass'},
191
- :message => 'Accept header must be set.'
192
- )
193
- end
194
-
195
- it 'fails with 406 Not Acceptable if type is a range' do
196
- expect {
197
- env = subject.call('HTTP_ACCEPT' => '*/*').last
198
- }.to throw_symbol(
199
- :error,
200
- :status => 406,
201
- :headers => {'X-Cascade' => 'pass'},
202
- :message => 'Accept header must not contain ranges ("*").'
203
- )
204
- end
205
-
206
- it 'fails with 406 Not Acceptable if subtype is a range' do
207
- expect {
208
- env = subject.call('HTTP_ACCEPT' => 'application/*').last
209
- }.to throw_symbol(
210
- :error,
211
- :status => 406,
212
- :headers => {'X-Cascade' => 'pass'},
213
- :message => 'Accept header must not contain ranges ("*").'
214
- )
215
- end
216
-
217
- it 'succeeds if proper header is set' do
218
- subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json').first.should == 200
219
- end
220
- end
221
-
222
- context 'when :strict and :cascade=>false are set' do
223
- before do
224
- @options[:versions] = ['v1']
225
- @options[:version_options][:strict] = true
226
- @options[:version_options][:cascade] = false
227
- end
228
-
229
- it 'fails with 406 Not Acceptable if header is not set' do
230
- expect {
231
- env = subject.call({}).last
232
- }.to throw_symbol(
233
- :error,
234
- :status => 406,
235
- :headers => {},
236
- :message => 'Accept header must be set.'
237
- )
238
- end
239
-
240
- it 'fails with 406 Not Acceptable if header is empty' do
241
- expect {
242
- env = subject.call('HTTP_ACCEPT' => '').last
243
- }.to throw_symbol(
244
- :error,
245
- :status => 406,
246
- :headers => {},
247
- :message => 'Accept header must be set.'
248
- )
249
- end
250
-
251
- it 'fails with 406 Not Acceptable if type is a range' do
252
- expect {
253
- env = subject.call('HTTP_ACCEPT' => '*/*').last
254
- }.to throw_symbol(
255
- :error,
256
- :status => 406,
257
- :headers => {},
258
- :message => 'Accept header must not contain ranges ("*").'
259
- )
260
- end
261
-
262
- it 'fails with 406 Not Acceptable if subtype is a range' do
263
- expect {
264
- env = subject.call('HTTP_ACCEPT' => 'application/*').last
265
- }.to throw_symbol(
266
- :error,
267
- :status => 406,
268
- :headers => {},
269
- :message => 'Accept header must not contain ranges ("*").'
270
- )
271
- end
272
-
273
- it 'succeeds if proper header is set' do
274
- subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v1+json').first.should == 200
275
- end
276
- end
277
- end
@@ -1,58 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Grape::Middleware::Versioner::Param do
4
-
5
- let(:app) { lambda{|env| [200, env, env['api.version']]} }
6
- subject { Grape::Middleware::Versioner::Param.new(app, @options || {}) }
7
-
8
- it 'sets the API version based on the default param (apiver)' do
9
- env = Rack::MockRequest.env_for("/awesome", {:params => {"apiver" => "v1"}})
10
- subject.call(env)[1]["api.version"].should == 'v1'
11
- end
12
-
13
- it 'cuts (only) the version out of the params', :focus => true do
14
- env = Rack::MockRequest.env_for("/awesome", {:params => {"apiver" => "v1", "other_param" => "5"}})
15
- subject.call(env)[1]['rack.request.query_hash']["apiver"].should be_nil
16
- subject.call(env)[1]['rack.request.query_hash']["other_param"].should == "5"
17
- end
18
-
19
- it 'provides a nil version if no version is given' do
20
- env = Rack::MockRequest.env_for("/")
21
- subject.call(env).last.should be_nil
22
- end
23
-
24
- context 'with specified parameter name' do
25
- before{ @options = {:parameter => ['v']}}
26
- it 'sets the API version based on the custom parameter name' do
27
- env = Rack::MockRequest.env_for("/awesome", {:params => {"v" => "v1"}})
28
- s = subject.call(env)[1]["api.version"] == "v1"
29
- end
30
- it 'does not set the API version based on the default param' do
31
- env = Rack::MockRequest.env_for("/awesome", {:params => {"apiver" => "v1"}})
32
- s = subject.call(env)[1]["api.version"] == nil
33
- end
34
- end
35
-
36
- context 'with specified versions' do
37
- before{ @options = {:versions => ['v1', 'v2']}}
38
- it 'throws an error if a non-allowed version is specified' do
39
- env = Rack::MockRequest.env_for("/awesome", {:params => {"apiver" => "v3"}})
40
- catch(:error){subject.call(env)}[:status].should == 404
41
- end
42
-
43
- it 'allows versions that have been specified' do
44
- env = Rack::MockRequest.env_for("/awesome", {:params => {"apiver" => "v1"}})
45
- subject.call(env)[1]["api.version"].should == 'v1'
46
- end
47
- end
48
-
49
- it 'returns a 200 when no version is set (matches the first version found)' do
50
- @options = {
51
- :versions => ['v1'],
52
- :version_options => {:using => :header}
53
- }
54
- env = Rack::MockRequest.env_for("/awesome", {:params => {}})
55
- subject.call(env).first.should == 200
56
- end
57
-
58
- end
@@ -1,44 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Grape::Middleware::Versioner::Path do
4
- let(:app) { lambda{|env| [200, env, env['api.version']]} }
5
- subject { Grape::Middleware::Versioner::Path.new(app, @options || {}) }
6
-
7
- it 'sets the API version based on the first path' do
8
- subject.call('PATH_INFO' => '/v1/awesome').last.should == 'v1'
9
- end
10
-
11
- it 'does not cut the version out of the path' do
12
- subject.call('PATH_INFO' => '/v1/awesome')[1]['PATH_INFO'].should == '/v1/awesome'
13
- end
14
-
15
- it 'provides a nil version if no path is given' do
16
- subject.call('PATH_INFO' => '/').last.should be_nil
17
- end
18
-
19
- context 'with a pattern' do
20
- before { @options = {:pattern => /v./i} }
21
- it 'sets the version if it matches' do
22
- subject.call('PATH_INFO' => '/v1/awesome').last.should == 'v1'
23
- end
24
-
25
- it 'ignores the version if it fails to match' do
26
- subject.call('PATH_INFO' => '/awesome/radical').last.should be_nil
27
- end
28
- end
29
-
30
- [ [ 'v1', 'v2'], [ :v1, :v2 ], [ :v1, 'v2' ], [ 'v1', :v2 ] ].each do |versions|
31
- context 'with specified versions as #{versions}' do
32
- before { @options = { :versions => versions } }
33
-
34
- it 'throws an error if a non-allowed version is specified' do
35
- catch(:error){subject.call('PATH_INFO' => '/v3/awesome')}[:status].should == 404
36
- end
37
-
38
- it 'allows versions that have been specified' do
39
- subject.call('PATH_INFO' => '/v1/asoasd').last.should == 'v1'
40
- end
41
- end
42
- end
43
-
44
- end
@@ -1,22 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Grape::Middleware::Versioner do
4
-
5
- let(:klass) { Grape::Middleware::Versioner }
6
-
7
- it 'recognizes :path' do
8
- klass.using(:path).should == Grape::Middleware::Versioner::Path
9
- end
10
-
11
- it 'recognizes :header' do
12
- klass.using(:header).should == Grape::Middleware::Versioner::Header
13
- end
14
-
15
- it 'recognizes :param' do
16
- klass.using(:param).should == Grape::Middleware::Versioner::Param
17
- end
18
-
19
- it 'recognizes :accept_version_header' do
20
- klass.using(:accept_version_header).should == Grape::Middleware::Versioner::AcceptVersionHeader
21
- end
22
- end
@@ -1,133 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Grape::Util::HashStack do
4
- let(:klass){ Grape::Util::HashStack }
5
-
6
- describe '#get' do
7
- it 'finds the first available key' do
8
- subject[:abc] = 123
9
- subject.push(:abc => 345)
10
- subject.get(:abc).should == 345
11
- end
12
-
13
- it 'is nil if the key has not been set' do
14
- subject[:abc].should be_nil
15
- end
16
- end
17
-
18
- describe '#set' do
19
- it 'sets a value on the highest frame' do
20
- subject.push
21
- subject.set(:abc, 123)
22
- subject.stack.last[:abc].should == 123
23
- end
24
- end
25
-
26
- describe '#imbue' do
27
- it 'pushes a new value onto the end of an array' do
28
- subject[:abc] = []
29
- subject.imbue(:abc, [123])
30
- subject.imbue(:abc, [456])
31
- subject[:abc].should == [123, 456]
32
- end
33
-
34
- it 'merges a hash that is passed' do
35
- subject[:abc] = {:foo => 'bar'}
36
- subject.imbue(:abc, {:baz => 'wich'})
37
- subject[:abc].should == {:foo => 'bar', :baz => 'wich'}
38
- end
39
-
40
- it 'sets the value if not a hash or array' do
41
- subject.imbue(:abc, 123)
42
- subject[:abc].should == 123
43
- end
44
-
45
- it 'is able to imbue an array without explicit setting' do
46
- subject.imbue(:arr, [1])
47
- subject.imbue(:arr, [2])
48
- subject[:arr].should == [1,2]
49
- end
50
-
51
- it 'is able to imbue a hash without explicit setting' do
52
- subject.imbue(:hash, :foo => 'bar')
53
- subject.imbue(:hash, :baz => 'wich')
54
- subject[:hash].should == {:foo => 'bar', :baz => 'wich'}
55
- end
56
- end
57
-
58
- describe '#push' do
59
- it 'returns a HashStack' do
60
- subject.push(klass.new).should be_kind_of(klass)
61
- end
62
-
63
- it 'places the passed value on the top of the stack' do
64
- subject.push(:abc => 123)
65
- subject.stack.should == [{}, {:abc => 123}]
66
- end
67
-
68
- it 'pushes an empty hash by default' do
69
- subject[:abc] = 123
70
- subject.push
71
- subject.stack.should == [{:abc => 123}, {}]
72
- end
73
- end
74
-
75
- describe '#pop' do
76
- it 'removes and return the top frame' do
77
- subject.push(:abc => 123)
78
- subject.pop.should == {:abc => 123}
79
- subject.stack.size.should == 1
80
- end
81
- end
82
-
83
- describe '#peek' do
84
- it 'returns the top frame without removing it' do
85
- subject.push(:abc => 123)
86
- subject.peek.should == {:abc => 123}
87
- subject.stack.size.should == 2
88
- end
89
- end
90
-
91
- describe '#prepend' do
92
- it 'returns a HashStack' do
93
- subject.prepend(klass.new).should be_kind_of(klass)
94
- end
95
-
96
- it "prepends a HashStack's stack onto its own stack" do
97
- other = klass.new.push(:abc => 123)
98
- subject.prepend(other).stack.should == [{}, {:abc => 123}, {}]
99
- end
100
- end
101
-
102
- describe '#concat' do
103
- it 'returns a HashStack' do
104
- subject.concat(klass.new).should be_kind_of(klass)
105
- end
106
-
107
- it "appends a HashStack's stack onto its own stack" do
108
- other = klass.new.push(:abc => 123)
109
- subject.concat(other).stack.should == [{}, {}, {:abc => 123}]
110
- end
111
- end
112
-
113
- describe '#update' do
114
- it 'merges! into the top frame' do
115
- subject.update(:abc => 123)
116
- subject.stack.should == [{:abc => 123}]
117
- end
118
-
119
- it 'returns a HashStack' do
120
- subject.update(:abc => 123).should be_kind_of(klass)
121
- end
122
- end
123
-
124
- describe '#clone' do
125
- it 'performs a deep copy' do
126
- subject[:abc] = 123
127
- subject.push :def => 234
128
- clone = subject.clone
129
- clone[:def] = 345
130
- subject[:def].should == 234
131
- end
132
- end
133
- end
@@ -1,143 +0,0 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
3
-
4
- describe Grape::Validations::CoerceValidator do
5
- subject { Class.new(Grape::API) }
6
- def app; subject end
7
-
8
- describe 'coerce' do
9
- it "i18n error on malformed input" do
10
- I18n.load_path << File.expand_path('../zh-CN.yml',__FILE__)
11
- I18n.reload!
12
- I18n.locale = :'zh-CN'
13
- subject.params { requires :age, :type => Integer }
14
- subject.get '/single' do 'int works'; end
15
-
16
- get '/single', :age => '43a'
17
- last_response.status.should == 400
18
- last_response.body.should == '年龄格式不正确'
19
- I18n.locale = :en
20
-
21
- end
22
- it 'error on malformed input' do
23
- subject.params { requires :int, :type => Integer }
24
- subject.get '/single' do 'int works'; end
25
-
26
- get '/single', :int => '43a'
27
- last_response.status.should == 400
28
- last_response.body.should == 'invalid parameter: int'
29
-
30
- get '/single', :int => '43'
31
- last_response.status.should == 200
32
- last_response.body.should == 'int works'
33
- end
34
-
35
- it 'error on malformed input (Array)' do
36
- subject.params { requires :ids, :type => Array[Integer] }
37
- subject.get '/array' do 'array int works'; end
38
-
39
- get 'array', { :ids => ['1', '2', 'az'] }
40
- last_response.status.should == 400
41
- last_response.body.should == 'invalid parameter: ids'
42
-
43
- get 'array', { :ids => ['1', '2', '890'] }
44
- last_response.status.should == 200
45
- last_response.body.should == 'array int works'
46
- end
47
-
48
- context 'complex objects' do
49
- module CoerceValidatorSpec
50
- class User
51
- include Virtus
52
- attribute :id, Integer
53
- attribute :name, String
54
- end
55
- end
56
-
57
- it 'error on malformed input for complex objects' do
58
- subject.params { requires :user, :type => CoerceValidatorSpec::User }
59
- subject.get '/user' do 'complex works'; end
60
-
61
- get '/user', :user => "32"
62
- last_response.status.should == 400
63
- last_response.body.should == 'invalid parameter: user'
64
-
65
- get '/user', :user => { :id => 32, :name => 'Bob' }
66
- last_response.status.should == 200
67
- last_response.body.should == 'complex works'
68
- end
69
- end
70
-
71
- context 'coerces' do
72
- it 'Integer' do
73
- subject.params { requires :int, :coerce => Integer }
74
- subject.get '/int' do params[:int].class; end
75
-
76
- get '/int', { :int => "45" }
77
- last_response.status.should == 200
78
- last_response.body.should == 'Fixnum'
79
- end
80
-
81
- it 'Array of Integers' do
82
- subject.params { requires :arry, :coerce => Array[Integer] }
83
- subject.get '/array' do params[:arry][0].class; end
84
-
85
- get '/array', { :arry => [ '1', '2', '3' ] }
86
- last_response.status.should == 200
87
- last_response.body.should == 'Fixnum'
88
- end
89
-
90
- it 'Array of Bools' do
91
- subject.params { requires :arry, :coerce => Array[Virtus::Attribute::Boolean] }
92
- subject.get '/array' do params[:arry][0].class; end
93
-
94
- get 'array', { :arry => [1, 0] }
95
- last_response.status.should == 200
96
- last_response.body.should == 'TrueClass'
97
- end
98
-
99
- it 'Bool' do
100
- subject.params { requires :bool, :coerce => Virtus::Attribute::Boolean }
101
- subject.get '/bool' do params[:bool].class; end
102
-
103
- get '/bool', { :bool => 1 }
104
- last_response.status.should == 200
105
- last_response.body.should == 'TrueClass'
106
-
107
- get '/bool', { :bool => 0 }
108
- last_response.status.should == 200
109
- last_response.body.should == 'FalseClass'
110
-
111
- get '/bool', { :bool => 'false' }
112
- last_response.status.should == 200
113
- last_response.body.should == 'FalseClass'
114
-
115
- get '/bool', { :bool => 'true' }
116
- last_response.status.should == 200
117
- last_response.body.should == 'TrueClass'
118
- end
119
-
120
- it 'file' do
121
- subject.params { requires :file, :coerce => Rack::Multipart::UploadedFile }
122
- subject.post '/upload' do params[:file].filename; end
123
-
124
- post '/upload', { :file => Rack::Test::UploadedFile.new(__FILE__) }
125
- last_response.status.should == 201
126
- last_response.body.should == File.basename(__FILE__).to_s
127
- end
128
-
129
- it 'Nests integers' do
130
- subject.params do
131
- group :integers do
132
- requires :int, :coerce => Integer
133
- end
134
- end
135
- subject.get '/int' do params[:integers][:int].class; end
136
-
137
- get '/int', { :integers => { :int => "45" } }
138
- last_response.status.should == 200
139
- last_response.body.should == 'Fixnum'
140
- end
141
- end
142
- end
143
- end