/// <summary>
        /// Sets up the HttpContext objects to simulate a request.
        /// </summary>
        /// <param name="url">The Uri to hit (via POST).</param>
        /// <param name="httpVerb">The HTTP method to use.</param>
        /// <param name="formVariables">The form variables to send.</param>
        /// <param name="headers">The headers to send.</param>
        public virtual HttpSimulator SimulateRequest(
            Uri url,
            HttpVerb httpVerb = HttpVerb.GET,
            NameValueCollection formVariables = null,
            NameValueCollection headers       = null)
        {
            HttpContext.Current = null;

            ParseRequestUrl(url);

            if (ResponseWriter == null)
            {
                builder        = new StringBuilder();
                ResponseWriter = new StringWriter(builder);
            }

            SetHttpRuntimeInternals();

            string query = ExtractQueryStringPart(url);

            if (formVariables != null)
            {
                _formVars.Add(formVariables);
            }

            if (_formVars.Count > 0)
            {
                httpVerb = HttpVerb.POST; //Need to enforce
            }
            if (headers != null)
            {
                _headers.Add(headers);
            }

            workerRequest = new SimulatedHttpRequest(ApplicationPath, PhysicalApplicationPath, PhysicalPath, Page, query, ResponseWriter, host, port, httpVerb.ToString());

            workerRequest.Form.Add(_formVars);
            workerRequest.Headers.Add(_headers);

            if (_referer != null)
            {
                workerRequest.SetReferer(_referer);
            }

            InitializeSession();

            InitializeApplication();

            #region Console Debug Info

            //Console.WriteLine("host: " + host);
            //Console.WriteLine("virtualDir: " + applicationPath);
            //Console.WriteLine("page: " + localPath);
            //Console.WriteLine("pathPartAfterApplicationPart: " + _page);
            //Console.WriteLine("appPhysicalDir: " + physicalApplicationPath);
            //Console.WriteLine("Request.Url.LocalPath: " + HttpContext.Current.Request.Url.LocalPath);
            //Console.WriteLine("Request.Url.Host: " + HttpContext.Current.Request.Url.Host);
            //Console.WriteLine("Request.FilePath: " + HttpContext.Current.Request.FilePath);
            //Console.WriteLine("Request.Path: " + HttpContext.Current.Request.Path);
            //Console.WriteLine("Request.RawUrl: " + HttpContext.Current.Request.RawUrl);
            //Console.WriteLine("Request.Url: " + HttpContext.Current.Request.Url);
            //Console.WriteLine("Request.Url.Port: " + HttpContext.Current.Request.Url.Port);
            //Console.WriteLine("Request.ApplicationPath: " + HttpContext.Current.Request.ApplicationPath);
            //Console.WriteLine("Request.PhysicalPath: " + HttpContext.Current.Request.PhysicalPath);
            //Console.WriteLine("HttpRuntime.AppDomainAppPath: " + HttpRuntime.AppDomainAppPath);
            //Console.WriteLine("HttpRuntime.AppDomainAppVirtualPath: " + HttpRuntime.AppDomainAppVirtualPath);
            //Console.WriteLine("HostingEnvironment.ApplicationPhysicalPath: " + HostingEnvironment.ApplicationPhysicalPath);
            //Console.WriteLine("HostingEnvironment.ApplicationVirtualPath: " + HostingEnvironment.ApplicationVirtualPath);

            #endregion

            return(this);
        }
Example #2
0
        /// <summary>
        /// Sets up the HttpContext objects to simulate a request.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="httpVerb"></param>
        /// <param name="formVariables"></param>
        /// <param name="headers"></param>
        protected virtual HttpSimulator SimulateRequest(Uri url, HttpVerb httpVerb, NameValueCollection formVariables, NameValueCollection headers)
        {
            HttpContext.Current = null;

            ParseRequestUrl(url);

            if (this.responseWriter == null)
            {
                this.builder = new StringBuilder();
                this.responseWriter = new StringWriter(builder);
            }

            SetHttpRuntimeInternals();

            string query = ExtractQueryStringPart(url);

            if (formVariables != null)
                _formVars.Add(formVariables);

            if (_formVars.Count > 0)
                httpVerb = HttpVerb.POST; //Need to enforce this.

            if (headers != null)
                _headers.Add(headers);

            this.workerRequest = new SimulatedHttpRequest(ApplicationPath, PhysicalApplicationPath, PhysicalPath, Page, query, this.responseWriter, host, port, httpVerb.ToString());

            this.workerRequest.Form.Add(_formVars);
            this.workerRequest.Headers.Add(_headers);

            if (_referer != null)
                this.workerRequest.SetReferer(_referer);

            InitializeSession();

            InitializeApplication();

            #region Console Debug INfo

            Console.WriteLine("host: " + host);
            Console.WriteLine("virtualDir: " + applicationPath);
            Console.WriteLine("page: " + localPath);
            Console.WriteLine("pathPartAfterApplicationPart: " + _page);
            Console.WriteLine("appPhysicalDir: " + physicalApplicationPath);
            Console.WriteLine("Request.Url.LocalPath: " + HttpContext.Current.Request.Url.LocalPath);
            Console.WriteLine("Request.Url.Host: " + HttpContext.Current.Request.Url.Host);
            Console.WriteLine("Request.FilePath: " + HttpContext.Current.Request.FilePath);
            Console.WriteLine("Request.Path: " + HttpContext.Current.Request.Path);
            Console.WriteLine("Request.RawUrl: " + HttpContext.Current.Request.RawUrl);
            Console.WriteLine("Request.Url: " + HttpContext.Current.Request.Url);
            Console.WriteLine("Request.Url.Port: " + HttpContext.Current.Request.Url.Port);
            Console.WriteLine("Request.ApplicationPath: " + HttpContext.Current.Request.ApplicationPath);
            Console.WriteLine("Request.PhysicalPath: " + HttpContext.Current.Request.PhysicalPath);
            Console.WriteLine("HttpRuntime.AppDomainAppPath: " + HttpRuntime.AppDomainAppPath);
            Console.WriteLine("HttpRuntime.AppDomainAppVirtualPath: " + HttpRuntime.AppDomainAppVirtualPath);
            Console.WriteLine("HostingEnvironment.ApplicationPhysicalPath: " + HostingEnvironment.ApplicationPhysicalPath);
            Console.WriteLine("HostingEnvironment.ApplicationVirtualPath: " + HostingEnvironment.ApplicationVirtualPath);

            #endregion

            return this;
        }