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.
 
 
 
 

56 lines
2.0 KiB

  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using MQTTnet.Client;
  6. using MQTTnet.Client.Options;
  7. using MQTTnet.Client.Subscribing;
  8. using MQTTnet.Formatter;
  9. using MQTTnet.Tests.Mockups;
  10. namespace MQTTnet.Tests.MQTTv5
  11. {
  12. [TestClass]
  13. public class Feature_Tests
  14. {
  15. [TestMethod]
  16. public async Task Use_User_Properties()
  17. {
  18. using (var testEnvironment = new TestEnvironment())
  19. {
  20. await testEnvironment.StartServerAsync();
  21. var sender = await testEnvironment.ConnectClientAsync(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));
  22. var receiver = await testEnvironment.ConnectClientAsync(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));
  23. var message = new MqttApplicationMessageBuilder()
  24. .WithTopic("A")
  25. .WithUserProperty("x", "1")
  26. .WithUserProperty("y", "2")
  27. .WithUserProperty("z", "3")
  28. .WithUserProperty("z", "4"); // z is here two times to test list of items
  29. await receiver.SubscribeAsync(new MqttClientSubscribeOptions
  30. {
  31. TopicFilters = new List<TopicFilter>
  32. {
  33. new TopicFilter { Topic = "#" }
  34. }
  35. }, CancellationToken.None);
  36. MqttApplicationMessage receivedMessage = null;
  37. receiver.UseApplicationMessageReceivedHandler(e => receivedMessage = e.ApplicationMessage);
  38. await sender.PublishAsync(message.Build(), CancellationToken.None);
  39. await Task.Delay(1000);
  40. Assert.IsNotNull(receivedMessage);
  41. Assert.AreEqual("A", receivedMessage.Topic);
  42. Assert.AreEqual(4, receivedMessage.UserProperties.Count);
  43. }
  44. }
  45. }
  46. }