@@ -16,6 +16,7 @@ import 'package:mockito/mockito.dart';
1616import 'mock.dart' ;
1717
1818MockReferencePlatform mockReference = MockReferencePlatform ();
19+ MockReferencePlatform mockJpgReference = MockReferencePlatform ();
1920MockListResultPlatform mockListResultPlatform = MockListResultPlatform ();
2021MockUploadTaskPlatform mockUploadTaskPlatform = MockUploadTaskPlatform ();
2122MockDownloadTaskPlatform mockDownloadTaskPlatform = MockDownloadTaskPlatform ();
@@ -308,6 +309,130 @@ Future<void> main() async {
308309 });
309310 });
310311
312+ group ('putData() contentType inference' , () {
313+ late Reference jpgRef;
314+
315+ setUp (() {
316+ when (kMockStoragePlatform.ref (any)).thenReturn (mockJpgReference);
317+ when (mockJpgReference.bucket).thenReturn (testBucket);
318+ when (mockJpgReference.fullPath).thenReturn ('foo/photo.jpg' );
319+ when (mockJpgReference.name).thenReturn ('photo.jpg' );
320+ jpgRef = storage.ref ('foo/photo.jpg' );
321+ });
322+
323+ test ('infers contentType from ref name when no metadata' , () {
324+ List <int > list = utf8.encode ('hello' );
325+ Uint8List data = Uint8List .fromList (list);
326+ when (mockJpgReference.putData (data, any))
327+ .thenReturn (mockUploadTaskPlatform);
328+
329+ jpgRef.putData (data);
330+
331+ final captured = verify (mockJpgReference.putData (data, captureAny))
332+ .captured
333+ .single as SettableMetadata ;
334+ expect (captured.contentType, 'image/jpeg' );
335+ });
336+
337+ test ('infers contentType when metadata has no contentType' , () {
338+ List <int > list = utf8.encode ('hello' );
339+ Uint8List data = Uint8List .fromList (list);
340+ when (mockJpgReference.putData (data, any))
341+ .thenReturn (mockUploadTaskPlatform);
342+
343+ jpgRef.putData (data, SettableMetadata (contentLanguage: 'en' ));
344+
345+ final captured = verify (mockJpgReference.putData (data, captureAny))
346+ .captured
347+ .single as SettableMetadata ;
348+ expect (captured.contentType, 'image/jpeg' );
349+ expect (captured.contentLanguage, 'en' );
350+ });
351+
352+ test ('preserves explicit contentType' , () {
353+ List <int > list = utf8.encode ('hello' );
354+ Uint8List data = Uint8List .fromList (list);
355+ when (mockJpgReference.putData (data, any))
356+ .thenReturn (mockUploadTaskPlatform);
357+
358+ jpgRef.putData (
359+ data, SettableMetadata (contentType: 'application/octet-stream' ));
360+
361+ final captured = verify (mockJpgReference.putData (data, captureAny))
362+ .captured
363+ .single as SettableMetadata ;
364+ expect (captured.contentType, 'application/octet-stream' );
365+ });
366+
367+ test ('preserves customMetadata when inferring contentType' , () {
368+ List <int > list = utf8.encode ('hello' );
369+ Uint8List data = Uint8List .fromList (list);
370+ when (mockJpgReference.putData (data, any))
371+ .thenReturn (mockUploadTaskPlatform);
372+
373+ jpgRef.putData (
374+ data, SettableMetadata (customMetadata: {'activity' : 'test' }));
375+
376+ final captured = verify (mockJpgReference.putData (data, captureAny))
377+ .captured
378+ .single as SettableMetadata ;
379+ expect (captured.contentType, 'image/jpeg' );
380+ expect (captured.customMetadata, {'activity' : 'test' });
381+ });
382+
383+ test ('no inference when ref has no extension' , () {
384+ // Reset to the default mock with no extension
385+ when (kMockStoragePlatform.ref (any)).thenReturn (mockReference);
386+ when (mockReference.name).thenReturn (testName);
387+ final noExtRef = storage.ref ();
388+
389+ List <int > list = utf8.encode ('hello' );
390+ Uint8List data = Uint8List .fromList (list);
391+ when (mockReference.putData (data)).thenReturn (mockUploadTaskPlatform);
392+
393+ noExtRef.putData (data);
394+
395+ verify (mockReference.putData (data));
396+ });
397+ });
398+
399+ group ('putBlob() contentType inference' , () {
400+ late Reference jpgRef;
401+
402+ setUp (() {
403+ when (kMockStoragePlatform.ref (any)).thenReturn (mockJpgReference);
404+ when (mockJpgReference.bucket).thenReturn (testBucket);
405+ when (mockJpgReference.fullPath).thenReturn ('foo/photo.jpg' );
406+ when (mockJpgReference.name).thenReturn ('photo.jpg' );
407+ jpgRef = storage.ref ('foo/photo.jpg' );
408+ });
409+
410+ test ('infers contentType from ref name when no metadata' , () {
411+ when (mockJpgReference.putBlob (any, any))
412+ .thenReturn (mockUploadTaskPlatform);
413+
414+ jpgRef.putBlob ('blob-data' );
415+
416+ final captured = verify (mockJpgReference.putBlob (any, captureAny))
417+ .captured
418+ .single as SettableMetadata ;
419+ expect (captured.contentType, 'image/jpeg' );
420+ });
421+
422+ test ('preserves explicit contentType' , () {
423+ when (mockJpgReference.putBlob (any, any))
424+ .thenReturn (mockUploadTaskPlatform);
425+
426+ jpgRef.putBlob (
427+ 'blob-data' , SettableMetadata (contentType: 'text/plain' ));
428+
429+ final captured = verify (mockJpgReference.putBlob (any, captureAny))
430+ .captured
431+ .single as SettableMetadata ;
432+ expect (captured.contentType, 'text/plain' );
433+ });
434+ });
435+
311436 test ('hashCode()' , () {
312437 expect (testRef.hashCode, Object .hash (storage, testFullPath));
313438 });
0 commit comments