public async Task AllowUserDefinedUpstreamTransformer() { // Arrange const string version = "v1"; const string key = "projects"; HttpContext httpContext = GetHttpContext(requestPath: $"/{version}/{key}"); var next = new TestRequestDelegate(); // What is being tested var swaggerForOcelotOptions = new SwaggerForOcelotUIOptions() { ReConfigureUpstreamSwaggerJson = ExampleUserDefinedUpstreamTransformer }; TestSwaggerEndpointOptions testSwaggerEndpointOptions = CreateSwaggerEndpointOptions(key, version); var rerouteOptions = new TestReRouteOptions(); // downstreamSwagger is returned when client.GetStringAsync is called by the middleware. string downstreamSwagger = await GetBaseOpenApi("OpenApiBase"); HttpClient httClientMock = GetHttpClient(downstreamSwagger); var httpClientFactory = new TestHttpClientFactory(httClientMock); // upstreamSwagger is returned after swaggerJsonTransformer transforms the downstreamSwagger string upstreamSwagger = await GetBaseOpenApi("OpenApiBaseTransformed"); var swaggerJsonTransformer = new TestSwaggerJsonTransformer(upstreamSwagger); var swaggerForOcelotMiddleware = new SwaggerForOcelotMiddleware( next.Invoke, swaggerForOcelotOptions, rerouteOptions, testSwaggerEndpointOptions, httpClientFactory, swaggerJsonTransformer); // Act await swaggerForOcelotMiddleware.Invoke(httpContext, DummySwaggerServiceDiscoveryProvider.Default); httpContext.Response.Body.Seek(0, SeekOrigin.Begin); string transformedUpstreamSwagger = await new StreamReader(httpContext.Response.Body).ReadToEndAsync(); // Assert AreEqual(transformedUpstreamSwagger, upstreamSwagger); }
public async Task AllowVersionPlaceholder() { // Arrange const string version = "v1"; const string key = "projects"; HttpContext httpContext = GetHttpContext(requestPath: $"/{version}/{key}"); var next = new TestRequestDelegate(); // What is being tested var swaggerForOcelotOptions = new SwaggerForOcelotUIOptions(); TestSwaggerEndpointOptions swaggerEndpointOptions = CreateSwaggerEndpointOptions(key, version); var rerouteOptions = new TestReRouteOptions(new List <ReRouteOptions> { new ReRouteOptions { SwaggerKey = "projects", UpstreamPathTemplate = "/api/{version}/projects/Values/{everything}", DownstreamPathTemplate = "/api/{version}/Values/{everything}", }, new ReRouteOptions { SwaggerKey = "projects", UpstreamPathTemplate = "/api/projects/Projects", DownstreamPathTemplate = "/api/Projects", }, new ReRouteOptions { SwaggerKey = "projects", UpstreamPathTemplate = "/api/projects/Projects/{everything}", DownstreamPathTemplate = "/api/Projects/{everything}", } }); // downstreamSwagger is returned when client.GetStringAsync is called by the middleware. string downstreamSwagger = await GetBaseOpenApi("OpenApiWithVersionPlaceholderBase"); HttpClient httClientMock = GetHttpClient(downstreamSwagger); var httpClientFactory = new TestHttpClientFactory(httClientMock); // upstreamSwagger is returned after swaggerJsonTransformer transforms the downstreamSwagger string expectedSwagger = await GetBaseOpenApi("OpenApiWithVersionPlaceholderBaseTransformed"); var swaggerJsonTransformerMock = new Mock <ISwaggerJsonTransformer>(); swaggerJsonTransformerMock .Setup(x => x.Transform( It.IsAny <string>(), It.IsAny <IEnumerable <ReRouteOptions> >(), It.IsAny <string>())) .Returns(( string swaggerJson, IEnumerable <ReRouteOptions> reRouteOptions, string serverOverride) => new SwaggerJsonTransformer() .Transform(swaggerJson, reRouteOptions, serverOverride)); var swaggerForOcelotMiddleware = new SwaggerForOcelotMiddleware( next.Invoke, swaggerForOcelotOptions, rerouteOptions, swaggerEndpointOptions, httpClientFactory, swaggerJsonTransformerMock.Object); // Act await swaggerForOcelotMiddleware.Invoke(httpContext, 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 <ReRouteOptions> >(), It.IsAny <string>()), Times.Once); }
public async Task AllowUserDefinedUpstreamTransformer() { // Arrange const string version = "v1"; const string key = "test"; var httpContext = GetHttpContext(requestPath: $"/{version}/{key}"); var next = new TestRequestDelegate(); // What is being tested var swaggerForOcelotOptions = new SwaggerForOcelotUIOptions() { ReConfigureUpstreamSwaggerJson = ExampleUserDefinedUpstreamTransformer }; var rerouteOptions = new TestReRouteOptions(); var swaggerEndpointOptions = new TestSwaggerEndpointOptions( new List <SwaggerEndPointOptions>() { new SwaggerEndPointOptions() { Key = key, Config = new List <SwaggerEndPointConfig>() { new SwaggerEndPointConfig() { Name = "Test Service", Version = version, Url = $"http://test.com/{version}/{key}/swagger.json" } } } }); // downstreamSwagger is returned when client.GetStringAsync is called by the middleware. var downstreamSwagger = await GetBaseOpenApi("OpenApiBase"); var httClientMock = GetHttpClient(downstreamSwagger); var httpClientFactory = new TestHttpClientFactory(httClientMock); // upstreamSwagger is returned after swaggerJsonTransformer transforms the downstreamSwagger var upstreamSwagger = await GetBaseOpenApi("OpenApiBaseTransformed"); var swaggerJsonTransformer = new TestSwaggerJsonTransformer(upstreamSwagger); var swaggerForOcelotMiddleware = new SwaggerForOcelotMiddleware( next.Invoke, swaggerForOcelotOptions, rerouteOptions, swaggerEndpointOptions, httpClientFactory, swaggerJsonTransformer); // Act await swaggerForOcelotMiddleware.Invoke(httpContext); httpContext.Response.Body.Seek(0, SeekOrigin.Begin); var transformedUpstreamSwagger = await new StreamReader(httpContext.Response.Body).ReadToEndAsync(); // Assert await AreEqual(transformedUpstreamSwagger, "OpenApiBaseTransformedReconfigured"); }