Ejemplo n.º 1
0
    public void LogsInformationWhenKestrelAddressesAreOverridden()
    {
        var logger = new TestApplicationErrorLogger();

        var overriddenAddress = "http://localhost:5000";
        var addresses         = new ServerAddressesFeature();

        addresses.InternalCollection.Add(overriddenAddress);

        var options = new KestrelServerOptions();

        options.ListenAnyIP(8080);

        var addressBindContext = TestContextFactory.CreateAddressBindContext(
            addresses,
            options,
            logger,
            endpoint => Task.CompletedTask);

        addressBindContext.ServerAddressesFeature.PreferHostingUrls = true;

        var bindTask = AddressBinder.BindAsync(options.ListenOptions, addressBindContext, CancellationToken.None);

        Assert.True(bindTask.IsCompletedSuccessfully);

        var log = Assert.Single(logger.Messages);

        Assert.Equal(LogLevel.Information, log.LogLevel);
        Assert.Equal(CoreStrings.FormatOverridingWithPreferHostingUrls(nameof(addressBindContext.ServerAddressesFeature.PreferHostingUrls), overriddenAddress), log.Message);
    }
Ejemplo n.º 2
0
    public async Task DefaultAddressBinderBindsToHttpPort5000()
    {
        var logger    = new MockLogger();
        var addresses = new ServerAddressesFeature();
        var services  = new ServiceCollection();

        services.AddLogging();
        var options = new KestrelServerOptions()
        {
            ApplicationServices = services.BuildServiceProvider()
        };

        var endpoints = new List <ListenOptions>();

        var addressBindContext = TestContextFactory.CreateAddressBindContext(
            addresses,
            options,
            logger,
            listenOptions =>
        {
            endpoints.Add(listenOptions);
            return(Task.CompletedTask);
        });

        await AddressBinder.BindAsync(options.ListenOptions, addressBindContext, CancellationToken.None);

        Assert.Contains(endpoints, e => e.IPEndPoint.Port == 5000 && !e.IsTls);
    }
Ejemplo n.º 3
0
    public async Task WrapsAddressInUseExceptionAsIOException()
    {
        var addresses = new ServerAddressesFeature();

        addresses.InternalCollection.Add("http://localhost:5000");
        var options = new KestrelServerOptions();

        var addressBindContext = TestContextFactory.CreateAddressBindContext(
            addresses,
            options,
            NullLogger.Instance,
            endpoint => throw new AddressInUseException("already in use"));

        await Assert.ThrowsAsync <IOException>(() =>
                                               AddressBinder.BindAsync(options.ListenOptions, addressBindContext, CancellationToken.None));
    }
Ejemplo n.º 4
0
    public async Task DefaultAddressBinderWithoutDevCertButHttpsConfiguredBindsToHttpsPorts()
    {
        var x509Certificate2 = TestResources.GetTestCertificate();
        var logger           = new MockLogger();
        var addresses        = new ServerAddressesFeature();
        var services         = new ServiceCollection();

        services.AddLogging();
        var options = new KestrelServerOptions()
        {
            // This stops the dev cert from being loaded
            IsDevCertLoaded     = true,
            ApplicationServices = services.BuildServiceProvider()
        };

        options.ConfigureEndpointDefaults(e =>
        {
            if (e.IPEndPoint.Port == 5001)
            {
                e.UseHttps(new HttpsConnectionAdapterOptions {
                    ServerCertificate = x509Certificate2
                });
            }
        });

        var endpoints = new List <ListenOptions>();

        var addressBindContext = TestContextFactory.CreateAddressBindContext(
            addresses,
            options,
            logger,
            listenOptions =>
        {
            endpoints.Add(listenOptions);
            return(Task.CompletedTask);
        });

        await AddressBinder.BindAsync(options.ListenOptions, addressBindContext, CancellationToken.None);

        Assert.Contains(endpoints, e => e.IPEndPoint.Port == 5000 && !e.IsTls);
        Assert.Contains(endpoints, e => e.IPEndPoint.Port == 5001 && e.IsTls);
    }
Ejemplo n.º 5
0
    public async Task FlowsCancellationTokenToCreateBinddingCallback()
    {
        var addresses = new ServerAddressesFeature();

        addresses.InternalCollection.Add("http://localhost:5000");
        var options = new KestrelServerOptions();

        var addressBindContext = TestContextFactory.CreateAddressBindContext(
            addresses,
            options,
            NullLogger.Instance,
            (endpoint, cancellationToken) =>
        {
            cancellationToken.ThrowIfCancellationRequested();
            return(Task.CompletedTask);
        });

        await Assert.ThrowsAsync <OperationCanceledException>(() =>
                                                              AddressBinder.BindAsync(options.ListenOptions, addressBindContext, new CancellationToken(true)));
    }
Ejemplo n.º 6
0
    public async Task FallbackToIPv4WhenIPv6AnyBindFails(string address)
    {
        var logger    = new MockLogger();
        var addresses = new ServerAddressesFeature();

        addresses.InternalCollection.Add(address);
        var options = new KestrelServerOptions();

        var ipV6Attempt = false;
        var ipV4Attempt = false;

        var addressBindContext = TestContextFactory.CreateAddressBindContext(
            addresses,
            options,
            logger,
            endpoint =>
        {
            if (endpoint.IPEndPoint.Address == IPAddress.IPv6Any)
            {
                ipV6Attempt = true;
                throw new InvalidOperationException("EAFNOSUPPORT");
            }

            if (endpoint.IPEndPoint.Address == IPAddress.Any)
            {
                ipV4Attempt = true;
            }

            return(Task.CompletedTask);
        });

        await AddressBinder.BindAsync(options.ListenOptions, addressBindContext, CancellationToken.None);

        Assert.True(ipV4Attempt, "Should have attempted to bind to IPAddress.Any");
        Assert.True(ipV6Attempt, "Should have attempted to bind to IPAddress.IPv6Any");
        Assert.Contains(logger.Messages, f => f.Equals(CoreStrings.FormatFallbackToIPv4Any(80)));
    }