diff --git a/Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Connection_Tests.cs b/Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Connection_Tests.cs index 955a6b3df..d05d42b0c 100644 --- a/Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Connection_Tests.cs +++ b/Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Connection_Tests.cs @@ -220,7 +220,7 @@ public async Task Return_Non_Success() var client = testEnvironment.CreateClient(); - var response = await client.ConnectAsync(testEnvironment.CreateDefaultClientOptionsBuilder().WithoutThrowOnNonSuccessfulConnectResponse().Build()); + var response = await client.ConnectAsync(testEnvironment.CreateDefaultClientOptionsBuilder().Build()); Assert.IsNotNull(response); Assert.AreEqual(MqttClientConnectResultCode.QuotaExceeded, response.ResultCode); diff --git a/Source/MQTTnet.Tests/Server/General.cs b/Source/MQTTnet.Tests/Server/General.cs index eccb324d4..0cf7c43f6 100644 --- a/Source/MQTTnet.Tests/Server/General.cs +++ b/Source/MQTTnet.Tests/Server/General.cs @@ -102,8 +102,10 @@ public async Task Deny_Connection() return CompletedTask.Instance; }; - var connectingFailedException = await Assert.ThrowsExceptionAsync(() => testEnvironment.ConnectClient()); - Assert.AreEqual(MqttClientConnectResultCode.NotAuthorized, connectingFailedException.ResultCode); + var client = testEnvironment.CreateClient(); + var response = await client.ConnectAsync(testEnvironment.CreateDefaultClientOptions()); + + Assert.AreEqual(MqttClientConnectResultCode.NotAuthorized, response.ResultCode); } } diff --git a/Source/MQTTnet.Tests/Server/Security_Tests.cs b/Source/MQTTnet.Tests/Server/Security_Tests.cs index a319c0e14..725199b96 100644 --- a/Source/MQTTnet.Tests/Server/Security_Tests.cs +++ b/Source/MQTTnet.Tests/Server/Security_Tests.cs @@ -22,7 +22,7 @@ public async Task Do_Not_Affect_Authorized_Clients() using (var testEnvironment = CreateTestEnvironment()) { testEnvironment.IgnoreClientLogErrors = true; - + await testEnvironment.StartServer(); var publishedApplicationMessages = new List(); @@ -81,15 +81,15 @@ await invalidClient.ConnectAsync( } await LongTestDelay(); - + await validClient.PublishStringAsync("HELLO 2"); - + await LongTestDelay(); - + await validClient.PublishStringAsync("HELLO 3"); - + await LongTestDelay(); - + Assert.AreEqual(3, publishedApplicationMessages.Count); Assert.AreEqual(1, testEnvironment.Server.GetClientsAsync().GetAwaiter().GetResult().Count); } @@ -133,7 +133,6 @@ public async Task Use_Username_Null_Password_Empty() var ex = await Assert.ThrowsExceptionAsync(async () => await client.ConnectAsync(clientOptions)); Assert.IsInstanceOfType(ex.InnerException, typeof(MqttProtocolViolationException)); Assert.AreEqual("Error while authenticating. If the User Name Flag is set to 0, the Password Flag MUST be set to 0 [MQTT-3.1.2-22].", ex.Message, false); - Assert.AreEqual(MqttClientConnectResultCode.UnspecifiedError, ex.ResultCode); } } @@ -164,8 +163,8 @@ async Task TestCredentials(string userName, string password) var clientOptions = new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).WithCredentials(userName, password).Build(); - var ex = await Assert.ThrowsExceptionAsync(() => client.ConnectAsync(clientOptions)); - Assert.AreEqual(MqttClientConnectResultCode.BadUserNameOrPassword, ex.Result.ResultCode); + var response = await client.ConnectAsync(clientOptions); + Assert.AreEqual(MqttClientConnectResultCode.BadUserNameOrPassword, response.ResultCode); } } } diff --git a/Source/MQTTnet.Tests/Server/Server_Reference_Tests.cs b/Source/MQTTnet.Tests/Server/Server_Reference_Tests.cs index f8f76c185..7d859f571 100644 --- a/Source/MQTTnet.Tests/Server/Server_Reference_Tests.cs +++ b/Source/MQTTnet.Tests/Server/Server_Reference_Tests.cs @@ -4,51 +4,39 @@ using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; -using MQTTnet.Adapter; using MQTTnet.Client; using MQTTnet.Formatter; -using MQTTnet.Implementations; using MQTTnet.Internal; using MQTTnet.Protocol; -namespace MQTTnet.Tests.Server +namespace MQTTnet.Tests.Server; + +[TestClass] +public sealed class Server_Reference_Tests : BaseTestClass { - [TestClass] - public sealed class Server_Reference_Tests : BaseTestClass + [TestMethod] + public async Task Server_Reports_With_Reference_Server() { - [TestMethod] - public async Task Server_Reports_With_Reference_Server() + using (var testEnvironment = CreateTestEnvironment()) { - using (var testEnvironment = CreateTestEnvironment()) + testEnvironment.IgnoreClientLogErrors = true; + + var server = await testEnvironment.StartServer(); + + server.ValidatingConnectionAsync += e => { - testEnvironment.IgnoreClientLogErrors = true; - - var server = await testEnvironment.StartServer(); - - server.ValidatingConnectionAsync += e => - { - e.ReasonCode = MqttConnectReasonCode.ServerMoved; - e.ServerReference = "new_server"; - return CompletedTask.Instance; - }; - - try - { - var client = testEnvironment.CreateClient(); - - await client.ConnectAsync(new MqttClientOptionsBuilder() - .WithProtocolVersion(MqttProtocolVersion.V500) - .WithTcpServer("127.0.0.1", testEnvironment.ServerPort) - .Build()); - - Assert.Fail(); - } - catch (MqttConnectingFailedException e) - { - Assert.AreEqual(MqttClientConnectResultCode.ServerMoved, e.ResultCode); - Assert.AreEqual("new_server", e.Result.ServerReference); - } - } + e.ReasonCode = MqttConnectReasonCode.ServerMoved; + e.ServerReference = "new_server"; + return CompletedTask.Instance; + }; + + var client = testEnvironment.CreateClient(); + + var response = await client.ConnectAsync( + new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500).WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build()); + + Assert.AreEqual(MqttClientConnectResultCode.ServerMoved, response.ResultCode); + Assert.AreEqual("new_server", response.ServerReference); } } } \ No newline at end of file diff --git a/Source/MQTTnet/Adapter/MqttConnectingFailedException.cs b/Source/MQTTnet/Adapter/MqttConnectingFailedException.cs index 3700fdca4..669bb59e7 100644 --- a/Source/MQTTnet/Adapter/MqttConnectingFailedException.cs +++ b/Source/MQTTnet/Adapter/MqttConnectingFailedException.cs @@ -3,19 +3,13 @@ // See the LICENSE file in the project root for more information. using System; -using MQTTnet.Client; using MQTTnet.Exceptions; namespace MQTTnet.Adapter; public sealed class MqttConnectingFailedException : MqttCommunicationException { - public MqttConnectingFailedException(string message, Exception innerException, MqttClientConnectResult connectResult) : base(message, innerException) + public MqttConnectingFailedException(string message, Exception innerException) : base(message, innerException) { - Result = connectResult; } - - public MqttClientConnectResult Result { get; } - - public MqttClientConnectResultCode ResultCode => Result?.ResultCode ?? MqttClientConnectResultCode.UnspecifiedError; } \ No newline at end of file diff --git a/Source/MQTTnet/Client/MqttClient.cs b/Source/MQTTnet/Client/MqttClient.cs index 951e7e40f..59e26ec9b 100644 --- a/Source/MQTTnet/Client/MqttClient.cs +++ b/Source/MQTTnet/Client/MqttClient.cs @@ -171,11 +171,6 @@ public async Task ConnectAsync(MqttClientOptions option } catch (Exception exception) { - if (exception is MqttConnectingFailedException connectingFailedException) - { - connectResult = connectingFailedException.Result; - } - _disconnectReason = (int)MqttClientDisconnectOptionsReason.UnspecifiedError; _logger.Error(exception, "Error while connecting with server"); @@ -479,20 +474,7 @@ async Task Authenticate(IMqttChannelAdapter channelAdap } catch (Exception exception) { - throw new MqttConnectingFailedException($"Error while authenticating. {exception.Message}", exception, null); - } - - // This is no feature. It is basically a backward compatibility option and should be removed in the future. - // The client should not throw any exception if the transport layer connection was successful and the server - // did send a proper ACK packet with a non success response. - if (options.ThrowOnNonSuccessfulConnectResponse) - { - if (result.ResultCode != MqttClientConnectResultCode.Success) - { - _logger.Warning( - "Client will now throw an _MqttConnectingFailedException_. This is obsolete and will be removed in the future. Consider setting _ThrowOnNonSuccessfulResponseFromServer=False_ in client options."); - throw new MqttConnectingFailedException($"Connecting with MQTT server failed ({result.ResultCode}).", null, result); - } + throw new MqttConnectingFailedException($"Error while authenticating. {exception.Message}", exception); } _logger.Verbose("Authenticated MQTT connection with server established."); diff --git a/Source/MQTTnet/Client/Options/MqttClientOptions.cs b/Source/MQTTnet/Client/Options/MqttClientOptions.cs index 5b6f1e670..3338350e2 100644 --- a/Source/MQTTnet/Client/Options/MqttClientOptions.cs +++ b/Source/MQTTnet/Client/Options/MqttClientOptions.cs @@ -104,11 +104,6 @@ public sealed class MqttClientOptions /// public uint SessionExpiryInterval { get; set; } - /// - /// Gets or sets whether an exception should be thrown when the server has sent a non success ACK packet. - /// - public bool ThrowOnNonSuccessfulConnectResponse { get; set; } = true; - /// /// Gets or sets the timeout which will be applied at socket level and internal operations. /// The default value is the same as for sockets in .NET in general. diff --git a/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs b/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs index 052e8acd7..fb8a56f28 100644 --- a/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs +++ b/Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs @@ -230,16 +230,6 @@ public MqttClientOptionsBuilder WithoutPacketFragmentation() return this; } - /// - /// The client will not throw an exception when the MQTT server responds with a non success ACK packet. - /// This will become the default behavior in future versions of the library. - /// - public MqttClientOptionsBuilder WithoutThrowOnNonSuccessfulConnectResponse() - { - _options.ThrowOnNonSuccessfulConnectResponse = false; - return this; - } - public MqttClientOptionsBuilder WithProtocolType(ProtocolType protocolType) { _tcpOptions.ProtocolType = protocolType;