示例#1
0
        public async Task KentorOwinCookieSaverMiddleware_AddsOwinCookies()
        {
            var context = CreateOwinContext();

            var httpContext = context.Get <HttpContextBase>(typeof(HttpContextBase).FullName);

            httpContext.Response.Cookies.Add(new HttpCookie("SystemWebCookie", "SystemWebValue"));

            var next = new MiddlewareMock();

            var subject = new KentorOwinCookieSaverMiddleware(next);

            await subject.Invoke(context);

            httpContext.Response.Cookies.AllKeys.Should().Contain("OwinCookie");

            var cookie = httpContext.Response.Cookies["OwinCookie"];

            // Time will be parsed to a local time, taking time zone into account. So let's parse the given
            // time and then convert it to local time.
            var expectedExpires = new DateTime(2021, 01, 13, 22, 23, 01, DateTimeKind.Utc).ToLocalTime();

            cookie.Value.Should().Be("OwinValue");
            cookie.Path.Should().Be("/");
            cookie.Expires.Should().Be(expectedExpires);
            cookie.Secure.Should().BeTrue("cookie string contains Secure");
            cookie.HttpOnly.Should().BeTrue("cookie string contains HttpOnly");
            cookie.IsFromHeader().Should().BeTrue();
        }
        public async Task KentorOwinCookieSaverMiddleware_InvokesNext()
        {
            var next = new MiddlewareMock();
            var context = CreateOwinContext();

            var subject = new KentorOwinCookieSaverMiddleware(next);

            await subject.Invoke(context);

            next.callingContext.Should().Be(context);
        }
示例#3
0
        public async Task KentorOwinCookieSaverMiddleware_InvokesNext()
        {
            var next    = new MiddlewareMock();
            var context = CreateOwinContext();

            var subject = new KentorOwinCookieSaverMiddleware(next);

            await subject.Invoke(context);

            next.callingContext.Should().Be(context);
        }
示例#4
0
        public async Task KentorOwinCookieSaverMiddleware_HandlesMissingHttpContext()
        {
            var context = new OwinContext();

            var next = new MiddlewareMock();

            var subject = new KentorOwinCookieSaverMiddleware(next);

            // Should not throw.
            await subject.Invoke(context);
        }
示例#5
0
        public async Task KentorOwinCookieSaverMiddleware_RoundtripsMinimalCookie()
        {
            var context = CreateOwinContext();
            var next    = new MiddlewareMock();
            var subject = new KentorOwinCookieSaverMiddleware(next);

            await subject.Invoke(context);

            // The interesting cookie is at offset 86 and is 26 chars long.
            var before = context.Response.Headers["Set-Cookie"].Substring(86, 26);

            var rebuiltHeader = RegenerateSetCookieHeader(context)
                                .Single(s => s.StartsWith("MinimalCookie"));

            rebuiltHeader.Should().Be(before);
        }
示例#6
0
        public async Task KentorOwinCookieSaverMiddleware_RoundtripsComplexCookie()
        {
            var context = CreateOwinContext();
            var next    = new MiddlewareMock();
            var subject = new KentorOwinCookieSaverMiddleware(next);

            await subject.Invoke(context);

            // The first cookie is 85 chars long.
            var before = context.Response.Headers["Set-Cookie"].Substring(0, 85);

            var rebuiltHeader = RegenerateSetCookieHeader(context)
                                .Single(s => s.StartsWith("OwinCookie"));

            rebuiltHeader.Should().Be(before);
        }
示例#7
0
        public async Task KentorOwinCookieSaverMiddleware_AbortsOnHeadersSent()
        {
            var context = CreateOwinContext();
            var next    = new MiddlewareMock();
            var subject = new KentorOwinCookieSaverMiddleware(next);

            // The property has an internal setter.
            var headersWrittenProperty = typeof(HttpResponse)
                                         .GetProperty("HeadersWritten");

            headersWrittenProperty.GetSetMethod(true).Invoke(
                context.Environment[typeof(HttpContext).FullName]
                .As <HttpContext>().Response,
                new object[] { true });

            await subject.Invoke(context);

            // With headers already written, the middleware should not try
            // to write to the cookie collection.
            context.Environment[typeof(HttpContextBase).FullName]
            .As <HttpContextBase>().Response.Cookies
            .Should().BeEmpty();
        }
        public async Task KentorOwinCookieSaverMiddleware_AddsOwinCookies()
        {
            var context = CreateOwinContext();

            var httpContext = context.Get<HttpContextBase>(typeof(HttpContextBase).FullName);

            httpContext.Response.Cookies.Add(new HttpCookie("SystemWebCookie", "SystemWebValue"));

            var next = new MiddlewareMock();

            var subject = new KentorOwinCookieSaverMiddleware(next);

            await subject.Invoke(context);

            httpContext.Response.Cookies.AllKeys.Should().Contain("OwinCookie");

            var cookie = httpContext.Response.Cookies["OwinCookie"];

            // Time will be parsed to a local time, taking time zone into account. So let's parse the given
            // time and then convert it to local time.
            var expectedExpires = new DateTime(2021, 01, 13, 22, 23, 01, DateTimeKind.Utc).ToLocalTime();

            cookie.Value.Should().Be("OwinValue");
            cookie.Path.Should().Be("/");
            cookie.Expires.Should().Be(expectedExpires);
            cookie.Secure.Should().BeTrue("cookie string contains Secure");
            cookie.HttpOnly.Should().BeTrue("cookie string contains HttpOnly");
            cookie.IsFromHeader().Should().BeTrue();
        }
        public async Task KentorOwinCookieSaverMiddleware_HandlesMissingHttpContext()
        {
            var context = new OwinContext();

            var next = new MiddlewareMock();

            var subject = new KentorOwinCookieSaverMiddleware(next);

            // Should not throw.
            await subject.Invoke(context);
        }
        public async Task KentorOwinCookieSaverMiddleware_RoundtripsMinimalCookie()
        {
            var context = CreateOwinContext();
            var next = new MiddlewareMock();
            var subject = new KentorOwinCookieSaverMiddleware(next);

            await subject.Invoke(context);

            // The interesting cookie is at offset 86 and is 26 chars long.
            var before = context.Response.Headers["Set-Cookie"].Substring(86, 26);

            var rebuiltHeader = RegenerateSetCookieHeader(context)
                .Single(s => s.StartsWith("MinimalCookie"));

            rebuiltHeader.Should().Be(before);
        }
        public async Task KentorOwinCookieSaverMiddleware_RoundtripsComplexCookie()
        {
            var context = CreateOwinContext();
            var next = new MiddlewareMock();
            var subject = new KentorOwinCookieSaverMiddleware(next);

            await subject.Invoke(context);

            // The first cookie is 85 chars long.
            var before = context.Response.Headers["Set-Cookie"].Substring(0, 85);

            var rebuiltHeader = RegenerateSetCookieHeader(context)
                .Single(s => s.StartsWith("OwinCookie"));

            rebuiltHeader.Should().Be(before);
        }