public async Task Server_SetConnectionLimit_Success()
        {
            // This is just to get a dynamic port
            string address;

            using (Utilities.CreateHttpServer(out address, httpContext => Task.FromResult(0))) { }

            var server = Utilities.CreatePump();

            server.Listener.Options.UrlPrefixes.Add(AzureRelayUrlPrefix.Create(address, Utilities.CreateTokenProvider()));
            Assert.Null(server.Listener.Options.MaxConnections);
            server.Listener.Options.MaxConnections = 3;

            using (server)
            {
                await server.StartAsync(new DummyApplication(), CancellationToken.None);

                using (var client1 = await SendHungRequestAsync("GET", address))
                    using (var client2 = await SendHungRequestAsync("GET", address))
                    {
                        using (var client3 = await SendHungRequestAsync("GET", address))
                        {
                            // Maxed out, refuses connection and throws
                            await Assert.ThrowsAsync <HttpRequestException>(() => SendRequestAsync(address));
                        }

                        // A connection has been closed, try again.
                        string responseText = await SendRequestAsync(address);

                        Assert.Equal(string.Empty, responseText);
                    }
            }
        }
        public async Task Server_SetConnectionLimitInfinite_Success()
        {
            // This is just to get a dynamic port
            string address;

            using (Utilities.CreateHttpServer(out address, httpContext => Task.FromResult(0))) { }

            var server = Utilities.CreatePump();

            server.Listener.Options.UrlPrefixes.Add(AzureRelayUrlPrefix.Create(address, Utilities.CreateTokenProvider()));
            server.Listener.Options.MaxConnections = -1; // infinite

            using (server)
            {
                await server.StartAsync(new DummyApplication(), CancellationToken.None);

                using (var client1 = await SendHungRequestAsync("GET", address))
                    using (var client2 = await SendHungRequestAsync("GET", address))
                        using (var client3 = await SendHungRequestAsync("GET", address))
                        {
                            // Doesn't max out
                            string responseText = await SendRequestAsync(address);

                            Assert.Equal(string.Empty, responseText);
                        }
            }
        }
예제 #3
0
        internal static MessagePump CreateServer(string url, string path, RequestDelegate app, TokenProvider tp = null)
        {
            var server = CreatePump(tp);

            server.Features.Get <IServerAddressesFeature>().Addresses.Add(
                AzureRelayUrlPrefix.Create(new Uri(new Uri(url), path).AbsoluteUri, tp != null ? tp : Utilities.CreateTokenProvider()).ToString());
            server.StartAsync(new DummyApplication(app), CancellationToken.None).Wait();
            return(server);
        }
예제 #4
0
        public void OverridingIServerAdressesFeatureWithDirectConfiguration_WarnsOnStart(string serverAddress)
        {
            var overrideAddress = Utilities.GetRelayUrl();

            using (var server = Utilities.CreatePump())
            {
                var serverAddressesFeature = server.Features.Get <IServerAddressesFeature>();
                serverAddressesFeature.Addresses.Add(serverAddress);
                server.Listener.Options.UrlPrefixes.Add(AzureRelayUrlPrefix.Create(overrideAddress, Utilities.CreateTokenProvider()));

                server.StartAsync(new DummyApplication(), CancellationToken.None).Wait();

                Assert.Equal(overrideAddress, serverAddressesFeature.Addresses.Single());
            }
        }
예제 #5
0
        public void DoesNotOverrideDirectConfigurationWithIServerAddressesFeature_IfAddressesIsEmpty()
        {
            var serverAddress = Utilities.GetRelayUrl();

            using (var server = Utilities.CreatePump())
            {
                var serverAddressesFeature = server.Features.Get <IServerAddressesFeature>();
                serverAddressesFeature.PreferHostingUrls = true;
                server.Listener.Options.UrlPrefixes.Add(AzureRelayUrlPrefix.Create(serverAddress, Utilities.CreateTokenProvider()));

                server.StartAsync(new DummyApplication(), CancellationToken.None).Wait();

                Assert.Equal(serverAddress, serverAddressesFeature.Addresses.Single());
            }
        }
예제 #6
0
        internal static MessagePump CreateDynamicHttpServer(string basePath, out string root, out string baseAddress, Action <AzureRelayOptions> configureOptions, RequestDelegate app, TokenProvider tp = null)
        {
            var rootUri = new Uri(GetRelayUrl());
            var prefix  = AzureRelayUrlPrefix.Create(new Uri(rootUri, basePath).AbsoluteUri, tp);

            root        = rootUri.ToString();
            baseAddress = prefix.FullPrefix;

            var server = CreatePump(CreateTokenProvider());

            server.Features.Get <IServerAddressesFeature>().Addresses.Add(baseAddress);
            configureOptions(server.Listener.Options);
            server.StartAsync(new DummyApplication(app), CancellationToken.None).Wait();
            return(server);
        }
예제 #7
0
        private IServer CreateServer(out string root, RequestDelegate app)
        {
            // TODO: We're just doing this to get a dynamic port. This can be removed later when we add support for hot-adding prefixes.
            var dynamicServer = Utilities.CreateHttpServerReturnRoot("/", out root, app);

            dynamicServer.Dispose();
            var rootUri = new Uri(root);
            var server  = Utilities.CreatePump();

            foreach (string path in new[] { "/", "/11", "/2/3", "/2", "/11/2" })
            {
                server.Listener.Options.UrlPrefixes.Add(AzureRelayUrlPrefix.Create(new Uri(rootUri, path).AbsoluteUri, Utilities.CreateTokenProvider()));
            }

            server.StartAsync(new DummyApplication(app), CancellationToken.None).Wait();
            return(server);
        }
예제 #8
0
        internal static IWebHost CreateDynamicHost(string basePath, out string root, out string baseAddress, Action <AzureRelayOptions> configureOptions, RequestDelegate app, TokenProvider tp = null)
        {
            var prefix = AzureRelayUrlPrefix.Create(new Uri(new Uri(Utilities.GetRelayUrl()), basePath).AbsoluteUri, tp);

            root        = prefix.FullPrefix;
            baseAddress = prefix.ToString();

            var builder = new WebHostBuilder()
                          .UseAzureRelay(options =>
            {
                options.UrlPrefixes.Add(prefix);
                configureOptions(options);
            })
                          .Configure(appBuilder => appBuilder.Run(app));

            var host = builder.Build();

            host.Start();
            return(host);
        }
        public async Task Server_SetQueueLimit_Success()
        {
            // This is just to get a dynamic port
            string address;

            using (Utilities.CreateHttpServer(out address, httpContext => Task.FromResult(0))) { }

            var server = Utilities.CreatePump();

            server.Listener.Options.UrlPrefixes.Add(AzureRelayUrlPrefix.Create(address, Utilities.CreateTokenProvider()));
            server.Listener.Options.RequestQueueLimit = 1001;

            using (server)
            {
                await server.StartAsync(new DummyApplication(), CancellationToken.None);

                string response = await SendRequestAsync(address);

                Assert.Equal(string.Empty, response);
            }
        }
        public async Task Server_SetHttp503VebosittHittingThrottle_Success()
        {
            // This is just to get a dynamic port
            string address;

            using (Utilities.CreateHttpServer(out address, httpContext => Task.FromResult(0))) { }

            var server = Utilities.CreatePump();

            server.Listener.Options.UrlPrefixes.Add(AzureRelayUrlPrefix.Create(address, Utilities.CreateTokenProvider()));
            Assert.Null(server.Listener.Options.MaxConnections);
            server.Listener.Options.MaxConnections = 3;

            using (server)
            {
                await server.StartAsync(new DummyApplication(), CancellationToken.None);

                using (var client1 = await SendHungRequestAsync("GET", address))
                    using (var client2 = await SendHungRequestAsync("GET", address))
                    {
                        using (var client3 = await SendHungRequestAsync("GET", address))
                        {
                            using (HttpClient client4 = new HttpClient())
                            {
                                // Maxed out, refuses connection should return 503
                                HttpResponseMessage response = await client4.GetAsync(address);

                                Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode);
                            }
                        }

                        // A connection has been closed, try again.
                        string responseText = await SendRequestAsync(address);

                        Assert.Equal(string.Empty, responseText);
                    }
            }
        }
예제 #11
0
        public Task StartAsync <TContext>(IHttpApplication <TContext> application, CancellationToken cancellationToken)
        {
            if (application == null)
            {
                throw new ArgumentNullException(nameof(application));
            }

            var hostingUrlsPresent = _serverAddresses.Addresses.Count > 0;

            if (_serverAddresses.PreferHostingUrls && hostingUrlsPresent)
            {
                if (_options.UrlPrefixes.Count > 0)
                {
                    LogHelper.LogWarning(_logger, $"Overriding endpoints added to {nameof(AzureRelayOptions.UrlPrefixes)} since {nameof(IServerAddressesFeature.PreferHostingUrls)} is set to true." +
                                         $" Binding to address(es) '{string.Join(", ", _serverAddresses.Addresses)}' instead. ");

                    Listener.Options.UrlPrefixes.Clear();
                }

                foreach (var value in _serverAddresses.Addresses)
                {
                    Listener.Options.UrlPrefixes.Add(AzureRelayUrlPrefix.Create(value));
                }
            }
            else if (_options.UrlPrefixes.Count > 0)
            {
                if (hostingUrlsPresent)
                {
                    LogHelper.LogWarning(_logger, $"Overriding address(es) '{string.Join(", ", _serverAddresses.Addresses)}'. " +
                                         $"Binding to endpoints added to {nameof(AzureRelayOptions.UrlPrefixes)} instead.");

                    _serverAddresses.Addresses.Clear();
                }

                foreach (var prefix in _options.UrlPrefixes)
                {
                    _serverAddresses.Addresses.Add(prefix.FullPrefix);
                }
            }
            else if (hostingUrlsPresent)
            {
                foreach (var value in _serverAddresses.Addresses)
                {
                    Listener.Options.UrlPrefixes.Add(AzureRelayUrlPrefix.Create(value));
                }
            }
            else
            {
                LogHelper.LogDebug(_logger, $"No listening endpoints were configured.");
                throw new InvalidOperationException("No listening endpoints were configured.");
            }

            // Can't call Start twice
            Contract.Assert(_application == null);
            Contract.Assert(application != null);

            _application = new ApplicationWrapper <TContext>(application);

            Listener.Start();

            return(Task.CompletedTask);
        }