Ejemplo n.º 1
0
 private IHostBuilder CreateHostBuilder(RequestDelegate requestDelegate, HttpProtocols?protocol = null, Action <KestrelServerOptions> configureKestrel = null)
 {
     return(new HostBuilder()
            .ConfigureWebHost(webHostBuilder =>
     {
         webHostBuilder
         .UseKestrel(o =>
         {
             if (configureKestrel == null)
             {
                 o.Listen(IPAddress.Parse("127.0.0.1"), 0, listenOptions =>
                 {
                     listenOptions.Protocols = protocol ?? HttpProtocols.Http3;
                     listenOptions.UseHttps();
                 });
             }
             else
             {
                 configureKestrel(o);
             }
         })
         .Configure(app =>
         {
             app.Run(requestDelegate);
         });
     })
            .ConfigureServices(AddTestLogging));
 }
Ejemplo n.º 2
0
        public HttpClient CreateClient(HttpProtocols?httpProtocol = null, DelegatingHandler?messageHandler = null)
        {
            HttpClient client;

            if (messageHandler != null)
            {
                messageHandler.InnerHandler = new HttpClientHandler();
                client = new HttpClient(messageHandler);
            }
            else
            {
                client = new HttpClient();
            }

            switch (httpProtocol ?? HttpProtocols.Http2)
            {
            case HttpProtocols.Http1:
                client.BaseAddress = new Uri(_server.GetUrl(HttpProtocols.Http1));
                break;

            case HttpProtocols.Http2:
                client.DefaultRequestVersion = new Version(2, 0);
                client.BaseAddress           = new Uri(_server.GetUrl(HttpProtocols.Http2));
                break;

            default:
                throw new ArgumentException("Unexpected value.", nameof(httpProtocol));
            }

            return(client);
        }
Ejemplo n.º 3
0
        public static EndpointContext <TRequest, TResponse> CreateGrpcEndpoint <TRequest, TResponse>(
            int port,
            UnaryServerMethod <TRequest, TResponse> callHandler,
            string methodName,
            HttpProtocols?protocols = null,
            bool?isHttps            = null)
            where TRequest : class, IMessage, new()
            where TResponse : class, IMessage, new()
        {
            var server = CreateServer(port, protocols, isHttps);
            var method = server.DynamicGrpc.AddUnaryMethod(callHandler, methodName);
            var url    = server.GetUrl(isHttps.GetValueOrDefault(false) ? TestServerEndpointName.Http2WithTls : TestServerEndpointName.Http2);

            return(new EndpointContext <TRequest, TResponse>(server, method, url));
        }
Ejemplo n.º 4
0
        public static GrpcTestFixture <Startup> CreateServer(int port, HttpProtocols?protocols = null, bool?isHttps = null)
        {
            var endpointName = isHttps.GetValueOrDefault(false) ? TestServerEndpointName.Http2WithTls : TestServerEndpointName.Http2;

            return(new GrpcTestFixture <Startup>(
                       null,
                       (options, urls) =>
            {
                urls[endpointName] = isHttps.GetValueOrDefault(false)
                        ? $"https://127.0.0.1:{port}"
                        : $"http://127.0.0.1:{port}";
                options.ListenLocalhost(port, listenOptions =>
                {
                    listenOptions.Protocols = protocols ?? HttpProtocols.Http2;

                    if (isHttps.GetValueOrDefault(false))
                    {
                        var basePath = Path.GetDirectoryName(typeof(InProcessTestServer).Assembly.Location);
                        var certPath = Path.Combine(basePath !, "server1.pfx");
                        listenOptions.UseHttps(certPath, "1111");
                    }
                });
            },
Ejemplo n.º 5
0
 public static IHostBuilder CreateHostBuilder(Action <IServiceCollection> configureServices, RequestDelegate requestDelegate, HttpProtocols?protocol = null, Action <KestrelServerOptions> configureKestrel = null)
 {
     return(new HostBuilder()
            .ConfigureWebHost(webHostBuilder =>
     {
         webHostBuilder
         .UseKestrel(o =>
         {
             if (configureKestrel == null)
             {
                 o.Listen(IPAddress.Parse("127.0.0.1"), 0, listenOptions =>
                 {
                     listenOptions.Protocols = protocol ?? HttpProtocols.Http3;
                     listenOptions.UseHttps();
                 });
             }
             else
             {
                 configureKestrel(o);
             }
         })
         .Configure(app =>
         {
             app.Run(requestDelegate);
         });
     })
            .ConfigureServices(configureServices)
            .ConfigureHostOptions(o =>
     {
         if (Debugger.IsAttached)
         {
             // Avoid timeout while debugging.
             o.ShutdownTimeout = TimeSpan.FromHours(1);
         }
         else
         {
             o.ShutdownTimeout = TimeSpan.FromSeconds(1);
         }
     }));
 }
Ejemplo n.º 6
0
 private IHostBuilder CreateHostBuilder(RequestDelegate requestDelegate, HttpProtocols?protocol = null, Action <KestrelServerOptions> configureKestrel = null)
 {
     return(Http3Helpers.CreateHostBuilder(AddTestLogging, requestDelegate, protocol, configureKestrel));
 }