You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MqttApplicationMessageBuilderTests.cs 2.5 KiB

7 jaren geleden
7 jaren geleden
5 jaren geleden
7 jaren geleden
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using MQTTnet.Protocol;
  6. namespace MQTTnet.Tests
  7. {
  8. [TestClass]
  9. public class MqttApplicationMessageBuilderTests
  10. {
  11. [TestMethod]
  12. public void CreateApplicationMessage_TopicOnly()
  13. {
  14. var message = new MqttApplicationMessageBuilder().WithTopic("Abc").Build();
  15. Assert.AreEqual("Abc", message.Topic);
  16. Assert.IsFalse(message.Retain);
  17. Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, message.QualityOfServiceLevel);
  18. }
  19. [TestMethod]
  20. public void CreateApplicationMessage_TimeStampPayload()
  21. {
  22. var message = new MqttApplicationMessageBuilder().WithTopic("xyz").WithPayload(TimeSpan.FromSeconds(360).ToString()).Build();
  23. Assert.AreEqual("xyz", message.Topic);
  24. Assert.IsFalse(message.Retain);
  25. Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, message.QualityOfServiceLevel);
  26. Assert.AreEqual(Encoding.UTF8.GetString(message.Payload), "00:06:00");
  27. }
  28. [TestMethod]
  29. public void CreateApplicationMessage_StreamPayload()
  30. {
  31. var stream = new MemoryStream(Encoding.UTF8.GetBytes("xHello")) { Position = 1 };
  32. var message = new MqttApplicationMessageBuilder().WithTopic("123").WithPayload(stream).Build();
  33. Assert.AreEqual("123", message.Topic);
  34. Assert.IsFalse(message.Retain);
  35. Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, message.QualityOfServiceLevel);
  36. Assert.AreEqual(Encoding.UTF8.GetString(message.Payload), "Hello");
  37. }
  38. [TestMethod]
  39. public void CreateApplicationMessage_Retained()
  40. {
  41. var message = new MqttApplicationMessageBuilder().WithTopic("lol").WithRetainFlag().Build();
  42. Assert.AreEqual("lol", message.Topic);
  43. Assert.IsTrue(message.Retain);
  44. Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, message.QualityOfServiceLevel);
  45. }
  46. [TestMethod]
  47. public void CreateApplicationMessage_QosLevel2()
  48. {
  49. var message = new MqttApplicationMessageBuilder().WithTopic("rofl").WithRetainFlag().WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce).Build();
  50. Assert.AreEqual("rofl", message.Topic);
  51. Assert.IsTrue(message.Retain);
  52. Assert.AreEqual(MqttQualityOfServiceLevel.ExactlyOnce, message.QualityOfServiceLevel);
  53. }
  54. }
  55. }