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.
 
 
 
 

89 lines
3.5 KiB

  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using MQTTnet.Client;
  3. using MQTTnet.Client.Subscribing;
  4. using MQTTnet.Server;
  5. using MQTTnet.Tests.Mockups;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace MQTTnet.Tests
  10. {
  11. [TestClass]
  12. public class Session_Tests
  13. {
  14. public TestContext TestContext { get; set; }
  15. [TestMethod]
  16. public async Task Set_Session_Item()
  17. {
  18. using (var testEnvironment = new TestEnvironment(TestContext))
  19. {
  20. var serverOptions = new MqttServerOptionsBuilder()
  21. .WithConnectionValidator(delegate (MqttConnectionValidatorContext context)
  22. {
  23. // Don't validate anything. Just set some session items.
  24. context.SessionItems["can_subscribe_x"] = true;
  25. context.SessionItems["default_payload"] = "Hello World";
  26. })
  27. .WithSubscriptionInterceptor(delegate (MqttSubscriptionInterceptorContext context)
  28. {
  29. if (context.TopicFilter.Topic == "x")
  30. {
  31. context.AcceptSubscription = context.SessionItems["can_subscribe_x"] as bool? == true;
  32. }
  33. })
  34. .WithApplicationMessageInterceptor(delegate (MqttApplicationMessageInterceptorContext context)
  35. {
  36. context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(context.SessionItems["default_payload"] as string);
  37. });
  38. await testEnvironment.StartServerAsync(serverOptions);
  39. string receivedPayload = null;
  40. var client = await testEnvironment.ConnectClientAsync();
  41. client.UseApplicationMessageReceivedHandler(delegate (MqttApplicationMessageReceivedEventArgs args)
  42. {
  43. receivedPayload = args.ApplicationMessage.ConvertPayloadToString();
  44. });
  45. var subscribeResult = await client.SubscribeAsync("x");
  46. Assert.AreEqual(MqttClientSubscribeResultCode.GrantedQoS0, subscribeResult.Items[0].ResultCode);
  47. var client2 = await testEnvironment.ConnectClientAsync();
  48. await client2.PublishAsync("x");
  49. await Task.Delay(1000);
  50. Assert.AreEqual("Hello World", receivedPayload);
  51. }
  52. }
  53. [TestMethod]
  54. public async Task Get_Session_Items_In_Status()
  55. {
  56. using (var testEnvironment = new TestEnvironment(TestContext))
  57. {
  58. var serverOptions = new MqttServerOptionsBuilder()
  59. .WithConnectionValidator(delegate (MqttConnectionValidatorContext context)
  60. {
  61. // Don't validate anything. Just set some session items.
  62. context.SessionItems["can_subscribe_x"] = true;
  63. context.SessionItems["default_payload"] = "Hello World";
  64. });
  65. await testEnvironment.StartServerAsync(serverOptions);
  66. var client = await testEnvironment.ConnectClientAsync();
  67. var sessionStatus = await testEnvironment.Server.GetSessionStatusAsync();
  68. var session = sessionStatus.First();
  69. Assert.AreEqual(true, session.Items["can_subscribe_x"]);
  70. }
  71. }
  72. }
  73. }