Exemplo n.º 1
0
        static void UmbracoApplicationBase_ApplicationInit(object sender, EventArgs e)
        {
            var app = (HttpApplication)sender;

            app.BeginRequest += (s, args) =>
            {
                var cookies        = app.Request.Cookies;
                var segmentAliases = cookies.AllKeys
                                     .Where(x => x.Equals(NcbtSegmentsCookieName))
                                     .Select(cookies.Get)
                                     .Where(x => x != null)
                                     .SelectMany(x => x.Values.AllKeys)
                                     .Where(x => x != null)
                                     .Distinct();

                foreach (var segmentAlias in segmentAliases)
                {
                    var segment = Models.Segment.GetByAlias(segmentAlias);

                    if (segment == null)
                    {
                        continue;
                    }

                    CookieTrackingDataSource.AddToSegment(new HttpContextWrapper(app.Context), segment);
                }

                // Expire the old cookie.
                app.Response.Cookies.Add(new HttpCookie(NcbtSegmentsCookieName)
                {
                    Expires = DateTime.UtcNow.AddDays(-1)
                });
            };
        }
Exemplo n.º 2
0
        public void AddToSegment_throws_ArgumentNullException_when_segment_is_null()
        {
            var sut = new CookieTrackingDataSource(() => new FakeHttpContext());

            Action test = () => sut.AddToSegment("visitorId", null);

            Assert.Throws <ArgumentNullException>(test);
        }
Exemplo n.º 3
0
        public void AddToSegment_adds_segment_cookie(string segmentAlias)
        {
            var httpContextBase = new FakeHttpContext();
            var sut             = new CookieTrackingDataSource(() => httpContextBase);

            sut.AddToSegment("visitorId", new Segment {
                Alias = segmentAlias, Persistence = -1
            });

            var expected = string.Format(CookieTrackingDataSource.CookieNameFormat, segmentAlias);

            Assert.Contains(expected, httpContextBase.Response.Cookies.AllKeys);
        }
Exemplo n.º 4
0
        public void AddToSegment_with_X_days_persistence_adds_cookie_with_X_days_expiration(int days)
        {
            var segmentAlias    = "facebook";
            var httpContextBase = new FakeHttpContext();
            var sut             = new CookieTrackingDataSource(() => httpContextBase);

            sut.AddToSegment("visitorId", new Segment {
                Alias = segmentAlias, Persistence = days
            });

            var cookie = httpContextBase.Response.Cookies.Get(string.Format(CookieTrackingDataSource.CookieNameFormat, segmentAlias));

            Assert.True(DateTime.UtcNow.AddDays(days) - cookie.Expires < TimeSpan.FromSeconds(1));
        }
Exemplo n.º 5
0
        public void AddToSegment_with_no_persistence_adds_cookie_with_immediate_expiration()
        {
            var segmentAlias    = "facebook";
            var httpContextBase = new FakeHttpContext();
            var sut             = new CookieTrackingDataSource(() => httpContextBase);

            sut.AddToSegment("visitorId", new Segment {
                Alias = segmentAlias, Persistence = 0
            });

            var cookie = httpContextBase.Response.Cookies.Get(string.Format(CookieTrackingDataSource.CookieNameFormat, segmentAlias));

            Assert.Equal(DateTime.MinValue, cookie.Expires);
        }
Exemplo n.º 6
0
        public void AddToSegment_with_X_days_persistence_does_not_update_existing_cookies(int days)
        {
            var segmentAlias    = "facebook";
            var cookieName      = string.Format(CookieTrackingDataSource.CookieNameFormat, segmentAlias);
            var httpContextBase = new FakeHttpContext();
            var existingCookie  = new HttpCookie(cookieName)
            {
                Expires = DateTime.UtcNow.AddDays(days)
            };

            httpContextBase.Request.Cookies.Add(existingCookie);
            var sut = new CookieTrackingDataSource(() => httpContextBase);

            sut.AddToSegment("visitorId", new Segment {
                Alias = segmentAlias, Persistence = days
            });

            Assert.Null(httpContextBase.Response.Cookies.Get(cookieName));
        }