@@ -203,6 +203,60 @@ def initialize
203
203
end
204
204
205
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
206
260
describe 'set' do
207
261
context 'as file path' do
208
262
let ( :file_path ) { '/some/file/path' }
@@ -216,60 +270,56 @@ def initialize
216
270
subject . header 'Cache-Control' , 'cache'
217
271
subject . header 'Content-Length' , 123
218
272
subject . header 'Transfer-Encoding' , 'base64'
219
-
220
- subject . file file_path
221
273
end
222
274
223
- it 'returns value wrapped in StreamResponse' do
224
- expect ( subject . file ) . to eq file_response
225
- end
275
+ it 'sends no deprecation warnings' do
276
+ expect ( subject ) . to_not receive ( :warn )
226
277
227
- it 'sets Cache-Control header to no-cache' do
228
- expect ( subject . header [ 'Cache-Control' ] ) . to eq 'no-cache'
278
+ subject . sendfile file_path
229
279
end
230
280
231
- it 'sets Content-Length header to nil' do
232
- expect ( subject . header [ 'Content-Length' ] ) . to eq nil
233
- end
281
+ it 'returns value wrapped in StreamResponse' do
282
+ subject . sendfile file_path
234
283
235
- it 'sets Transfer-Encoding header to nil' do
236
- expect ( subject . header [ 'Transfer-Encoding' ] ) . to eq nil
284
+ expect ( subject . sendfile ) . to eq file_response
237
285
end
238
- end
239
286
240
- context 'as object (backward compatibility) ' do
241
- let ( :file_object ) { double ( 'StreamerObject' , each : nil ) }
287
+ it 'does not change the Cache-Control header ' do
288
+ subject . sendfile file_path
242
289
243
- let ( :file_response ) do
244
- Grape ::ServeStream ::StreamResponse . new ( file_object )
290
+ expect ( subject . header [ 'Cache-Control' ] ) . to eq 'cache'
245
291
end
246
292
247
- before do
248
- allow ( subject ) . to receive ( :warn )
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
249
297
end
250
298
251
- it 'emits a warning that a stream object should be sent to the stream method ' do
252
- expect ( subject ) . to receive ( :warn ) . with ( /Argument as FileStreamer-like/ )
299
+ it 'does not change the Transfer-Encoding header ' do
300
+ subject . sendfile file_path
253
301
254
- subject . file file_object
302
+ expect ( subject . header [ 'Transfer-Encoding' ] ) . to eq 'base64'
255
303
end
304
+ end
256
305
257
- it 'returns value wrapped in StreamResponse ' do
258
- subject . file file_object
306
+ context 'as object ' do
307
+ let ( :file_object ) { double ( 'StreamerObject' , each : nil ) }
259
308
260
- expect ( subject . file ) . to eq file_response
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/ )
261
311
end
262
312
end
263
313
end
264
314
265
315
it 'returns default' do
266
- expect ( subject . file ) . to be nil
316
+ expect ( subject . sendfile ) . to be nil
267
317
end
268
318
end
269
319
270
320
describe '#stream' do
271
321
describe 'set' do
272
- context 'as a file path (backward compatibility) ' do
322
+ context 'as a file path' do
273
323
let ( :file_path ) { '/some/file/path' }
274
324
275
325
let ( :file_response ) do
@@ -278,19 +328,39 @@ def initialize
278
328
end
279
329
280
330
before do
281
- allow ( subject ) . to receive ( :warn )
331
+ subject . header 'Cache-Control' , 'cache'
332
+ subject . header 'Content-Length' , 123
333
+ subject . header 'Transfer-Encoding' , 'base64'
282
334
end
283
335
284
- it 'emits a warning to use file method to stream a file ' do
285
- expect ( subject ) . to receive ( :warn ) . with ( /file file_path/ )
336
+ it 'emits no deprecation warnings ' do
337
+ expect ( subject ) . to_not receive ( :warn )
286
338
287
339
subject . stream file_path
288
340
end
289
341
290
- it 'returns value wrapped in StreamResponse' do
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
291
349
subject . stream file_path
292
350
293
- expect ( subject . file ) . to eq file_response
351
+ expect ( subject . header [ 'Cache-Control' ] ) . to eq 'no-cache'
352
+ end
353
+
354
+ it 'sets Content-Length header to nil' do
355
+ subject . stream file_path
356
+
357
+ expect ( subject . header [ 'Content-Length' ] ) . to eq nil
358
+ end
359
+
360
+ it 'sets Transfer-Encoding header to nil' do
361
+ subject . stream file_path
362
+
363
+ expect ( subject . header [ 'Transfer-Encoding' ] ) . to eq nil
294
364
end
295
365
end
296
366
@@ -305,26 +375,35 @@ def initialize
305
375
subject . header 'Cache-Control' , 'cache'
306
376
subject . header 'Content-Length' , 123
307
377
subject . header 'Transfer-Encoding' , 'base64'
378
+ end
379
+
380
+ it 'emits no deprecation warnings' do
381
+ expect ( subject ) . to_not receive ( :warn )
382
+
308
383
subject . stream stream_object
309
384
end
310
385
311
386
it 'returns value wrapped in StreamResponse' do
312
- expect ( subject . stream ) . to eq stream_response
313
- end
387
+ subject . stream stream_object
314
388
315
- it 'also sets result of file to value wrapped in StreamResponse' do
316
- expect ( subject . file ) . to eq stream_response
389
+ expect ( subject . stream ) . to eq stream_response
317
390
end
318
391
319
392
it 'sets Cache-Control header to no-cache' do
393
+ subject . stream stream_object
394
+
320
395
expect ( subject . header [ 'Cache-Control' ] ) . to eq 'no-cache'
321
396
end
322
397
323
398
it 'sets Content-Length header to nil' do
399
+ subject . stream stream_object
400
+
324
401
expect ( subject . header [ 'Content-Length' ] ) . to eq nil
325
402
end
326
403
327
404
it 'sets Transfer-Encoding header to nil' do
405
+ subject . stream stream_object
406
+
328
407
expect ( subject . header [ 'Transfer-Encoding' ] ) . to eq nil
329
408
end
330
409
end
0 commit comments