@@ -208,31 +208,55 @@ def initialize
208208 let ( :file_path ) { '/some/file/path' }
209209
210210 let ( :file_response ) do
211- file_body = Grape ::ServeFile ::FileBody . new ( file_path )
212- Grape ::ServeFile :: FileResponse . new ( file_body )
211+ file_body = Grape ::ServeStream ::FileBody . new ( file_path )
212+ Grape ::ServeStream :: StreamResponse . new ( file_body )
213213 end
214214
215215 before do
216+ subject . header 'Cache-Control' , 'cache'
217+ subject . header 'Content-Length' , 123
218+ subject . header 'Transfer-Encoding' , 'base64'
219+
216220 subject . file file_path
217221 end
218222
219- it 'returns value wrapped in FileResponse ' do
223+ it 'returns value wrapped in StreamResponse ' do
220224 expect ( subject . file ) . to eq file_response
221225 end
226+
227+ it 'sets Cache-Control header to no-cache' do
228+ expect ( subject . header [ 'Cache-Control' ] ) . to eq 'no-cache'
229+ end
230+
231+ it 'sets Content-Length header to nil' do
232+ expect ( subject . header [ 'Content-Length' ] ) . to eq nil
233+ end
234+
235+ it 'sets Transfer-Encoding header to nil' do
236+ expect ( subject . header [ 'Transfer-Encoding' ] ) . to eq nil
237+ end
222238 end
223239
224240 context 'as object (backward compatibility)' do
225- let ( :file_object ) { Class . new }
241+ let ( :file_object ) { double ( 'StreamerObject' , each : nil ) }
226242
227243 let ( :file_response ) do
228- Grape ::ServeFile :: FileResponse . new ( file_object )
244+ Grape ::ServeStream :: StreamResponse . new ( file_object )
229245 end
230246
231247 before do
248+ allow ( subject ) . to receive ( :warn )
249+ end
250+
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/ )
253+
232254 subject . file file_object
233255 end
234256
235- it 'returns value wrapped in FileResponse' do
257+ it 'returns value wrapped in StreamResponse' do
258+ subject . file file_object
259+
236260 expect ( subject . file ) . to eq file_response
237261 end
238262 end
@@ -245,38 +269,77 @@ def initialize
245269
246270 describe '#stream' do
247271 describe 'set' do
248- let ( :file_object ) { Class . new }
272+ context 'as a file path (backward compatibility)' do
273+ let ( :file_path ) { '/some/file/path' }
249274
250- before do
251- subject . header 'Cache-Control' , 'cache'
252- subject . header 'Content-Length' , 123
253- subject . header 'Transfer-Encoding' , 'base64'
254- subject . stream file_object
255- end
275+ let ( :file_response ) do
276+ file_body = Grape ::ServeStream ::FileBody . new ( file_path )
277+ Grape ::ServeStream ::StreamResponse . new ( file_body )
278+ end
256279
257- it 'returns value wrapped in FileResponse' do
258- expect ( subject . stream ) . to eq Grape :: ServeFile :: FileResponse . new ( file_object )
259- end
280+ before do
281+ allow ( subject ) . to receive ( :warn )
282+ end
260283
261- it 'also sets result of file to value wrapped in FileResponse' do
262- expect ( subject . file ) . to eq Grape ::ServeFile ::FileResponse . new ( file_object )
263- end
284+ it 'emits a warning to use file method to stream a file' do
285+ expect ( subject ) . to receive ( :warn ) . with ( /file file_path/ )
286+
287+ subject . stream file_path
288+ end
289+
290+ it 'returns value wrapped in StreamResponse' do
291+ subject . stream file_path
264292
265- it 'sets Cache-Control header to no-cache' do
266- expect ( subject . header [ 'Cache-Control' ] ) . to eq 'no-cache'
293+ expect ( subject . file ) . to eq file_response
294+ end
267295 end
268296
269- it 'sets Content-Length header to nil' do
270- expect ( subject . header [ 'Content-Length' ] ) . to eq nil
297+ context 'as a stream object' do
298+ let ( :stream_object ) { double ( 'StreamerObject' , each : nil ) }
299+
300+ let ( :stream_response ) do
301+ Grape ::ServeStream ::StreamResponse . new ( stream_object )
302+ end
303+
304+ before do
305+ subject . header 'Cache-Control' , 'cache'
306+ subject . header 'Content-Length' , 123
307+ subject . header 'Transfer-Encoding' , 'base64'
308+ subject . stream stream_object
309+ end
310+
311+ it 'returns value wrapped in StreamResponse' do
312+ expect ( subject . stream ) . to eq stream_response
313+ end
314+
315+ it 'also sets result of file to value wrapped in StreamResponse' do
316+ expect ( subject . file ) . to eq stream_response
317+ end
318+
319+ it 'sets Cache-Control header to no-cache' do
320+ expect ( subject . header [ 'Cache-Control' ] ) . to eq 'no-cache'
321+ end
322+
323+ it 'sets Content-Length header to nil' do
324+ expect ( subject . header [ 'Content-Length' ] ) . to eq nil
325+ end
326+
327+ it 'sets Transfer-Encoding header to nil' do
328+ expect ( subject . header [ 'Transfer-Encoding' ] ) . to eq nil
329+ end
271330 end
272331
273- it 'sets Transfer-Encoding header to nil' do
274- expect ( subject . header [ 'Transfer-Encoding' ] ) . to eq nil
332+ context 'as a non-stream object' do
333+ let ( :non_stream_object ) { double ( 'NonStreamerObject' ) }
334+
335+ it 'raises an error that the object must implement :each' do
336+ expect { subject . stream non_stream_object } . to raise_error ( ArgumentError , /:each/ )
337+ end
275338 end
276339 end
277340
278341 it 'returns default' do
279- expect ( subject . file ) . to be nil
342+ expect ( subject . stream ) . to be nil
280343 end
281344 end
282345
0 commit comments