public void ServerError()
 {
     try
     {
         template.Execute <object>("status/server", HttpMethod.GET, null, null);
         Assert.Fail("RestTemplate should throw an exception");
     }
     catch (Exception ex)
     {
         HttpServerErrorException serverErrorException = ex as HttpServerErrorException;
         Assert.IsNotNull(serverErrorException, "Exception HttpServerErrorException expected");
         Assert.AreEqual("GET request for 'http://localhost:1337/status/server' resulted in 500 - InternalServerError (Internal Server Error).", serverErrorException.Message);
         Assert.IsTrue(serverErrorException.Response.Body.Length == 0);
         Assert.IsTrue(serverErrorException.Response.Headers.ContentLength == 0);
         Assert.AreEqual(String.Empty, serverErrorException.GetResponseBodyAsString());
         Assert.AreEqual(HttpStatusCode.InternalServerError, serverErrorException.Response.StatusCode);
         Assert.AreEqual("Internal Server Error", serverErrorException.Response.StatusDescription);
     }
 }
        public void ServerErrorAsync()
        {
            ManualResetEvent manualEvent = new ManualResetEvent(false);
            Exception        exception   = null;

            template.ExecuteAsync("status/server", HttpMethod.GET, null, null,
                                  delegate(RestOperationCompletedEventArgs <object> args)
            {
                try
                {
                    Assert.IsFalse(args.Cancelled, "Invalid response");
                    Assert.IsNotNull(args.Error, "Invalid response");
                    HttpServerErrorException serverErrorException = args.Error as HttpServerErrorException;
                    Assert.IsNotNull(serverErrorException, "Exception HttpServerErrorException expected");
                    Assert.AreEqual("GET request for 'http://localhost:1337/status/server' resulted in 500 - InternalServerError (Internal Server Error).", serverErrorException.Message);
                    Assert.IsTrue(serverErrorException.Response.Body.Length == 0);
                    Assert.IsTrue(serverErrorException.Response.Headers.ContentLength == 0);
                    Assert.AreEqual(String.Empty, serverErrorException.GetResponseBodyAsString());
                    Assert.AreEqual(HttpStatusCode.InternalServerError, serverErrorException.Response.StatusCode);
                    Assert.AreEqual("Internal Server Error", serverErrorException.Response.StatusDescription);
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
                finally
                {
                    manualEvent.Set();
                }
            });

            manualEvent.WaitOne();
            if (exception != null)
            {
                throw exception;
            }
        }