How can I programmatically Fail a WebTest?

I am trying to validate a header value exist in a response. Since ExtractHttpHeader only supports checking for the existence of header names and not values,  I am traversing the test Context values looking for a specified key/value pair. I want to fail the web test if the values do not match.



Answer this question

How can I programmatically Fail a WebTest?

  • Ibrahim Dwaikat

    The best way is to write a custom validation rule. Public properties in your rule are exposed in the property grid, so you can add properties for the header name and expected value. Then use the

    e.Response.Headers[m_header]

    header collection to check for the value at runtime in the Validate method of the rule.

    Good luck!

    Ed.



  • Yusufp

    Great response!  Your recommendation was dead on.  Here is the code snippet used in Validate() for anyone else needing to validate response header values:

    for(int i = 0; i < e.Response.Headers.Keys.Count; ++i)

    {

      if (e.Response.Headers.GetKey(i) == HeaderStringParameterName)

      {

        string strVal = e.Response.Headers.Get(HeaderStringParameterName);

        if (strVal == HeaderStringParameterExpectedValue)

        {

          e.IsValid = true;

          e.Message = "Found";

          break;

        }

      }

    }


  • How can I programmatically Fail a WebTest?