diff --git a/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/BlobClientCore.cs b/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/BlobClientCore.cs index ddbe72e..814dc90 100644 --- a/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/BlobClientCore.cs +++ b/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/BlobClientCore.cs @@ -260,7 +260,7 @@ public async Task OpenWriteAsync(bool overwrite, BlobOpenWriteOptions? o using var blob = AcquireBlob(cancellationToken); - if (!blob.Value.TryOpenWrite(options?.OpenConditions, options?.BufferSize, options?.Metadata, out var stream, out var error)) + if (!blob.Value.TryOpenWrite(options?.OpenConditions, options?.BufferSize, options?.Metadata, options?.HttpHeaders, out var stream, out var error)) { throw error.GetClientException(); } diff --git a/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/InMemoryBlockBlob.cs b/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/InMemoryBlockBlob.cs index 96e0675..82a64da 100644 --- a/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/InMemoryBlockBlob.cs +++ b/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/InMemoryBlockBlob.cs @@ -255,7 +255,13 @@ public bool TryGetBlockList( return true; } - public bool TryOpenWrite(RequestConditions? conditions, long? bufferSize, IDictionary? metadata, [NotNullWhen(true)] out BlobWriteStream? stream, [NotNullWhen(false)] out OpenWriteError? error) + public bool TryOpenWrite( + RequestConditions? conditions, + long? bufferSize, + IDictionary? metadata, + BlobHttpHeaders? httpHeaders, + [NotNullWhen(true)] out BlobWriteStream? stream, + [NotNullWhen(false)] out OpenWriteError? error) { if (ShouldThrowBlobAlreadyExistsError(conditions)) { @@ -271,7 +277,7 @@ public bool TryOpenWrite(RequestConditions? conditions, long? bufferSize, IDicti return false; } - SetCommitedState(null, metadata, []); + SetCommitedState(httpHeaders, metadata, []); var client = InMemoryBlockBlobClient.FromAccount(Container.Service.Account, ContainerName, Name); diff --git a/tests/Tests/Storage/Blobs/BlobClientTests.cs b/tests/Tests/Storage/Blobs/BlobClientTests.cs index 781ca28..a531abe 100644 --- a/tests/Tests/Storage/Blobs/BlobClientTests.cs +++ b/tests/Tests/Storage/Blobs/BlobClientTests.cs @@ -143,6 +143,39 @@ public void OpenWrite_Should_Create_Blob_With_Content(BlobClientType clientType) ShouldHaveBlocks(containerClient.GetBlockBlobClient(blobName), commited: 1, uncommited: 0); } + [TestMethod] + [TestCategory(TestCategory.AzureInfra)] + [DataRow(BlobClientType.Generic)] + [DataRow(BlobClientType.Block)] + public void OpenWrite_Should_Create_Blob_With_Headers(BlobClientType clientType) + { + var containerClient = ImplementationProvider.GetBlobContainerClient(); + + containerClient.CreateIfNotExists(); + + var blobName = Guid.NewGuid().ToString(); + + var blobClient = containerClient.GetBlobBaseClient(blobName, clientType); + + var headers = new BlobHttpHeaders + { + ContentType = "application/json", + ContentEncoding = "br" + }; + + using (var stream = OpenWrite(blobClient, true, headers: headers)) + using (var streamWriter = new StreamWriter(stream)) + { + streamWriter.Write("test-data"); + } + + blobClient.DownloadContent().Value.Content.ToString().Should().Be("test-data"); + blobClient.GetProperties().Value.ContentType.Should().Be("application/json"); + blobClient.GetProperties().Value.ContentEncoding.Should().Be("br"); + + ShouldHaveBlocks(containerClient.GetBlockBlobClient(blobName), commited: 1, uncommited: 0); + } + [TestMethod] [TestCategory(TestCategory.AzureInfra)] [DataRow(BlobClientType.Generic)] @@ -812,7 +845,7 @@ private static void Upload(BlobBaseClient blobClient, byte[] content, BlobUpload } } - private static Stream OpenWrite(BlobBaseClient blobClient, bool overwrite, IDictionary? metadata = null, BlobRequestConditions? conditions = null) + private static Stream OpenWrite(BlobBaseClient blobClient, bool overwrite, IDictionary? metadata = null, BlobHttpHeaders? headers = null, BlobRequestConditions? conditions = null) { if (blobClient is BlobClient genericClient) { @@ -830,6 +863,12 @@ private static Stream OpenWrite(BlobBaseClient blobClient, bool overwrite, IDict options.OpenConditions = conditions; } + if (headers is not null) + { + options ??= new(); + options.HttpHeaders = headers; + } + return genericClient.OpenWrite(overwrite, options); } @@ -849,6 +888,12 @@ private static Stream OpenWrite(BlobBaseClient blobClient, bool overwrite, IDict options.OpenConditions = conditions; } + if (headers is not null) + { + options ??= new(); + options.HttpHeaders = headers; + } + return blockClient.OpenWrite(overwrite, options); }