示例#1
0
        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 routeOptions = new TestRouteOptions();

            // 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,
                routeOptions,
                swaggerJsonTransformer,
                Substitute.For <ISwaggerProvider>());

            // Act
            await swaggerForOcelotMiddleware.Invoke(
                httpContext,
                new SwaggerEndPointProvider(testSwaggerEndpointOptions, OcelotSwaggerGenOptions.Default),
                new DownstreamSwaggerDocsRepository(Microsoft.Extensions.Options.Options.Create(swaggerForOcelotOptions),
                                                    httpClientFactory, 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 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);

            httpContext.Response.Body.Seek(0, SeekOrigin.Begin);
            string transformedUpstreamSwagger = await new StreamReader(httpContext.Response.Body).ReadToEndAsync();

            // Assert
            AreEqual(transformedUpstreamSwagger, upstreamSwagger);
        }
        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");
        }