Files
MQTTnet/Source/MQTTnet.Tests/MqttApplicationMessageBuilder_Tests.cs
T
Christian e43e7f8e36 Use latest language features (#2176)
* Make use of language features line AsyncDispose, init etc.

* Fix build errors

* Improve Unit Tests

* Improve Unit Tests

* Improve Unit Tests

* Update samples

* Remove obsolete benchmarks

* Code cleanup

* Improve unit tests

* Improve Unit Tests

* Fix Unit Tests

* Improve Unit Tests

* Improve Unit Tests

* Restore unit tests

* Exclude load tests from unit tests
2025-08-30 10:52:59 +02:00

65 lines
2.6 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Protocol;
namespace MQTTnet.Tests;
// ReSharper disable InconsistentNaming
[TestClass]
public sealed class MqttApplicationMessageBuilder_Tests
{
[TestMethod]
public void CreateApplicationMessage_TopicOnly()
{
var message = new MqttApplicationMessageBuilder().WithTopic("Abc").Build();
Assert.AreEqual("Abc", message.Topic);
Assert.IsFalse(message.Retain);
Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, message.QualityOfServiceLevel);
}
[TestMethod]
public void CreateApplicationMessage_TimeStampPayload()
{
var message = new MqttApplicationMessageBuilder().WithTopic("xyz").WithPayload(TimeSpan.FromSeconds(360).ToString()).Build();
Assert.AreEqual("xyz", message.Topic);
Assert.IsFalse(message.Retain);
Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, message.QualityOfServiceLevel);
Assert.AreEqual(Encoding.UTF8.GetString(message.Payload), "00:06:00");
}
[TestMethod]
public void CreateApplicationMessage_StreamPayload()
{
var stream = new MemoryStream("xHello"u8.ToArray()) { Position = 1 };
var message = new MqttApplicationMessageBuilder().WithTopic("123").WithPayload(stream).Build();
Assert.AreEqual("123", message.Topic);
Assert.IsFalse(message.Retain);
Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, message.QualityOfServiceLevel);
Assert.AreEqual(Encoding.UTF8.GetString(message.Payload), "Hello");
}
[TestMethod]
public void CreateApplicationMessage_Retained()
{
var message = new MqttApplicationMessageBuilder().WithTopic("lol").WithRetainFlag().Build();
Assert.AreEqual("lol", message.Topic);
Assert.IsTrue(message.Retain);
Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, message.QualityOfServiceLevel);
}
[TestMethod]
public void CreateApplicationMessage_QosLevel2()
{
var message = new MqttApplicationMessageBuilder().WithTopic("rofl").WithRetainFlag().WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build();
Assert.AreEqual("rofl", message.Topic);
Assert.IsTrue(message.Retain);
Assert.AreEqual(MqttQualityOfServiceLevel.ExactlyOnce, message.QualityOfServiceLevel);
}
}