public void SetUp() { var headers = new FormCollection { { "Referer", ExpectedRefererUrl } }; MockRequest.Setup(r => r.Headers).Returns(headers); this._autoApprovalService = new Mock <IAutoApprovalService>(); this._docTypeService = new Mock <IDocTypeService>(); this._subDocTypeService = new Mock <ISubDocTypeService>(); this._manCoService = new Mock <IManCoService>(); this._userService = new Mock <IUserService>(); this._logger = new Mock <ILogger>(); this.SetUpVariables(); _controller = new AutoApprovalController( this._autoApprovalService.Object, this._docTypeService.Object, this._subDocTypeService.Object, this._manCoService.Object, this._userService.Object, this._logger.Object); SetControllerContext(_controller); }
public API_Moq_HttpContext setupNormalRequestValues() { var genericIdentity = new GenericIdentity("genericIdentity"); var genericPrincipal = new GenericPrincipal(genericIdentity, new string[] {}); MockContext.Setup(context => context.User).Returns(genericPrincipal); MockContext.Setup(context => context.Cache).Returns(HttpRuntime.Cache); MockContext.Setup(context => context.Server.MapPath(It.IsAny <string>())).Returns((string path) => this.BaseDir.pathCombine(path)); //Request MockRequest.Setup(request => request.InputStream).Returns(new MemoryStream()); MockRequest.Setup(request => request.Headers).Returns(new NameValueCollection()); MockRequest.Setup(request => request.QueryString).Returns(new NameValueCollection()); MockRequest.Setup(request => request.Form).Returns(new NameValueCollection()); //Response var outputStream = new MemoryStream(); MockResponse.Setup(response => response.OutputStream).Returns(outputStream); //var writer = new StringWriter(); // context.Expect(ctx => ctx.Response.Output).Returns(writer); MockResponse.Setup(response => response.Write(It.IsAny <string>())).Callback((string code) => outputStream.Write(code.asciiBytes(), 0, code.size())); var cache = new Mock <HttpCachePolicyBase>(); MockResponse.SetupGet(response => response.Cache).Returns(cache.Object); return(this); }
public void SetUp() { _syndicationFeedDomain = new Mock <ISyndicationFeedService>(); MockRequest.Setup(r => r.Url).Returns(new Uri("http://foo.com/feed/rss")); var headers = new NameValueCollection(); headers.Add("HOST", "localhost"); MockRequest.Setup(r => r.Headers).Returns(headers); MockHttpContext.Setup(h => h.Request).Returns(MockRequest.Object); }
public void GivenANotLoggdInUser_WhenILogout_TheUserIsRedirectedToTheHomePage() { var cookies = new HttpCookieCollection(); MockRequest.Setup(r => r.Cookies).Returns(cookies); var result = _sessionController.Delete() as RedirectToRouteResult; Assert.That(result, Is.Not.Null); Assert.That(result.RouteValues["controller"], Is.EqualTo("Home").IgnoreCase); Assert.That(result.RouteValues["action"], Is.EqualTo("Index").IgnoreCase); }
public void SetUp() { _postDomain = new Mock <IPostService>(); _controller = new CommentController(_postDomain.Object, null); var headers = new FormCollection { { "Referer", ExpectedRefererUrl } }; MockRequest.Setup(r => r.Headers).Returns(headers); SetControllerContext(_controller); }
public void GivenALoggdInUser_WhenILogout_ThenTheCookieIsExpired() { var cookies = new HttpCookieCollection(); string cookieName = "USER"; cookies.Add(new HttpCookie(cookieName)); MockRequest.Setup(r => r.Cookies).Returns(cookies); _controller.Logout(); HttpCookie cookie = FakeResponse.Cookies[GetCookieUserFilterAttribute.UserCookieName]; Assert.That(cookie.Expires, Is.EqualTo(new DateTime(1970, 1, 1))); }
public HttpContextMoq SetupNormalRequestValues() { var MockUser = new Mock <IPrincipal>(); var MockIdentity = new Mock <IIdentity>(); MockContext.Setup(context => context.User).Returns(MockUser.Object); MockUser.Setup(context => context.Identity).Returns(MockIdentity.Object); MockRequest.Setup(request => request.Url).Returns(new Uri("http://localhost/")); MockRequest.Setup(request => request.PathInfo).Returns(string.Empty); MockRequest.Setup(request => request.ServerVariables).Returns(new NameValueCollection()); return(this); }
public void SetUp() { var headers = new FormCollection { { "Referer", ExpectedRefererUrl } }; MockRequest.Setup(r => r.Headers).Returns(headers); _documentService = new Mock <IDocumentService>(); _userService = new Mock <IUserService>(); _manCoService = new Mock <IManCoService>(); _logger = new Mock <ILogger>(); _documentService.Setup(d => d.GetDocument(It.IsAny <string>())).Returns(new Document()); }
private void SetMockRequestServerVariables(string userHostAddress) { //{ "SERVER_NAME", "localhost" }, //{ "SCRIPT_NAME", "localhost" }, //{ "SERVER_PORT", "80" }, //{ "HTTPS", "www.melaos.com" }, MockRequest.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 }); }
public void SetUp() { _applicationDomain = new Mock <IApplicationService>(); _indexService = new Mock <IIndexService>(); _logger = new Mock <ILogger>(); _controller = new IndexController(_applicationDomain.Object, _indexService.Object, _logger.Object); var headers = new FormCollection { { "Referer", ExpectedRefererUrl } }; MockRequest.Setup(r => r.Headers).Returns(headers); _indexService.Setup(r => r.GetIndex(It.IsAny <int>())).Returns(new IndexDefinition()); SetControllerContext(_controller); }
public API_Moq_HttpContext setupNormalRequestValues() { var genericIdentity = new GenericIdentity("genericIdentity"); var genericPrincipal = new GenericPrincipal(genericIdentity, new string[] {}); MockContext.Setup(context => context.User).Returns(genericPrincipal); MockContext.Setup(context => context.Cache).Returns(HttpRuntime.Cache); //Request MockRequest.Setup(request => request.InputStream).Returns(new MemoryStream()); MockRequest.Setup(request => request.Cookies).Returns(new HttpCookieCollection()); MockRequest.Setup(request => request.Headers).Returns(new NameValueCollection()); MockRequest.Setup(request => request.QueryString).Returns(new NameValueCollection()); MockRequest.Setup(request => request.Form).Returns(new NameValueCollection()); MockRequest.Setup(request => request.ServerVariables).Returns(new NameValueCollection()); MockRequest.Setup(request => request.ContentType).Returns(""); MockRequest.Setup(request => request.Url).Returns(() => RequestUrl); MockRequest.Setup(request => request.IsLocal).Returns(() => RequestUrl.Host == "localhost" || RequestUrl.Host == "127.0.0.1"); MockRequest.Setup(request => request.IsSecureConnection).Returns(() => RequestUrl.Scheme.lower() == "https"); //Response var outputStream = new MemoryStream(); var redirectTarget = ""; MockResponse.SetupGet(response => response.Cache).Returns(new Mock <HttpCachePolicyBase>().Object); MockResponse.Setup(response => response.Cookies).Returns(new HttpCookieCollection()); MockResponse.Setup(response => response.Headers).Returns(new NameValueCollection()); MockResponse.Setup(response => response.OutputStream).Returns(outputStream); MockResponse.Setup(response => response.Write(It.IsAny <string>())).Callback((string code) => outputStream.Write(code.asciiBytes(), 0, code.size())); MockResponse.Setup(response => response.AddHeader(It.IsAny <string>(), It.IsAny <string>())).Callback((string name, string value) => MockResponse.Object.Headers.Add(name, value)); MockResponse.Setup(response => response.Redirect(It.IsAny <string>())).Callback((string target) => { redirectTarget = target; throw new Exception("Thread was being aborted."); }); MockResponse.Setup(response => response.IsRequestBeingRedirected).Returns(() => redirectTarget.valid()); MockResponse.Setup(response => response.RedirectLocation).Returns(() => redirectTarget); //Server MockServer.Setup(server => server.MapPath(It.IsAny <string>())).Returns((string path) => BaseDir.pathCombine(path)); MockServer.Setup(server => server.Transfer(It.IsAny <string>())).Callback((string target) => { redirectTarget = target; throw new Exception("Thread was being aborted."); }); // use the redirectTarget to hold this value MockServer.Setup(server => server.Transfer(It.IsAny <string>(), It.IsAny <bool>())).Callback((string target, bool preserveForm) => { redirectTarget = target; throw new Exception("Thread was being aborted."); }); // use the redirectTarget to hold this value //var writer = new StringWriter(); //context.Expect(ctx => ctx.Response.Output).Returns(writer); return(this); }
public void GivenALoggdInUser_WhenILogout_ThenTheUserIsRemovedFromTheContext() { var cookies = new HttpCookieCollection(); string cookieName = "USER"; // Add response cookies FakeResponse.Cookies.Add(new HttpCookie(cookieName)); Assert.That(FakeResponse.Cookies.Count, Is.EqualTo(1)); // Add request cookies cookies.Add(new HttpCookie(cookieName)); MockRequest.Setup(r => r.Cookies).Returns(cookies); MockHttpContext.SetupProperty(h => h.User); _sessionController.HttpContext.User = new UserViewModel(); Assert.That(_sessionController.HttpContext.User, Is.Not.Null); var result = _sessionController.Delete() as RedirectToRouteResult; Assert.That(result, Is.Not.Null); Assert.That(_sessionController.HttpContext.User, Is.Null); }
public void SetUp() { var headers = new FormCollection { { "Referer", ExpectedRefererUrl } }; MockRequest.Setup(r => r.Headers).Returns(headers); _appManCoEmailService = new Mock <IAppManCoEmailService>(); _applicationService = new Mock <IApplicationService>(); _manCoService = new Mock <IManCoService>(); _docTypeService = new Mock <IDocTypeService>(); _userService = new Mock <IUserService>(); _logger = new Mock <ILogger>(); _controller = new OtfController(_appManCoEmailService.Object, _applicationService.Object, _manCoService.Object, _docTypeService.Object, _userService.Object, _logger.Object); SetControllerContext(_controller); }
private void SetMockRequestUserHostAddress(string userHostAddress) { MockRequest.Setup(r => r.Object.UserHostAddress).Returns(userHostAddress); // custom }
private void SetMockRequestRequestType(HttpVerbs verb) { MockRequest.Setup(y => y.Object.RequestType).Returns(verb == HttpVerbs.Get ? "GET" : "POST"); // custom }
private void SetMockRequestCookies(HttpCookieCollection cookies) { MockRequest.Setup(x => x.Object.Cookies).Returns(cookies); // custom }
private void SetMockRequestForm(NameValueCollection formData) { MockRequest.Setup(r => r.Object.Form).Returns(formData); // custom }
public MockHttpContextNewContainer(HttpVerbs verb, HttpCookieCollection cookies, string userHostAddress, string cookieNameToSet) { // Instantiate variables cookies = cookies ?? new HttpCookieCollection(); NameValueCollection formData = new NameValueCollection(); Hashtable items = new Hashtable(); InitMocksOfMocks(); HttpContext context = HttpContext(userHostAddress: userHostAddress); // --------------------------Setup Mock of Mocks CreateMockContext(context); //CreateMockContextSession(); CreateMockUserIdentity(context); //MockRequest.Setup(r => r.Object).Returns(new HttpRequestWrapper(context.Request)); HttpRequestBase request = new HttpRequestWrapper(context.Request); SetMockRequestUserHostAddress(userHostAddress); SetMockRequestRequestType(verb); SetMockRequestServerVariables(userHostAddress); SetMockRequestCookies(cookies); SetMockRequestForm(formData); MockRequest.Setup(r => r.Object.TotalBytes).Returns(request.TotalBytes); MockRequest.Setup(r => r.Object.Unvalidated).Returns(request.Unvalidated); MockRequest.Setup(r => r.Object.Url).Returns(request.Url); MockRequest.Setup(r => r.Object.UrlReferrer).Returns(request.UrlReferrer); MockRequest.Setup(r => r.Object.UserAgent).Returns(request.UserAgent); MockRequest.Setup(r => r.Object.UserHostName).Returns(request.UserHostName); MockRequest.Setup(r => r.Object.UserLanguages).Returns(request.UserLanguages); MockRequest.Setup(r => r.Object.AcceptTypes).Returns(request.AcceptTypes); MockRequest.Setup(r => r.Object.AnonymousID).Returns(request.AnonymousID); MockRequest.Setup(r => r.Object.ApplicationPath).Returns(request.ApplicationPath); MockRequest.Setup(r => r.Object.ClientCertificate).Returns(request.ClientCertificate); MockRequest.Setup(r => r.Object.ContentLength).Returns(request.ContentLength); MockRequest.Setup(r => r.Object.ContentType).Returns(request.ContentType); MockRequest.Setup(r => r.Object.CurrentExecutionFilePath).Returns(request.CurrentExecutionFilePath); MockRequest.Setup(r => r.Object.CurrentExecutionFilePathExtension).Returns(request.CurrentExecutionFilePathExtension); MockRequest.Setup(r => r.Object.FilePath).Returns(request.FilePath); MockRequest.Setup(r => r.Object.Files).Returns(request.Files); MockRequest.Setup(r => r.Object.Filter).Returns(request.Filter); MockRequest.Setup(r => r.Object.Headers).Returns(request.Headers); MockRequest.Setup(r => r.Object.HttpMethod).Returns(request.HttpMethod); MockRequest.Setup(r => r.Object.InputStream).Returns(request.InputStream); MockRequest.Setup(r => r.Object.IsAuthenticated).Returns(request.IsAuthenticated); MockRequest.Setup(r => r.Object.IsLocal).Returns(request.IsLocal); MockRequest.Setup(r => r.Object.IsSecureConnection).Returns(request.IsSecureConnection); MockRequest.Setup(r => r.Object.LogonUserIdentity).Returns(request.LogonUserIdentity); MockRequest.Setup(r => r.Object.Params).Returns(request.Params); MockRequest.Setup(r => r.Object.Path).Returns(request.Path); MockRequest.Setup(r => r.Object.PathInfo).Returns(request.PathInfo); MockRequest.Setup(r => r.Object.PhysicalPath).Returns(request.PhysicalPath); MockRequest.Setup(r => r.Object.QueryString).Returns(request.QueryString); MockRequest.Setup(r => r.Object.RawUrl).Returns(request.RawUrl); MockRequest.Setup(r => r.Object.ReadEntityBodyMode).Returns(request.ReadEntityBodyMode); MockRequest.Setup(r => r.Object.RequestContext).Returns(request.RequestContext); MockRequest.Setup(r => r.Object.TimedOutToken).Returns(request.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); MockResponse.Setup(r => r.Object).Returns(new HttpResponseWrapper(context.Response)); MockResponse.Setup(r => r.Object.Cookies).Returns(cookies); SetMockResponseCookie(cookies, cookieNameToSet); MockSessionState.Setup(ss => ss.Object).Returns(new HttpSessionStateWrapper(context.Session)); //MockSessionState.Setup(s => s.Object.SessionID).Returns(Guid.NewGuid().ToString()); MockServerUtility.Setup(su => su.Object).Returns(new HttpServerUtilityWrapper(context.Server)); MockApplication.Setup(a => a.Object).Returns(new HttpApplicationStateWrapper(context.Application)); SetMocksFromMocksofMocks(); }