using System.Buffers; using System.IO.Pipelines; using System.Text; using NATS.Server.Protocol; namespace NATS.Server.Tests; public class ParserTests { private static async Task> ParseAsync(string input) { var pipe = new Pipe(); var commands = new List(); // Write input to pipe var bytes = Encoding.ASCII.GetBytes(input); await pipe.Writer.WriteAsync(bytes); pipe.Writer.Complete(); // Parse from pipe var parser = new NatsParser(maxPayload: NatsProtocol.MaxPayloadSize); while (true) { var result = await pipe.Reader.ReadAsync(); var buffer = result.Buffer; while (parser.TryParse(ref buffer, out var cmd)) commands.Add(cmd); pipe.Reader.AdvanceTo(buffer.Start, buffer.End); if (result.IsCompleted) break; } return commands; } [Fact] public async Task Parse_PING() { var cmds = await ParseAsync("PING\r\n"); cmds.ShouldHaveSingleItem(); cmds[0].Type.ShouldBe(CommandType.Ping); } [Fact] public async Task Parse_PONG() { var cmds = await ParseAsync("PONG\r\n"); cmds.ShouldHaveSingleItem(); cmds[0].Type.ShouldBe(CommandType.Pong); } [Fact] public async Task Parse_CONNECT() { var cmds = await ParseAsync("CONNECT {\"verbose\":false,\"echo\":true}\r\n"); cmds.ShouldHaveSingleItem(); cmds[0].Type.ShouldBe(CommandType.Connect); Encoding.ASCII.GetString(cmds[0].Payload.ToArray()).ShouldContain("verbose"); } [Fact] public async Task Parse_SUB_without_queue() { var cmds = await ParseAsync("SUB foo 1\r\n"); cmds.ShouldHaveSingleItem(); cmds[0].Type.ShouldBe(CommandType.Sub); cmds[0].Subject.ShouldBe("foo"); cmds[0].Queue.ShouldBeNull(); cmds[0].Sid.ShouldBe("1"); } [Fact] public async Task Parse_SUB_with_queue() { var cmds = await ParseAsync("SUB foo workers 1\r\n"); cmds.ShouldHaveSingleItem(); cmds[0].Type.ShouldBe(CommandType.Sub); cmds[0].Subject.ShouldBe("foo"); cmds[0].Queue.ShouldBe("workers"); cmds[0].Sid.ShouldBe("1"); } [Fact] public async Task Parse_UNSUB() { var cmds = await ParseAsync("UNSUB 1\r\n"); cmds.ShouldHaveSingleItem(); cmds[0].Type.ShouldBe(CommandType.Unsub); cmds[0].Sid.ShouldBe("1"); cmds[0].MaxMessages.ShouldBe(-1); } [Fact] public async Task Parse_UNSUB_with_max() { var cmds = await ParseAsync("UNSUB 1 5\r\n"); cmds.ShouldHaveSingleItem(); cmds[0].Type.ShouldBe(CommandType.Unsub); cmds[0].Sid.ShouldBe("1"); cmds[0].MaxMessages.ShouldBe(5); } [Fact] public async Task Parse_PUB_with_payload() { var cmds = await ParseAsync("PUB foo 5\r\nHello\r\n"); cmds.ShouldHaveSingleItem(); cmds[0].Type.ShouldBe(CommandType.Pub); cmds[0].Subject.ShouldBe("foo"); cmds[0].ReplyTo.ShouldBeNull(); Encoding.ASCII.GetString(cmds[0].Payload.ToArray()).ShouldBe("Hello"); } [Fact] public async Task Parse_PUB_with_reply() { var cmds = await ParseAsync("PUB foo reply 5\r\nHello\r\n"); cmds.ShouldHaveSingleItem(); cmds[0].Type.ShouldBe(CommandType.Pub); cmds[0].Subject.ShouldBe("foo"); cmds[0].ReplyTo.ShouldBe("reply"); Encoding.ASCII.GetString(cmds[0].Payload.ToArray()).ShouldBe("Hello"); } [Fact] public async Task Parse_multiple_commands() { var cmds = await ParseAsync("PING\r\nPONG\r\nSUB foo 1\r\n"); cmds.Count.ShouldBe(3); cmds[0].Type.ShouldBe(CommandType.Ping); cmds[1].Type.ShouldBe(CommandType.Pong); cmds[2].Type.ShouldBe(CommandType.Sub); } [Fact] public async Task Parse_PUB_zero_payload() { var cmds = await ParseAsync("PUB foo 0\r\n\r\n"); cmds.ShouldHaveSingleItem(); cmds[0].Type.ShouldBe(CommandType.Pub); cmds[0].Payload.ToArray().ShouldBeEmpty(); } [Fact] public async Task Parse_case_insensitive() { var cmds = await ParseAsync("ping\r\npub FOO 3\r\nabc\r\n"); cmds.Count.ShouldBe(2); cmds[0].Type.ShouldBe(CommandType.Ping); cmds[1].Type.ShouldBe(CommandType.Pub); } [Fact] public async Task Parse_HPUB() { // HPUB subject 12 17\r\nNATS/1.0\r\n\r\nHello\r\n var header = "NATS/1.0\r\n\r\n"; var payload = "Hello"; var total = header.Length + payload.Length; var cmds = await ParseAsync($"HPUB foo {header.Length} {total}\r\n{header}{payload}\r\n"); cmds.ShouldHaveSingleItem(); cmds[0].Type.ShouldBe(CommandType.HPub); cmds[0].Subject.ShouldBe("foo"); cmds[0].HeaderSize.ShouldBe(header.Length); } [Fact] public async Task Parse_INFO() { var cmds = await ParseAsync("INFO {\"server_id\":\"test\"}\r\n"); cmds.ShouldHaveSingleItem(); cmds[0].Type.ShouldBe(CommandType.Info); } }