Exemplo n.º 1
0
        public void apiroutingtable___addroute_returns_null_for_null_registration()
        {
            var routingTable = new ApiRoutingTable();

            routingTable.AddRoute(null);

            routingTable.GetRoutes().Count.Should().Be(0);
        }
Exemplo n.º 2
0
        public async Task openapi___generates_v2_v3_for_json_and_yaml_for_lists()
        {
            var table = new ApiRoutingTable();

            table.AddRoute(new DeepSleepRouteRegistration(
                               template: "/test/list",
                               httpMethods: new[] { "GET" },
                               controller: typeof(ListController),
                               endpoint: nameof(ListController.List),
                               config: new DeepSleepRequestConfiguration()));

            table.AddRoute(new DeepSleepRouteRegistration(
                               template: "/test/list1",
                               httpMethods: new[] { "GET" },
                               controller: typeof(ListController),
                               endpoint: nameof(ListController.List1),
                               config: new DeepSleepRequestConfiguration()));

            table.AddRoute(new DeepSleepRouteRegistration(
                               template: "/test/list2",
                               httpMethods: new[] { "GET" },
                               controller: typeof(ListController),
                               endpoint: nameof(ListController.List2),
                               config: new DeepSleepRequestConfiguration()));

            table.AddRoute(new DeepSleepRouteRegistration(
                               template: "/test/list/container",
                               httpMethods: new[] { "GET" },
                               controller: typeof(ListController),
                               endpoint: nameof(ListController.ListContainer),
                               config: new DeepSleepRequestConfiguration()));

            var configuration = new DeepSleepOasConfiguration
            {
                Info = new OpenApiInfo
                {
                    Description = "Test",
                    Title       = "Test"
                },
                PrefixNamesWithNamespace = false
            };

            var mockServiceProvider = new Mock <IServiceProvider>();

            mockServiceProvider.Setup(m => m.GetService(It.Is <Type>(t => t == typeof(IDeepSleepOasConfiguration)))).Returns(configuration);
            mockServiceProvider.Setup(m => m.GetService(It.Is <Type>(t => t == typeof(IApiRoutingTable)))).Returns(table);

            var generator = new DeepSleepOasGenerator(mockServiceProvider.Object);
            var document  = await generator.Generate().ConfigureAwait(false);

            var resultsJsonV2 = document.Serialize(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Json);
            var resultsJsonV3 = document.Serialize(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json);
            var resultsYamlV2 = document.Serialize(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Yaml);
            var resultsYamlV3 = document.Serialize(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Yaml);
        }
Exemplo n.º 3
0
        public void console___writes_header()
        {
            var routingTable = new ApiRoutingTable();

            routingTable.AddRoute(new DeepSleepRouteRegistration(
                                      template: "/myroute/{test}/other/{test2}/id",
                                      httpMethods: new[] { "GET" },
                                      controller: typeof(TestController),
                                      endpoint: nameof(TestController.Get)));

            ApiCoreHttpExtensionMethods.WriteDeepSleepToConsole(routingTable: routingTable, null);
        }
Exemplo n.º 4
0
        public async void pipeline_method___returns_false_for_method_not_allowed_for_unsupported_method()
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Routing        = new ApiRoutingInfo
                {
                    Template = new ApiRoutingTemplate(null),
                    Route    = null
                },
                Request = new ApiRequestInfo
                {
                    Path = "/test/path"
                }
            };

            context.Routing.Template.Locations.Add(new ApiEndpointLocation(controller: null, methodInfo: null, httpMethod: "POST"));
            context.Routing.Template.Locations.Add(new ApiEndpointLocation(controller: null, methodInfo: null, httpMethod: "put"));
            context.Routing.Template.Locations.Add(new ApiEndpointLocation(controller: null, methodInfo: null, httpMethod: ""));
            context.Routing.Template.Locations.Add(new ApiEndpointLocation(controller: null, methodInfo: null, httpMethod: "PATCH"));
            context.Routing.Template.Locations.Add(new ApiEndpointLocation(controller: null, methodInfo: null, httpMethod: null));
            context.Routing.Template.Locations.Add(new ApiEndpointLocation(controller: null, methodInfo: null, httpMethod: "DelEte"));
            context.Routing.Template.Locations.Add(new ApiEndpointLocation(controller: null, methodInfo: null, httpMethod: "get"));


            var resolver = new ApiRouteResolver();
            var routes   = new ApiRoutingTable();

            routes.AddRoute(new DeepSleepRouteRegistration(
                                template: "test/path",
                                httpMethods: new[] { "GET" },
                                controller: typeof(Mocks.MockController),
                                endpoint: nameof(Mocks.MockController.Get)));


            var processed = await context.ProcessHttpRequestMethod(routes, resolver, null).ConfigureAwait(false);

            processed.Should().BeFalse();

            context.Response.Should().NotBeNull();
            context.Response.ResponseObject.Should().BeNull();
            context.Response.StatusCode.Should().Be(405);

            context.Response.Headers.Should().NotBeNull();
            context.Response.Headers.Should().HaveCount(1);
            context.Response.Headers[0].Name.Should().Be("Allow");
            context.Response.Headers[0].Value.Should().Be("POST, PUT, PATCH, DELETE, GET, HEAD");
        }
Exemplo n.º 5
0
        public void apiroutingtable___addroute_throws_for_missing_method()
        {
            var routingTable = new ApiRoutingTable();

            var exception = Assert.Throws <MissingMethodException>(() =>
            {
                routingTable.AddRoute(new DeepSleepRouteRegistration(
                                          template: "/test",
                                          httpMethods: new[] { "get" },
                                          controller: typeof(ApiRoutingTableTestController),
                                          endpoint: "test10000"));
            });

            exception.Should().NotBeNull();
            exception.Message.Should().Be("Endpoint 'test10000' does not exist on controller 'DeepSleep.Tests.ApiRoutingTableTestController'");
        }
Exemplo n.º 6
0
        public void apiroutingtable___addroute_throws_for_ambiguous_method_overload()
        {
            var routingTable = new ApiRoutingTable();

            var exception = Assert.Throws <Exception>(() =>
            {
                routingTable.AddRoute(new DeepSleepRouteRegistration(
                                          template: "/test",
                                          httpMethods: new[] { "get" },
                                          controller: typeof(ApiRoutingTableTestController),
                                          endpoint: nameof(ApiRoutingTableTestController.Test3)));
            });

            exception.Should().NotBeNull();
            exception.Message.Should().Be("DeepSleep api routing does not support routes mapped to overloaded methods for api routes.  You must rename or move the method for route '[GET] test'.");
            exception.InnerException.Should().NotBeNull();
            exception.InnerException.GetType().Should().Be(typeof(AmbiguousMatchException));
        }
Exemplo n.º 7
0
        public void apiroutingtable___addroutes_success()
        {
            var routingTable = new ApiRoutingTable();

            routingTable.AddRoutes(new List <DeepSleepRouteRegistration>
            {
                new DeepSleepRouteRegistration(
                    template: "/test",
                    httpMethods: new[] { "GET" },
                    controller: typeof(ApiRoutingTableTestController),
                    endpoint: nameof(ApiRoutingTableTestController.Test)),

                new DeepSleepRouteRegistration(
                    template: "/test2",
                    httpMethods: new[] { "GET" },
                    controller: typeof(ApiRoutingTableTestController),
                    endpoint: nameof(ApiRoutingTableTestController.Test2))
            });

            routingTable.GetRoutes().Count.Should().Be(2);
        }
Exemplo n.º 8
0
        public void apiroutingtable___addroute_throws_for_duplication_template_method()
        {
            var routingTable = new ApiRoutingTable();

            routingTable.AddRoute(new DeepSleepRouteRegistration(
                                      template: "/test",
                                      httpMethods: new[] { "GET" },
                                      controller: typeof(ApiRoutingTableTestController),
                                      endpoint: nameof(ApiRoutingTableTestController.Test)));

            var exception = Assert.Throws <Exception>(() =>
            {
                routingTable.AddRoute(new DeepSleepRouteRegistration(
                                          template: "/test",
                                          httpMethods: new[] { "get" },
                                          controller: typeof(ApiRoutingTableTestController),
                                          endpoint: nameof(ApiRoutingTableTestController.Test2)));
            });

            exception.Should().NotBeNull();
            exception.Message.Should().Be("Route 'GET /test' already has been added.");
        }
Exemplo n.º 9
0
        /// <summary>Uses the deep sleep services.</summary>
        /// <param name="services">The services.</param>
        /// <param name="configure">The configure.</param>
        /// <returns></returns>
        public static IServiceCollection UseDeepSleepServices(this IServiceCollection services, Action <IDeepSleepServiceConfiguration> configure = null)
        {
            var configuration = new DeepSleepServiceConfiguration
            {
                DefaultRequestConfiguration = ApiRequestContext.GetDefaultRequestConfiguration(),
                ExcludePaths = new List <string>(),
                IncludePaths = new List <string> {
                    ApiPaths.All()
                },
                WriteConsoleHeader  = true,
                DiscoveryStrategies = new List <IDeepSleepDiscoveryStrategy>()
            };

            if (configure != null)
            {
                configure(configuration);
            }

            configuration.DefaultRequestConfiguration = configuration.DefaultRequestConfiguration ?? ApiRequestContext.GetDefaultRequestConfiguration();
            configuration.ExcludePaths = configuration.ExcludePaths ?? new List <string>();
            configuration.IncludePaths = configuration.IncludePaths ?? new List <string>();

            var routingTable = new ApiRoutingTable(routePrefix: configuration.RoutePrefix);

            services
            .AddScoped <IApiRequestContextResolver, ApiRequestContextResolver>()
            .AddScoped <IFormUrlEncodedObjectSerializer, FormUrlEncodedObjectSerializer>()
            .AddScoped <IUriRouteResolver, ApiRouteResolver>()
            .AddScoped <IMultipartStreamReader, MultipartStreamReader>()
            .AddScoped <IDeepSleepMediaSerializerFactory, DeepSleepMediaSerializerWriterFactory>()
            .AddScoped <IApiValidationProvider, ApiEndpointValidationProvider>()
            .AddSingleton <IApiRequestPipeline, IApiRequestPipeline>((p) => ApiRequestPipeline.GetDefaultRequestPipeline())
            .AddSingleton <IDeepSleepRequestConfiguration, IDeepSleepRequestConfiguration>((p) => configuration.DefaultRequestConfiguration)
            .AddSingleton <IDeepSleepServiceConfiguration, IDeepSleepServiceConfiguration>((p) => configuration)
            .AddSingleton <IApiRoutingTable, IApiRoutingTable>((p) => routingTable);

            return(services);
        }