HOWTO: Useful ASP page to return configurable HTTP Status codes


Sometimes, you just want a simple test page on IIS to do some automated testing of your code that interacts with IIS. Here is another useful one in the series.


Question:


Hi,


I have some negative testing therefore need to have IIS to return 403, 404 or 500. How can I configure each? An automated way would be the best but manual is OK too.


Thanks,


Answer:


There are a couple of approaches to do this. You can either:



  1. Configure IIS to go down the actual custom error codepath to generate responses with those status codes

  2. Make a request to a URL that generates responses with those status codes

The former can be accomplished in the following manner:



  1. There are many types of 403s. For example, you can configure IIS to:


    • Disallow Read permissions and then try to access a resource handled by the Static File Handler.

    • Disallow Dir Browsing and make sure no file resource matches DefaultDoc and then make a request to “/”

    • Etc… just check out the 403*.htm custom error pages for ideas

  2. 404 can be generated by making a request to /NotExist.htm

  3. 500 can be generated by making a request to an ASP page with a syntax error (for example, just remove the %> tag of an ASP page

The latter is easier to configure/use and is shown as a code sample at the end of this blog entry. You just need to copy the ASP page to IIS, make sure ASP is enabled, and make a request to that ASP page with the correct querystring parameter. i.e.


/ResponseStatus.asp?status=403%20Test!


//David

<%
DIM status
status = Request.QueryString( “status” )

IF NOT IsEmpty( status ) THEN
Response.Status = status
END IF
%>

Comments (5)

  1. Rich says:

    If I try the code above on my IIS 6 box on Windows 2003 SP1 depending on my IE 6 "Show friendly HTTP Error Messages" setting, I get different results.  If for example I send http://mysite/ResponseStatus.asp?status=500 and ShowFriendly=1, then I get the "Server Error" response I’m expecting.  If ShowFriendly=0 or cleared, then I receive a blank page that contains the following HTML:

    "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <HTML><HEAD>

    <META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>

    <BODY></BODY></HTML>"

    Where is this coming from?  Auto-generated by IIS? IE?

    Thanks for the help,

    Rich

  2. David.Wang says:

    Rich – the ASP code sends a HTTP response with the specified status code and no entity body. Thus, what you are observing is browser-dependent behavior.

    If ShowFriendly=1, IE chooses to show its hardcoded "friendly" HTML server error page, regardless of what the server sent.

    If ShowFriendly=0, IE parses the non-standard HTML body of "" sent by the server, and instead generates the more proper HTML text you copy/pasted and displays that.

    All of this is by-design.

    //David

  3. R0y says:

    try Send HTTP Tool to verify you HTTP status code.

  4. SavantTools says:

    Nice suggestion.  Here is a page that does almost exactly what you suggest.  It may be useful to people that want a quick and easy want to test HTTP error codes and status codes.

    savanttools.com/test-http-status-codes.asp

  5. Kamesh says:

    HOw can i set http redirect such as 403 for direct accessing images such in appache I am using .htaccess

    RewriteEngine on

    RewriteCond %{HTTP_REFERER} !^http://(www.)?localhost [NC]

    RewriteCond %{HTTP_REFERER} !^http://(www.)?localhost.*$ [NC]

    RewriteRule .(gif|jpg)$ – [F]