Example #1
0
        protected void AddCookie(ControllerBase controllerBase, string key, string value)
        {
            if (controllerBase.ControllerContext.HttpContext == null)
            {
                controllerBase.ControllerContext.HttpContext = new DefaultHttpContext();
            }

            var cookieCollection = controllerBase.Request.Cookies as MockRequestCookieCollection;

            if (cookieCollection == null)
            {
                // We will have to edit this cookieCollection before adding it.
                // Otherwise Request.Cookies will ignore our changes.
                cookieCollection = new MockRequestCookieCollection();
            }

            cookieCollection[key] = value;

            var httpContext = (DefaultHttpContext)controllerBase.ControllerContext.HttpContext;

            httpContext.Request.Cookies = cookieCollection;
        }
Example #2
0
        private void CreateMockHttpRequestWithOptionalFormValues(
            Mock <HttpContext> httpContextMock)
        {
            var requestMock    = new Mock <HttpRequest>();
            var requestHeaders = new HeaderDictionary();
            var requestCookies = new MockRequestCookieCollection(
                new Dictionary <string, string>
            {
                {
                    "cookie_settings",
                    JsonConvert.SerializeObject(
                        new CookieSettings {
                        GoogleAnalyticsGpg = true, GoogleAnalyticsGovUk = true, RememberSettings = true
                    })
                }
            });

            requestMock.SetupGet(x => x.Headers).Returns(requestHeaders);
            requestMock.SetupGet(x => x.Cookies).Returns(requestCookies);

            httpContextMock.SetupGet(ctx => ctx.Request).Returns(requestMock.Object);
            httpContextMock.SetupGet(ctx => ctx.Request.Headers).Returns(requestHeaders);
            httpContextMock.SetupGet(ctx => ctx.Request.Cookies).Returns(requestCookies);

            if (requestFormValues != null)
            {
                var requestForm = new FormCollection(requestFormValues);

                requestMock.SetupGet(req => req.Form).Returns(requestForm);

                httpContextMock.SetupGet(ctx => ctx.Request.HasFormContentType).Returns(true);
                httpContextMock.SetupGet(ctx => ctx.Request.Form).Returns(requestForm);
            }

            httpContextMock.SetupGet(ctx => ctx.Request.HttpContext).Returns(httpContextMock.Object);
        }