Exemple #1
0
        private HttpAgnosticLoopbackServer(HttpAgnosticOptions options)
        {
            _options      = options;
            _listenSocket = new Socket(_options.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            _listenSocket.Bind(new IPEndPoint(_options.Address, 0));
            _listenSocket.Listen(_options.ListenBacklog);

            var localEndPoint = (IPEndPoint)_listenSocket.LocalEndPoint;
            var host          = _options.Address.AddressFamily == AddressFamily.InterNetworkV6 ? $"[{localEndPoint.Address}]" : localEndPoint.Address.ToString();
            var scheme        = _options.UseSsl ? "https" : "http";

            _uri = new Uri($"{scheme}://{host}:{localEndPoint.Port}/");
        }
Exemple #2
0
        private static HttpAgnosticOptions CreateOptions(GenericLoopbackOptions options)
        {
            HttpAgnosticOptions httpOptions = new HttpAgnosticOptions();

            if (options != null)
            {
                httpOptions.Address       = options.Address;
                httpOptions.UseSsl        = options.UseSsl;
                httpOptions.SslProtocols  = options.SslProtocols;
                httpOptions.ListenBacklog = options.ListenBacklog;
            }
            return(httpOptions);
        }
Exemple #3
0
 public static HttpAgnosticLoopbackServer CreateServer(HttpAgnosticOptions options)
 {
     return(new HttpAgnosticLoopbackServer(options));
 }
Exemple #4
0
        public static async Task CreateClientAndServerAsync(Func <Uri, Task> clientFunc, Func <GenericLoopbackServer, Task> serverFunc, HttpAgnosticOptions httpOptions, int timeout = 60_000)
        {
            using (var server = HttpAgnosticLoopbackServer.CreateServer(httpOptions ?? new HttpAgnosticOptions()))
            {
                Task clientTask = clientFunc(server.Address);
                Task serverTask = serverFunc(server);

                await new Task[] { clientTask, serverTask }.WhenAllOrAnyFailed(timeout).ConfigureAwait(false);
            }
        }