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
153 changes: 144 additions & 9 deletions Tests/Resgrid.Tests/Web/Tts/S3StorageServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Amazon.S3;
Expand Down Expand Up @@ -41,15 +43,7 @@ public async Task upload_async_should_buffer_non_seekable_stream_for_retries()
return new PutObjectResponse();
});

var service = new S3StorageService(
s3Client.Object,
Options.Create(new S3StorageOptions
{
Bucket = "tts-bucket",
AccessKey = "access-key",
SecretKey = "secret-key"
}),
Mock.Of<ILogger<S3StorageService>>());
var service = CreateService(s3Client.Object);

await using var content = new NonSeekableReadStream(new byte[] { 1, 2, 3, 4 });

Expand All @@ -61,6 +55,147 @@ public async Task upload_async_should_buffer_non_seekable_stream_for_retries()
s3Client.Verify(x => x.PutObjectAsync(It.IsAny<PutObjectRequest>(), It.IsAny<CancellationToken>()), Times.Exactly(2));
}

[Test]
public async Task upload_async_should_treat_format_exception_as_success_when_object_exists_after_upload()
{
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>()))
.ReturnsAsync(new GetObjectMetadataResponse());

var handler = new RecordingHttpMessageHandler((_, _) =>
Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)));
var service = CreateService(s3Client.Object, handler);

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

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

handler.Requests.Should().BeEmpty();
s3Client.Verify(x => x.GetObjectMetadataAsync(It.Is<GetObjectMetadataRequest>(request => request.BucketName == "tts-bucket" && request.Key == "tts/audio.wav"), It.IsAny<CancellationToken>()), Times.Once);
s3Client.Verify(x => x.GetPreSignedURL(It.IsAny<GetPreSignedUrlRequest>()), Times.Never);
}

[Test]
public async Task upload_async_should_fall_back_to_presigned_put_when_metadata_check_times_out()
{
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 TaskCanceledException("metadata timeout"));
s3Client
.Setup(x => x.GetPreSignedURL(It.IsAny<GetPreSignedUrlRequest>()))
.Returns("https://upload.example.com/tts/audio.wav?signature=timeout");

var handler = new RecordingHttpMessageHandler(async (request, cancellationToken) =>
{
var body = await request.Content!.ReadAsByteArrayAsync(cancellationToken);
body.Should().Equal(6, 7, 8, 9);
request.Method.Should().Be(HttpMethod.Put);
request.RequestUri.Should().Be(new Uri("https://upload.example.com/tts/audio.wav?signature=timeout"));
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[] { 6, 7, 8, 9 }, 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_put_response_is_malformed_and_object_is_missing()
{
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 AmazonS3Exception("missing")
{
StatusCode = HttpStatusCode.NotFound,
ErrorCode = "NoSuchKey"
});
s3Client
.Setup(x => x.GetPreSignedURL(It.IsAny<GetPreSignedUrlRequest>()))
.Returns<GetPreSignedUrlRequest>(request =>
{
request.BucketName.Should().Be("tts-bucket");
request.Key.Should().Be("tts/audio.wav");
request.Verb.Should().Be(HttpVerb.PUT);
request.ContentType.Should().Be("audio/wav");
return "https://upload.example.com/tts/audio.wav?signature=123";
});

var handler = new RecordingHttpMessageHandler(async (request, cancellationToken) =>
{
var body = await request.Content!.ReadAsByteArrayAsync(cancellationToken);
body.Should().Equal(5, 4, 3, 2);
request.Method.Should().Be(HttpMethod.Put);
request.RequestUri.Should().Be(new Uri("https://upload.example.com/tts/audio.wav?signature=123"));
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[] { 5, 4, 3, 2 }, 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);
}

private static S3StorageService CreateService(IAmazonS3 s3Client, RecordingHttpMessageHandler handler = null)
{
handler ??= new RecordingHttpMessageHandler((_, _) => Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)));
var httpClientFactory = new Mock<IHttpClientFactory>(MockBehavior.Strict);
httpClientFactory.Setup(x => x.CreateClient(nameof(S3StorageService))).Returns(new HttpClient(handler, disposeHandler: false));

return new S3StorageService(
s3Client,
Options.Create(new S3StorageOptions
{
Bucket = "tts-bucket",
AccessKey = "access-key",
SecretKey = "secret-key"
}),
Mock.Of<ILogger<S3StorageService>>(),
httpClientFactory.Object);
}

private sealed class RecordingHttpMessageHandler : HttpMessageHandler
{
private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _handler;

public RecordingHttpMessageHandler(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handler)
{
_handler = handler;
}

public List<HttpRequestMessage> Requests { get; } = new();

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Requests.Add(request);
return await _handler(request, cancellationToken);
}
}

private sealed class NonSeekableReadStream : Stream
{
private readonly MemoryStream _inner;
Expand Down
1 change: 1 addition & 0 deletions Web/Resgrid.Web.Tts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpClient();
builder.Services.AddTtsConfiguration();
builder.Services.Configure<ForwardedHeadersOptions>(TtsRequestIdentity.ConfigureForwardedHeaders);
builder.Services.AddHealthChecks()
Expand Down
Loading
Loading