Skip to content

Commit bbcc4f4

Browse files
dundichkrivchenko-kv
andauthored
fix upload file with nested path (#8)
* fix upload file with nested path * Local variables should not shadow class fields or properties --------- Co-authored-by: krivchenko-kv <krivchenko-kv@activebt.ru>
1 parent c0aa1fb commit bbcc4f4

3 files changed

Lines changed: 42 additions & 23 deletions

File tree

src/Storage.Tests/ObjectShould.cs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Net;
1+
using System.Net;
22
using static Storage.Tests.StorageFixture;
33

44
namespace Storage.Tests;
@@ -8,8 +8,8 @@ public sealed class ObjectShould : IClassFixture<StorageFixture>
88
private readonly S3Client _client;
99
private readonly CancellationToken _ct;
1010
private readonly StorageFixture _fixture;
11-
private readonly S3Client _notExistsBucketClient; // don't dispose it
12-
11+
private readonly S3Client _notExistsBucketClient; // don't dispose it
12+
1313
public ObjectShould(StorageFixture fixture)
1414
{
1515
_ct = CancellationToken.None;
@@ -136,9 +136,9 @@ public async Task DisposeFileStream()
136136
public async Task DisposeStorageFile()
137137
{
138138
var fileName = await CreateTestFile();
139-
using var fileGetResult = await _client.GetFile(fileName, _ct);
140-
141-
// ReSharper disable once DisposeOnUsingVariable
139+
using var fileGetResult = await _client.GetFile(fileName, _ct);
140+
141+
// ReSharper disable once DisposeOnUsingVariable
142142
fileGetResult.Dispose();
143143

144144
await DeleteTestFile(fileName);
@@ -370,28 +370,28 @@ public async Task ReadFileStream()
370370

371371
var buffer = new byte[1024];
372372
var file = await _client.GetFile(fileName, _ct);
373-
var fileStream = await file.GetStream(_ct);
374-
373+
var fileStream = await file.GetStream(_ct);
374+
375375
#pragma warning disable CA1835
376376
var read = await fileStream.ReadAsync(buffer, _ct);
377377
read.Should().BeGreaterThan(0);
378378

379379
read = await fileStream.ReadAsync(buffer, 10, 20, _ct);
380-
read.Should().BeGreaterThan(0);
381-
382-
// ReSharper disable once MethodHasAsyncOverloadWithCancellation
380+
read.Should().BeGreaterThan(0);
381+
382+
// ReSharper disable once MethodHasAsyncOverloadWithCancellation
383383
read = fileStream.Read(buffer, 10, 20);
384-
read.Should().BeGreaterThan(0);
384+
read.Should().BeGreaterThan(0);
385385
#pragma warning restore CA1835
386-
386+
387387
await DeleteTestFile(fileName);
388388
}
389389

390390
[Fact]
391391
public async Task Upload()
392392
{
393393
var fileName = _fixture.Create<string>();
394-
using var data = GetByteStream(12 * 1024 * 1024); // 12 Mb
394+
using var data = GetByteStream(12 * 1024 * 1024); // 12 Mb
395395
var filePutResult = await _client.UploadFile(fileName, StreamContentType, data, _ct);
396396

397397
filePutResult
@@ -505,8 +505,27 @@ await _notExistsBucketClient
505505
await _notExistsBucketClient
506506
.Invoking(client => client.UploadFile(fileName, StreamContentType, fileArray, _ct))
507507
.Should().ThrowAsync<HttpRequestException>();
508+
}
509+
510+
[Theory]
511+
[InlineData("some/foo/audio.wav", 1024)]
512+
[InlineData("another/path/test.mp3", 2048)]
513+
public async Task UploadFileWithNestedPath(string nestedFileName, int dataSize)
514+
{
515+
var data = GetByteArray(dataSize); // Пример данных, которые вы хотите загрузить
516+
517+
// Act
518+
var result = await _client.UploadFile(nestedFileName, StreamContentType, data, _ct);
519+
Assert.True(result);
520+
521+
// Assert
522+
var exists = await _client.IsFileExists(nestedFileName, _ct);
523+
Assert.True(exists, "The object should exist in the S3 bucket");
524+
525+
await DeleteTestFile(nestedFileName);
508526
}
509527

528+
510529
[Fact]
511530
public async Task ThrowIfUploadDisposed()
512531
{
@@ -534,8 +553,8 @@ private async Task<string> CreateTestFile(
534553
int? size = null)
535554
{
536555
fileName ??= _fixture.Create<string>();
537-
using var data = GetByteStream(size ?? 1 * 1024 * 1024); // 1 Mb
538-
556+
using var data = GetByteStream(size ?? 1 * 1024 * 1024); // 1 Mb
557+
539558
var uploadResult = await _client.UploadFile(fileName, contentType, data, _ct);
540559

541560
uploadResult
@@ -570,8 +589,8 @@ private async Task EnsureFileSame(string fileName, MemoryStream expectedBytes)
570589
memoryStream
571590
.ToArray().SequenceEqual(expectedBytes.ToArray())
572591
.Should().BeTrue();
573-
}
574-
592+
}
593+
575594
private Task DeleteTestFile(string fileName)
576595
{
577596
return _client.DeleteFile(fileName, _ct);

src/Storage/Utils/HttpDescription.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Buffers;
1+
using System.Buffers;
22
using System.Collections.Frozen;
33
using System.Text;
44

@@ -7,7 +7,7 @@ namespace Storage.Utils;
77
internal readonly struct HttpDescription
88
{
99
private static readonly FrozenSet<char> _validUrlCharacters =
10-
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~".ToFrozenSet();
10+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~/".ToFrozenSet();
1111

1212
private readonly string _headerEnd;
1313
private readonly string _headerStart;

src/Storage/Utils/ValueStringBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System.Buffers;
1+
using System.Buffers;
22
using System.Globalization;
33

44
namespace Storage.Utils;
55

6-
internal ref struct ValueStringBuilder(Span<char> buffer)
6+
internal ref struct ValueStringBuilder(Span<char> initialBuffer)
77
{
8-
private Span<char> _buffer = buffer;
8+
private Span<char> _buffer = initialBuffer;
99
private int _length = 0;
1010
private char[]? _array = null;
1111

0 commit comments

Comments
 (0)