Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
satano committed Dec 3, 2023
1 parent bb579b1 commit eeaff6c
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
</ItemGroup>
<ItemGroup>
<None Remove="Resources\AggregatesOpenApiResource.json" />
<None Remove="Resources\DifferentOcelotRoutesForOneDownstream.json" />
<None Remove="Resources\DifferentOcelotRoutesForOneDownstreamTransformed.json" />
<None Remove="Tests\BasicConfigurationWithSchemaInHostOverride.json" />
<None Remove="Tests\Issue_128.json" />
<None Remove="Tests\Issue_135.json" />
Expand All @@ -20,6 +22,8 @@
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\AggregatesOpenApiResource.json" />
<EmbeddedResource Include="Resources\DifferentOcelotRoutesForOneDownstream.json" />
<EmbeddedResource Include="Resources\DifferentOcelotRoutesForOneDownstreamTransformed.json" />
<EmbeddedResource Include="Resources\OpenApiWithVersionPlaceholderBase.json" />
<EmbeddedResource Include="Resources\OpenApiWithVersionPlaceholderBaseTransformed.json" />
<EmbeddedResource Include="Resources\OpenApiBase.json" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"openapi": "3.0.1",
"info": {
"title": "WebApplication1",
"version": "1.0"
},
"paths": {
"/api/test": {
"get": {
"tags": [
"WebApplication1"
],
"operationId": "testGet",
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
},
"post": {
"tags": [
"WebApplication1"
],
"operationId": "testPost",
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"openapi": "3.0.1",
"info": {
"title": "WebApplication1",
"version": "1.0"
},
"paths": {
"/all/ocelot": {
"get": {
"tags": [
"WebApplication1"
],
"operationId": "testGet",
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
},
"post": {
"tags": [
"WebApplication1"
],
"operationId": "testPost",
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/post/ocelot": {
"post": {
"tags": [
"WebApplication1"
],
"operationId": "testPost",
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,87 @@ namespace MMLib.SwaggerForOcelot.Tests
{
public class SwaggerForOcelotMiddlewareShould
{
[Fact]
public async Task TransformDifferentOcelotRoutesForOneDownstreamPath()
{
// Arrange
const string version = "v1";
const string key = "test";
HttpContext httpContext = GetHttpContext(requestPath: $"/{version}/{key}");
IMemoryCache memoryCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));

var next = new TestRequestDelegate();

// What is being tested
var swaggerForOcelotOptions = new SwaggerForOcelotUIOptions();
TestSwaggerEndpointOptions swaggerEndpointOptions = CreateSwaggerEndpointOptions(key, version);
var routeOptions = new TestRouteOptions(new List<RouteOptions>
{
new ()
{
SwaggerKey = "test",
UpstreamPathTemplate = "/post/ocelot",
UpstreamHttpMethod = [ "Post" ],
DownstreamPathTemplate = "/api/test",
},
new()
{
SwaggerKey = "test",
UpstreamPathTemplate = "/all/ocelot",
DownstreamPathTemplate = "/api/test",
}
});

// downstreamSwagger is returned when client.GetStringAsync is called by the middleware.
string downstreamSwagger = await GetBaseOpenApi("DifferentOcelotRoutesForOneDownstream");
HttpClient httClientMock = GetHttpClient(downstreamSwagger);
var httpClientFactory = new TestHttpClientFactory(httClientMock);

// upstreamSwagger is returned after swaggerJsonTransformer transforms the downstreamSwagger
string expectedSwagger = await GetBaseOpenApi("DifferentOcelotRoutesForOneDownstreamTransformed");

var swaggerJsonTransformerMock = new Mock<ISwaggerJsonTransformer>();
swaggerJsonTransformerMock
.Setup(x => x.Transform(
It.IsAny<string>(),
It.IsAny<IEnumerable<RouteOptions>>(),
It.IsAny<string>(),
It.IsAny<SwaggerEndPointOptions>()))
.Returns((
string swaggerJson,
IEnumerable<RouteOptions> routeOptions,
string serverOverride,
SwaggerEndPointOptions options) => new SwaggerJsonTransformer(OcelotSwaggerGenOptions.Default, memoryCache)
.Transform(swaggerJson, routeOptions, serverOverride, options));
var swaggerForOcelotMiddleware = new SwaggerForOcelotMiddleware(
next.Invoke,
swaggerForOcelotOptions,
routeOptions,
swaggerJsonTransformerMock.Object,
Substitute.For<ISwaggerProvider>());

// Act
await swaggerForOcelotMiddleware.Invoke(
httpContext,
new SwaggerEndPointProvider(swaggerEndpointOptions, OcelotSwaggerGenOptions.Default),
new DownstreamSwaggerDocsRepository(Options.Create(swaggerForOcelotOptions),
httpClientFactory, DummySwaggerServiceDiscoveryProvider.Default));
httpContext.Response.Body.Seek(0, SeekOrigin.Begin);

// Assert
using (var streamReader = new StreamReader(httpContext.Response.Body))
{
string transformedUpstreamSwagger = await streamReader.ReadToEndAsync();
AreEqual(transformedUpstreamSwagger, expectedSwagger);
}

swaggerJsonTransformerMock.Verify(x => x.Transform(
It.IsAny<string>(),
It.IsAny<IEnumerable<RouteOptions>>(),
It.IsAny<string>(),
It.IsAny<SwaggerEndPointOptions>()), Times.Once);
}

[Fact]
public async Task AllowVersionPlaceholder()
{
Expand Down

0 comments on commit eeaff6c

Please sign in to comment.