public async Task ApiThrottlerTest_ByToken_QueryString() { ContainerBuilder builder = new ContainerBuilder(); // Fix user-agent & remote ip builder.RegisterType <FixRequestTestDataMiddleware>(); builder.RegisterType <ThrottlingMiddleware>(); builder.RegisterType <MorpherCache>() .As <IMorpherCache>() .WithParameter("name", "ApiThrottler") .SingleInstance(); builder.RegisterInstance(mockAnalyzer).As <IMorpher>(); builder.RegisterType <ApiThrottler>().As <IApiThrottler>(); IAttributeUrls attributeUrls = Mock.Of <IAttributeUrls>(urls => urls.Urls == new Dictionary <string, ThrottleThisAttribute>() { { "get:/russian/declension", new ThrottleThisAttribute(1, TarificationMode.PerRequest) } }); builder.RegisterInstance(attributeUrls) .As <IAttributeUrls>() .Keyed <IAttributeUrls>("ApiThrottler"); Mock <IMorpherDatabase> morpherDatabaseMock = new Mock <IMorpherDatabase>(); Guid testToken = Guid.NewGuid(); morpherDatabaseMock.Setup(database => database.GetUserLimits(testToken)).Returns(new MorpherCacheObject() { PaidUser = false, UserId = Guid.Empty, QueriesLeft = 3, Unlimited = false }); builder.RegisterInstance(morpherDatabaseMock.Object).As <IMorpherDatabase>().SingleInstance(); using (var server = PrepareTestServer(builder)) { using (var client = server.HttpClient) { for (int i = 0; i < 3; i++) { var result = await client.GetAsync($"/russian/declension?s=Тест&token={testToken}"); if (result.StatusCode == HttpStatusCode.InternalServerError) { throw new Exception(await result.Content.ReadAsStringAsync()); } Assert.AreEqual(true, result.IsSuccessStatusCode, "StatudCode != OK"); } var errorResult = await client.GetAsync($"/russian/declension?s=Тест&token={testToken}"); Assert.AreEqual(HttpStatusCode.PaymentRequired, errorResult.StatusCode); } } }
public LoggingMiddleware( OwinMiddleware next, IMorpherLog morpherLog, IAttributeUrls attributeUrls) : base(next) { _morpherLog = morpherLog; _attributeUrls = attributeUrls; }
public ThrottlingMiddleware( OwinMiddleware next, IApiThrottler apiThrottler, IAttributeUrls attributeUrls) : base(next) { _apiThrottler = apiThrottler; _attributeUrls = attributeUrls; }
public async Task LogWithError() { // Arrange ContainerBuilder builder = new ContainerBuilder(); builder.RegisterInstance(mockAnalyzer).As <IMorpher>(); // LoggingMiddleware использует два класса для работы с логами в бд. DatabaseLogMock databaseLogMock = new DatabaseLogMock(); builder.RegisterInstance(databaseLogMock).As <IDatabaseLog>().SingleInstance(); builder.RegisterType <MorpherLog>().As <IMorpherLog>().SingleInstance(); builder.Register(context => new MorpherExceptionFilterAttribute()) .AsWebApiExceptionFilterFor <ApiController>().SingleInstance(); builder.RegisterType <MorpherCache>() .As <IMorpherCache>() .WithParameter("name", "ApiThrottler") .SingleInstance(); IAttributeUrls attributeUrls = Mock.Of <IAttributeUrls>(urls => urls.Urls == new Dictionary <string, ThrottleThisAttribute>() { { "get:/russian/declension", new ThrottleThisAttribute(1, TarificationMode.PerRequest) } }); builder.RegisterInstance(attributeUrls) .As <IAttributeUrls>() .Keyed <IAttributeUrls>("Logger"); builder.RegisterType <LoggingMiddleware>(); using (var testServer = PrepareTestServer(builder)) { // Act using (var client = testServer.HttpClient) { var result = await client.GetAsync("/russian/declension?s="); if (result.StatusCode == HttpStatusCode.InternalServerError) { throw new Exception(await result.Content.ReadAsStringAsync()); } } // Assert var morpheLog = _container.Resolve <IMorpherLog>(); morpheLog.Sync(); Assert.AreEqual(1, databaseLogMock.Logs.Count); var logEntity = databaseLogMock.Logs.First(); Assert.AreEqual(new RequiredParameterIsNotSpecifiedException("s").Code, logEntity.ErrorCode); } }
public async Task ApiThrottlerTest_BySymbol() { ContainerBuilder builder = new ContainerBuilder(); // Fix user-agent & remote ip builder.RegisterType <FixRequestTestDataMiddleware>(); builder.RegisterType <ThrottlingMiddleware>(); MorpherCache cache = new MorpherCache("ApiThrottler"); builder.RegisterInstance(cache).As <IMorpherCache>(); builder.RegisterInstance(mockAnalyzer).As <IMorpher>(); builder.RegisterType <ApiThrottler>().As <IApiThrottler>(); IAttributeUrls attributeUrls = Mock.Of <IAttributeUrls>(urls => urls.Urls == new Dictionary <string, ThrottleThisAttribute>() { { "post:/russian/addstressmarks", new ThrottleThisAttribute(10, TarificationMode.PerSymbol) } }); builder.RegisterInstance(attributeUrls) .As <IAttributeUrls>() .Keyed <IAttributeUrls>("ApiThrottler"); Mock <IMorpherDatabase> morpherDatabaseMock = new Mock <IMorpherDatabase>(); morpherDatabaseMock.Setup(database => database.IsIpBlocked("0.0.0.0")).Returns(false); morpherDatabaseMock.Setup(database => database.GetDefaultDailyQueryLimit()).Returns(3); builder.RegisterInstance(morpherDatabaseMock.Object).As <IMorpherDatabase>().SingleInstance(); using (var server = PrepareTestServer(builder)) { using (var client = server.HttpClient) { var result = await client.PostAsync("/russian/addstressmarks", new StringContent("здесь 17 символов", Encoding.UTF8, "text/plain")); if (result.StatusCode == HttpStatusCode.InternalServerError) { throw new Exception(await result.Content.ReadAsStringAsync()); } Assert.AreEqual(true, result.IsSuccessStatusCode, "StatudCode != OK"); var cacheEntry = (MorpherCacheObject)cache.Get("0.0.0.0"); Assert.AreEqual(1, cacheEntry.QueriesLeft); } } }