public void UseProxy_Action_ProxySet()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaWebSocket("uri")
            .UseProxy(
                options =>
            {
                options.Address       = "address";
                options.Username      = "******";
                options.Password      = "******";
                options.Domain        = "domain";
                options.BypassOnLocal = true;
                options.BypassList    = new[] { "local1", "local2" };
            });

            var config = builder.Build();

            config.ChannelOptions.As <MqttClientWebSocketOptions>().ProxyOptions.Should().NotBeNull();
            config.ChannelOptions.As <MqttClientWebSocketOptions>().ProxyOptions.Address.Should()
            .Be("address");
            config.ChannelOptions.As <MqttClientWebSocketOptions>().ProxyOptions.Username.Should().Be("user");
            config.ChannelOptions.As <MqttClientWebSocketOptions>().ProxyOptions.Password.Should().Be("pass");
            config.ChannelOptions.As <MqttClientWebSocketOptions>().ProxyOptions.Domain.Should().Be("domain");
            config.ChannelOptions.As <MqttClientWebSocketOptions>().ProxyOptions.BypassOnLocal.Should()
            .BeTrue();
            config.ChannelOptions.As <MqttClientWebSocketOptions>().ProxyOptions.BypassList.Should()
            .BeEquivalentTo("local1", "local2");
        }
        public void Constructor_WithWebSocketBaseConfig_InitializedFromBaseConfig()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .UseProtocolVersion(MqttProtocolVersion.V311)
            .ConnectViaWebSocket("uri")
            .UseProxy(
                options =>
            {
                options.Address       = "address";
                options.Username      = "******";
                options.Password      = "******";
                options.Domain        = "domain";
                options.BypassOnLocal = true;
                options.BypassList    = new[] { "local1", "local2" };
            })
            .EnableTls(
                parameters =>
            {
                parameters.SslProtocol = SslProtocols.Tls12;
                parameters.AllowUntrustedCertificates = true;
            })
            .UseExtendedAuthenticationExchangeHandler(new TestExtendedAuthenticationExchangeHandler());

            var baseConfig = builder.Build();
            var config     = new MqttClientConfigBuilder(baseConfig).Build();

            config.Should().BeEquivalentTo(baseConfig);
            config.Should().NotBeSameAs(baseConfig);
        }
        public void Default_ProtocolVersionV500Set()
        {
            var builder = new MqttClientConfigBuilder();

            builder.ConnectViaTcp("tests-server");

            var config = builder.Build();

            config.ProtocolVersion.Should().Be(MqttProtocolVersion.V500);
        }
        public void ConnectViaWebSocket_Uri_ChannelOptionsSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder.ConnectViaWebSocket("uri");

            var config = builder.Build();

            config.ChannelOptions.Should().BeOfType <MqttClientWebSocketOptions>();
            config.ChannelOptions.As <MqttClientWebSocketOptions>().Uri.Should().Be("uri");
        }
        public void UseProtocolVersion_ProtocolVersionSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .UseProtocolVersion(MqttProtocolVersion.V311);

            var config = builder.Build();

            config.ProtocolVersion.Should().Be(MqttProtocolVersion.V311);
        }
        public void DisableResponseInformation_RequestResponseInformationSetToFalse()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .DisableResponseInformation();

            var config = builder.Build();

            config.RequestResponseInformation.Should().BeFalse();
        }
        public void RequestProblemInformation_RequestProblemInformationSetToTrue()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .RequestProblemInformation();

            var config = builder.Build();

            config.RequestProblemInformation.Should().BeTrue();
        }
        public void LimitUnacknowledgedPublications_Int_ReceiveMaximumSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .LimitUnacknowledgedPublications(42);

            var config = builder.Build();

            config.ReceiveMaximum.Should().Be(42);
        }
        public void LimitPacketSize_Long_MaximumPacketSizeSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .LimitPacketSize(42);

            var config = builder.Build();

            config.MaximumPacketSize.Should().Be(42);
        }
        public void LimitTopicAlias_Int_TopicAliasMaximum()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .LimitTopicAlias(42);

            var config = builder.Build();

            config.TopicAliasMaximum.Should().Be(42);
        }
        public void WithSessionExpiration_TimeSpan_SessionExpiryIntervalSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .WithSessionExpiration(TimeSpan.FromSeconds(42));

            var config = builder.Build();

            config.SessionExpiryInterval.Should().Be(42);
        }
        public void DisableKeepAlive_KeepAliveIntervalSetToZero()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .DisableKeepAlive();

            var config = builder.Build();

            config.KeepAlivePeriod.Should().Be(TimeSpan.Zero);
        }
        public void SendKeepAlive_TimeSpan_KeepAliveIntervalSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .SendKeepAlive(TimeSpan.FromMinutes(42));

            var config = builder.Build();

            config.KeepAlivePeriod.Should().Be(TimeSpan.FromMinutes(42));
        }
        public void WithCommunicationTimeout_TimeSpan_TimeoutSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .WithCommunicationTimeout(TimeSpan.FromSeconds(42));

            var config = builder.Build();

            config.CommunicationTimeout.TotalSeconds.Should().Be(42);
        }
        public void RequestPersistentSession_CleanSessionSetToFalse()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .RequestPersistentSession();

            var config = builder.Build();

            config.CleanSession.Should().BeFalse();
        }
        public void DisableTls_UseTlsSetToFalse()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .DisableTls();

            var config = builder.Build();

            config.ChannelOptions !.TlsOptions.UseTls.Should().BeFalse();
        }
        public void ConnectViaTcp_ServerAndPort_ChannelOptionsSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder.ConnectViaTcp("tests-server", 1234);

            var config = builder.Build();

            config.ChannelOptions.Should().BeOfType <MqttClientTcpOptions>();
            config.ChannelOptions.As <MqttClientTcpOptions>().Server.Should().Be("tests-server");
            config.ChannelOptions.As <MqttClientTcpOptions>().Port.Should().Be(1234);
        }
        public void WithClientId_String_ClientIdSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .WithClientId("client-42");

            var config = builder.Build();

            config.ClientId.Should().Be("client-42");
        }
        public void WithAuthentication_Method_AuthenticationMethodAndDataSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .WithAuthentication("method", null);

            var config = builder.Build();

            config.AuthenticationMethod.Should().Be("method");
            config.AuthenticationData.Should().BeNull();
        }
        public void WithAuthentication_MethodAndData_AuthenticationMethodAndDataSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .WithAuthentication("method", new byte[] { 0x01, 0x02, 0x03 });

            var config = builder.Build();

            config.AuthenticationMethod.Should().Be("method");
            config.AuthenticationData.Should().BeEquivalentTo(new byte[] { 0x01, 0x02, 0x03 });
        }
        public void UseExtendedAuthenticationExchangeHandler_Instance_HandlerSet()
        {
            var instance = new TestExtendedAuthenticationExchangeHandler();
            var builder  = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .UseExtendedAuthenticationExchangeHandler(instance);

            var config = builder.Build();

            config.ExtendedAuthenticationExchangeHandler.Should().BeSameAs(instance);
        }
        public void WithCredentials_UserNameAndPasswordString_CredentialsSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .WithCredentials("user", "pass");

            var config = builder.Build();

            config.Credentials.Should().NotBeNull();
            config.Credentials !.Username.Should().Be("user");
            config.Credentials !.Password.Should().BeEquivalentTo(Encoding.UTF8.GetBytes("pass"));
        }
        public void AddUserProperty_NamesAndValues_UserPropertiesAdded()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .AddUserProperty("prop1", "value1")
            .AddUserProperty("prop2", "value2");

            var config = builder.Build();

            config.UserProperties.Should().BeEquivalentTo(
                new MqttUserProperty("prop1", "value1"),
                new MqttUserProperty("prop2", "value2"));
        }
        public void UseExtendedAuthenticationExchangeHandler_Type_HandlerSet()
        {
            var serviceProvider = Substitute.For <IServiceProvider>();

            serviceProvider.GetService(typeof(TestExtendedAuthenticationExchangeHandler))
            .Returns(new TestExtendedAuthenticationExchangeHandler());

            var builder = new MqttClientConfigBuilder(serviceProvider);

            builder
            .ConnectViaTcp("tests-server")
            .UseExtendedAuthenticationExchangeHandler(typeof(TestExtendedAuthenticationExchangeHandler));

            var config = builder.Build();

            config.ExtendedAuthenticationExchangeHandler.Should()
            .BeOfType <TestExtendedAuthenticationExchangeHandler>();
        }
        public void EnableTls_Action_TlsParametersSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .EnableTls(
                parameters =>
            {
                parameters.SslProtocol = SslProtocols.Tls12;
                parameters.AllowUntrustedCertificates = true;
            });

            var config = builder.Build();

            config.ChannelOptions !.TlsOptions.UseTls.Should().BeTrue();
            config.ChannelOptions.TlsOptions.SslProtocol.Should().Be(SslProtocols.Tls12);
            config.ChannelOptions.TlsOptions.AllowUntrustedCertificates.Should().BeTrue();
        }
        public void WithCredentials_ClientCredentials_CredentialsSet()
        {
            var passwordBytes = Encoding.UTF8.GetBytes("pass");
            var builder       = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .WithCredentials(
                new MqttClientCredentials
            {
                Username = "******",
                Password = passwordBytes
            });

            var config = builder.Build();

            config.Credentials.Should().NotBeNull();
            config.Credentials !.Username.Should().Be("user");
            config.Credentials !.Password.Should().BeEquivalentTo(passwordBytes);
        }
        public void Constructor_WithTcpBaseConfig_InitializedFromBaseConfig()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .UseProtocolVersion(MqttProtocolVersion.V311)
            .ConnectViaTcp("tests-server", 1234)
            .EnableTls(
                parameters =>
            {
                parameters.SslProtocol = SslProtocols.Tls12;
                parameters.AllowUntrustedCertificates = true;
            })
            .UseExtendedAuthenticationExchangeHandler(new TestExtendedAuthenticationExchangeHandler());

            var baseConfig = builder.Build();
            var config     = new MqttClientConfigBuilder(baseConfig).Build();

            config.Should().BeEquivalentTo(baseConfig);
            config.Should().NotBeSameAs(baseConfig);
        }
        public void SendLastWillMessage_Action_WillMessageAndDelaySet()
        {
            var builder = new MqttClientConfigBuilder();

            builder
            .ConnectViaTcp("tests-server")
            .SendLastWillMessage(
                lastWill => lastWill
                .Message(
                    new TestEventOne
            {
                Content = "I died!"
            })
                .WithDelay(TimeSpan.FromSeconds(42))
                .ProduceTo("testaments"));

            var config = builder.Build();

            config.WillMessage.Should().NotBeNull();
            config.WillMessage !.Topic.Should().Be("testaments");
            config.WillMessage.Payload.Should().NotBeNullOrEmpty();
            config.WillDelayInterval.Should().Be(42);
        }
        public void ConnectViaWebSocket_Action_ChannelOptionsSet()
        {
            var builder = new MqttClientConfigBuilder();

            builder.ConnectViaWebSocket(
                options =>
            {
                options.Uri            = "uri";
                options.RequestHeaders = new Dictionary <string, string>
                {
                    { "header", "value" }
                };
            });

            var config = builder.Build();

            config.ChannelOptions.Should().BeOfType <MqttClientWebSocketOptions>();
            config.ChannelOptions.As <MqttClientWebSocketOptions>().Uri.Should().Be("uri");
            config.ChannelOptions.As <MqttClientWebSocketOptions>().RequestHeaders.Should().BeEquivalentTo(
                new Dictionary <string, string>
            {
                { "header", "value" }
            });
        }