mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-26 22:02:50 +08:00

* started messing around with this on the train last night * mega hacking away to change middleware into Ocelot iddleware * scoped data repo back in * broken commit getting tests working * another broken commit farting around with tests * all unit tests passing again * mw pipeline for ocelot...still loads of hacks but getting there now to get acceptance tests working, then fix config so you can have aggregate and then imlement multiplexer, then mapping to response...loads to do * all tests passing before aggregation feature implemented * removed all the request middleware stuff we dont need it * updated how errors work...tho i think there could be edge case here when aggregating because one downstream could error and this would effect another * removed multiplexer so you dont have to send route down, this isnt very thread safe...sigh * hacking around getting the config for aggregates in, this might change * refactored builder and unit tests passing now * Updated a bunch of ports for tests * plugged in code to create reroutes that are aggregates * made multiplexer a class * hacked test to death * simple aggregator done, initial validation done * removed request id from context, it is still specific for http request * now aggregates to json always * docs for aggregate reroutes * Updated docs
179 lines
6.9 KiB
C#
179 lines
6.9 KiB
C#
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Ocelot.Configuration.File;
|
|
using Shouldly;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using TestStack.BDDfy;
|
|
using Xunit;
|
|
|
|
namespace Ocelot.AcceptanceTests
|
|
{
|
|
public class ClientRateLimitTests : IDisposable
|
|
{
|
|
private IWebHost _builder;
|
|
private readonly Steps _steps;
|
|
private int _counterOne;
|
|
|
|
|
|
public ClientRateLimitTests()
|
|
{
|
|
_steps = new Steps();
|
|
}
|
|
|
|
|
|
public void Dispose()
|
|
{
|
|
_builder?.Dispose();
|
|
_steps.Dispose();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_call_withratelimiting()
|
|
{
|
|
var configuration = new FileConfiguration
|
|
{
|
|
ReRoutes = new List<FileReRoute>
|
|
{
|
|
new FileReRoute
|
|
{
|
|
DownstreamPathTemplate = "/api/ClientRateLimit",
|
|
DownstreamHostAndPorts = new List<FileHostAndPort>
|
|
{
|
|
new FileHostAndPort
|
|
{
|
|
Host = "localhost",
|
|
Port = 51876,
|
|
}
|
|
},
|
|
DownstreamScheme = "http",
|
|
UpstreamPathTemplate = "/api/ClientRateLimit",
|
|
UpstreamHttpMethod = new List<string> { "Get" },
|
|
RequestIdKey = _steps.RequestIdKey,
|
|
|
|
RateLimitOptions = new FileRateLimitRule()
|
|
{
|
|
EnableRateLimiting = true,
|
|
ClientWhitelist = new List<string>(),
|
|
Limit = 3,
|
|
Period = "1s",
|
|
PeriodTimespan = 1000
|
|
}
|
|
}
|
|
},
|
|
GlobalConfiguration = new FileGlobalConfiguration()
|
|
{
|
|
RateLimitOptions = new FileRateLimitOptions()
|
|
{
|
|
ClientIdHeader = "ClientId",
|
|
DisableRateLimitHeaders = false,
|
|
QuotaExceededMessage = "",
|
|
RateLimitCounterPrefix = "",
|
|
HttpStatusCode = 428
|
|
|
|
},
|
|
RequestIdKey ="oceclientrequest"
|
|
}
|
|
};
|
|
|
|
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", "/api/ClientRateLimit"))
|
|
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
|
.And(x => _steps.GivenOcelotIsRunning())
|
|
.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();
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void should_call_middleware_withWhitelistClient()
|
|
{
|
|
var configuration = new FileConfiguration
|
|
{
|
|
ReRoutes = new List<FileReRoute>
|
|
{
|
|
new FileReRoute
|
|
{
|
|
DownstreamPathTemplate = "/api/ClientRateLimit",
|
|
DownstreamHostAndPorts = new List<FileHostAndPort>
|
|
{
|
|
new FileHostAndPort
|
|
{
|
|
Host = "localhost",
|
|
Port = 51876,
|
|
}
|
|
},
|
|
DownstreamScheme = "http",
|
|
UpstreamPathTemplate = "/api/ClientRateLimit",
|
|
UpstreamHttpMethod = new List<string> { "Get" },
|
|
RequestIdKey = _steps.RequestIdKey,
|
|
|
|
RateLimitOptions = new FileRateLimitRule()
|
|
{
|
|
EnableRateLimiting = true,
|
|
ClientWhitelist = new List<string>() { "ocelotclient1"},
|
|
Limit = 3,
|
|
Period = "1s",
|
|
PeriodTimespan = 100
|
|
}
|
|
}
|
|
},
|
|
GlobalConfiguration = new FileGlobalConfiguration()
|
|
{
|
|
RateLimitOptions = new FileRateLimitOptions()
|
|
{
|
|
ClientIdHeader = "ClientId",
|
|
DisableRateLimitHeaders = false,
|
|
QuotaExceededMessage = "",
|
|
RateLimitCounterPrefix = ""
|
|
},
|
|
RequestIdKey = "oceclientrequest"
|
|
}
|
|
};
|
|
|
|
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", "/api/ClientRateLimit"))
|
|
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
|
.And(x => _steps.GivenOcelotIsRunning())
|
|
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit", 4))
|
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(200))
|
|
.BDDfy();
|
|
}
|
|
|
|
|
|
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath)
|
|
{
|
|
_builder = new WebHostBuilder()
|
|
.UseUrls(baseUrl)
|
|
.UseKestrel()
|
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
.UseIISIntegration()
|
|
.UseUrls(baseUrl)
|
|
.Configure(app =>
|
|
{
|
|
app.UsePathBase(basePath);
|
|
app.Run(context =>
|
|
{
|
|
_counterOne++;
|
|
context.Response.StatusCode = 200;
|
|
context.Response.WriteAsync(_counterOne.ToString());
|
|
return Task.CompletedTask;
|
|
});
|
|
})
|
|
.Build();
|
|
|
|
_builder.Start();
|
|
}
|
|
|
|
|
|
}
|
|
}
|