Example #1
0
        public void SetUserAndSessionContextForWebRequestSetsCookiesIfTelemetryItemIsInitialized()
        {
            var webRequest = WebRequest.Create(new Uri("http://bing.com"));
            var telemetry  = new DependencyTelemetry();

            telemetry.Context.Session.Id = "SessionID";
            telemetry.Context.User.Id    = "UserID";

            string cookieValue = DependencyCollectorTestHelpers.GetCookieValueFromWebRequest(webRequest as HttpWebRequest, "ai_session");

            Assert.IsNull(cookieValue);
            WebRequestDependencyTrackingHelpers.SetUserAndSessionContextForWebRequest(telemetry, webRequest);
            cookieValue = DependencyCollectorTestHelpers.GetCookieValueFromWebRequest(webRequest as HttpWebRequest, "ai_session");
            Assert.IsNotNull(cookieValue);
            Assert.AreEqual("ai_session=SessionID", cookieValue);

            cookieValue = DependencyCollectorTestHelpers.GetCookieValueFromWebRequest(webRequest as HttpWebRequest, "ai_user");
            Assert.IsNotNull(cookieValue);
            Assert.AreEqual("ai_user=UserID", cookieValue);
        }
        public void AssociateTelemetryWithWebRequestDoNotPopulateWebRequestCookiesByDefault()
        {
            this.telemetry.AssociateTelemetryWithWebRequest(this.webRequest);
            string sessionCookie = DependencyCollectorTestHelpers.GetCookieValueFromWebRequest(this.webRequest as HttpWebRequest, "ai_session");

            Assert.IsNull(sessionCookie);
            string userCookie = DependencyCollectorTestHelpers.GetCookieValueFromWebRequest(this.webRequest as HttpWebRequest, "ai_user");

            Assert.IsNull(userCookie);

            var newTelemetry = new DependencyTelemetry();

            newTelemetry.Context.User.Id    = "UserId";
            newTelemetry.Context.Session.Id = "SessionId";

            // Note, webRequest is already associated with the telemetry. And it should not be overriden.
            newTelemetry.AssociateTelemetryWithWebRequest(this.webRequest);
            sessionCookie = DependencyCollectorTestHelpers.GetCookieValueFromWebRequest(this.webRequest as HttpWebRequest, "ai_session");
            Assert.IsNull(sessionCookie);
            userCookie = DependencyCollectorTestHelpers.GetCookieValueFromWebRequest(this.webRequest as HttpWebRequest, "ai_user");
            Assert.IsNull(userCookie);
        }