Пример #1
0
        public void TestListBucketsResponseUnmarshallingException200OK()
        {
            Tester.Reset();

            var context = CreateTestContext();
            var request = new ListBucketsRequest();

            ((RequestContext)context.RequestContext).OriginalRequest = request;
            ((RequestContext)context.RequestContext).Request         = new ListBucketsRequestMarshaller().Marshall(request);
            ((RequestContext)context.RequestContext).Unmarshaller    = new ListBucketsResponseUnmarshaller();

            var response = MockWebResponse.CreateFromResource("MalformedResponse.txt")
                           as HttpWebResponse;

            context.ResponseContext.HttpResponse = new HttpWebRequestResponseData(response);

            try
            {
                RuntimePipeline.InvokeSync(context);
                Assert.Fail();
            }
            catch (AmazonUnmarshallingException aue)
            {
                Assert.IsTrue(aue.Message.Contains("HTTP Status Code: 200 OK"));
                Assert.AreEqual(HttpStatusCode.OK, aue.StatusCode);
                Assert.IsNotNull(aue.InnerException);
                Assert.AreEqual("Data at the root level is invalid. Line 1, position 1.", aue.InnerException.Message);
            }
        }
Пример #2
0
        public void TestSuppressed404Async()
        {
            Tester.Reset();
            Tester.Action = (int callCount) =>
            {
                var errorResponse = (HttpWebResponse)MockWebResponse.CreateFromResource("404Response.txt");
                throw new HttpErrorResponseException(new HttpWebRequestResponseData(errorResponse));
            };

            var context = CreateAsyncTestContext();
            var request = new GetBucketPolicyRequest
            {
                BucketName = "nonexistentbucket"
            };

            ((RequestContext)context.RequestContext).OriginalRequest = request;
            ((RequestContext)context.RequestContext).Request         = new GetBucketPolicyRequestMarshaller().Marshall(request);
            ((RequestContext)context.RequestContext).Unmarshaller    = GetBucketPolicyResponseUnmarshaller.Instance;

            var asyncResult = RuntimePipeline.InvokeAsync(context);

            asyncResult.AsyncWaitHandle.WaitOne();

            Assert.IsNull(((RuntimeAsyncResult)asyncResult).Exception);
            Assert.AreEqual(1, Tester.CallCount);
        }
Пример #3
0
        public void TestListBucketsResponseUnmarshalling()
        {
            Tester.Reset();

            var context = CreateTestContext();
            var request = new ListBucketsRequest();

            ((RequestContext)context.RequestContext).OriginalRequest = request;
            ((RequestContext)context.RequestContext).Request         = new ListBucketsRequestMarshaller().Marshall(request);
            ((RequestContext)context.RequestContext).Unmarshaller    = ListBucketsResponseUnmarshaller.Instance;

            var response = MockWebResponse.CreateFromResource("ListBucketsResponse.txt")
                           as HttpWebResponse;

            context.ResponseContext.HttpResponse = new HttpWebRequestResponseData(response);

            RuntimePipeline.InvokeSync(context);

            Assert.AreEqual(1, Tester.CallCount);
            Assert.IsInstanceOfType(context.ResponseContext.Response, typeof(ListBucketsResponse));

            var listBucketsResponse = context.ResponseContext.Response as ListBucketsResponse;

            Assert.AreEqual(4, listBucketsResponse.Buckets.Count);
            Assert.AreEqual("-UUNhfhfx0J622sdKihbDfqEvIa94CkVQvcb4AGlNmRbpbInOTYXSA==", listBucketsResponse.ResponseMetadata.Metadata[HeaderKeys.XAmzCloudFrontIdHeader]);
        }
Пример #4
0
        public void TestListBucketsResponseUnmarshallingAsync()
        {
            Tester.Reset();

            var context = CreateAsyncTestContext();
            var request = new ListBucketsRequest();

            ((RequestContext)context.RequestContext).OriginalRequest = request;
            ((RequestContext)context.RequestContext).Request         = new ListBucketsRequestMarshaller().Marshall(request);
            ((RequestContext)context.RequestContext).Unmarshaller    = ListBucketsResponseUnmarshaller.Instance;

            var response = MockWebResponse.CreateFromResource("ListBucketsResponse.txt")
                           as HttpWebResponse;

            context.ResponseContext.HttpResponse = new HttpWebRequestResponseData(response);

            var asyncResult = RuntimePipeline.InvokeAsync(context);

            asyncResult.AsyncWaitHandle.WaitOne();

            Assert.AreEqual(1, Tester.CallCount);
            Assert.IsInstanceOfType(context.ResponseContext.Response, typeof(ListBucketsResponse));

            var listBucketsResponse = context.ResponseContext.Response as ListBucketsResponse;

            Assert.AreEqual(4, listBucketsResponse.Buckets.Count);
        }
        protected virtual IExecutionContext CreateTestContext()
        {
            var putObjectRequest = new PutObjectRequest
            {
                Key         = "TestKey",
                BucketName  = "TestBucket",
                ContentBody = "Test Content"
            };
            var requestContext = new RequestContext(true)
            {
                OriginalRequest = putObjectRequest,
                Request         = new PutObjectRequestMarshaller().Marshall(putObjectRequest),
                Unmarshaller    = PutObjectResponseUnmarshaller.Instance,
                ClientConfig    = new AmazonS3Config()
            };

            requestContext.Request.Endpoint = new Uri("https://s3.amazonaws.com");

            var putObjectResponse = MockWebResponse.CreateFromResource("PutObjectResponse.txt")
                                    as HttpWebResponse;

            return(new Amazon.Runtime.Internal.ExecutionContext(requestContext,
                                                                new ResponseContext
            {
                HttpResponse = new HttpWebRequestResponseData(putObjectResponse)
            }));
        }
Пример #6
0
        protected IAsyncExecutionContext CreateAsyncTestContext(AbstractAWSSigner signer)
        {
            var putObjectRequest = new PutObjectRequest
            {
                Key         = "Test",
                BucketName  = "Test",
                ContentBody = "Test Content"
            };
            var requestContext = new AsyncRequestContext(true, signer == null ? new NullSigner() : signer)
            {
                OriginalRequest = putObjectRequest,
                Request         = new PutObjectRequestMarshaller().Marshall(putObjectRequest),
                Unmarshaller    = PutObjectResponseUnmarshaller.Instance,
                ClientConfig    = new AmazonS3Config()
            };

            requestContext.Request.Endpoint = new Uri("https://s3.amazonaws.com");

            var putObjectResponse = MockWebResponse.CreateFromResource("PutObjectResponse.txt")
                                    as HttpWebResponse;

            return(new Amazon.Runtime.Internal.AsyncExecutionContext(
                       requestContext,
                       new AsyncResponseContext
            {
                HttpResponse = new HttpWebRequestResponseData(putObjectResponse)
            }
                       ));
        }
Пример #7
0
            private HttpWebResponse CreateResponse(MockHttpRequest request)
            {
                // Extract the last segment of the URI, this is the custom URI
                // sent by the unit tests.
                var resourceName = request.RequestUri.Host.Split('.').Last();

                return(MockWebResponse.CreateFromResource(resourceName));
            }
Пример #8
0
            public Task <IWebResponseData> GetResponseAsync(System.Threading.CancellationToken cancellationToken)
            {
                if (this.GetResponseAction != null)
                {
                    this.GetResponseAction();
                }

                return(Task.FromResult <IWebResponseData>(new HttpWebRequestResponseData(
                                                              MockWebResponse.CreateFromResource(this.RequestUri.Host)
                                                              as HttpWebResponse)));
            }
Пример #9
0
            public Amazon.Runtime.Internal.Transform.IWebResponseData EndGetResponse(IAsyncResult asyncResult)
            {
                if (this.GetResponseAction != null)
                {
                    this.GetResponseAction();
                }

                return(new HttpWebRequestResponseData(
                           MockWebResponse.CreateFromResource(this.RequestUri.Host)
                           as HttpWebResponse));
            }
Пример #10
0
            public Amazon.Runtime.Internal.Transform.IWebResponseData GetResponse()
            {
                if (this.GetResponseAction != null)
                {
                    this.GetResponseAction();
                }

                // Extract the last segment of the URI, this is the custom URI
                // sent by the unit tests.
                var resourceName = this.RequestUri.Host.Split('.').Last();

                return(new HttpWebRequestResponseData(
                           MockWebResponse.CreateFromResource(resourceName)
                           as HttpWebResponse));
            }
Пример #11
0
            private HttpWebResponse CreateResponse(MockHttpRequest request)
            {
                // Extract the last segment of the URI, this is the custom URI
                // sent by the unit tests.
                var resourceName = request.RequestUri.Host.Split('.').Last();
                var response     = MockWebResponse.CreateFromResource(resourceName);

                if (response.StatusCode >= HttpStatusCode.OK && response.StatusCode <= (HttpStatusCode)299)
                {
                    return(response);
                }
                else
                {
                    throw new HttpErrorResponseException(new HttpWebRequestResponseData(response));
                }
            }
Пример #12
0
            protected override void CustomizeRuntimePipeline(RuntimePipeline pipeline)
            {
                base.CustomizeRuntimePipeline(pipeline);

                this.MockFactory = new Mock <IHttpRequestFactory <Stream> >();
                this.MockFactory.Setup(foo => foo.CreateHttpRequest(It.IsAny <Uri>()))
                .Returns((Uri uri) =>
                {
                    var request = new Mock <IHttpRequest <Stream> >();
                    request.Setup(foo => foo.GetResponse())
                    .Returns(new HttpWebRequestResponseData(MockWebResponse.CreateFromResource("ListObjectsResponse.txt")));
                    return(request.Object);
                })
                .Callback((Uri uri) => this.LastRequestUri = uri);
                pipeline.ReplaceHandler <HttpHandler <Stream> >(new HttpHandler <Stream>(this.MockFactory.Object, this));
            }
Пример #13
0
        public void Test404Async()
        {
            Tester.Reset();
            Tester.Action = (int callCount) =>
            {
                var errorResponse = (HttpWebResponse)MockWebResponse.CreateFromResource("404Response.txt");
                throw new HttpErrorResponseException(new HttpWebRequestResponseData(errorResponse));
            };

            var context = CreateAsyncTestContext();

            var asyncResult = RuntimePipeline.InvokeAsync(context);

            asyncResult.AsyncWaitHandle.WaitOne();
            Assert.IsTrue(((RuntimeAsyncResult)asyncResult).Exception.GetType()
                          == typeof(AmazonS3Exception));
            Assert.AreEqual(1, Tester.CallCount);
        }
Пример #14
0
        public async Task Test404Async()
        {
            Tester.Reset();
            Tester.Action = (int callCount) =>
            {
                var errorResponse = (HttpWebResponse)MockWebResponse.CreateFromResource("404Response.txt");
                throw new HttpErrorResponseException(new HttpWebRequestResponseData(errorResponse));
            };

            var context = CreateTestContext();

            await Utils.AssertExceptionExpectedAsync(() =>
            {
                return(RuntimePipeline.InvokeAsync <AmazonWebServiceResponse>(context));
            },
                                                     typeof(AmazonS3Exception));

            Assert.AreEqual(1, Tester.CallCount);
        }
Пример #15
0
        public void Test404()
        {
            Tester.Reset();
            Tester.Action = (int callCount) =>
            {
                var errorResponse = (HttpWebResponse)MockWebResponse.CreateFromResource("404Response.txt");
                throw new HttpErrorResponseException(new HttpWebRequestResponseData(errorResponse));
            };

            var context = CreateTestContext();

            var exception = Utils.AssertExceptionExpected(() =>
            {
                RuntimePipeline.InvokeSync(context);
            },
                                                          typeof(AmazonS3Exception));

            Assert.AreEqual("NoSuchBucket", ((AmazonS3Exception)exception).ErrorCode);
            Assert.AreEqual(HttpStatusCode.NotFound, ((AmazonS3Exception)exception).StatusCode);
            Assert.AreEqual(1, Tester.CallCount);
        }
Пример #16
0
        public async Task TestSuppressed404Async()
        {
            Tester.Reset();
            Tester.Action = (int callCount) =>
            {
                var errorResponse = (HttpWebResponse)MockWebResponse.CreateFromResource("404Response.txt");
                throw new HttpErrorResponseException(new HttpWebRequestResponseData(errorResponse));
            };

            var context = CreateTestContext();
            var request = new GetBucketPolicyRequest
            {
                BucketName = "nonexistentbucket"
            };

            ((RequestContext)context.RequestContext).OriginalRequest = request;
            ((RequestContext)context.RequestContext).Request         = new GetBucketPolicyRequestMarshaller().Marshall(request);
            ((RequestContext)context.RequestContext).Unmarshaller    = GetBucketPolicyResponseUnmarshaller.Instance;

            await RuntimePipeline.InvokeAsync <AmazonWebServiceResponse>(context);

            Assert.AreEqual(1, Tester.CallCount);
        }
Пример #17
0
        public void Test400WithErrorTypeHeader()
        {
            Tester.Reset();
            Tester.Action = (int callCount) =>
            {
                var errorResponse = (HttpWebResponse)MockWebResponse.CreateFromResource("400WithErrorTypeHeader.txt");
                throw new HttpErrorResponseException(new HttpWebRequestResponseData(errorResponse));
            };

            var context = CreateTestContext();

            ((RequestContext)context.RequestContext).Unmarshaller =
                Amazon.IotData.Model.Internal.MarshallTransformations.UpdateThingShadowResponseUnmarshaller.Instance;

            var exception = Utils.AssertExceptionExpected <Amazon.IotData.Model.InvalidRequestException>(() =>
            {
                RuntimePipeline.InvokeSync(context);
            });

            Assert.AreEqual("InvalidRequestException", exception.ErrorCode);
            Assert.AreEqual(HttpStatusCode.BadRequest, exception.StatusCode);
            Assert.AreEqual(1, Tester.CallCount);
        }
        protected virtual IExecutionContext CreateTestContext(AbstractAWSSigner signer, ResponseUnmarshaller responseUnmarshaller, ClientConfig config)
        {
            var putObjectRequest = new PutObjectRequest
            {
                Key         = "TestKey",
                BucketName  = "TestBucket",
                ContentBody = "Test Content"
            };

            if (responseUnmarshaller == null)
            {
                responseUnmarshaller = PutObjectResponseUnmarshaller.Instance;
            }

            var requestContext = new RequestContext(true, signer == null ? new NullSigner() : signer)
            {
                OriginalRequest = putObjectRequest,
                Request         = new PutObjectRequestMarshaller().Marshall(putObjectRequest),
                Unmarshaller    = responseUnmarshaller,
                ClientConfig    = config ?? new AmazonS3Config
                {
                    RegionEndpoint = RegionEndpoint.USEast1
                }
            };

            requestContext.Request.Endpoint = new Uri("https://s3.amazonaws.com");

            var putObjectResponse = MockWebResponse.CreateFromResource("PutObjectResponse.txt")
                                    as HttpWebResponse;

            return(new Amazon.Runtime.Internal.ExecutionContext(requestContext,
                                                                new ResponseContext
            {
                HttpResponse = new HttpWebRequestResponseData(putObjectResponse)
            }));
        }