Ejemplo n.º 1
0
        public async void DisallowAnonymousClients()
        {
            // Given
            var isNextCalled  = false;
            var context       = new DefaultHttpContext();
            var configuration = new CongestionControlConfiguration {
                AllowAnonymousClients = false
            };
            var loggerMock = new Mock <ILogger <ClientResolutionMiddleware> >();

            var clientIdentifierProviderMock = new Mock <IClientIdentifierProvider>();

            clientIdentifierProviderMock
            .Setup(mock => mock.ExecuteAsync(Moq.It.IsAny <HttpContext>()))
            .ReturnsAsync((string)null);

            async Task Next(HttpContext httpContext)
            {
                await Task.CompletedTask;

                isNextCalled = true;
            }

            var middleware = new ClientResolutionMiddleware(Next,
                                                            configuration,
                                                            new List <IClientIdentifierProvider> {
                clientIdentifierProviderMock.Object
            },
                                                            loggerMock.Object);

            // When the middleware is invoked
            await middleware.Invoke(context);

            // Then it should not execute next delegate in pipeline
            isNextCalled.Should().BeFalse();

            // And it should return unauthorized response
            context.Response.StatusCode.Should().Be((int)HttpStatusCode.Unauthorized);
        }
Ejemplo n.º 2
0
        public async void ClientIdentityResolved()
        {
            // Given
            var clientId      = Guid.NewGuid().ToString();
            var isNextCalled  = false;
            var context       = new DefaultHttpContext();
            var configuration = new CongestionControlConfiguration();
            var loggerMock    = new Mock <ILogger <ClientResolutionMiddleware> >();

            var clientIdentifierProviderMock = new Mock <IClientIdentifierProvider>();

            clientIdentifierProviderMock
            .Setup(mock => mock.ExecuteAsync(Moq.It.IsAny <HttpContext>()))
            .ReturnsAsync(clientId);

            async Task Next(HttpContext httpContext)
            {
                await Task.CompletedTask;

                isNextCalled = true;
            }

            var middleware = new ClientResolutionMiddleware(Next,
                                                            configuration,
                                                            new List <IClientIdentifierProvider> {
                clientIdentifierProviderMock.Object
            },
                                                            loggerMock.Object);

            // When the middleware is invoked
            await middleware.Invoke(context);

            // Then it should execute next delegate in pipeline
            isNextCalled.Should().BeTrue();

            // And it should add client ID to http context items
            context.Items.GetClientId().Should().Be(clientId);
        }