mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-20 08:02:50 +08:00
253 lines
11 KiB
C#
253 lines
11 KiB
C#
namespace Ocelot.AcceptanceTests
|
|
{
|
|
using Microsoft.AspNetCore.Http;
|
|
using Ocelot.Configuration.File;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using TestStack.BDDfy;
|
|
using Xunit;
|
|
|
|
public class CaseSensitiveRoutingTests : IDisposable
|
|
{
|
|
private readonly Steps _steps;
|
|
private readonly ServiceHandler _serviceHandler;
|
|
|
|
public CaseSensitiveRoutingTests()
|
|
{
|
|
_serviceHandler = new ServiceHandler();
|
|
_steps = new Steps();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_response_200_when_global_ignore_case_sensitivity_set()
|
|
{
|
|
int port = RandomPortFinder.GetRandomPort();
|
|
|
|
var configuration = new FileConfiguration
|
|
{
|
|
Routes = new List<FileRoute>
|
|
{
|
|
new FileRoute
|
|
{
|
|
DownstreamPathTemplate = "/api/products/{productId}",
|
|
DownstreamHostAndPorts = new List<FileHostAndPort>
|
|
{
|
|
new FileHostAndPort
|
|
{
|
|
Host = "localhost",
|
|
Port = port,
|
|
}
|
|
},
|
|
DownstreamScheme = "http",
|
|
UpstreamPathTemplate = "/products/{productId}",
|
|
UpstreamHttpMethod = new List<string> { "Get" },
|
|
}
|
|
}
|
|
};
|
|
|
|
this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/api/products/1", 200, "Some Product"))
|
|
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
|
.And(x => _steps.GivenOcelotIsRunning())
|
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_response_200_when_route_ignore_case_sensitivity_set()
|
|
{
|
|
int port = RandomPortFinder.GetRandomPort();
|
|
|
|
var configuration = new FileConfiguration
|
|
{
|
|
Routes = new List<FileRoute>
|
|
{
|
|
new FileRoute
|
|
{
|
|
DownstreamPathTemplate = "/api/products/{productId}",
|
|
DownstreamHostAndPorts = new List<FileHostAndPort>
|
|
{
|
|
new FileHostAndPort
|
|
{
|
|
Host = "localhost",
|
|
Port = port,
|
|
}
|
|
},
|
|
DownstreamScheme = "http",
|
|
UpstreamPathTemplate = "/products/{productId}",
|
|
UpstreamHttpMethod = new List<string> { "Get" },
|
|
RouteIsCaseSensitive = false,
|
|
}
|
|
}
|
|
};
|
|
|
|
this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/api/products/1", 200, "Some Product"))
|
|
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
|
.And(x => _steps.GivenOcelotIsRunning())
|
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_response_404_when_route_respect_case_sensitivity_set()
|
|
{
|
|
int port = RandomPortFinder.GetRandomPort();
|
|
|
|
var configuration = new FileConfiguration
|
|
{
|
|
Routes = new List<FileRoute>
|
|
{
|
|
new FileRoute
|
|
{
|
|
DownstreamPathTemplate = "/api/products/{productId}",
|
|
DownstreamHostAndPorts = new List<FileHostAndPort>
|
|
{
|
|
new FileHostAndPort
|
|
{
|
|
Host = "localhost",
|
|
Port = port,
|
|
}
|
|
},
|
|
DownstreamScheme = "http",
|
|
UpstreamPathTemplate = "/products/{productId}",
|
|
UpstreamHttpMethod = new List<string> { "Get" },
|
|
RouteIsCaseSensitive = true,
|
|
}
|
|
}
|
|
};
|
|
|
|
this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/api/products/1", 200, "Some Product"))
|
|
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
|
.And(x => _steps.GivenOcelotIsRunning())
|
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_response_200_when_route_respect_case_sensitivity_set()
|
|
{
|
|
int port = RandomPortFinder.GetRandomPort();
|
|
|
|
var configuration = new FileConfiguration
|
|
{
|
|
Routes = new List<FileRoute>
|
|
{
|
|
new FileRoute
|
|
{
|
|
DownstreamPathTemplate = "/api/products/{productId}",
|
|
DownstreamHostAndPorts = new List<FileHostAndPort>
|
|
{
|
|
new FileHostAndPort
|
|
{
|
|
Host = "localhost",
|
|
Port = port,
|
|
}
|
|
},
|
|
DownstreamScheme = "http",
|
|
UpstreamPathTemplate = "/PRODUCTS/{productId}",
|
|
UpstreamHttpMethod = new List<string> { "Get" },
|
|
RouteIsCaseSensitive = true,
|
|
}
|
|
}
|
|
};
|
|
|
|
this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/api/products/1", 200, "Some Product"))
|
|
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
|
.And(x => _steps.GivenOcelotIsRunning())
|
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_response_404_when_global_respect_case_sensitivity_set()
|
|
{
|
|
int port = RandomPortFinder.GetRandomPort();
|
|
|
|
var configuration = new FileConfiguration
|
|
{
|
|
Routes = new List<FileRoute>
|
|
{
|
|
new FileRoute
|
|
{
|
|
DownstreamPathTemplate = "/api/products/{productId}",
|
|
DownstreamHostAndPorts = new List<FileHostAndPort>
|
|
{
|
|
new FileHostAndPort
|
|
{
|
|
Host = "localhost",
|
|
Port = port,
|
|
}
|
|
},
|
|
DownstreamScheme = "http",
|
|
UpstreamPathTemplate = "/products/{productId}",
|
|
UpstreamHttpMethod = new List<string> { "Get" },
|
|
RouteIsCaseSensitive = true,
|
|
}
|
|
}
|
|
};
|
|
|
|
this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/api/products/1", 200, "Some Product"))
|
|
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
|
.And(x => _steps.GivenOcelotIsRunning())
|
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_response_200_when_global_respect_case_sensitivity_set()
|
|
{
|
|
int port = RandomPortFinder.GetRandomPort();
|
|
|
|
var configuration = new FileConfiguration
|
|
{
|
|
Routes = new List<FileRoute>
|
|
{
|
|
new FileRoute
|
|
{
|
|
DownstreamPathTemplate = "/api/products/{productId}",
|
|
DownstreamHostAndPorts = new List<FileHostAndPort>
|
|
{
|
|
new FileHostAndPort
|
|
{
|
|
Host = "localhost",
|
|
Port = port,
|
|
}
|
|
},
|
|
DownstreamScheme = "http",
|
|
UpstreamPathTemplate = "/PRODUCTS/{productId}",
|
|
UpstreamHttpMethod = new List<string> { "Get" },
|
|
RouteIsCaseSensitive = true,
|
|
}
|
|
}
|
|
};
|
|
|
|
this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/api/products/1", 200, "Some Product"))
|
|
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
|
.And(x => _steps.GivenOcelotIsRunning())
|
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
|
.BDDfy();
|
|
}
|
|
|
|
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
|
|
{
|
|
_serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
|
|
{
|
|
context.Response.StatusCode = statusCode;
|
|
await context.Response.WriteAsync(responseBody);
|
|
});
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_serviceHandler?.Dispose();
|
|
_steps.Dispose();
|
|
}
|
|
}
|
|
}
|