locked
Mock / Mole HttpContext

    Question

  • Hello,

    I have a Http Module for Authentication that uses cookies that i want to test.
    So i need to mock the HttpContext and make the HttpRequest Cookies and HttpResponse Cookies work, but i cannot find out how to do it.

    Do anybody know how it can be done ?

    I have been able to create the HttpContext and set a HttpRequest and a HttpResponse, but i cant seem to do anything about the Cookies collection.
    It keeps throwing an exception that the property is not moled.

    Do anybody know of a complete (or almost) working mock / mole of the HttpContext which can be found somewhere and used instead of trying to create everything myself ?

    Hope someone can help me.


    Best regards

    Martin :)

    Friday, September 10, 2010 12:19 PM

Answers

  • Each mole has a 'fallback behavior' that triggers whenever a method was invoke and a mole was not registered. The default behavior is to throw an exception but this can be changed. For example,

        var mReq = new MHttpRequest(ctx.Request)
        { InstanceBehavior = MoleBehaviors.Fallthrough };

    In this case, attach the Fallthrough behavior will simply disable Moles for methods not registered.


    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Saturday, September 11, 2010 6:17 AM
    Owner

All replies

  • Your best bet is to take a look at the source of the System.Web behaviors, which is in the Behaviors.zip that ships with the installer (open the System.Web.Behaviors). It contains the full source of the behavior for HttpContext, HttpServer, ... I will extend the behavior to support the cookies in the next version meanwhile,

        var cookies = new HttpCookieCollection(); // no using moles
        var request = new MHttpRequest():
        var context = new MHttpContext();

        // hookup
        MHttpContext.CurrentGet = () => context;
        context.RequestGet = () => request;
        request.CookiesGet = () => cookies;


    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Friday, September 10, 2010 1:45 PM
    Owner
  • Thanks for your quick reply.

    Currently my problem is that if i do something like this

    HttpRequest req = new HttpRequest("", "http://www.mysiteishere.com", "");		
    HttpResponse rsp = new HttpResponse(new StringWriter());
    HttpContext ctx = new HttpContext(req, rsp);
    
    


    It will create a HttpContext with most stuff initalized (cookies etc) which is great.
    But I need to update a few Properties that is not initalized correctly.

    If i create a Mole of HttpRequest like this

    MHttpRequest mReq = new MHttpRequest(ctx.Request);<br/>
    

    It will override all the stuff already initalized and i have to setup the whole Object.
    How do create a mole that doesnt do anything with the Object already created but just lets me change whatever I want ?


    For some weird reason if i do something like this anywhere in my code (even though i put it in a method that is not executed)

    var newCtx = new BHttpContext();
    or
    var newRes = new BHttpResponse();

    Creating the HttpContext (first code example) will throw an exception. Is this a bug or maybe I dont undertand how it works ?


    Martin :)

    Friday, September 10, 2010 11:49 PM
  • Each mole has a 'fallback behavior' that triggers whenever a method was invoke and a mole was not registered. The default behavior is to throw an exception but this can be changed. For example,

        var mReq = new MHttpRequest(ctx.Request)
        { InstanceBehavior = MoleBehaviors.Fallthrough };

    In this case, attach the Fallthrough behavior will simply disable Moles for methods not registered.


    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Saturday, September 11, 2010 6:17 AM
    Owner