public static HttpRequest CreateHttpRequestPartialMock(string url, string queryString, string userHostAddress) { MyMockHttpRequest = new Mock <Mock <HttpRequest> >(MockBehavior.Loose); // Instantiate Concrete instance var httpRequest = new HttpRequest("", url, queryString); // Note that the type of 'wrapper' is dynamic dynamic request = new AccessPrivateWrapper(httpRequest); // Hit the property ServerVariables to fire late binding var property = request.ServerVariables; // attempt to access RequestWorker to verify its null var wr = request._wr; // Grab assembly reference var assembly = typeof(HttpRequest).Assembly; // Grab reference to internal HttpServerVarsCollection var type = assembly.GetType("System.Web.HttpServerVarsCollection"); // Grab type reference to internal IIS7WorkerRequest var type2 = assembly.GetType("System.Web.Hosting.IIS7WorkerRequest"); var type3 = assembly.GetType("System.Web.HttpValueCollection"); var serverVariables = InstantiateServerVariablesViaReflection(type, wr, httpRequest); //--------------------------------------------- //// Not supported //MethodInfo add = typeof(NameValueCollection).GetMethod("Add", flags, // null, CallingConventions.Standard, // new Type[] {typeof (NameValueCollection)}, // null); //add.Invoke(serverVariables as object, new object[] {serverVar}); ////add.Invoke(serverVariables as object, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] {}); //--------------------------------------------- // Unlock and Add ServerVariables AddServerVariableViaReflection("REMOTE_ADDR", userHostAddress, ref serverVariables, type3); // alternate method - SetServerVariableManagedOnly AddServerVariableViaReflection("REMOTE_HOST", userHostAddress, ref serverVariables, type3); // default ip location AddServerVariableViaReflection("HTTP_X_FORWARDED_FOR", userHostAddress, ref serverVariables, type3); // behind proxy or router ip location AddServerVariableViaReflection("HTTP_FORWARDED", userHostAddress, ref serverVariables, type3); // behind proxy or router ip location // Relock ServerVariables UseReflectionToLockServerVariable(ref serverVariables, type3); // Assign our collection instance to the private field of our concrete request request._serverVariables = serverVariables; //// Update our fields from the serverVariables collection //MethodInfo fillInServerVariables = typeof(HttpRequest).GetMethod("FillInServerVariablesCollection", // BindingFlags.NonPublic | // BindingFlags.Instance, // null, CallingConventions.Standard, // new Type[0], // null); #region " Clone HttpRequest " ////request.FillInServerVariablesCollection(); ////Object bob = new object(); ////GetDynamicMember(bob, "_serverVariables"); ////myMockHttpRequest.Setup(r => r.Object).Returns(httpRequest); ////_myMockHttpRequest.Setup(r => r.Object.UserHostAddress).Returns(userHostAddress); // custom ////if (!string.IsNullOrEmpty(queryString)) ////{ //// MyMockHttpRequest.Setup(r => r.Object.QueryString).Returns(HttpUtility.ParseQueryString(queryString)); //// //MockRequest.Setup(r => r.Object.QueryString).Returns(request.QueryString); ////} ////if (!string.IsNullOrEmpty(url)) ////{ //// MyMockHttpRequest.SetupGet(r => r.Object.Url).Returns(new Uri(url)); //// //MockRequest.Setup(r => r.Object.Url).Returns(request.Url); ////} //////{ "SERVER_NAME", "localhost" }, //////{ "SCRIPT_NAME", "localhost" }, //////{ "SERVER_PORT", "80" }, //////{ "HTTPS", "www.melaos.com" }, ////MyMockHttpRequest.Setup(r => r.Object.ServerVariables).Returns(new NameValueCollection //// { //// // custom //// {"REMOTE_ADDR", userHostAddress}, //// {"REMOTE_HOST", userHostAddress}, // default ip location //// {"HTTP_X_FORWARDED_FOR", userHostAddress}, // behind proxy or router ip location //// {"HTTP_FORWARDED", userHostAddress} // behind proxy or router ip location //// }); //MyMockHttpRequest.Setup(r => r.Object.TotalBytes).Returns(httpRequest.TotalBytes); //MyMockHttpRequest.Setup(r => r.Object.Unvalidated).Returns(httpRequest.Unvalidated); ////MyMockHttpRequest.Setup(r => r.Object.Unvalidated).Returns(new UnvalidatedRequestValuesWrapper(httpRequest.Unvalidated)); //MyMockHttpRequest.Setup(r => r.Object.UrlReferrer).Returns(httpRequest.UrlReferrer); //MyMockHttpRequest.Setup(r => r.Object.UserAgent).Returns(httpRequest.UserAgent); //MyMockHttpRequest.Setup(r => r.Object.UserHostName).Returns(httpRequest.UserHostName); //MyMockHttpRequest.Setup(r => r.Object.UserLanguages).Returns(httpRequest.UserLanguages); //MyMockHttpRequest.Setup(r => r.Object.AcceptTypes).Returns(httpRequest.AcceptTypes); //MyMockHttpRequest.Setup(r => r.Object.AnonymousID).Returns(httpRequest.AnonymousID); //MyMockHttpRequest.Setup(r => r.Object.ApplicationPath).Returns(httpRequest.ApplicationPath); //MyMockHttpRequest.Setup(r => r.Object.ClientCertificate).Returns(httpRequest.ClientCertificate); //MyMockHttpRequest.Setup(r => r.Object.ContentLength).Returns(httpRequest.ContentLength); //MyMockHttpRequest.Setup(r => r.Object.ContentType).Returns(httpRequest.ContentType); //MyMockHttpRequest.Setup(r => r.Object.Cookies).Returns(httpRequest.Cookies); //MyMockHttpRequest.Setup(r => r.Object.CurrentExecutionFilePath).Returns(httpRequest.CurrentExecutionFilePath); //MyMockHttpRequest.Setup(r => r.Object.CurrentExecutionFilePathExtension).Returns(httpRequest.CurrentExecutionFilePathExtension); //MyMockHttpRequest.Setup(r => r.Object.FilePath).Returns(httpRequest.FilePath); //MyMockHttpRequest.Setup(r => r.Object.Files).Returns(httpRequest.Files); ////MyMockHttpRequest.Setup(r => r.Object.Files).Returns(new HttpFileCollectionWrapper(httpRequest.Files)); //MyMockHttpRequest.Setup(r => r.Object.Filter).Returns(httpRequest.Filter); //MyMockHttpRequest.Setup(r => r.Object.Form).Returns(httpRequest.Form); //MyMockHttpRequest.Setup(r => r.Object.Headers).Returns(httpRequest.Headers); //MyMockHttpRequest.Setup(r => r.Object.HttpMethod).Returns(httpRequest.HttpMethod); //MyMockHttpRequest.Setup(r => r.Object.InputStream).Returns(httpRequest.InputStream); //MyMockHttpRequest.Setup(r => r.Object.IsAuthenticated).Returns(httpRequest.IsAuthenticated); //MyMockHttpRequest.Setup(r => r.Object.IsLocal).Returns(httpRequest.IsLocal); //MyMockHttpRequest.Setup(r => r.Object.IsSecureConnection).Returns(httpRequest.IsSecureConnection); //MyMockHttpRequest.Setup(r => r.Object.LogonUserIdentity).Returns(httpRequest.LogonUserIdentity); //MyMockHttpRequest.Setup(r => r.Object.Params).Returns(httpRequest.Params); //MyMockHttpRequest.Setup(r => r.Object.Path).Returns(httpRequest.Path); //MyMockHttpRequest.Setup(r => r.Object.PathInfo).Returns(httpRequest.PathInfo); //MyMockHttpRequest.Setup(r => r.Object.PhysicalPath).Returns(httpRequest.PhysicalPath); //MyMockHttpRequest.Setup(r => r.Object.RawUrl).Returns(httpRequest.RawUrl); //MyMockHttpRequest.Setup(r => r.Object.ReadEntityBodyMode).Returns(httpRequest.ReadEntityBodyMode); //MyMockHttpRequest.Setup(r => r.Object.RequestContext).Returns(httpRequest.RequestContext); //MyMockHttpRequest.Setup(r => r.Object.RequestType).Returns(httpRequest.RequestType); //MyMockHttpRequest.Setup(r => r.Object.TimedOutToken).Returns(httpRequest.TimedOutToken); ////if (request.AppRelativeCurrentExecutionFilePath != null) ////{ //// MockRequest.Setup(r => r.Object.AppRelativeCurrentExecutionFilePath).Returns( //// request.AppRelativeCurrentExecutionFilePath); // potential null ref ////} ////if (request.Browser != null) ////{ //// MockRequest.Setup(r => r.Object.Browser).Returns(request.Browser); ////} ////MockRequest.Setup(r => r.Object.ContentEncoding).Returns(request.ContentEncoding); ////MockRequest.Setup(r => r.Object.HttpChannelBinding).Returns(request.HttpChannelBinding); ////MockRequest.Setup(r => r.Object.PhysicalApplicationPath).Returns(request.PhysicalApplicationPath); //Mock<HttpRequest> mockHttpRequest = MyMockHttpRequest.Object; ////mockHttpRequest.Setup(r => r.UserHostAddress).Returns(userHostAddress); #endregion return(httpRequest); //mockHttpRequest.Object; }
public static HttpRequest CreateHttpRequestPartialMock(string url, string queryString, string userHostAddress) { MyMockHttpRequest = new Mock<Mock<HttpRequest>>(MockBehavior.Loose); // Instantiate Concrete instance var httpRequest = new HttpRequest("", url, queryString); // Note that the type of 'wrapper' is dynamic dynamic request = new AccessPrivateWrapper(httpRequest); // Hit the property ServerVariables to fire late binding var property = request.ServerVariables; // attempt to access RequestWorker to verify its null var wr = request._wr; // Grab assembly reference var assembly = typeof(HttpRequest).Assembly; // Grab reference to internal HttpServerVarsCollection var type = assembly.GetType("System.Web.HttpServerVarsCollection"); // Grab type reference to internal IIS7WorkerRequest var type2 = assembly.GetType("System.Web.Hosting.IIS7WorkerRequest"); var type3 = assembly.GetType("System.Web.HttpValueCollection"); var serverVariables = InstantiateServerVariablesViaReflection(type, wr, httpRequest); //--------------------------------------------- //// Not supported //MethodInfo add = typeof(NameValueCollection).GetMethod("Add", flags, // null, CallingConventions.Standard, // new Type[] {typeof (NameValueCollection)}, // null); //add.Invoke(serverVariables as object, new object[] {serverVar}); ////add.Invoke(serverVariables as object, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] {}); //--------------------------------------------- // Unlock and Add ServerVariables AddServerVariableViaReflection("REMOTE_ADDR", userHostAddress, ref serverVariables, type3); // alternate method - SetServerVariableManagedOnly AddServerVariableViaReflection("REMOTE_HOST", userHostAddress, ref serverVariables, type3);// default ip location AddServerVariableViaReflection("HTTP_X_FORWARDED_FOR", userHostAddress, ref serverVariables, type3); // behind proxy or router ip location AddServerVariableViaReflection("HTTP_FORWARDED", userHostAddress, ref serverVariables, type3); // behind proxy or router ip location // Relock ServerVariables UseReflectionToLockServerVariable(ref serverVariables, type3); // Assign our collection instance to the private field of our concrete request request._serverVariables = serverVariables; //// Update our fields from the serverVariables collection //MethodInfo fillInServerVariables = typeof(HttpRequest).GetMethod("FillInServerVariablesCollection", // BindingFlags.NonPublic | // BindingFlags.Instance, // null, CallingConventions.Standard, // new Type[0], // null); #region " Clone HttpRequest " ////request.FillInServerVariablesCollection(); ////Object bob = new object(); ////GetDynamicMember(bob, "_serverVariables"); ////myMockHttpRequest.Setup(r => r.Object).Returns(httpRequest); ////_myMockHttpRequest.Setup(r => r.Object.UserHostAddress).Returns(userHostAddress); // custom ////if (!string.IsNullOrEmpty(queryString)) ////{ //// MyMockHttpRequest.Setup(r => r.Object.QueryString).Returns(HttpUtility.ParseQueryString(queryString)); //// //MockRequest.Setup(r => r.Object.QueryString).Returns(request.QueryString); ////} ////if (!string.IsNullOrEmpty(url)) ////{ //// MyMockHttpRequest.SetupGet(r => r.Object.Url).Returns(new Uri(url)); //// //MockRequest.Setup(r => r.Object.Url).Returns(request.Url); ////} //////{ "SERVER_NAME", "localhost" }, //////{ "SCRIPT_NAME", "localhost" }, //////{ "SERVER_PORT", "80" }, //////{ "HTTPS", "www.melaos.com" }, ////MyMockHttpRequest.Setup(r => r.Object.ServerVariables).Returns(new NameValueCollection //// { //// // custom //// {"REMOTE_ADDR", userHostAddress}, //// {"REMOTE_HOST", userHostAddress}, // default ip location //// {"HTTP_X_FORWARDED_FOR", userHostAddress}, // behind proxy or router ip location //// {"HTTP_FORWARDED", userHostAddress} // behind proxy or router ip location //// }); //MyMockHttpRequest.Setup(r => r.Object.TotalBytes).Returns(httpRequest.TotalBytes); //MyMockHttpRequest.Setup(r => r.Object.Unvalidated).Returns(httpRequest.Unvalidated); ////MyMockHttpRequest.Setup(r => r.Object.Unvalidated).Returns(new UnvalidatedRequestValuesWrapper(httpRequest.Unvalidated)); //MyMockHttpRequest.Setup(r => r.Object.UrlReferrer).Returns(httpRequest.UrlReferrer); //MyMockHttpRequest.Setup(r => r.Object.UserAgent).Returns(httpRequest.UserAgent); //MyMockHttpRequest.Setup(r => r.Object.UserHostName).Returns(httpRequest.UserHostName); //MyMockHttpRequest.Setup(r => r.Object.UserLanguages).Returns(httpRequest.UserLanguages); //MyMockHttpRequest.Setup(r => r.Object.AcceptTypes).Returns(httpRequest.AcceptTypes); //MyMockHttpRequest.Setup(r => r.Object.AnonymousID).Returns(httpRequest.AnonymousID); //MyMockHttpRequest.Setup(r => r.Object.ApplicationPath).Returns(httpRequest.ApplicationPath); //MyMockHttpRequest.Setup(r => r.Object.ClientCertificate).Returns(httpRequest.ClientCertificate); //MyMockHttpRequest.Setup(r => r.Object.ContentLength).Returns(httpRequest.ContentLength); //MyMockHttpRequest.Setup(r => r.Object.ContentType).Returns(httpRequest.ContentType); //MyMockHttpRequest.Setup(r => r.Object.Cookies).Returns(httpRequest.Cookies); //MyMockHttpRequest.Setup(r => r.Object.CurrentExecutionFilePath).Returns(httpRequest.CurrentExecutionFilePath); //MyMockHttpRequest.Setup(r => r.Object.CurrentExecutionFilePathExtension).Returns(httpRequest.CurrentExecutionFilePathExtension); //MyMockHttpRequest.Setup(r => r.Object.FilePath).Returns(httpRequest.FilePath); //MyMockHttpRequest.Setup(r => r.Object.Files).Returns(httpRequest.Files); ////MyMockHttpRequest.Setup(r => r.Object.Files).Returns(new HttpFileCollectionWrapper(httpRequest.Files)); //MyMockHttpRequest.Setup(r => r.Object.Filter).Returns(httpRequest.Filter); //MyMockHttpRequest.Setup(r => r.Object.Form).Returns(httpRequest.Form); //MyMockHttpRequest.Setup(r => r.Object.Headers).Returns(httpRequest.Headers); //MyMockHttpRequest.Setup(r => r.Object.HttpMethod).Returns(httpRequest.HttpMethod); //MyMockHttpRequest.Setup(r => r.Object.InputStream).Returns(httpRequest.InputStream); //MyMockHttpRequest.Setup(r => r.Object.IsAuthenticated).Returns(httpRequest.IsAuthenticated); //MyMockHttpRequest.Setup(r => r.Object.IsLocal).Returns(httpRequest.IsLocal); //MyMockHttpRequest.Setup(r => r.Object.IsSecureConnection).Returns(httpRequest.IsSecureConnection); //MyMockHttpRequest.Setup(r => r.Object.LogonUserIdentity).Returns(httpRequest.LogonUserIdentity); //MyMockHttpRequest.Setup(r => r.Object.Params).Returns(httpRequest.Params); //MyMockHttpRequest.Setup(r => r.Object.Path).Returns(httpRequest.Path); //MyMockHttpRequest.Setup(r => r.Object.PathInfo).Returns(httpRequest.PathInfo); //MyMockHttpRequest.Setup(r => r.Object.PhysicalPath).Returns(httpRequest.PhysicalPath); //MyMockHttpRequest.Setup(r => r.Object.RawUrl).Returns(httpRequest.RawUrl); //MyMockHttpRequest.Setup(r => r.Object.ReadEntityBodyMode).Returns(httpRequest.ReadEntityBodyMode); //MyMockHttpRequest.Setup(r => r.Object.RequestContext).Returns(httpRequest.RequestContext); //MyMockHttpRequest.Setup(r => r.Object.RequestType).Returns(httpRequest.RequestType); //MyMockHttpRequest.Setup(r => r.Object.TimedOutToken).Returns(httpRequest.TimedOutToken); ////if (request.AppRelativeCurrentExecutionFilePath != null) ////{ //// MockRequest.Setup(r => r.Object.AppRelativeCurrentExecutionFilePath).Returns( //// request.AppRelativeCurrentExecutionFilePath); // potential null ref ////} ////if (request.Browser != null) ////{ //// MockRequest.Setup(r => r.Object.Browser).Returns(request.Browser); ////} ////MockRequest.Setup(r => r.Object.ContentEncoding).Returns(request.ContentEncoding); ////MockRequest.Setup(r => r.Object.HttpChannelBinding).Returns(request.HttpChannelBinding); ////MockRequest.Setup(r => r.Object.PhysicalApplicationPath).Returns(request.PhysicalApplicationPath); //Mock<HttpRequest> mockHttpRequest = MyMockHttpRequest.Object; ////mockHttpRequest.Setup(r => r.UserHostAddress).Returns(userHostAddress); #endregion return httpRequest; //mockHttpRequest.Object; }