/// <summary> /// Takes all the necessary steps to create a blog and set up the HTTP Context /// with the blog. /// </summary> /// <param name="subfolder">The 'virtualized' subfolder the blog lives in.</param> /// <param name="applicationPath">The name of the IIS virtual directory the blog lives in.</param> /// <param name="port">The port for this blog.</param> /// <param name="page">The page to request.</param> /// <param name="userName">Name of the user.</param> /// <param name="password">The password.</param> /// <returns> /// Returns a reference to a string builder. /// The stringbuilder will end up containing the Response of any simulated /// requests. /// </returns> internal static SimulatedRequestContext SetupBlog(string subfolder, string applicationPath, int port, string page, string userName, string password) { var repository = new DatabaseObjectProvider(); string host = GenerateUniqueString(); HttpContext.Current = null; //I wish this returned the blog it created. repository.CreateBlog("Unit Test Blog", userName, password, host, subfolder); Blog blog = repository.GetBlog(host, subfolder); var sb = new StringBuilder(); TextWriter output = new StringWriter(sb); SimulatedHttpRequest request = SetHttpContextWithBlogRequest(host, port, subfolder, applicationPath, page, output, "GET"); BlogRequest.Current.Blog = blog; if (Config.CurrentBlog != null) { Config.CurrentBlog.AutoFriendlyUrlEnabled = true; } HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(userName), new[] { "Administrators" }); return(new SimulatedRequestContext(request, sb, output, host)); }
public SimulatedRequestContext(SimulatedHttpRequest request, StringBuilder responseText, TextWriter responseWriter, string host) { ResponseStringBuilder = responseText; ResponseTextWriter = responseWriter; SimulatedRequest = request; HostName = host; }
public static SimulatedHttpRequest SetHttpContextWithBlogRequest(string host, int port, string subfolder, string applicationPath, string page, TextWriter output, string httpVerb) { HttpContext.Current = null; applicationPath = HttpHelper.StripSurroundingSlashes(applicationPath); // Subtext.Web subfolder = StripSlashes(subfolder); // MyBlog string appPhysicalDir = @"c:\projects\SubtextSystem\"; if (applicationPath.Length == 0) { applicationPath = "/"; } else { appPhysicalDir += applicationPath + @"\"; // c:\projects\SubtextSystem\Subtext.Web\ applicationPath = "/" + applicationPath; // /Subtext.Web } SetHttpRequestApplicationPath(applicationPath); if (subfolder.Length > 0) { page = subfolder + "/" + page; // MyBlog/default.aspx } string query = string.Empty; var workerRequest = new SimulatedHttpRequest(applicationPath, appPhysicalDir, appPhysicalDir + page, page, query, output, host, port, httpVerb); HttpContext.Current = new HttpContext(workerRequest); BlogRequest.Current = new BlogRequest(host, subfolder, HttpContext.Current.Request.Url, host == "localhost"); return(workerRequest); }
/// <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 (_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 this. } if (headers != null) { _headers.Add(headers); } _workerRequest = new SimulatedHttpRequest(ApplicationPath, PhysicalApplicationPath, PhysicalPath, Page, query, _responseWriter, Host, Port, httpVerb.ToString()); _workerRequest.CurrentExecutionPath = _currentExecutionPath; _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; }
/// <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 (_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 this. } if (headers != null) { _headers.Add(headers); } _workerRequest = new SimulatedHttpRequest(ApplicationPath, PhysicalApplicationPath, PhysicalPath, Page, query, _responseWriter, Host, Port, httpVerb.ToString()); _workerRequest.CurrentExecutionPath = _currentExecutionPath; _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); }