mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 06:38:14 +08:00
tests showing how this could work (#241)
* tests showing how this could work * test passing * test needs work * skip test as doesnt really do anything
This commit is contained in:
@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Middleware;
|
||||
using Shouldly;
|
||||
@ -61,7 +63,7 @@ namespace Ocelot.AcceptanceTests
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200, ""))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
@ -104,7 +106,7 @@ namespace Ocelot.AcceptanceTests
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200, ""))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
@ -147,7 +149,7 @@ namespace Ocelot.AcceptanceTests
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200, ""))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
@ -190,7 +192,7 @@ namespace Ocelot.AcceptanceTests
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200, ""))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
@ -233,7 +235,7 @@ namespace Ocelot.AcceptanceTests
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200, ""))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
@ -276,7 +278,7 @@ namespace Ocelot.AcceptanceTests
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200, ""))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
@ -285,12 +287,59 @@ namespace Ocelot.AcceptanceTests
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact(Skip = "This is just an example to show how you could hook into Ocelot pipeline with your own middleware. At the moment you must use Response.OnCompleted callback and cannot change the response :( I will see if this can be changed one day!")]
|
||||
public void should_fix_issue_237()
|
||||
{
|
||||
Func<object, Task> callback = state =>
|
||||
{
|
||||
var httpContext = (HttpContext)state;
|
||||
|
||||
if (httpContext.Response.StatusCode > 400)
|
||||
{
|
||||
Debug.WriteLine("COUNT CALLED");
|
||||
Console.WriteLine("COUNT CALLED");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var fileConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/west",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 41880,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41880", 200, "/test"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunningWithMiddleareBeforePipeline<FakeMiddleware>(callback))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheCounterIs(int expected)
|
||||
{
|
||||
_counter.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode)
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string basePath)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
@ -300,9 +349,19 @@ namespace Ocelot.AcceptanceTests
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
|
||||
if(string.IsNullOrEmpty(basePath))
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
}
|
||||
else if(context.Request.Path.Value != basePath)
|
||||
{
|
||||
context.Response.StatusCode = 404;;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
})
|
||||
@ -316,5 +375,24 @@ namespace Ocelot.AcceptanceTests
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
|
||||
public class FakeMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly Func<object, Task> _callback;
|
||||
|
||||
public FakeMiddleware(RequestDelegate next, Func<object, Task> callback)
|
||||
{
|
||||
_next = next;
|
||||
_callback = callback;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
await _next(context);
|
||||
|
||||
context.Response.OnCompleted(_callback, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user