none
Testing WebFaultException exceptions RRS feed

  • Question

  • I'm trying to write unit tests to make sure that the correct HttpStatusCodes are being returned during various scenarios.

    This is what my service code looks like:

    public void UpdateCase(Case caseData)
    {
       try
       {
           _visionObject.UpdateCase(caseData);
       }
       catch
       {
          throw;
       }
     }

    And this is the class code:

    public void UpdateCase(Case caseData)
    {
        var selectedCase = _cases.FirstOrDefault(c =>
             c.CaseId.Equals(caseData.CaseId));
    
        if (selectedCase == null)
        {
           ErrorData errorData = new ErrorData("Case " +
                 caseData.CaseId + " not found.", "1054");
           throw new WebFaultException<ErrorData>(errorData,
                     HttpStatusCode.NoContent);
         }
         
         selectedCase.Caller = caseData.Caller;
    }
    


    Basically if the record is not found, I want to return an error.

    Running the call in Fiddler works perfectly. I can see the http status code and the ErrorData json data. But I'm having a hard time creating a unit test to validate the http status code. I've tried multiple ways, each resulting in different results:

    This one does not catch any errors

            [TestMethod]
            public void UpdateInvalidCase()
            {
                Case updatedCaseData = new Case();
    
                updatedCaseData.Caller = "James Bond";
                updatedCaseData.CaseId = "008";
                try
                {
                 
                    var request = (HttpWebRequest)HttpWebRequest.Create("http://localhost:58569/Services/VisionService.svc/case");
                    request.Accept = "application/json";
                    request.ContentType = "application/json";
                    request.Method = "PUT";
                    var serializer = new DataContractJsonSerializer(typeof(Case));
                    var requestStream = request.GetRequestStream();
                    serializer.WriteObject(requestStream, updatedCaseData);
                    requestStream.Close();
                    var response = request.GetResponse();
                    
                    string s = "";
                }
                catch (WebException webEx)
                {
                    Assert.AreEqual(405, (int)((HttpWebResponse)webEx.Response).StatusCode, "Expected 405 error but received " + ((int)((HttpWebResponse)webEx.Response).StatusCode).ToString());
                }
                catch(Exception ex)
                {
                    string s = ex.Message;
                }
    
    
            }
    

    This one returns an Exception type(Expecting element 'root' from namespace ''.. Encountered 'None'  with name '', namespace ''. ")

            [TestMethod]
            public void UpdateInvalidCase()
            {
                Case updatedCaseData = new Case();
    
                updatedCaseData.Caller = "James Bond";
                updatedCaseData.CaseId = "008";
                try
                {
                    var request = (HttpWebRequest)HttpWebRequest.Create("http://localhost:58569/Services/VisionService.svc/case");
                    request.Accept = "application/json";
                    request.ContentType = "application/json";
                    request.Method = "PUT";
                    var serializer = new DataContractJsonSerializer(typeof(Case));
                    var requestStream = request.GetRequestStream();
                    serializer.WriteObject(requestStream, updatedCaseData);
                    requestStream.Close();
                    var response = request.GetResponse();
                    if (response.ContentLength == 0)
                    {
                        response.Close();
                    }
                    var responseStream = response.GetResponseStream();
                    var responseObject = (Case)serializer.ReadObject(responseStream);
                    responseStream.Close();
    
    
                    //var returnValue = SendDataToServer("http://localhost:58569/Services/VisionService.svc/case", "PUT", updatedCaseData);
    
    
    
                }
                catch (WebException webEx)
                {
                    Assert.AreEqual(405, (int)((HttpWebResponse)webEx.Response).StatusCode, "Expected 405 error but received " + ((int)((HttpWebResponse)webEx.Response).StatusCode).ToString());
                }
                catch(Exception ex)
                {
                    string s = ex.Message;
                }

    What is the best way to catch and confirm the returned httpstatuscode in a unit test for GET and other methods like POST and PUT?

    Thank you.

    Tuesday, January 13, 2015 9:39 PM

Answers

  • I found the issue. I was returning 2xx errors so the catch did not get executed. My bad. Returning 3xx, 4xx or 5xx status codes will hit the catch WebException code.
    • Marked as answer by ad_ Wednesday, January 14, 2015 5:00 PM
    Wednesday, January 14, 2015 5:00 PM

All replies

  • This is WCF forum, wrong forum?

    Vote if help you

    Wednesday, January 14, 2015 5:16 AM
  • Is this not a WCF test question? You could argue this is an HTTP question or a WebRequest question, or a WebResponse question, but I'm really trying to test a WCF functionality (WebFaultException).

    Where would you recommend to post this then?

    Thanks

    Wednesday, January 14, 2015 1:49 PM
  • I found the issue. I was returning 2xx errors so the catch did not get executed. My bad. Returning 3xx, 4xx or 5xx status codes will hit the catch WebException code.
    • Marked as answer by ad_ Wednesday, January 14, 2015 5:00 PM
    Wednesday, January 14, 2015 5:00 PM