0

I am doing unit testing for my mvc application. It includes many class library. I have mocked the http context in unit testing project using NSubstitute and created the cookie. But it is not available in class library while debug the source. How can we get cookies in class library?

Unit Test

        [Test]
        [TestModule(ModuleAttribute.ModuleName)]
        public void MethodName()
        {
            var httpContext = MockHttphelpers.MockHttpContextBaseWithUrlReferrer("~/pagename", "https://test.abc.com");

            var checkSecureConn = httpContext.Request.IsSecureConnection;
            var userCookie = new HttpCookie("TrackReferrer");
            userCookie.Value = "http://www.google.com,http://test.abc.com";
            userCookie.Secure = true;
            userCookie.HttpOnly = true;
            httpContext.Request.Cookies.Add(userCookie);
            this.TrackingInfo.GetCustomerOriginReferral();

            Assert.AreEqual(httpContext.Session["sessionname"].ToString(), "www.google.com"); 
        }

Class Library

public void GetReferral()
        {
            string urlReferrer = HttpContext.Current.Request.UrlReferrer == null ? string.Empty : HttpContext.Current.Request.UrlReferrer.ToString().ToLower();
            if (!string.IsNullOrEmpty(urlReferrer) && new Uri(urlReferrer).Host != HttpContext.Current.Request.Url.Host && HttpContext.Current.Request.Cookies["TrackReferrer"] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["TrackReferrer"].Value))
            {
                //Code
            }
        }

HttpContext Mock

public static HttpContextBase MockHttpContextBase()
        {
            var httpContextBase = Substitute.For<HttpContextBase>();

            var httpRequestBase = Substitute.For<HttpRequestBase>();
            var httpResponseBase = Substitute.For<HttpResponseBase>();
            var httpSessionStateBase = Substitute.For<HttpSessionStateBase>();
            var httpServerUtilityBase = Substitute.For<HttpServerUtilityBase>();

            httpRequestBase.Cookies.Returns(new HttpCookieCollection());
            httpResponseBase.Cookies.Returns(new HttpCookieCollection());
            httpRequestBase.ServerVariables.Returns(MockRequestObjects.ServerVariables);
            httpContextBase.Request.Returns(httpRequestBase);
            httpContextBase.Response.Returns(httpResponseBase);
            httpContextBase.Session.Returns(httpSessionStateBase);
            httpContextBase.Server.Returns(httpServerUtilityBase);

            // Initializing HttpContext.Current
            HttpContext.Current = MockHttpContext(); 
            HttpContext.Current.Request.Browser = new HttpBrowserCapabilities() { Capabilities = new Dictionary<string, string> { { "supportsEmptyStringInCookieValue", "false" } } };

            // Initializing Session in HttpContextBase in ControllerContext
            httpContextBase.Session.Returns(MockHttpContextControllerContextSession());

            // Initializing ApplicationInstance in HttpContextBase in ControllerContext
            httpContextBase.ApplicationInstance = new HttpApplication();

            // Initializing ApplicationInstance in HttpContext.Current
            HttpContext.Current.ApplicationInstance = new HttpApplication();
            httpContextBase.Request.ServerVariables.Returns(MockRequestObjects.ServerVariables);

            // Initializing FormCollection in HttpContextBase in ControllerContext
            httpContextBase.Request.Form.Returns(new FormCollection());

            return httpContextBase;
        }
Melody
  • 1,203
  • 2
  • 17
  • 28
  • Please share a [MCV example](https://stackoverflow.com/help/mcve). – Tanveer Badar Aug 07 '18 at 04:45
  • you call `HttpContext.Current = MockHttpContext(); ` which appears to have nothing to do with the `httpContextBase` variable that is setup in the method. – Nkosi Aug 07 '18 at 09:38

0 Answers0