public async Task Passes_through_to_specific_service_with_delegate()
        {
            RequestDelegate _next = (ctx) => Task.Run(() => { throw new NotImplementedException(); });

            var host       = TestHelpers.CreatePluginHost(new[] { typeof(EndpointMiddlewareFacts).GetTypeInfo().Assembly, typeof(EndpointDescriptor).GetTypeInfo().Assembly });
            var middleware = new EndpointMiddleware(_next, host, new LoggerFactory());

            var context = new DefaultHttpContext();

            context.Request.Path = PathString.FromUriComponent("/findsymbols");

            var memoryStream = new MemoryStream();

            context.Request.Body = new MemoryStream(
                Encoding.UTF8.GetBytes(
                    JsonConvert.SerializeObject(new FindSymbolsRequest
            {
                Language = LanguageNames.CSharp
            })
                    )
                );

            await middleware.Invoke(context);

            Assert.True(true);
        }
Beispiel #2
0
        public async Task Passes_through_to_all_services_with_delegate()
        {
            RequestDelegate _next = _ => Task.Run(() => { throw new NotImplementedException(); });

            using (var host = CreatePlugInHost(
                       GetAssembly <EndpointMiddlewareFacts>(),
                       GetAssembly <OmniSharpEndpointMetadata>()))
            {
                var middleware = new EndpointMiddleware(_next, host.CompositionHost, this.LoggerFactory);

                var context = new DefaultHttpContext();
                context.Request.Path = PathString.FromUriComponent("/findsymbols");

                context.Request.Body = new MemoryStream(
                    Encoding.UTF8.GetBytes(
                        JsonConvert.SerializeObject(new FindSymbolsRequest
                {
                })
                        )
                    );

                await middleware.Invoke(context);

                Assert.True(true);
            }
        }
        public async Task Passes_through_to_services()
        {
            RequestDelegate _next = (ctx) => Task.Run(() => { throw new NotImplementedException(); });

            var host       = TestHelpers.CreatePluginHost(new[] { typeof(EndpointMiddlewareFacts).GetTypeInfo().Assembly, typeof(EndpointDescriptor).GetTypeInfo().Assembly });
            var middleware = new EndpointMiddleware(_next, host, new LoggerFactory());

            var context = new DefaultHttpContext();

            context.Request.Path = PathString.FromUriComponent("/gotodefinition");

            var memoryStream = new MemoryStream();

            context.Request.Body = new MemoryStream(
                Encoding.UTF8.GetBytes(
                    JsonConvert.SerializeObject(new GotoDefinitionRequest
            {
                FileName = "bar.cs",
                Line     = 2,
                Column   = 14,
                Timeout  = 60000
            })
                    )
                );

            await middleware.Invoke(context);

            Assert.True(true);
        }
    public async Task Invoke_WithEndpoint_ThrowsIfAuthAttributesWereFound_ButAuthMiddlewareNotInvoked()
    {
        // Arrange
        var expected = "Endpoint Test contains authorization metadata, but a middleware was not found that supports authorization." +
                       Environment.NewLine +
                       "Configure your application startup by adding app.UseAuthorization() in the application startup code. " +
                       "If there are calls to app.UseRouting() and app.UseEndpoints(...), the call to app.UseAuthorization() must go between them.";
        var httpContext = new DefaultHttpContext
        {
            RequestServices = new ServiceProvider()
        };

        RequestDelegate throwIfCalled = (c) =>
        {
            throw new InvalidTimeZoneException("Should not be called");
        };

        httpContext.SetEndpoint(new Endpoint(throwIfCalled, new EndpointMetadataCollection(Mock.Of <IAuthorizeData>()), "Test"));

        var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, throwIfCalled, RouteOptions);

        // Act & Assert
        var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => middleware.Invoke(httpContext));

        // Assert
        Assert.Equal(expected, ex.Message);
    }
Beispiel #5
0
        public async Task Passes_through_to_services()
        {
            RequestDelegate _next = _ => Task.Run(() => { throw new NotImplementedException(); });

            using (var host = CreatePlugInHost(
                       GetAssembly <EndpointMiddlewareFacts>(),
                       GetAssembly <OmniSharpEndpointMetadata>()))
            {
                var middleware = new EndpointMiddleware(_next, host.CompositionHost, this.LoggerFactory);

                var context = new DefaultHttpContext();
                context.Request.Path = PathString.FromUriComponent("/gotodefinition");

                context.Request.Body = new MemoryStream(
                    Encoding.UTF8.GetBytes(
                        JsonConvert.SerializeObject(new GotoDefinitionRequest
                {
                    FileName = "bar.cs",
                    Line     = 2,
                    Column   = 14,
                    Timeout  = 60000
                })
                        )
                    );

                await middleware.Invoke(context);

                Assert.True(true);
            }
        }
    public async Task Invoke_WithEndpoint_InvokesDelegate()
    {
        // Arrange
        var httpContext = new DefaultHttpContext();

        httpContext.RequestServices = new ServiceProvider();

        var             calledEndpoint = false;
        RequestDelegate endpointFunc   = (c) =>
        {
            calledEndpoint = true;
            return(Task.CompletedTask);
        };

        httpContext.SetEndpoint(new Endpoint(endpointFunc, EndpointMetadataCollection.Empty, "Test"));

        RequestDelegate next = (c) =>
        {
            throw new InvalidTimeZoneException("Should not be called");
        };

        var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, next, RouteOptions);

        // Act
        await middleware.Invoke(httpContext);

        // Assert
        Assert.True(calledEndpoint);
    }
    public async Task Invoke_WithEndpoint_DoesNotThrowIfUnhandledCorsAttributesWereFound_ButSuppressedViaOptions()
    {
        // Arrange
        var httpContext = new DefaultHttpContext
        {
            RequestServices = new ServiceProvider()
        };

        var             calledEndpoint = false;
        RequestDelegate endpointFunc   = (c) =>
        {
            calledEndpoint = true;
            return(Task.CompletedTask);
        };

        httpContext.SetEndpoint(new Endpoint(endpointFunc, new EndpointMetadataCollection(Mock.Of <IAuthorizeData>()), "Test"));

        var routeOptions = Options.Create(new RouteOptions {
            SuppressCheckForUnhandledSecurityMetadata = true
        });

        RequestDelegate next = (c) =>
        {
            throw new InvalidTimeZoneException("Should not be called");
        };

        var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, next, routeOptions);

        // Act
        await middleware.Invoke(httpContext);

        // Assert
        Assert.True(calledEndpoint);
    }
    public async Task Invoke_WithEndpoint_WorksIfCorsMetadataWasFound_AndCorsMiddlewareInvoked()
    {
        // Arrange
        var httpContext = new DefaultHttpContext
        {
            RequestServices = new ServiceProvider()
        };

        var             calledEndpoint = false;
        RequestDelegate endpointFunc   = (c) =>
        {
            calledEndpoint = true;
            return(Task.CompletedTask);
        };

        httpContext.SetEndpoint(new Endpoint(endpointFunc, new EndpointMetadataCollection(Mock.Of <ICorsMetadata>()), "Test"));

        httpContext.Items[EndpointMiddleware.CorsMiddlewareInvokedKey] = true;

        RequestDelegate next = (c) =>
        {
            throw new InvalidTimeZoneException("Should not be called");
        };

        var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, next, RouteOptions);

        // Act
        await middleware.Invoke(httpContext);

        // Assert
        Assert.True(calledEndpoint);
    }
        public async Task Passes_through_for_invalid_path()
        {
            RequestDelegate _next = (ctx) => Task.Run(() => { throw new NotImplementedException(); });

            var host       = TestHelpers.CreatePluginHost(new[] { typeof(EndpointMiddlewareFacts).GetTypeInfo().Assembly });
            var middleware = new EndpointMiddleware(_next, host, new LoggerFactory());

            var context = new DefaultHttpContext();

            context.Request.Path = PathString.FromUriComponent("/notvalid");

            await Assert.ThrowsAsync <NotImplementedException>(() => middleware.Invoke(context));
        }
Beispiel #10
0
        public async Task Passes_through_for_invalid_path()
        {
            RequestDelegate _next = _ => Task.Run(() => { throw new NotImplementedException(); });

            using (var host = CreatePlugInHost(GetAssembly <EndpointMiddlewareFacts>()))
            {
                var middleware = new EndpointMiddleware(_next, host.CompositionHost, this.LoggerFactory);

                var context = new DefaultHttpContext();
                context.Request.Path = PathString.FromUriComponent("/notvalid");

                await Assert.ThrowsAsync <NotImplementedException>(() => middleware.Invoke(context));
            }
        }
Beispiel #11
0
        public async Task Should_throw_if_type_is_not_mergeable()
        {
            RequestDelegate _next = async(ctx) => await Task.Run(() => { throw new NotImplementedException(); });

            using (var host = CreatePlugInHost(GetAssembly <EndpointMiddlewareFacts>()))
            {
                var middleware = new EndpointMiddleware(_next, host.CompositionHost, this.LoggerFactory);

                var context = new DefaultHttpContext();
                context.Request.Path = PathString.FromUriComponent("/throw");

                context.Request.Body = new MemoryStream(
                    Encoding.UTF8.GetBytes(
                        JsonConvert.SerializeObject(new ThrowRequest())
                        )
                    );

                await Assert.ThrowsAsync <NotSupportedException>(async() => await middleware.Invoke(context));
            }
        }
    public async Task Invoke_NoFeature_NoOps()
    {
        // Arrange
        var httpContext = new DefaultHttpContext();

        httpContext.RequestServices = new ServiceProvider();

        var             calledNext = false;
        RequestDelegate next       = (c) =>
        {
            calledNext = true;
            return(Task.CompletedTask);
        };

        var middleware = new EndpointMiddleware(NullLogger <EndpointMiddleware> .Instance, next, RouteOptions);

        // Act
        await middleware.Invoke(httpContext);

        // Assert
        Assert.True(calledNext);
    }