Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Tests/Resgrid.Tests/Web/Tts/S3StorageServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,41 @@ public async Task upload_async_should_treat_format_exception_as_success_when_obj
s3Client.Verify(x => x.GetPreSignedURL(It.IsAny<GetPreSignedUrlRequest>()), Times.Never);
}

[Test]
public async Task upload_async_should_fall_back_to_presigned_put_when_metadata_response_is_malformed()
{
var s3Client = new Mock<IAmazonS3>(MockBehavior.Strict);
s3Client
.Setup(x => x.PutObjectAsync(It.IsAny<PutObjectRequest>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new FormatException("bad expiration header"));
s3Client
.Setup(x => x.GetObjectMetadataAsync(It.IsAny<GetObjectMetadataRequest>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new FormatException("bad metadata expiration header"));
s3Client
.Setup(x => x.GetPreSignedURL(It.IsAny<GetPreSignedUrlRequest>()))
.Returns("https://upload.example.com/tts/audio.wav?signature=metadata-format");

var handler = new RecordingHttpMessageHandler(async (request, cancellationToken) =>
{
var body = await request.Content!.ReadAsByteArrayAsync(cancellationToken);
body.Should().Equal(2, 4, 6, 8);
request.Method.Should().Be(HttpMethod.Put);
request.RequestUri.Should().Be(new Uri("https://upload.example.com/tts/audio.wav?signature=metadata-format"));
request.Content!.Headers.ContentType!.MediaType.Should().Be("audio/wav");

return new HttpResponseMessage(HttpStatusCode.OK);
});
var service = CreateService(s3Client.Object, handler);

await using var content = new MemoryStream(new byte[] { 2, 4, 6, 8 }, writable: false);

await service.UploadAsync("tts/audio.wav", content, "audio/wav", CancellationToken.None);

handler.Requests.Should().HaveCount(1);
s3Client.Verify(x => x.GetObjectMetadataAsync(It.IsAny<GetObjectMetadataRequest>(), It.IsAny<CancellationToken>()), Times.Once);
s3Client.Verify(x => x.GetPreSignedURL(It.IsAny<GetPreSignedUrlRequest>()), Times.Once);
}

[Test]
public async Task upload_async_should_fall_back_to_presigned_put_when_metadata_check_times_out()
{
Expand Down
7 changes: 7 additions & 0 deletions Web/Resgrid.Web.Tts/Services/S3StorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ private async Task<bool> WasUploadPersistedAsync(string objectKey, CancellationT
"Unable to verify whether {ObjectKey} exists after the PUT response parsing failure. Falling back to a presigned PUT upload.",
objectKey);
}
catch (FormatException ex)
{
_logger.LogWarning(
ex,
"Unable to verify whether {ObjectKey} exists after the PUT response parsing failure because the metadata response could not be parsed. Falling back to a presigned PUT upload.",
objectKey);
}
catch (HttpRequestException ex)
{
_logger.LogWarning(
Expand Down
Loading