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.
 
 
 
 

58 lines
2.1 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. public TestContext TestContext { get; set; }
  16. [TestMethod]
  17. public async Task Use_User_Properties()
  18. {
  19. using (var testEnvironment = new TestEnvironment(TestContext))
  20. {
  21. await testEnvironment.StartServerAsync();
  22. var sender = await testEnvironment.ConnectClientAsync(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));
  23. var receiver = await testEnvironment.ConnectClientAsync(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));
  24. var message = new MqttApplicationMessageBuilder()
  25. .WithTopic("A")
  26. .WithUserProperty("x", "1")
  27. .WithUserProperty("y", "2")
  28. .WithUserProperty("z", "3")
  29. .WithUserProperty("z", "4"); // z is here two times to test list of items
  30. await receiver.SubscribeAsync(new MqttClientSubscribeOptions
  31. {
  32. TopicFilters = new List<TopicFilter>
  33. {
  34. new TopicFilter { Topic = "#" }
  35. }
  36. }, CancellationToken.None);
  37. MqttApplicationMessage receivedMessage = null;
  38. receiver.UseApplicationMessageReceivedHandler(e => receivedMessage = e.ApplicationMessage);
  39. await sender.PublishAsync(message.Build(), CancellationToken.None);
  40. await Task.Delay(1000);
  41. Assert.IsNotNull(receivedMessage);
  42. Assert.AreEqual("A", receivedMessage.Topic);
  43. Assert.AreEqual(4, receivedMessage.UserProperties.Count);
  44. }
  45. }
  46. }
  47. }