views:

422

answers:

1

We have an ASP.Net site that redirects you to a url that shows a session-id. like this:

http://localhost/%28S%28f3rjcw45q4cqarboeme53lbx%29%29/main.aspx

This id is unique with every request.

Is it possible to test this site using a standard visual studio 2008/2010 webtest? How can I provide the test this data?

I have to call a couple of different pages using that same id.

+1  A: 

Yes, it is relatively easy to do this. You will need to create a coded webtest however.

In my example we have a login post that will return the url including the session string. Just after the we yield the login post request (request3) to the enumerator I call the following.

WebTestRequest request3 = new WebTestRequest((this.Context["WebServer1"].ToString() + "/ICS/Login/English/Login.aspx"));
//more request setup code removed for clarity
yield return request3;
string responseUrl = Context.LastResponse.ResponseUri.AbsoluteUri;
string cookieUrl = GetUrlCookie(responseUrl, this.Context["WebServer1"].ToString(),"/main.aspx"); 
request3 = null;

Where GetUrlCookie is something like this:

public static string GetUrlCookie(string fullUrl, string webServerUrl, string afterUrlPArt)
    {
        string result = fullUrl.Substring(webServerUrl.Length);
        result = result.Substring(0, result.Length - afterUrlPArt.Length);
        return result;
    }

Once you have the session cookie string, you can substitute it really easy in any subsequent urls for request/post e.g.

WebTestRequest request4 = new WebTestRequest((this.Context["WebServer1"].ToString() + cookieUrl + "/mySecureForm.aspx"));

I apologise for my code being so rough, but it was deprecated in my project and is pulled from the first version of the codebase - and for saying it was easy :)

For any load testing, depending on your application, you may have to come up with a stored procedure to call to provide distinct login information each time the test is run.

Note, because the response url cannot be determined ahead of time, for the login post you will have to temporarily turn off the urlValidationEventHandler. To do this I store the validationruleeventhandler in a local variable:

        ValidateResponseUrl validationRule1 = new ValidateResponseUrl();
        urlValidationRuleEventHandler = new EventHandler<ValidationEventArgs>(validationRule1.Validate);

So can then turn it on and off as I require:

this.ValidateResponse -= urlValidationRuleEventHandler ;
this.ValidateResponse += urlValidationRuleEventHandler ;

The alternative is to code your own such as this (reflectored from the Visual Studio code and changed to be case insensitive.

class QueryLessCaseInsensitiveValidateResponseUrl : ValidateResponseUrl
{
    public override void Validate(object sender, ValidationEventArgs e)
    {
        Uri uri;
        string uriString = string.IsNullOrEmpty(e.Request.ExpectedResponseUrl) ? e.Request.Url : e.Request.ExpectedResponseUrl;
        if (!Uri.TryCreate(e.Request.Url, UriKind.Absolute, out uri))
        {
            e.Message = "The request URL could not be parsed";
            e.IsValid = false;
        }
        else
        {
            Uri uri2;
            string leftPart = uri.GetLeftPart(UriPartial.Path);
            if (!Uri.TryCreate(uriString, UriKind.Absolute, out uri2))
            {
                e.Message = "The request URL could not be parsed";
                e.IsValid = false;
            }
            else
            {
                uriString = uri2.GetLeftPart(UriPartial.Path);
                ////this removes the query string
                //uriString.Substring(0, uriString.Length - uri2.Query.Length);
                Uri uritemp = new Uri(uriString);
                if (uritemp.Query.Length > 0)
                {
                    string fred = "There is a problem";
                }
                //changed to ignore case
                if (string.Equals(leftPart, uriString, StringComparison.OrdinalIgnoreCase))
                {
                    e.IsValid = true;
                }
                else
                {
                    e.Message = string.Format("The value of the ExpectedResponseUrl property '{0}' does not equal the actual response URL '{1}'. QueryString parameters were ignored.", new object[] { uriString, leftPart });
                    e.IsValid = false;
                }
            }
        }
    }
}
Nat