0

I am trying unit-test a method. The method takes in a single argument, which belongs to the class HttpContext in NET Framework Class Library. So I guess I need to prepare an object of HttpContext.

All that the method needs from the argument, say context, are

  • context.Request.QueryString
  • context.Request.Headers

Here are my thoughts and questions

  1. To learn how to use HttpContext, I click going to definition in Visual Studio, I found that the class has a constructor

        public HttpContext(HttpRequest request, HttpResponse response);
    

    so I need to provide its two arguments, in order to create a object of the class.

    To provide the first argument, which is an object of HttpRequest, I found its constructor

    //
    // Summary:
    //     Initializes an System.Web.HttpRequest object.
    //
    // Parameters:
    //   filename:
    //     The name of the file associated with the request.
    //
    //   url:
    //     The information regarding the URL of the current request.
    //
    //   queryString:
    //     The entire query string sent with the request (everything after the'?').
    public HttpRequest(string filename, string url, string queryString);
    
    • What does filename mean?

    • Which is url, the full url containing the query strings, or just the part before ??

    To provide the second argument of class HttpResponse, I found its constructor

    public HttpResponse(TextWriter writer);
    

    The method to be tested actually doesn't need context.Response. So can I simply create this as the second argument to the constructor of HttpContext:

    writer = new TextWriter ();
    response = new HttpResponse(writer);
    

    given that the constructors of TextWriter are protected?

    protected TextWriter();
    protected TextWriter(IFormatProvider formatProvider);
    
  2. After I can create an object of HttpContext, do I need to call some method to parse the url and query strings to obtain

    • context.Request.QueryString
    • context.Request.Headers

    or the HttpContext will implicitly do the work without me explicitly calling any method?

Thanks.

Tim
  • 1
  • 122
  • 314
  • 481
  • 1
    Probably contain useful answers: http://stackoverflow.com/q/4379450/11683, http://stackoverflow.com/q/1452418/11683 – GSerg Feb 15 '17 at 22:27
  • Some sample code to create a mock HTTPContext here: http://stackoverflow.com/a/10126711/4191475 – Andy Arndt Feb 15 '17 at 22:34

0 Answers0