public void GetValidRouteDefsTest()
        {
            TestingUtils.ResetRoutesRepository();
            var result = RoutesUtils.GetValidRouteDefs(new List <IRouteDefinitions>()
            {
                new Routes()
            });

            Assert.Equal(7, result.Keys.Count);
            Assert.Equal(2, result["PUT"].Count);
        }
 public void GetValidRouteDefsTest_InvalidRouteTemplate()
 {
     TestingUtils.ResetRoutesRepository();
     try
     {
         RoutesUtils.GetValidRouteDefs(new List <IRouteDefinitions>()
         {
             new InvalidRouteTemplateRoutes()
         });
     }
     catch (Exception)
     {
         return;
     }
     Assert.True(false);
 }
        public void RegisterRoutesAsyncTest_ByRouteProvider()
        {
            TestingUtils.ResetRoutesRepository();
            var routesRepository = new RoutesRepository();

            routesRepository.RegisterRoutesAsync(new ValidRouteProvider()).Wait();
            var useProvider     = GetPrivateValueFromRoutesRepository <bool>("_useProvider", routesRepository);
            var routeProvider   = GetPrivateValueFromRoutesRepository <IValidRouteDefinitionProvider>("_routeDefinitionProvider", routesRepository);
            var refreshTimespan = GetPrivateValueFromRoutesRepository <TimeSpan>("_refreshTimespan", routesRepository);

            Assert.Equal(TimeSpan.MinValue, refreshTimespan);
            Assert.True(useProvider);
            Assert.NotNull(routeProvider);
            var routes = routesRepository.GetRoutesAsync().Result;

            Assert.Single(routes.Values);
            Assert.Equal(HttpMethod.Post, routes.Values.First().First().Method);
            Assert.Single(routes.Values.First());
        }
        public void SentinelTest_ThisOneShouldntPassButDoes()
        {
            TestingUtils.ResetSentinel();
            TestingUtils.ResetRoutesRepository();

            var routesRepository = new RoutesRepository();

            routesRepository.RegisterRoutesAsync(new List <IRouteDefinitions>()
            {
                new SpecialRouteTemplateRoutes()
            }).Wait();
            var httpContext = new DefaultHttpContext();

            httpContext.Response.Body = new MemoryStream();
            var request = new DefaultHttpRequest(httpContext)
            {
                Method = HttpMethod.Post.Method,
                Path   = "/v1/yolo/nolo/thisIsDefinitelyNotAnInteger",
                Query  = new QueryCollection(new Dictionary <string, Microsoft.Extensions.Primitives.StringValues>()
                {
                    { "No", "true" }
                }),
                Body = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new TestingType()
                {
                    Yo = "lo", No = "low"
                })))
            };

            httpContext.Items.Add("ExpectedClaims", new List <Claim>()
            {
                new Claim("PityTheFoolType", "low"),
                new Claim("PityTheFoolJsonPath", "low"),
                new Claim("PityTheFoolKeyValue", "true"),
                new Claim("PityTheFoolRegex", "yolo"),
            });
            var sentinel = new Sentinel(reqDelegate, routesRepository);
            var result   = sentinel.Invoke(httpContext);

            Assert.NotNull(request);
            Assert.NotNull(result);
            Assert.Equal((int)HttpStatusCode.Created, httpContext.Response.StatusCode);
        }
        public void RegisterRoutesAsyncTest_ByRouteDefinitions()
        {
            TestingUtils.ResetRoutesRepository();
            var routesRepository = new RoutesRepository();

            routesRepository.RegisterRoutesAsync(new List <IRouteDefinitions>()
            {
                new AnotherRoutes()
            }).Wait();
            var useProvider   = GetPrivateValueFromRoutesRepository <bool>("_useProvider", routesRepository);
            var routeProvider = GetPrivateValueFromRoutesRepository <IValidRouteDefinitionProvider>("_routeDefinitionProvider", routesRepository);

            Assert.False(useProvider);
            Assert.Null(routeProvider);
            var routes = routesRepository.GetRoutesAsync();

            Assert.Equal(2, routes.Result.Values.First().Count);
            Assert.Equal(HttpMethod.Put, routes.Result.Values.First().First().Method);
            Assert.Equal(HttpMethod.Put, routes.Result.Values.First().ElementAt(1).Method);
        }
        public void MatchRouteAsyncTest_NoMatch()
        {
            TestingUtils.ResetRoutesRepository();
            var routesRepository = new RoutesRepository();

            routesRepository.RegisterRoutesAsync(new List <IRouteDefinitions>()
            {
                new SpecialRouteTemplateRoutes()
            }).Wait();
            var httpContext = new DefaultHttpContext();
            var request     = new DefaultHttpRequest(httpContext)
            {
                Method = HttpMethod.Post.Method,
                Path   = "/v1/yolo/nolo/asdf/kjlkjl"
            };
            var result = RoutesUtils.MatchRouteAsync(httpContext, routesRepository).Result;

            Assert.NotNull(request);
            Assert.Null(result.Key);
            Assert.Null(result.Value);
        }
        public void MatchRouteAsyncTest()
        {
            TestingUtils.ResetRoutesRepository();
            var routesRepository = new RoutesRepository();

            routesRepository.RegisterRoutesAsync(new List <IRouteDefinitions>()
            {
                new SpecialRouteTemplateRoutes()
            }).Wait();
            var httpContext = new DefaultHttpContext();
            var request     = new DefaultHttpRequest(httpContext)
            {
                Method = HttpMethod.Post.Method,
                Path   = "/v1/yolo/nolo/1"
            };
            var result = RoutesUtils.MatchRouteAsync(httpContext, routesRepository).Result;

            Assert.NotNull(request);
            Assert.Equal(4, result.Key.ClaimsConfig.ExtractionConfigs.Count);
            Assert.Equal(1, result.Key.ClaimsConfig.ValidationConfigs.Count);
            Assert.NotNull(result.Key.ClaimsConfig.BadRequestResponse);
            Assert.Equal("1", result.Value["id"]);
        }
 public void GetValidRouteDefsTest_NullRouteDefs()
 {
     TestingUtils.ResetRoutesRepository();
     Assert.Equal(new Dictionary <string, List <InternalRouteDefinition> >(), RoutesUtils.GetValidRouteDefs(null));
 }