Testing a web service for missing required fields in .NET.

Some of my current work involves using an external web service for which I have generated a client proxy (using the WSDL.exe tool).

I need to check that a web service is correctly handling missing required fields. For example, the last name and file name are required β€” if they are not present in the call, then a SOAP error block must be returned.

As you might have guessed, I am unable to exclude any required fields from my web service call when using auto-generated proxy code due to compile-time checks against the schema.

Instead, I used HttpWebRequest and HttpWebResponse to send / receive a hand-formatted SOAP envelope to the web service. This works, but since the service returns an HTTP 500 status code, an exception is thrown on the client and the response (containing this SOAP error block that I need) is NULL. Basically, I need a reverse flow to get the error data, so I can complete my unit test. I know the correct data is being returned because I can see this in my Fiddler trace, but I just cannot figure it out in my code.

Here's what I do for a manual call, with the names changed to protect the innocent:

private INVALID_POST = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:Envelope ...rest of SOAP envelope contents...";

private void DoInvalidRequestTest()
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://myserviceurl.svc");
    request.Method = "POST";
    request.Headers.Add("SOAPAction",
        "\"https://myserviceurl.svc/CreateTestThing\"");
    request.ContentType = "text/xml; charset=utf-8";
    request.ContentLength = INVALID_POST.Length;
    request.KeepAlive = true;

    using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
    {
        writer.Write(invalidPost);
    }

    try
    {
        // The following line will raise an exception because of the 500 code returned
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string reply = reader.ReadToEnd();
            }
        }
    }
    catch (Exception ex)
    {
        ... My exception handling code ...
    }
}

      

Please note that I am not using WCF, only WSE 3.

+1


a source to share


1 answer


How do I write my own SoapHttpClientProtocol and use SoapExtensions?



    [WebServiceBinding()]
    public class WebService
    : System.Web.Services.Protocols.SoapHttpClientProtocol
    {
        [SoapTrace]
        public object[] MethodTest(string param1, string param2)
        {
            object[] result = this.Invoke("MethodTest", new object[] { param1, param2 });
            return (object[])result[0];
        }
    }

      

+1


a source







All Articles