Example #1
0
        public async Task AuthenticatedTest()
        {
            //Arrange
            var context = new DefaultHttpContext();


            context.User = new System.Security.Claims.ClaimsPrincipal();
            context.User.AddIdentity(new System.Security.Claims.ClaimsIdentity("unitTestAuthentication"));
            context.User.AddIdentity(new System.Security.Claims.ClaimsIdentity());


            //just add a delegate so we can test to make sure it got called
            RequestDelegate next = x =>
            {
                x.Response.ContentType = "application/xml";  //just picked an arbitrary non default contenttype
                return(Task.FromResult <object>(null));
            };

            var middleware = new RequireAuthenticationMiddleware(next);

            //Act
            await middleware.Invoke(context);

            //Assert

            context.Response.ContentType.Should().Be("application/xml"); //make sure our delegate got called
        }
Example #2
0
        public async Task NothAuthenticatedShouldReturnUnauthorizedTest()
        {
            //Arrange
            var context = new DefaultHttpContext();

            //just add a delegate
            RequestDelegate next = x =>
            {
                x.Response.ContentType = "application/xml";  //just picked an arbitrary non default contenttype
                return(Task.FromResult <object>(null));
            };

            var middleware = new RequireAuthenticationMiddleware(next);

            //Act
            await middleware.Invoke(context);

            //Assert
            context.Response.StatusCode.Should().Be((int)HttpStatusCode.Unauthorized);
        }