0

I can't figure out why this unit test won't work. It is based off of the code sample found here: http://weblogs.asp.net/rashid/archive/2009/03/12/unit-testable-httpmodule-and-httphandler.aspx

[Test]
public void when_user_doesnt_authenticate_returns_304_status()
{
    _httpContext = new Mock<HttpContextBase>();
    _httpRequest = new Mock<HttpRequestBase>();
    _httpResponse = new Mock<HttpResponseBase>();          

    _httpContext.SetupGet(context => context.Request).Returns(_httpRequest.Object);        

    var module = new TAS.HttpModule.TASHttpModule();         
    _httpResponse.SetupSet(response => response.StatusCode = 304).Verifiable();
    _httpContext.SetupGet(context => context.Response).Returns(_httpResponse.Object);         
    module.OnAuthenticateRequest(_httpContext.Object);   
    _httpResponse.VerifyAll();

}

code from http module ( simplified to demonstrate problem)

public class TASHttpModule : BaseHttpModule

 public override void OnAuthenticateRequest(HttpContextBase context)
 {

    HttpResponseBase response = context.Response;
    response.StatusCode = 403;
    response.StatusDescription = "Access Denied";
    response.End();
 }
}
public class BaseHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += (sender, e) => OnBeginRequest(new HttpContextWrapper(((HttpApplication)sender).Context));
            context.Error += (sender, e) => OnError(new HttpContextWrapper(((HttpApplication)sender).Context));
            context.EndRequest += (sender, e) => OnEndRequest(new HttpContextWrapper(((HttpApplication)sender).Context));
            context.AuthenticateRequest += (sender, e) => OnAuthenticateRequest(new HttpContextWrapper(((HttpApplication)sender).Context));
        }

        public void Dispose()
        {
        }

        public virtual void OnBeginRequest(HttpContextBase context)
        {
        }

        public virtual void OnError(HttpContextBase context)
        {
        }

        public virtual void OnEndRequest(HttpContextBase context)
        {
        }
        public virtual void OnAuthenticateRequest(HttpContextBase context)
        {
        }
    }

The basic problem is that the response.StatusCode value cannot be set. It always reverts back to zero. So the test that checks the 304 status always fails. Any help would be greatly appreciated!

voam
  • 1,006
  • 13
  • 24

1 Answers1

-1

I have just managed to prove 304 and 403 are not equal. Note to self: use http status code enumeration instead of numbers directly.

voam
  • 1,006
  • 13
  • 24