Skip to content

Commit

Permalink
Fix Publish PayloadFormatIndicator (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
pglombardo authored Nov 10, 2023
1 parent 5fe54a7 commit 27efa2b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
6 changes: 1 addition & 5 deletions Source/HiveMQtt/MQTT5/Types/MQTT5PublishMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,7 @@ public string PayloadAsString
/// <exception cref="HiveMQttClientException">The exception raised if some value is out of range or invalid.</exception>
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;
}
Expand Down
29 changes: 29 additions & 0 deletions Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<bool>();
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);
}
}

0 comments on commit 27efa2b

Please sign in to comment.