public async Task Storage_ReturnsStoredFile() { var clientMock = new Mock <IAmazonS3>(); Stream stream = null; clientMock .Setup(c => c.PutObjectAsync(It.IsAny <PutObjectRequest>(), It.IsAny <CancellationToken>())) .Callback <PutObjectRequest, CancellationToken>((req, _) => stream = req.InputStream) .Returns(Task.FromResult(new PutObjectResponse())); clientMock .Setup(c => c.GetObjectAsync(It.IsAny <GetObjectRequest>(), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult(new GetObjectResponse { ResponseStream = stream })); var storage = new S3FileStorage(clientMock.Object, "bucket1"); const string text = "fooBar\nbaz"; const string fileName = "myFile"; await storage.WriteAllTextAsync(fileName, text); var actualText = await storage.ReadAllTextAsync(fileName); Assert.AreEqual(text, actualText); }
public void Storage_MissingFile_ThrowsStorageFileNotFoundException() { var clientMock = new Mock <IAmazonS3>(); clientMock .Setup(c => c.GetObjectAsync(It.IsAny <GetObjectRequest>(), It.IsAny <CancellationToken>())) .Returns(Task.FromException <GetObjectResponse>( new AmazonS3Exception("bar", ErrorType.Sender, "NoSuchKey", "1", HttpStatusCode.NotFound))); var storage = new S3FileStorage(clientMock.Object, "bucket1"); var ex = Assert.ThrowsAsync <StorageFileNotFoundException>( async() => await storage.ReadAllTextAsync("foo")); Assert.IsInstanceOf <AmazonS3Exception>(ex.InnerException); Assert.AreEqual("bar", ex.InnerException.Message); }