示例#1
0
        private async Task ProcessGetRequestHttpRequestExceptionTestAsync()
        {
            var exceptionObject = new ExceptionObject()
            {
                Description = "This is a test Exception Description",
                Message     = "This is a test Exception Message",
                Type        = EType.WrongHeaders
            };

            using (ShimsContext.Create())
            {
                ShimHttpClient.AllInstances.SendAsyncHttpRequestMessageCancellationToken = (client, message, arg3) =>
                {
                    //var webEx = new WebException("", WebExceptionStatus.UnknownError, )
                    throw new HttpRequestException();
                };

                ShimHttpWebResponse.AllInstances.ResponseStreamGet = (response) =>
                {
                    return(ShimsContext.ExecuteWithoutShims(() => response.GetResponseStream()));
                };

                try
                {
                    var result = await UncommonRequestHelper.ProcessGetRequestAsync <Person>("http://www.xciles.com/");

                    Assert.Fail("Should not be able to be here...");
                }
                catch (UncommonRequestException ex)
                {
                    Assert.IsTrue(ex.RequestExceptionStatus == EUncommonRequestExceptionStatus.ServiceError);
                    Assert.IsTrue(ex.StatusCode == HttpStatusCode.BadRequest);

                    //var responseResult = ex.ConvertExceptionResponseToObject<ExceptionObject>();
                    //Assert.IsTrue(responseResult != null);
                    //Assert.IsTrue(responseResult.Description == exceptionObject.Description);
                    //Assert.IsTrue(responseResult.Message == exceptionObject.Message);
                    //Assert.IsTrue(responseResult.Type == exceptionObject.Type);
                }
            }
        }
示例#2
0
        private async Task ProcessGetRequestWebExceptionTestAsync()
        {
            var exceptionObject = new ExceptionObject()
            {
                Description = "This is a test Exception Description",
                Message     = "This is a test Exception Message",
                Type        = EType.WrongHeaders
            };

            using (ShimsContext.Create())
            {
                ShimHttpClient.AllInstances.SendAsyncHttpRequestMessageCancellationToken = (client, message, arg3) =>
                {
                    return(Task.FromResult(new HttpResponseMessage()
                    {
                        Content = new StringContent(JsonConvert.SerializeObject(exceptionObject)),
                        StatusCode = HttpStatusCode.BadRequest
                    }));
                };

                try
                {
                    var result = await UncommonRequestHelper.ProcessGetRequestAsync <Person>("http://www.xciles.com/");

                    Assert.Fail("Should not be able to be here...");
                }
                catch (UncommonRequestException ex)
                {
                    Assert.IsTrue(ex.RequestExceptionStatus == EUncommonRequestExceptionStatus.ServiceError);
                    Assert.IsTrue(ex.StatusCode == HttpStatusCode.BadRequest);

                    var responseResult = ex.ConvertExceptionResponseToObject <ExceptionObject>();
                    Assert.IsTrue(responseResult != null);
                    Assert.IsTrue(responseResult.Description == exceptionObject.Description);
                    Assert.IsTrue(responseResult.Message == exceptionObject.Message);
                    Assert.IsTrue(responseResult.Type == exceptionObject.Type);
                }
            }
        }