Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 5
0
        private async Task TestMiddleWare(string requestMethod, Dictionary <string, string> requestHeader, string requestUri, int expectedStatus, Dictionary <string, string> expectedResponseHeader, bool allowExternalValidation)
        {
            var context = new DefaultHttpContext();

            context.Request.Method = requestMethod;
            context.Request.Path   = requestUri;

            foreach (var(key, value) in requestHeader)
            {
                context.Request.Headers.Add(key, new[] { value });
            }

            _fakeMessageHandler.Setup(mh => mh.Send(It.IsAny <HttpRequestMessage>())).Returns(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("")
            });

            var client = new HttpClient(_fakeMessageHandler.Object);

            var feature = new MockResponseFeature();

            context.Features.Set <IHttpResponseFeature>(feature);

            async Task Next(HttpContext ctx)
            {
                Console.WriteLine(ctx.Response.Headers.Count);
                await feature.InvokeCallBack();
            }

            var nextMiddleware = new MiddlewareMock(Next);

            await new IdentityProvider.Middleware.IdentityProviderMiddleware(nextMiddleware.InvokeAsync,
                                                                             new IdentityProviderOptions
            {
                BaseAddress               = new Uri(DEFAULT_SYSTEM_BASE_URI),
                AllowExternalValidation   = allowExternalValidation,
                TenantInformationCallback = () => new TenantInformation {
                    TenantId = "0", SystemBaseUri = "https://localhost"
                },
                HttpClient = client
            })
            .Invoke(context);

            context.Response.StatusCode.Should().Be(expectedStatus);


            nextMiddleware.HasBeenInvoked.Should().BeTrue("next middleware should not have been invoked if signature is wrong");
            foreach (var(key, value) in expectedResponseHeader)
            {
                context.Response.Headers[key].ToString().Should().BeEquivalentTo(value);
            }
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }