feat: implement mqtt packet-level parser and writer

This commit is contained in:
Joseph Doherty
2026-02-23 14:41:23 -05:00
parent 958c4aa8ed
commit 7faf42c588
5 changed files with 153 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
using NATS.Server.Mqtt;
namespace NATS.Server.Tests.Mqtt;
public class MqttPacketParserTests
{
[Fact]
public void Connect_packet_fixed_header_and_remaining_length_parse_correctly()
{
var packet = MqttPacketReader.Read(ConnectPacketBytes.Sample);
packet.Type.ShouldBe(MqttControlPacketType.Connect);
packet.RemainingLength.ShouldBe(12);
packet.Payload.Length.ShouldBe(12);
}
private static class ConnectPacketBytes
{
public static readonly byte[] Sample =
[
0x10, 0x0C, // CONNECT + remaining length
0x00, 0x04, (byte)'M', (byte)'Q', (byte)'T', (byte)'T',
0x04, 0x02, 0x00, 0x3C, // protocol level/flags/keepalive
0x00, 0x00, // empty client id
];
}
}