示例#1
0
 public ProxyMiddlewareBase(RequestDelegate next, IOptions <TOptions> serverOptions, ILoggerFactory loggerFactory)
 {
     _pathKey = serverOptions.Value.PathKey;
     _apiUri  = serverOptions.Value.ToUri().AbsoluteUri;
     _proxy   = new ProxyMiddleware(next, serverOptions.Value.ToProxyOptions());
     _logger  = loggerFactory.CreateLogger <ApiProxyMiddleware>();
 }
示例#2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseStaticFiles();

            // For any requests coming in from '/dist/**/*' or '__webpack_hmr', we wan't to proxy those requests
            // to our webpack dev server that is running.
            // Make sure you have 'gulp dev-server' running before starting the .NET web stuff.
            // NOTE: You may want to configure this to only run on a dev environment, and not production.
            var proxyOptions = new OptionsWrapper <ProxyOptions>(new ProxyOptions
            {
                Host = "localhost",
                Port = "5001"
            });

            app.Use(async(context, next) =>
            {
                if (!context.Request.Path.StartsWithSegments("/dist") &&
                    !context.Request.Path.StartsWithSegments("/__webpack_hmr"))
                {
                    await next();
                    return;
                }
                var proxyMiddleware = new ProxyMiddleware(httpContext => next.Invoke(), proxyOptions);
                await proxyMiddleware.Invoke(context);
            });

            app.UseJsEngine(); // this needs to be before MVC

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
        public void UsesProxy()
        {
            const string ENDPOINT = "http://foobar.com/v1";

            // Arrange
            var next  = new Mock <RequestDelegate>();
            var proxy = new Mock <IProxy>();
            var log   = new Mock <ILogger <ProxyMiddleware> >();

            var config = new Mock <IConfig>();
            // config.SetupGet(x => x.Endpoint).Returns(ENDPOINT);

            var request  = new Mock <HttpRequest>();
            var response = new Mock <HttpResponse>();
            var context  = new Mock <HttpContext>();

            context.SetupGet(x => x.Request).Returns(request.Object);
            context.SetupGet(x => x.Response).Returns(response.Object);

            var target = new ProxyMiddleware(next.Object, config.Object, proxy.Object, log.Object);

            // Act
            target.Invoke(context.Object).Wait();

            // Assert
            proxy.Verify(x => x.ProcessAsync(request.Object, response.Object), Times.Once);
        }
示例#4
0
        public void SetUp()
        {
            _rules = new List <ProxyRule>();
            _fakeHttpMessageHandler = new FakeHttpMessageHandler();
            _request      = new HttpRequestFake(new Uri("http://myserver.com/api/user"));
            _context      = new HttpContextFake(_request);
            _proxyOptions = new ProxyOptions(_rules);
            _proxyOptions.BackChannelMessageHandler = _fakeHttpMessageHandler;

            var options = Options.Create(_proxyOptions);

            _proxy = new ProxyMiddleware(next => Task.FromResult(_request), options);
        }
示例#5
0
 /// <summary>
 /// Proxy Middleware Constructor
 /// </summary>
 /// <param name="next"></param>
 /// <param name="apiServerOptions"></param>
 /// <param name="loggerFactory"></param>
 public ApiProxyMiddleware(RequestDelegate next, IOptions <ApiProxyServerOptions> apiServerOptions, ILoggerFactory loggerFactory)
 {
     _apiUri = apiServerOptions.Value.ToUri().AbsoluteUri;
     _proxy  = new ProxyMiddleware(next, apiServerOptions.Value.ToProxyOptions());
     _logger = loggerFactory.CreateLogger <ApiProxyMiddleware>();
 }