diff --git a/Source/HiveMQtt/MQTT5/Types/MQTT5PublishMessage.cs b/Source/HiveMQtt/MQTT5/Types/MQTT5PublishMessage.cs index fe39272b..547ffb29 100644 --- a/Source/HiveMQtt/MQTT5/Types/MQTT5PublishMessage.cs +++ b/Source/HiveMQtt/MQTT5/Types/MQTT5PublishMessage.cs @@ -168,11 +168,7 @@ public string PayloadAsString /// The exception raised if some value is out of range or invalid. public void Validate() { - if (this.PayloadFormatIndicator.HasValue && (this.PayloadFormatIndicator.Value is not MQTT5PayloadFormatIndicator.Unspecified and not MQTT5PayloadFormatIndicator.UTF8Encoded)) - { - throw new HiveMQttClientException("Payload Format Indicator must be Unspecified or UTF8Encoded."); - } - else + if (!this.PayloadFormatIndicator.HasValue) { this.PayloadFormatIndicator = MQTT5PayloadFormatIndicator.Unspecified; } diff --git a/Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs b/Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs index 19932aba..bdcec6b7 100644 --- a/Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs +++ b/Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs @@ -1,8 +1,10 @@ namespace HiveMQtt.Test.HiveMQClient; +using System.Text; using System.Threading.Tasks; using HiveMQtt.Client; using HiveMQtt.MQTT5.ReasonCodes; +using HiveMQtt.MQTT5.Types; using Xunit; public class PublishTest @@ -146,4 +148,31 @@ public async Task PublishWithOptionsAsync() var disconnectResult = await client.DisconnectAsync().ConfigureAwait(false); Assert.True(disconnectResult); } + + [Fact] + public async Task PublishPayloadFormatIndicatorAsync() + { + var client = new HiveMQClient(); + var connectResult = await client.ConnectAsync().ConfigureAwait(false); + Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success); + + var msg = new MQTT5PublishMessage("tests/PublishPayloadFormatIndicatorAsync", QualityOfService.AtMostOnceDelivery) + { + PayloadFormatIndicator = MQTT5PayloadFormatIndicator.UTF8Encoded, + Payload = Encoding.ASCII.GetBytes("blah"), + }; + + var taskCompletionSource = new TaskCompletionSource(); + client.OnPublishSent += (sender, args) => + { + Assert.Equal(MQTT5PayloadFormatIndicator.UTF8Encoded, args.PublishPacket.Message.PayloadFormatIndicator); + taskCompletionSource.SetResult(true); + }; + + var result = await client.PublishAsync(msg).ConfigureAwait(false); + var eventResult = await taskCompletionSource.Task.ConfigureAwait(false); + + var disconnectResult = await client.DisconnectAsync().ConfigureAwait(false); + Assert.True(disconnectResult); + } }