grape 1.7.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +37 -1
- data/CONTRIBUTING.md +1 -1
- data/README.md +30 -25
- data/UPGRADING.md +35 -0
- data/grape.gemspec +3 -6
- data/lib/grape/api.rb +2 -2
- data/lib/grape/content_types.rb +2 -8
- data/lib/grape/dsl/desc.rb +1 -1
- data/lib/grape/dsl/inside_route.rb +11 -11
- data/lib/grape/dsl/request_response.rb +2 -1
- data/lib/grape/dsl/settings.rb +2 -6
- data/lib/grape/endpoint.rb +28 -18
- data/lib/grape/error_formatter/base.rb +1 -1
- data/lib/grape/exceptions/base.rb +2 -2
- data/lib/grape/exceptions/missing_group_type.rb +1 -6
- data/lib/grape/exceptions/unsupported_group_type.rb +1 -6
- data/lib/grape/exceptions/validation_errors.rb +1 -6
- data/lib/grape/extensions/active_support/hash_with_indifferent_access.rb +3 -3
- data/lib/grape/extensions/hash.rb +4 -7
- data/lib/grape/extensions/hashie/mash.rb +3 -3
- data/lib/grape/formatter/serializable_hash.rb +7 -7
- data/lib/grape/http/headers.rb +12 -2
- data/lib/grape/middleware/auth/base.rb +1 -1
- data/lib/grape/middleware/auth/strategies.rb +1 -2
- data/lib/grape/middleware/error.rb +5 -5
- data/lib/grape/middleware/formatter.rb +6 -6
- data/lib/grape/middleware/versioner/header.rb +11 -19
- data/lib/grape/railtie.rb +9 -0
- data/lib/grape/request.rb +8 -2
- data/lib/grape/router/route.rb +1 -3
- data/lib/grape/util/lazy_value.rb +3 -11
- data/lib/grape/util/strict_hash_configuration.rb +3 -4
- data/lib/grape/validations/multiple_attributes_iterator.rb +1 -1
- data/lib/grape/validations/params_scope.rb +8 -2
- data/lib/grape/validations/single_attribute_iterator.rb +3 -1
- data/lib/grape/validations/types/custom_type_coercer.rb +2 -16
- data/lib/grape/validations/validators/base.rb +9 -20
- data/lib/grape/validations/validators/default_validator.rb +2 -20
- data/lib/grape/validations/validators/multiple_params_base.rb +4 -8
- data/lib/grape/validations/validators/values_validator.rb +14 -5
- data/lib/grape/version.rb +1 -1
- data/lib/grape.rb +26 -5
- metadata +13 -253
- data/lib/grape/config.rb +0 -34
- data/lib/grape/extensions/deep_mergeable_hash.rb +0 -21
- data/lib/grape/extensions/deep_symbolize_hash.rb +0 -32
- data/spec/grape/api/custom_validations_spec.rb +0 -256
- data/spec/grape/api/deeply_included_options_spec.rb +0 -56
- data/spec/grape/api/defines_boolean_in_params_spec.rb +0 -38
- data/spec/grape/api/documentation_spec.rb +0 -59
- data/spec/grape/api/inherited_helpers_spec.rb +0 -114
- data/spec/grape/api/instance_spec.rb +0 -103
- data/spec/grape/api/invalid_format_spec.rb +0 -45
- data/spec/grape/api/namespace_parameters_in_route_spec.rb +0 -38
- data/spec/grape/api/nested_helpers_spec.rb +0 -50
- data/spec/grape/api/optional_parameters_in_route_spec.rb +0 -43
- data/spec/grape/api/parameters_modification_spec.rb +0 -41
- data/spec/grape/api/patch_method_helpers_spec.rb +0 -79
- data/spec/grape/api/recognize_path_spec.rb +0 -21
- data/spec/grape/api/required_parameters_in_route_spec.rb +0 -37
- data/spec/grape/api/required_parameters_with_invalid_method_spec.rb +0 -26
- data/spec/grape/api/routes_with_requirements_spec.rb +0 -59
- data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +0 -41
- data/spec/grape/api/shared_helpers_spec.rb +0 -36
- data/spec/grape/api_remount_spec.rb +0 -473
- data/spec/grape/api_spec.rb +0 -4347
- data/spec/grape/config_spec.rb +0 -17
- data/spec/grape/dsl/callbacks_spec.rb +0 -45
- data/spec/grape/dsl/desc_spec.rb +0 -101
- data/spec/grape/dsl/headers_spec.rb +0 -62
- data/spec/grape/dsl/helpers_spec.rb +0 -100
- data/spec/grape/dsl/inside_route_spec.rb +0 -535
- data/spec/grape/dsl/logger_spec.rb +0 -24
- data/spec/grape/dsl/middleware_spec.rb +0 -60
- data/spec/grape/dsl/parameters_spec.rb +0 -180
- data/spec/grape/dsl/request_response_spec.rb +0 -206
- data/spec/grape/dsl/routing_spec.rb +0 -275
- data/spec/grape/dsl/settings_spec.rb +0 -261
- data/spec/grape/dsl/validations_spec.rb +0 -55
- data/spec/grape/endpoint/declared_spec.rb +0 -846
- data/spec/grape/endpoint_spec.rb +0 -1085
- data/spec/grape/entity_spec.rb +0 -336
- data/spec/grape/exceptions/base_spec.rb +0 -81
- data/spec/grape/exceptions/body_parse_errors_spec.rb +0 -145
- data/spec/grape/exceptions/invalid_accept_header_spec.rb +0 -358
- data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -15
- data/spec/grape/exceptions/invalid_response_spec.rb +0 -11
- data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -15
- data/spec/grape/exceptions/missing_group_type_spec.rb +0 -21
- data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -17
- data/spec/grape/exceptions/missing_option_spec.rb +0 -15
- data/spec/grape/exceptions/unknown_options_spec.rb +0 -15
- data/spec/grape/exceptions/unknown_validator_spec.rb +0 -15
- data/spec/grape/exceptions/unsupported_group_type_spec.rb +0 -23
- data/spec/grape/exceptions/validation_errors_spec.rb +0 -92
- data/spec/grape/exceptions/validation_spec.rb +0 -19
- data/spec/grape/extensions/param_builders/hash_spec.rb +0 -83
- data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +0 -105
- data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +0 -79
- data/spec/grape/integration/global_namespace_function_spec.rb +0 -29
- data/spec/grape/integration/rack_sendfile_spec.rb +0 -48
- data/spec/grape/integration/rack_spec.rb +0 -51
- data/spec/grape/loading_spec.rb +0 -44
- data/spec/grape/middleware/auth/base_spec.rb +0 -31
- data/spec/grape/middleware/auth/dsl_spec.rb +0 -60
- data/spec/grape/middleware/auth/strategies_spec.rb +0 -120
- data/spec/grape/middleware/base_spec.rb +0 -221
- data/spec/grape/middleware/error_spec.rb +0 -85
- data/spec/grape/middleware/exception_spec.rb +0 -294
- data/spec/grape/middleware/formatter_spec.rb +0 -461
- data/spec/grape/middleware/globals_spec.rb +0 -30
- data/spec/grape/middleware/stack_spec.rb +0 -155
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -122
- data/spec/grape/middleware/versioner/header_spec.rb +0 -345
- data/spec/grape/middleware/versioner/param_spec.rb +0 -171
- data/spec/grape/middleware/versioner/path_spec.rb +0 -62
- data/spec/grape/middleware/versioner_spec.rb +0 -21
- data/spec/grape/named_api_spec.rb +0 -19
- data/spec/grape/parser_spec.rb +0 -86
- data/spec/grape/path_spec.rb +0 -252
- data/spec/grape/presenters/presenter_spec.rb +0 -71
- data/spec/grape/request_spec.rb +0 -136
- data/spec/grape/util/inheritable_setting_spec.rb +0 -242
- data/spec/grape/util/inheritable_values_spec.rb +0 -79
- data/spec/grape/util/reverse_stackable_values_spec.rb +0 -134
- data/spec/grape/util/stackable_values_spec.rb +0 -128
- data/spec/grape/util/strict_hash_configuration_spec.rb +0 -38
- data/spec/grape/validations/attributes_doc_spec.rb +0 -153
- data/spec/grape/validations/instance_behaivour_spec.rb +0 -43
- data/spec/grape/validations/multiple_attributes_iterator_spec.rb +0 -40
- data/spec/grape/validations/params_scope_spec.rb +0 -1420
- data/spec/grape/validations/single_attribute_iterator_spec.rb +0 -57
- data/spec/grape/validations/types/array_coercer_spec.rb +0 -33
- data/spec/grape/validations/types/primitive_coercer_spec.rb +0 -150
- data/spec/grape/validations/types/set_coercer_spec.rb +0 -32
- data/spec/grape/validations/types_spec.rb +0 -111
- data/spec/grape/validations/validators/all_or_none_spec.rb +0 -162
- data/spec/grape/validations/validators/allow_blank_spec.rb +0 -575
- data/spec/grape/validations/validators/at_least_one_of_spec.rb +0 -205
- data/spec/grape/validations/validators/coerce_spec.rb +0 -1261
- data/spec/grape/validations/validators/default_spec.rb +0 -463
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +0 -233
- data/spec/grape/validations/validators/except_values_spec.rb +0 -192
- data/spec/grape/validations/validators/mutual_exclusion_spec.rb +0 -214
- data/spec/grape/validations/validators/presence_spec.rb +0 -315
- data/spec/grape/validations/validators/regexp_spec.rb +0 -161
- data/spec/grape/validations/validators/same_as_spec.rb +0 -57
- data/spec/grape/validations/validators/values_spec.rb +0 -696
- data/spec/grape/validations/validators/zh-CN.yml +0 -10
- data/spec/grape/validations_spec.rb +0 -2029
- data/spec/integration/eager_load/eager_load_spec.rb +0 -15
- data/spec/integration/multi_json/json_spec.rb +0 -7
- data/spec/integration/multi_xml/xml_spec.rb +0 -7
- data/spec/shared/versioning_examples.rb +0 -215
- data/spec/spec_helper.rb +0 -52
- data/spec/support/basic_auth_encode_helpers.rb +0 -11
- data/spec/support/chunks.rb +0 -14
- data/spec/support/content_type_helpers.rb +0 -15
- data/spec/support/endpoint_faker.rb +0 -25
- data/spec/support/file_streamer.rb +0 -13
- data/spec/support/integer_helpers.rb +0 -13
- data/spec/support/versioned_helpers.rb +0 -55
@@ -1,535 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Grape
|
4
|
-
module DSL
|
5
|
-
module InsideRouteSpec
|
6
|
-
class Dummy
|
7
|
-
include Grape::DSL::InsideRoute
|
8
|
-
|
9
|
-
attr_reader :env, :request, :new_settings
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
@env = {}
|
13
|
-
@header = {}
|
14
|
-
@new_settings = { namespace_inheritable: {}, namespace_stackable: {} }
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
describe Grape::Endpoint do
|
22
|
-
subject { Grape::DSL::InsideRouteSpec::Dummy.new }
|
23
|
-
|
24
|
-
describe '#version' do
|
25
|
-
it 'defaults to nil' do
|
26
|
-
expect(subject.version).to be_nil
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'returns env[api.version]' do
|
30
|
-
subject.env['api.version'] = 'dummy'
|
31
|
-
expect(subject.version).to eq 'dummy'
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe '#error!' do
|
36
|
-
it 'throws :error' do
|
37
|
-
expect { subject.error! 'Not Found', 404 }.to throw_symbol(:error)
|
38
|
-
end
|
39
|
-
|
40
|
-
describe 'thrown' do
|
41
|
-
before do
|
42
|
-
catch(:error) { subject.error! 'Not Found', 404 }
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'sets status' do
|
46
|
-
expect(subject.status).to eq 404
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
describe 'default_error_status' do
|
51
|
-
before do
|
52
|
-
subject.namespace_inheritable(:default_error_status, 500)
|
53
|
-
catch(:error) { subject.error! 'Unknown' }
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'sets status to default_error_status' do
|
57
|
-
expect(subject.status).to eq 500
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
# self.status(status || settings[:default_error_status])
|
62
|
-
# throw :error, message: message, status: self.status, headers: headers
|
63
|
-
end
|
64
|
-
|
65
|
-
describe '#redirect' do
|
66
|
-
describe 'default' do
|
67
|
-
before do
|
68
|
-
subject.redirect '/'
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'sets status to 302' do
|
72
|
-
expect(subject.status).to eq 302
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'sets location header' do
|
76
|
-
expect(subject.header['Location']).to eq '/'
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
describe 'permanent' do
|
81
|
-
before do
|
82
|
-
subject.redirect '/', permanent: true
|
83
|
-
end
|
84
|
-
|
85
|
-
it 'sets status to 301' do
|
86
|
-
expect(subject.status).to eq 301
|
87
|
-
end
|
88
|
-
|
89
|
-
it 'sets location header' do
|
90
|
-
expect(subject.header['Location']).to eq '/'
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
describe '#status' do
|
96
|
-
%w[GET PUT OPTIONS].each do |method|
|
97
|
-
it 'defaults to 200 on GET' do
|
98
|
-
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: method))
|
99
|
-
expect(subject).to receive(:request).and_return(request)
|
100
|
-
expect(subject.status).to eq 200
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
it 'defaults to 201 on POST' do
|
105
|
-
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'POST'))
|
106
|
-
expect(subject).to receive(:request).and_return(request)
|
107
|
-
expect(subject.status).to eq 201
|
108
|
-
end
|
109
|
-
|
110
|
-
it 'defaults to 204 on DELETE' do
|
111
|
-
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
|
112
|
-
expect(subject).to receive(:request).and_return(request)
|
113
|
-
expect(subject.status).to eq 204
|
114
|
-
end
|
115
|
-
|
116
|
-
it 'defaults to 200 on DELETE with a body present' do
|
117
|
-
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
|
118
|
-
subject.body 'content here'
|
119
|
-
expect(subject).to receive(:request).and_return(request)
|
120
|
-
expect(subject.status).to eq 200
|
121
|
-
end
|
122
|
-
|
123
|
-
it 'returns status set' do
|
124
|
-
subject.status 501
|
125
|
-
expect(subject.status).to eq 501
|
126
|
-
end
|
127
|
-
|
128
|
-
it 'accepts symbol for status' do
|
129
|
-
subject.status :see_other
|
130
|
-
expect(subject.status).to eq 303
|
131
|
-
end
|
132
|
-
|
133
|
-
it 'raises error if unknow symbol is passed' do
|
134
|
-
expect { subject.status :foo_bar }
|
135
|
-
.to raise_error(ArgumentError, 'Status code :foo_bar is invalid.')
|
136
|
-
end
|
137
|
-
|
138
|
-
it 'accepts unknown Integer status codes' do
|
139
|
-
expect { subject.status 210 }.not_to raise_error
|
140
|
-
end
|
141
|
-
|
142
|
-
it 'raises error if status is not a integer or symbol' do
|
143
|
-
expect { subject.status Object.new }
|
144
|
-
.to raise_error(ArgumentError, 'Status code must be Integer or Symbol.')
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
describe '#return_no_content' do
|
149
|
-
it 'sets the status code and body' do
|
150
|
-
subject.return_no_content
|
151
|
-
expect(subject.status).to eq 204
|
152
|
-
expect(subject.body).to eq ''
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
describe '#content_type' do
|
157
|
-
describe 'set' do
|
158
|
-
before do
|
159
|
-
subject.content_type 'text/plain'
|
160
|
-
end
|
161
|
-
|
162
|
-
it 'returns value' do
|
163
|
-
expect(subject.content_type).to eq 'text/plain'
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
it 'returns default' do
|
168
|
-
expect(subject.content_type).to be_nil
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
describe '#cookies' do
|
173
|
-
it 'returns an instance of Cookies' do
|
174
|
-
expect(subject.cookies).to be_a Grape::Cookies
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
describe '#body' do
|
179
|
-
describe 'set' do
|
180
|
-
before do
|
181
|
-
subject.body 'body'
|
182
|
-
end
|
183
|
-
|
184
|
-
it 'returns value' do
|
185
|
-
expect(subject.body).to eq 'body'
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
describe 'false' do
|
190
|
-
before do
|
191
|
-
subject.body false
|
192
|
-
end
|
193
|
-
|
194
|
-
it 'sets status to 204' do
|
195
|
-
expect(subject.body).to eq ''
|
196
|
-
expect(subject.status).to eq 204
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
it 'returns default' do
|
201
|
-
expect(subject.body).to be_nil
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
describe '#file' do
|
206
|
-
before do
|
207
|
-
allow(subject).to receive(:warn)
|
208
|
-
end
|
209
|
-
|
210
|
-
describe 'set' do
|
211
|
-
context 'as file path' do
|
212
|
-
let(:file_path) { '/some/file/path' }
|
213
|
-
|
214
|
-
it 'emits a warning that this method is deprecated' do
|
215
|
-
expect(subject).to receive(:warn).with(/Use sendfile or stream/)
|
216
|
-
|
217
|
-
subject.file file_path
|
218
|
-
end
|
219
|
-
|
220
|
-
it 'forwards the call to sendfile' do
|
221
|
-
expect(subject).to receive(:sendfile).with(file_path)
|
222
|
-
|
223
|
-
subject.file file_path
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
context 'as object (backward compatibility)' do
|
228
|
-
let(:file_object) { double('StreamerObject', each: nil) }
|
229
|
-
|
230
|
-
it 'emits a warning that this method is deprecated' do
|
231
|
-
expect(subject).to receive(:warn).with(/Use stream to use a Stream object/)
|
232
|
-
|
233
|
-
subject.file file_object
|
234
|
-
end
|
235
|
-
|
236
|
-
it 'forwards the call to stream' do
|
237
|
-
expect(subject).to receive(:stream).with(file_object)
|
238
|
-
|
239
|
-
subject.file file_object
|
240
|
-
end
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
|
-
describe 'get' do
|
245
|
-
it 'emits a warning that this method is deprecated' do
|
246
|
-
expect(subject).to receive(:warn).with(/Use sendfile or stream/)
|
247
|
-
|
248
|
-
subject.file
|
249
|
-
end
|
250
|
-
|
251
|
-
it 'fowards call to sendfile' do
|
252
|
-
expect(subject).to receive(:sendfile)
|
253
|
-
|
254
|
-
subject.file
|
255
|
-
end
|
256
|
-
end
|
257
|
-
end
|
258
|
-
|
259
|
-
describe '#sendfile' do
|
260
|
-
describe 'set' do
|
261
|
-
context 'as file path' do
|
262
|
-
let(:file_path) { '/some/file/path' }
|
263
|
-
|
264
|
-
let(:file_response) do
|
265
|
-
file_body = Grape::ServeStream::FileBody.new(file_path)
|
266
|
-
Grape::ServeStream::StreamResponse.new(file_body)
|
267
|
-
end
|
268
|
-
|
269
|
-
before do
|
270
|
-
subject.header 'Cache-Control', 'cache'
|
271
|
-
subject.header 'Content-Length', 123
|
272
|
-
subject.header 'Transfer-Encoding', 'base64'
|
273
|
-
end
|
274
|
-
|
275
|
-
it 'sends no deprecation warnings' do
|
276
|
-
expect(subject).not_to receive(:warn)
|
277
|
-
|
278
|
-
subject.sendfile file_path
|
279
|
-
end
|
280
|
-
|
281
|
-
it 'returns value wrapped in StreamResponse' do
|
282
|
-
subject.sendfile file_path
|
283
|
-
|
284
|
-
expect(subject.sendfile).to eq file_response
|
285
|
-
end
|
286
|
-
|
287
|
-
it 'does not change the Cache-Control header' do
|
288
|
-
subject.sendfile file_path
|
289
|
-
|
290
|
-
expect(subject.header['Cache-Control']).to eq 'cache'
|
291
|
-
end
|
292
|
-
|
293
|
-
it 'does not change the Content-Length header' do
|
294
|
-
subject.sendfile file_path
|
295
|
-
|
296
|
-
expect(subject.header['Content-Length']).to eq 123
|
297
|
-
end
|
298
|
-
|
299
|
-
it 'does not change the Transfer-Encoding header' do
|
300
|
-
subject.sendfile file_path
|
301
|
-
|
302
|
-
expect(subject.header['Transfer-Encoding']).to eq 'base64'
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
context 'as object' do
|
307
|
-
let(:file_object) { double('StreamerObject', each: nil) }
|
308
|
-
|
309
|
-
it 'raises an error that only a file path is supported' do
|
310
|
-
expect { subject.sendfile file_object }.to raise_error(ArgumentError, /Argument must be a file path/)
|
311
|
-
end
|
312
|
-
end
|
313
|
-
end
|
314
|
-
|
315
|
-
it 'returns default' do
|
316
|
-
expect(subject.sendfile).to be_nil
|
317
|
-
end
|
318
|
-
end
|
319
|
-
|
320
|
-
describe '#stream' do
|
321
|
-
describe 'set' do
|
322
|
-
context 'as a file path' do
|
323
|
-
let(:file_path) { '/some/file/path' }
|
324
|
-
|
325
|
-
let(:file_response) do
|
326
|
-
file_body = Grape::ServeStream::FileBody.new(file_path)
|
327
|
-
Grape::ServeStream::StreamResponse.new(file_body)
|
328
|
-
end
|
329
|
-
|
330
|
-
before do
|
331
|
-
subject.header 'Cache-Control', 'cache'
|
332
|
-
subject.header 'Content-Length', 123
|
333
|
-
subject.header 'Transfer-Encoding', 'base64'
|
334
|
-
end
|
335
|
-
|
336
|
-
it 'emits no deprecation warnings' do
|
337
|
-
expect(subject).not_to receive(:warn)
|
338
|
-
|
339
|
-
subject.stream file_path
|
340
|
-
end
|
341
|
-
|
342
|
-
it 'returns file body wrapped in StreamResponse' do
|
343
|
-
subject.stream file_path
|
344
|
-
|
345
|
-
expect(subject.stream).to eq file_response
|
346
|
-
end
|
347
|
-
|
348
|
-
it 'sets Cache-Control header to no-cache' do
|
349
|
-
subject.stream file_path
|
350
|
-
|
351
|
-
expect(subject.header['Cache-Control']).to eq 'no-cache'
|
352
|
-
end
|
353
|
-
|
354
|
-
it 'does not change Cache-Control header' do
|
355
|
-
subject.stream
|
356
|
-
|
357
|
-
expect(subject.header['Cache-Control']).to eq 'cache'
|
358
|
-
end
|
359
|
-
|
360
|
-
it 'sets Content-Length header to nil' do
|
361
|
-
subject.stream file_path
|
362
|
-
|
363
|
-
expect(subject.header['Content-Length']).to be_nil
|
364
|
-
end
|
365
|
-
|
366
|
-
it 'sets Transfer-Encoding header to nil' do
|
367
|
-
subject.stream file_path
|
368
|
-
|
369
|
-
expect(subject.header['Transfer-Encoding']).to be_nil
|
370
|
-
end
|
371
|
-
end
|
372
|
-
|
373
|
-
context 'as a stream object' do
|
374
|
-
let(:stream_object) { double('StreamerObject', each: nil) }
|
375
|
-
|
376
|
-
let(:stream_response) do
|
377
|
-
Grape::ServeStream::StreamResponse.new(stream_object)
|
378
|
-
end
|
379
|
-
|
380
|
-
before do
|
381
|
-
subject.header 'Cache-Control', 'cache'
|
382
|
-
subject.header 'Content-Length', 123
|
383
|
-
subject.header 'Transfer-Encoding', 'base64'
|
384
|
-
end
|
385
|
-
|
386
|
-
it 'emits no deprecation warnings' do
|
387
|
-
expect(subject).not_to receive(:warn)
|
388
|
-
|
389
|
-
subject.stream stream_object
|
390
|
-
end
|
391
|
-
|
392
|
-
it 'returns value wrapped in StreamResponse' do
|
393
|
-
subject.stream stream_object
|
394
|
-
|
395
|
-
expect(subject.stream).to eq stream_response
|
396
|
-
end
|
397
|
-
|
398
|
-
it 'sets Cache-Control header to no-cache' do
|
399
|
-
subject.stream stream_object
|
400
|
-
|
401
|
-
expect(subject.header['Cache-Control']).to eq 'no-cache'
|
402
|
-
end
|
403
|
-
|
404
|
-
it 'sets Content-Length header to nil' do
|
405
|
-
subject.stream stream_object
|
406
|
-
|
407
|
-
expect(subject.header['Content-Length']).to be_nil
|
408
|
-
end
|
409
|
-
|
410
|
-
it 'sets Transfer-Encoding header to nil' do
|
411
|
-
subject.stream stream_object
|
412
|
-
|
413
|
-
expect(subject.header['Transfer-Encoding']).to be_nil
|
414
|
-
end
|
415
|
-
end
|
416
|
-
|
417
|
-
context 'as a non-stream object' do
|
418
|
-
let(:non_stream_object) { double('NonStreamerObject') }
|
419
|
-
|
420
|
-
it 'raises an error that the object must implement :each' do
|
421
|
-
expect { subject.stream non_stream_object }.to raise_error(ArgumentError, /:each/)
|
422
|
-
end
|
423
|
-
end
|
424
|
-
end
|
425
|
-
|
426
|
-
it 'returns default' do
|
427
|
-
expect(subject.stream).to be_nil
|
428
|
-
expect(subject.header['Cache-Control']).to be_nil
|
429
|
-
end
|
430
|
-
end
|
431
|
-
|
432
|
-
describe '#route' do
|
433
|
-
before do
|
434
|
-
subject.env['grape.routing_args'] = {}
|
435
|
-
subject.env['grape.routing_args'][:route_info] = 'dummy'
|
436
|
-
end
|
437
|
-
|
438
|
-
it 'returns route_info' do
|
439
|
-
expect(subject.route).to eq 'dummy'
|
440
|
-
end
|
441
|
-
end
|
442
|
-
|
443
|
-
describe '#present' do
|
444
|
-
# see entity_spec.rb for entity representation spec coverage
|
445
|
-
|
446
|
-
describe 'dummy' do
|
447
|
-
before do
|
448
|
-
subject.present 'dummy'
|
449
|
-
end
|
450
|
-
|
451
|
-
it 'presents dummy object' do
|
452
|
-
expect(subject.body).to eq 'dummy'
|
453
|
-
end
|
454
|
-
end
|
455
|
-
|
456
|
-
describe 'with' do
|
457
|
-
describe 'entity' do
|
458
|
-
let(:entity_mock) do
|
459
|
-
entity_mock = Object.new
|
460
|
-
allow(entity_mock).to receive(:represent).and_return('dummy')
|
461
|
-
entity_mock
|
462
|
-
end
|
463
|
-
|
464
|
-
describe 'instance' do
|
465
|
-
before do
|
466
|
-
subject.present 'dummy', with: entity_mock
|
467
|
-
end
|
468
|
-
|
469
|
-
it 'presents dummy object' do
|
470
|
-
expect(subject.body).to eq 'dummy'
|
471
|
-
end
|
472
|
-
end
|
473
|
-
end
|
474
|
-
end
|
475
|
-
|
476
|
-
describe 'multiple entities' do
|
477
|
-
let(:entity_mock_one) do
|
478
|
-
entity_mock_one = Object.new
|
479
|
-
allow(entity_mock_one).to receive(:represent).and_return(dummy1: 'dummy1')
|
480
|
-
entity_mock_one
|
481
|
-
end
|
482
|
-
|
483
|
-
let(:entity_mock_two) do
|
484
|
-
entity_mock_two = Object.new
|
485
|
-
allow(entity_mock_two).to receive(:represent).and_return(dummy2: 'dummy2')
|
486
|
-
entity_mock_two
|
487
|
-
end
|
488
|
-
|
489
|
-
describe 'instance' do
|
490
|
-
before do
|
491
|
-
subject.present 'dummy1', with: entity_mock_one
|
492
|
-
subject.present 'dummy2', with: entity_mock_two
|
493
|
-
end
|
494
|
-
|
495
|
-
it 'presents both dummy objects' do
|
496
|
-
expect(subject.body[:dummy1]).to eq 'dummy1'
|
497
|
-
expect(subject.body[:dummy2]).to eq 'dummy2'
|
498
|
-
end
|
499
|
-
end
|
500
|
-
end
|
501
|
-
|
502
|
-
describe 'non mergeable entity' do
|
503
|
-
let(:entity_mock_one) do
|
504
|
-
entity_mock_one = Object.new
|
505
|
-
allow(entity_mock_one).to receive(:represent).and_return(dummy1: 'dummy1')
|
506
|
-
entity_mock_one
|
507
|
-
end
|
508
|
-
|
509
|
-
let(:entity_mock_two) do
|
510
|
-
entity_mock_two = Object.new
|
511
|
-
allow(entity_mock_two).to receive(:represent).and_return('not a hash')
|
512
|
-
entity_mock_two
|
513
|
-
end
|
514
|
-
|
515
|
-
describe 'instance' do
|
516
|
-
it 'fails' do
|
517
|
-
subject.present 'dummy1', with: entity_mock_one
|
518
|
-
expect do
|
519
|
-
subject.present 'dummy2', with: entity_mock_two
|
520
|
-
end.to raise_error ArgumentError, 'Representation of type String cannot be merged.'
|
521
|
-
end
|
522
|
-
end
|
523
|
-
end
|
524
|
-
end
|
525
|
-
|
526
|
-
describe '#declared' do
|
527
|
-
# see endpoint_spec.rb#declared for spec coverage
|
528
|
-
|
529
|
-
it 'is not available by default' do
|
530
|
-
expect { subject.declared({}) }.to raise_error(
|
531
|
-
Grape::DSL::InsideRoute::MethodNotYetAvailable
|
532
|
-
)
|
533
|
-
end
|
534
|
-
end
|
535
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe Grape::DSL::Logger do
|
4
|
-
subject { Class.new(dummy_logger) }
|
5
|
-
|
6
|
-
let(:dummy_logger) do
|
7
|
-
Class.new do
|
8
|
-
extend Grape::DSL::Logger
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
let(:logger) { instance_double(::Logger) }
|
13
|
-
|
14
|
-
describe '.logger' do
|
15
|
-
it 'sets a logger' do
|
16
|
-
subject.logger logger
|
17
|
-
expect(subject.logger).to eq logger
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'returns a logger' do
|
21
|
-
expect(subject.logger(logger)).to eq logger
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Grape
|
4
|
-
module DSL
|
5
|
-
module MiddlewareSpec
|
6
|
-
class Dummy
|
7
|
-
include Grape::DSL::Middleware
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
describe Middleware do
|
12
|
-
subject { Class.new(MiddlewareSpec::Dummy) }
|
13
|
-
|
14
|
-
let(:proc) { -> {} }
|
15
|
-
let(:foo_middleware) { Class.new }
|
16
|
-
let(:bar_middleware) { Class.new }
|
17
|
-
|
18
|
-
describe '.use' do
|
19
|
-
it 'adds a middleware with the right operation' do
|
20
|
-
expect(subject).to receive(:namespace_stackable).with(:middleware, [:use, foo_middleware, :arg1, proc])
|
21
|
-
|
22
|
-
subject.use foo_middleware, :arg1, &proc
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe '.insert' do
|
27
|
-
it 'adds a middleware with the right operation' do
|
28
|
-
expect(subject).to receive(:namespace_stackable).with(:middleware, [:insert, 0, :arg1, proc])
|
29
|
-
|
30
|
-
subject.insert 0, :arg1, &proc
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
describe '.insert_before' do
|
35
|
-
it 'adds a middleware with the right operation' do
|
36
|
-
expect(subject).to receive(:namespace_stackable).with(:middleware, [:insert_before, foo_middleware, :arg1, proc])
|
37
|
-
|
38
|
-
subject.insert_before foo_middleware, :arg1, &proc
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe '.insert_after' do
|
43
|
-
it 'adds a middleware with the right operation' do
|
44
|
-
expect(subject).to receive(:namespace_stackable).with(:middleware, [:insert_after, foo_middleware, :arg1, proc])
|
45
|
-
|
46
|
-
subject.insert_after foo_middleware, :arg1, &proc
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
describe '.middleware' do
|
51
|
-
it 'returns the middleware stack' do
|
52
|
-
subject.use foo_middleware, :arg1, &proc
|
53
|
-
subject.insert_before bar_middleware, :arg1, :arg2
|
54
|
-
|
55
|
-
expect(subject.middleware).to eq [[:use, foo_middleware, :arg1, proc], [:insert_before, bar_middleware, :arg1, :arg2]]
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|