Beispiel #1
0
        public void RemoteEntityNotFoundException_Constructor_MessageArgument_Null()
        {
            // This test verifies that the default constructor works.
            // Note: this test is useless except for code coverage since we are testing the constructor with no custom logic.
            var ex = new RemoteEntityNotFoundException(null);

            Assert.AreEqual("Exception of type 'Rightpoint.UnitTesting.Demo.Mvc.Exceptions.RemoteEntityNotFoundException' was thrown.", ex.Message);
            Assert.IsNull(ex.InnerException);
        }
Beispiel #2
0
        public void RemoteEntityNotFoundException_Constructor_MessageArgument_Valid()
        {
            // This test verifies that the default constructor works.
            // Note: this test is useless except for code coverage since we are testing the constructor with no custom logic.
            var ex = new RemoteEntityNotFoundException("test");

            Assert.AreEqual("test", ex.Message);
            Assert.IsNull(ex.InnerException);
        }
Beispiel #3
0
        public void RemoteEntityNotFoundException_Constructor_Serialization()
        {
            // This test verifies that the default constructor works.
            // Note: this test is useless except for code coverage since we are testing default serialization.
            RemoteEntityNotFoundException inputException = new RemoteEntityNotFoundException("test", new Exception("Inner"));

            byte[] bytes = BinarySerializer.Serialize(inputException);
            Assert.IsNotNull(bytes);

            RemoteEntityNotFoundException deserializedException = BinarySerializer.Deserialize <RemoteEntityNotFoundException>(bytes);

            Assert.IsNotNull(deserializedException);
            Assert.AreEqual(inputException.Message, deserializedException.Message);
            Assert.IsNotNull(deserializedException.InnerException);
            Assert.AreEqual(typeof(Exception), deserializedException.InnerException.GetType());
            Assert.AreEqual(inputException.InnerException.Message, deserializedException.InnerException.Message);
            Assert.IsNull(deserializedException.InnerException.InnerException);
        }
        private static async Task <Exception> ProcessWebExcpetionAsync(WebException ex)
        {
            string         response            = null;
            string         responseContentType = null;
            Uri            responseUri         = null;
            var            status            = ex.Status;
            HttpStatusCode?statusCode        = null;
            string         statusDescription = null;

            var webResponse = ex.Response as HttpWebResponse;

            if (webResponse != null)
            {
                responseUri         = webResponse.ResponseUri;
                responseContentType = webResponse.ContentType;
                statusCode          = webResponse.StatusCode;
                statusDescription   = webResponse.StatusDescription;

                var responseStream = webResponse.GetResponseStream();
                if (responseStream != null)
                {
                    try
                    {
                        if (responseStream.CanRead)
                        {
                            using (var reader = new StreamReader(responseStream))
                            {
                                response = await reader.ReadToEndAsync();
                            }
                        }
                    }
                    finally
                    {
                        responseStream.Dispose();
                        responseStream = null;
                    }
                }
            }

            Exception newEx   = null;
            var       message = string.IsNullOrWhiteSpace(response) ? "Unknown server error" : response;

            if (statusCode.HasValue)
            {
                switch (statusCode.Value)
                {
                case HttpStatusCode.NotFound:
                    newEx = new RemoteEntityNotFoundException(message, ex);
                    break;

                case HttpStatusCode.BadRequest:
                    newEx = new RemoteInvalidOperationException(message, ex);
                    break;

                default:
                    newEx = new RemoteException(message, ex);
                    break;
                }
            }
            else
            {
                newEx = new RemoteException(message, ex);
            }

            if (string.IsNullOrWhiteSpace(response) == false)
            {
                newEx.Data.Add("Response", response);
            }
            if (string.IsNullOrWhiteSpace(responseContentType) == false)
            {
                newEx.Data.Add("ContentType", responseContentType);
            }
            if (responseUri != null)
            {
                newEx.Data.Add("ResponseUri", responseUri.ToString());
            }
            if (statusCode.HasValue)
            {
                newEx.Data.Add("StatusCode", statusCode.ToString());
            }
            if (string.IsNullOrWhiteSpace(statusDescription) == false)
            {
                newEx.Data.Add("StatusDescription", statusDescription);
            }
            newEx.Data.Add("Status", status.ToString());

            return(newEx);
        }