refactor code

This commit is contained in:
geffzhang
2017-02-13 08:29:29 +08:00
parent 659d124780
commit 2fa6e66dd4
18 changed files with 119 additions and 138 deletions

View File

@ -67,7 +67,9 @@ namespace Ocelot.AcceptanceTests
ClientIdHeader = "ClientId",
DisableRateLimitHeaders = false,
QuotaExceededMessage = "",
RateLimitCounterPrefix = ""
RateLimitCounterPrefix = "",
HttpStatusCode = 428
},
RequestIdKey ="oceclientrequest"
}
@ -76,8 +78,12 @@ namespace Ocelot.AcceptanceTests
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879/api/ClientRateLimit"))
.And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit", 5))
.Then(x => _steps.ThenTheStatusCodeShouldBe(429))
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit",1))
.Then(x => _steps.ThenTheStatusCodeShouldBe(200))
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit", 2))
.Then(x => _steps.ThenTheStatusCodeShouldBe(200))
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit",1))
.Then(x => _steps.ThenTheStatusCodeShouldBe(428))
.BDDfy();
}
@ -154,55 +160,6 @@ namespace Ocelot.AcceptanceTests
_builder.Start();
}
//private void GetApiRateLimait(string url)
//{
// var clientId = "ocelotclient1";
// var request = new HttpRequestMessage(new HttpMethod("GET"), url);
// request.Headers.Add("ClientId", clientId);
// var response = _client.SendAsync(request);
// responseStatusCode = (int)response.Result.StatusCode;
// }
//}
//public void WhenIGetUrlOnTheApiGatewayMultipleTimes(string url, int times)
//{
// var clientId = "ocelotclient1";
// var tasks = new Task[times];
// for (int i = 0; i < times; i++)
// {
// var urlCopy = url;
// tasks[i] = GetForServiceDiscoveryTest(urlCopy);
// Thread.Sleep(_random.Next(40, 60));
// }
// Task.WaitAll(tasks);
//}
//private void WhenICallTheMiddlewareWithWhiteClient()
//{
// var clientId = "ocelotclient2";
// // Act
// for (int i = 0; i < 2; i++)
// {
// var request = new HttpRequestMessage(new HttpMethod("GET"), apiRateLimitPath);
// request.Headers.Add("ClientId", clientId);
// var response = _client.SendAsync(request);
// responseStatusCode = (int)response.Result.StatusCode;
// }
//}
//private void ThenresponseStatusCodeIs429()
//{
// responseStatusCode.ShouldBe(429);
//}
//private void ThenresponseStatusCodeIs200()
//{
// responseStatusCode.ShouldBe(200);
//}
}
}

View File

@ -101,7 +101,6 @@ namespace Ocelot.AcceptanceTests
})
.WithDictionaryHandle();
};
s.AddMemoryCache();
s.AddOcelotOutputCaching(settings);
s.AddOcelotFileConfiguration(configuration);
s.AddOcelot();

View File

@ -1 +1 @@
{"ReRoutes":[{"DownstreamPathTemplate":"41879/","UpstreamTemplate":"/","UpstreamHttpMethod":"Get","AuthenticationOptions":{"Provider":null,"ProviderRootUrl":null,"ScopeName":null,"RequireHttps":false,"AdditionalScopes":[],"ScopeSecret":null},"AddHeadersToRequest":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0},"ReRouteIsCaseSensitive":false,"ServiceName":null,"DownstreamScheme":"http","DownstreamHost":"localhost","DownstreamPort":41879,"QoSOptions":{"ExceptionsAllowedBeforeBreaking":0,"DurationOfBreak":0,"TimeoutValue":0},"LoadBalancer":null,"RateLimitOptions":{"ClientWhitelist":[],"EnableRateLimiting":false,"Period":null,"PeriodTimespan":0,"Limit":0}}],"GlobalConfiguration":{"RequestIdKey":null,"ServiceDiscoveryProvider":{"Provider":null,"Host":null,"Port":0},"RateLimitOptions":{"ClientIdHeader":"ClientId","QuotaExceededMessage":null,"RateLimitCounterPrefix":"ocelot","DisableRateLimitHeaders":false,"HttpStatusCode":429}}}
{"ReRoutes":[{"DownstreamPathTemplate":"41879/","UpstreamTemplate":"/","UpstreamHttpMethod":"Get","AuthenticationOptions":{"Provider":null,"ProviderRootUrl":null,"ScopeName":null,"RequireHttps":false,"AdditionalScopes":[],"ScopeSecret":null},"AddHeadersToRequest":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0},"ReRouteIsCaseSensitive":false,"ServiceName":null,"DownstreamScheme":"http","DownstreamHost":"localhost","DownstreamPort":41879,"QoSOptions":{"ExceptionsAllowedBeforeBreaking":0,"DurationOfBreak":0,"TimeoutValue":0},"LoadBalancer":null,"RateLimitOptions":{"ClientWhitelist":[],"EnableRateLimiting":false,"Period":null,"PeriodTimespan":0.0,"Limit":0}}],"GlobalConfiguration":{"RequestIdKey":null,"ServiceDiscoveryProvider":{"Provider":null,"Host":null,"Port":0},"RateLimitOptions":{"ClientIdHeader":"ClientId","QuotaExceededMessage":null,"RateLimitCounterPrefix":"ocelot","DisableRateLimitHeaders":false,"HttpStatusCode":429}}}

View File

@ -23,7 +23,6 @@
"Microsoft.AspNetCore.Http": "1.1.0",
"Microsoft.DotNet.InternalAbstractions": "1.0.0",
"Ocelot": "0.0.0-dev",
"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Ocelot.ManualTest": "0.0.0-dev",
"Microsoft.AspNetCore.TestHost": "1.1.0",
@ -34,7 +33,8 @@
"Shouldly": "2.8.2",
"TestStack.BDDfy": "4.3.2",
"Consul": "0.7.2.1",
"Microsoft.Extensions.Caching.Memory": "1.1.0"
"Microsoft.Extensions.Caching.Memory": "1.1.0",
"xunit": "2.2.0-rc1-build3507"
},
"runtimes": {
"win10-x64": {},

View File

@ -37,7 +37,6 @@ namespace Ocelot.ManualTest
})
.WithDictionaryHandle();
};
services.AddMemoryCache();
services.AddOcelotOutputCaching(settings);
services.AddOcelotFileConfiguration(Configuration);
services.AddOcelot();

View File

@ -20,6 +20,7 @@ using Xunit;
using TestStack.BDDfy;
using Ocelot.Configuration.Builder;
using Shouldly;
using Ocelot.Configuration;
namespace Ocelot.UnitTests.RateLimit
{
@ -70,11 +71,13 @@ namespace Ocelot.UnitTests.RateLimit
{
var downstreamRoute = new DownstreamRoute(new List<Ocelot.DownstreamRouteFinder.UrlMatcher.UrlPathPlaceholderNameAndValue>(),
new ReRouteBuilder().WithEnableRateLimiting(true).WithRateLimitOptions(
new Ocelot.Configuration.RateLimitOptions(true, "ClientId", new List<string>(), false, "", "", new Ocelot.Configuration.RateLimitRule() { Limit = 3, Period = "1s", PeriodTimespan = TimeSpan.FromSeconds(100) },429))
new Ocelot.Configuration.RateLimitOptions(true, "ClientId", new List<string>(), false, "", "", new Ocelot.Configuration.RateLimitRule("1s", TimeSpan.FromSeconds(100), 3), 429))
.Build());
this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
.When(x => x.WhenICallTheMiddleware())
.When(x => x.WhenICallTheMiddlewareMultipleTime(2))
.Then(x => x.ThenresponseStatusCodeIs200())
.When(x => x.WhenICallTheMiddlewareMultipleTime(2))
.Then(x => x.ThenresponseStatusCodeIs429())
.BDDfy();
}
@ -84,7 +87,7 @@ namespace Ocelot.UnitTests.RateLimit
{
var downstreamRoute = new DownstreamRoute(new List<Ocelot.DownstreamRouteFinder.UrlMatcher.UrlPathPlaceholderNameAndValue>(),
new ReRouteBuilder().WithEnableRateLimiting(true).WithRateLimitOptions(
new Ocelot.Configuration.RateLimitOptions(true, "ClientId", new List<string>() { "ocelotclient2" }, false, "", "", new Ocelot.Configuration.RateLimitRule() { Limit = 3, Period = "1s", PeriodTimespan = TimeSpan.FromSeconds(100) },429))
new Ocelot.Configuration.RateLimitOptions(true, "ClientId", new List<string>() { "ocelotclient2" }, false, "", "", new RateLimitRule( "1s", TimeSpan.FromSeconds(100),3),429))
.Build());
this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
@ -102,11 +105,11 @@ namespace Ocelot.UnitTests.RateLimit
.Returns(_downstreamRoute);
}
private void WhenICallTheMiddleware()
private void WhenICallTheMiddlewareMultipleTime(int times)
{
var clientId = "ocelotclient1";
// Act
for (int i = 0; i <10; i++)
for (int i = 0; i < times; i++)
{
var request = new HttpRequestMessage(new HttpMethod("GET"), _url);
request.Headers.Add("ClientId", clientId);

View File

@ -14,7 +14,6 @@
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
"Microsoft.AspNetCore.Http": "1.1.0",
"Ocelot": "0.0.0-dev",
"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Moq": "4.6.38-alpha",
"Microsoft.AspNetCore.TestHost": "1.1.0",
@ -24,7 +23,8 @@
"Shouldly": "2.8.2",
"TestStack.BDDfy": "4.3.2",
"Microsoft.AspNetCore.Authentication.OAuth": "1.1.0",
"Microsoft.DotNet.InternalAbstractions": "1.0.0"
"Microsoft.DotNet.InternalAbstractions": "1.0.0",
"xunit": "2.2.0-rc1-build3507"
},
"runtimes": {
"win10-x64": {},