diff --git a/tests/MMLib.SwaggerForOcelot.Tests/MMLib.SwaggerForOcelot.Tests.csproj b/tests/MMLib.SwaggerForOcelot.Tests/MMLib.SwaggerForOcelot.Tests.csproj index 200316a..5cbb65d 100644 --- a/tests/MMLib.SwaggerForOcelot.Tests/MMLib.SwaggerForOcelot.Tests.csproj +++ b/tests/MMLib.SwaggerForOcelot.Tests/MMLib.SwaggerForOcelot.Tests.csproj @@ -10,6 +10,8 @@ + + @@ -20,6 +22,8 @@ + + diff --git a/tests/MMLib.SwaggerForOcelot.Tests/Resources/DifferentOcelotRoutesForOneDownstream.json b/tests/MMLib.SwaggerForOcelot.Tests/Resources/DifferentOcelotRoutesForOneDownstream.json new file mode 100644 index 0000000..61e3bb6 --- /dev/null +++ b/tests/MMLib.SwaggerForOcelot.Tests/Resources/DifferentOcelotRoutesForOneDownstream.json @@ -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": {} +} \ No newline at end of file diff --git a/tests/MMLib.SwaggerForOcelot.Tests/Resources/DifferentOcelotRoutesForOneDownstreamTransformed.json b/tests/MMLib.SwaggerForOcelot.Tests/Resources/DifferentOcelotRoutesForOneDownstreamTransformed.json new file mode 100644 index 0000000..8eda668 --- /dev/null +++ b/tests/MMLib.SwaggerForOcelot.Tests/Resources/DifferentOcelotRoutesForOneDownstreamTransformed.json @@ -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": {} +} \ No newline at end of file diff --git a/tests/MMLib.SwaggerForOcelot.Tests/SwaggerForOcelotMiddlewareShould.cs b/tests/MMLib.SwaggerForOcelot.Tests/SwaggerForOcelotMiddlewareShould.cs index bd112a6..3b45240 100644 --- a/tests/MMLib.SwaggerForOcelot.Tests/SwaggerForOcelotMiddlewareShould.cs +++ b/tests/MMLib.SwaggerForOcelot.Tests/SwaggerForOcelotMiddlewareShould.cs @@ -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 + { + new() + { + SwaggerKey = "test", + UpstreamPathTemplate = "/all/ocelot", + DownstreamPathTemplate = "/api/test", + }, + new () + { + SwaggerKey = "test", + UpstreamPathTemplate = "/post/ocelot", + UpstreamHttpMethod = [ "Post" ], + 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(); + swaggerJsonTransformerMock + .Setup(x => x.Transform( + It.IsAny(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(( + string swaggerJson, + IEnumerable 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()); + + // 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(), + It.IsAny>(), + It.IsAny(), + It.IsAny()), Times.Once); + } + [Fact] public async Task AllowVersionPlaceholder() {