Browse Source

Add topic filter tests from MQTT spec

release/3.x.x
Christian 6 years ago
parent
commit
74a5522965
2 changed files with 28 additions and 2 deletions
  1. +1
    -1
      Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs
  2. +27
    -1
      Tests/MQTTnet.Core.Tests/TopicFilterComparerTests.cs

+ 1
- 1
Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs View File

@@ -129,7 +129,7 @@ namespace MQTTnet.Adapter
{ {
return null; return null;
} }
if (header.BodyLength == 0) if (header.BodyLength == 0)
{ {
return new ReceivedMqttPacket(header, new MemoryStream(new byte[0], false)); return new ReceivedMqttPacket(header, new MemoryStream(new byte[0], false));


+ 27
- 1
Tests/MQTTnet.Core.Tests/TopicFilterComparerTests.cs View File

@@ -60,7 +60,33 @@ namespace MQTTnet.Core.Tests
CompareAndAssert("A/B/C/D", "#", true); CompareAndAssert("A/B/C/D", "#", true);
} }


private void CompareAndAssert(string topic, string filter, bool expectedResult)
[TestMethod]
public void TopicFilterComparer_MultiLevel_Sport()
{
// Tests from official MQTT spec (4.7.1.2 Multi-level wildcard)
CompareAndAssert("sport/tennis/player1", "sport/tennis/player1/#", true);
CompareAndAssert("sport/tennis/player1/ranking", "sport/tennis/player1/#", true);
CompareAndAssert("sport/tennis/player1/score/wimbledon", "sport/tennis/player1/#", true);

CompareAndAssert("sport/tennis/player1", "sport/tennis/+", true);
CompareAndAssert("sport/tennis/player2", "sport/tennis/+", true);
CompareAndAssert("sport/tennis/player1/ranking", "sport/tennis/+", false);

CompareAndAssert("sport", "sport/#", true);
CompareAndAssert("sport", "sport/+", false);
CompareAndAssert("sport/", "sport/+", true);
}

[TestMethod]
public void TopicFilterComparer_SingleLevel_Finance()
{
// Tests from official MQTT spec (4.7.1.3 Single level wildcard)
CompareAndAssert("/finance", "+/+", true);
CompareAndAssert("/finance", "/+", true);
CompareAndAssert("/finance", "+", false);
}

private static void CompareAndAssert(string topic, string filter, bool expectedResult)
{ {
Assert.AreEqual(expectedResult, MqttTopicFilterComparer.IsMatch(topic, filter)); Assert.AreEqual(expectedResult, MqttTopicFilterComparer.IsMatch(topic, filter));
} }


Loading…
Cancel
Save