1+ using System ;
2+ using System . IO ;
3+ using System . Text ;
4+ using System . Threading . Tasks ;
5+ using FluentAssertions ;
16using ManagedCode . Storage . Tests . Common ;
27using Microsoft . Extensions . DependencyInjection ;
8+ using Xunit ;
39
410namespace ManagedCode . Storage . Tests . Storages . FileSystem ;
511
@@ -14,4 +20,66 @@ protected override ServiceProvider ConfigureServices()
1420 {
1521 return FileSystemConfigurator . ConfigureServices ( "managed-code-blob" ) ;
1622 }
17- }
23+
24+ [ Fact ]
25+ public async Task UploadAsync_AsStream_CorrectlyOverwritesFiles ( )
26+ {
27+ // Arrange
28+
29+ var uploadStream1 = new MemoryStream ( 90 * 1024 ) ;
30+ var buffer = new byte [ 90 * 1024 ] ;
31+ var random = new Random ( ) ;
32+ random . NextBytes ( buffer ) ;
33+ uploadStream1 . Write ( buffer , 0 , buffer . Length ) ;
34+
35+ var uploadStream2 = new MemoryStream ( 512 ) ;
36+ var zeroByteBuffer = new byte [ 512 ] ;
37+ uploadStream2 . Write ( zeroByteBuffer ) ;
38+ var filenameToUse = "UploadAsync_AsStream_CorrectlyOverwritesFiles.bin" ;
39+
40+ var temporaryDirectory = Path . GetTempPath ( ) ;
41+
42+ // Act
43+ var firstResult = await Storage . UploadAsync ( uploadStream1 , options =>
44+ {
45+ options . FileName = filenameToUse ;
46+ options . Directory = temporaryDirectory ;
47+ } ) ;
48+
49+ firstResult . IsSuccess . Should ( ) . BeTrue ( ) ;
50+
51+ // let's download it
52+ var downloadedResult = await Storage . DownloadAsync ( options =>
53+ {
54+ options . FileName = filenameToUse ;
55+ options . Directory = temporaryDirectory ;
56+ } ) ;
57+ downloadedResult . IsSuccess . Should ( ) . BeTrue ( ) ;
58+ // size
59+ downloadedResult . Value ! . FileInfo . Length . Should ( ) . Be ( 90 * 1024 ) ;
60+
61+
62+ var secondResult = await Storage . UploadAsync ( uploadStream2 , options =>
63+ {
64+ options . FileName = filenameToUse ;
65+ options . Directory = temporaryDirectory ;
66+ } ) ;
67+
68+ secondResult . IsSuccess . Should ( ) . BeTrue ( ) ;
69+
70+ // let's download it
71+ downloadedResult = await Storage . DownloadAsync ( options =>
72+ {
73+ options . FileName = filenameToUse ;
74+ options . Directory = temporaryDirectory ;
75+ } ) ;
76+ downloadedResult . IsSuccess . Should ( ) . BeTrue ( ) ;
77+ // size
78+ downloadedResult . Value ! . FileInfo . Length . Should ( ) . Be ( 512 ) ;
79+
80+ // content
81+ using var ms = new MemoryStream ( ) ;
82+ await downloadedResult . Value ! . FileStream . CopyToAsync ( ms ) ;
83+ ms . ToArray ( ) . Should ( ) . BeEquivalentTo ( zeroByteBuffer ) ;
84+ }
85+ }
0 commit comments