コード例 #1
0
        public async Task GetRetrievesS3()
        {
            var mock = Substitute.For <IAmazonS3>();

            mock.DoesS3BucketExistAsync("xxx").Returns(true);

            var oms    = new MemoryStream();
            var writer = new StreamWriter(oms);

            var dictionary = new Dictionary <string, object> {
                { "test", "wibble" }
            };

            writer.Write(JsonConvert.SerializeObject(dictionary));
            writer.Flush();
            oms.Seek(0, SeekOrigin.Begin);

            mock
            .GetObjectAsync(string.Empty, string.Empty)
            .ReturnsForAnyArgs(c => new GetObjectResponse
            {
                HttpStatusCode = HttpStatusCode.OK,
                ResponseStream = oms
            });

            var request = DummyRequest();
            var s3      = new S3PersistenceStore("xxx", mock);
            var result  = await s3.Get <string>(request, "test");

            Assert.Equal("wibble", result);
        }
コード例 #2
0
        public async Task SavePushesToS3()
        {
            var mock = Substitute.For <IAmazonS3>();

            mock.DoesS3BucketExistAsync("xxx").Returns(true);

            mock
            .GetObjectAsync(string.Empty, string.Empty)
            .ReturnsForAnyArgs(c => new GetObjectResponse {
                HttpStatusCode = HttpStatusCode.NotFound
            });

            var request = DummyRequest();
            var s3      = new S3PersistenceStore("xxx", mock);
            await s3.Set(request, "test", "wibble");

            mock.When(a => a.PutObjectAsync(Arg.Any <PutObjectRequest>())).Do(call =>
            {
                var put = call.Arg <PutObjectRequest>();
                Assert.Equal("xxx", put.BucketName);
                Assert.Equal("abc123", put.Key);
                Assert.NotNull(put.InputStream);
                using (var reader = new StreamReader(put.InputStream))
                {
                    var content = reader.ReadToEnd();
                    var data    = JsonConvert.DeserializeObject <JObject>(content);
                    Assert.Equal("wibble", data.Value <string>("test"));
                }
            });

            await s3.Save(request);
        }
コード例 #3
0
        public async Task InvalidBucketThrows()
        {
            var mock        = Substitute.For <IAmazonS3>();
            var bucketCheck = mock.DoesS3BucketExistAsync("xxx").Returns(false);

            var request = DummyRequest();
            var s3      = new S3PersistenceStore("xxx", mock);
            await Assert.ThrowsAsync <InvalidOperationException>(() => s3.Get <string>(request, "test"));
        }
コード例 #4
0
        public async Task SetCheckS3CorrectKey()
        {
            var mock        = Substitute.For <IAmazonS3>();
            var bucketCheck = mock.DoesS3BucketExistAsync("xxx").Returns(true);

            var existenceCheck = mock
                                 .GetObjectAsync(string.Empty, string.Empty)
                                 .ReturnsForAnyArgs(c => new GetObjectResponse {
                HttpStatusCode = HttpStatusCode.NotFound
            });

            var request = DummyRequest();
            var s3      = new S3PersistenceStore("xxx", mock);
            await s3.Set(request, "test", "wibble");

            await mock.Received(1).GetObjectAsync("xxx", "abc123");
        }
コード例 #5
0
        public async Task CheckS3BucketFirstTime()
        {
            var mock        = Substitute.For <IAmazonS3>();
            var bucketCheck = mock.DoesS3BucketExistAsync("xxx").Returns(true);

            var existenceCheck = mock
                                 .GetObjectAsync(string.Empty, string.Empty)
                                 .ReturnsForAnyArgs(c => new GetObjectResponse {
                HttpStatusCode = HttpStatusCode.NotFound
            });

            var request = DummyRequest();
            var s3      = new S3PersistenceStore("xxx", mock);
            var result  = await s3.Get <string>(request, "test");

            Assert.Null(result);

            await mock.Received(1).DoesS3BucketExistAsync("xxx");
        }
コード例 #6
0
        public async Task SetPutsValueLocally()
        {
            var mock = Substitute.For <IAmazonS3>();

            mock.DoesS3BucketExistAsync("xxx").Returns(true);

            mock
            .GetObjectAsync(string.Empty, string.Empty)
            .ReturnsForAnyArgs(c => new GetObjectResponse {
                HttpStatusCode = HttpStatusCode.NotFound
            });

            var request = DummyRequest();
            var s3      = new S3PersistenceStore("xxx", mock);
            await s3.Set(request, "test", "wibble");

            var result = await s3.Get <string>(request, "test");

            Assert.Equal("wibble", result);
        }
コード例 #7
0
        public async Task RemoveWorksWithNoKeys()
        {
            var mock = Substitute.For <IAmazonS3>();

            mock.DoesS3BucketExistAsync("xxx").Returns(true);
            mock
            .GetObjectAsync(string.Empty, string.Empty)
            .ReturnsForAnyArgs(c => new GetObjectResponse {
                HttpStatusCode = HttpStatusCode.NotFound
            });

            var store = new S3PersistenceStore("xxx", mock);
            await store.Set(DummyRequest(), "test", "value");

            var result1 = store.Remove(DummyRequest());
            var result2 = store.Remove(DummyRequest());

            Assert.True(result1);
            Assert.True(result2);
        }