Thursday, 2 March 2006

Unit Testing Web Applications in VSTS

I just realized I haven't blogged anything since the end of January. This is because I taught our first VSTS class in Boston at the end of January, and the buildup to that class was so intense that I decided to take it easy for a week or so, then other things start to pile up and less important things like blogging got left behind. Anyway, enough of my whining!

One of the chapters I taught in January was on unit testing web applications and web services. This is different to the web testing framework that comes as part of VSTS. In web testing you are performing functional testing of web applications. In VSTS you can also run in-web server tests on the code that makes up a web application. I will write another entry explaining how do to this later but here I wanted to point out an issue I found when unit testing web applications.

The lab we had written was ASP code had some security stuff going on, in particular it was using forms authentication and had an authorization entry that looked like this

    <authorization>
      <deny users="?"/>
    </authorization>
and then another section to allow access to the CreateUser.aspx page that looked like
  <location path="CreateUser.aspx">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
This is a fairly normal setup for ASP, deny unathenticated access to the site but allow all users access to specific pages (in this case we want a user to be able to create an account for themselves).

This is pretty innocuous code until it comes to unit testing in VSTS. I have a 'proof of concept' unit test that simply does this:

[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost(@"C:\path to web site", "/StocksWebSite")]
[UrlToTest("http://localhost/StocksWebSite")]
public void WebServerTest()
{
    HttpContext context = HttpContext.Current;

    Assert.IsNotNull(context);
}
If you run this test using the web.config I showed above you will get the following error: The web site could not be configured correctly; getting ASP.NET process information failed. Requesting 'http://localhost:10672/StocksWebSite/VSEnterpriseHelper.axd' returned an error: The information returned is invalid.. Notice the reference to VSEnterpriseHelper.axd. So what is going on here?

It turns out that VSEnterpriseHelper.axd is the URL used by VSTS to trigger the runtime to load our tests into the web server (another entry on this in the future). Therefore that URL needs to be available to run. With the web.config we have all URLs (except CreateUser.aspx) are blocked to un-authenticated users, which means that VSEnterpriseHelper.axd cannot be accessed. There is one simple fix for this, allow un-authenticated access to the URL. You can add the following to your web.config and the testing code will work:

<location path="VSEnterpriseHelper.axd">
  <system.web>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
</location>

Hope that helps the next person who hits this problem,

Posted by kevin at 9:46 AM in Net

 

Your comment:

SCode: (*) Generate another code
SCode

Please enter the code as seen in the image above to post your comment.

(not displayed)
 
 
 

Live Comment Preview:

 
« March »
SunMonTueWedThuFriSat
   1234
567891011
12131415161718
19202122232425
262728293031