public async void Can_Skip_When_Path_Excluded()
        {
            var contextFake = new DefaultHttpContext();

            contextFake.Request.Headers.Add("X-API-KEY", "WrongValue");

            contextFake.Request.Path = new PathString("/api");

            var middlewareFake = new ApiKeyMiddleware(next: async(innerHttpContext) =>
            {
                await _nextMock(innerHttpContext);
            }
                                                      , setupAction: c =>
            {
                c.ValidApiKeys = new List <string> {
                    "test", "test2"
                };
                c.NameInHeader          = "X-API-KEY";
                c.PathSegmentsToExclude = new List <string> {
                    "/api"
                };
            }
                                                      , logger: _loggerMock);

            await middlewareFake.Invoke(contextFake);

            _nextMock.Received(1);
            _loggerMock.Received(0).Log <Object>(Arg.Any <LogLevel>(), Arg.Is <EventId>(x => x.Id == LoggingEvents.Authentication), Arg.Any <Object>(), Arg.Any <Exception>(), Arg.Any <Func <Object, Exception, string> >());
        }
Exemplo n.º 2
0
        public void Setup()
        {
            _serviceProviderMock.Setup(x => x.GetService(typeof(IConfiguration))).Returns(_configMock.Object);
            _configMock
            .Setup(x => x.GetSection(It.Is <string>(m => m == "service_apiKey")))
            .Returns(_sectionApiHeaderMock.Object);
            _configMock
            .Setup(x => x.GetSection(It.Is <string>(m => m == ApiKeyMiddleware.TRUSTED_HOST_KEYNAME)))
            .Returns(_sectionTrustedHostMock.Object);

            _next  = (HttpContext hc) => { _isNextDelegateCalled = true; return(Task.CompletedTask); };
            _apiMw = new ApiKeyMiddleware(_next, "service_apiKey");
            _isNextDelegateCalled = false;
        }
        public ApiKeyMiddlewareTest()
        {
            _loggerMock = Substitute.For <ILogger <ApiKeyMiddleware> >();

            _nextMock = Substitute.For <RequestDelegate>();

            _middleware = new ApiKeyMiddleware(next: async(innerHttpContext) =>
            {
                await _nextMock(innerHttpContext);
            }
                                               , setupAction: c => {
                c.ValidApiKeys = new List <string> {
                    "test", "test2"
                };
                c.NameInHeader = "X-API-KEY";
                c.NameInQuery  = "apikey";
            }
                                               , logger: _loggerMock);
        }