-
Notifications
You must be signed in to change notification settings - Fork 55
/
NatsHeaderTest.cs
198 lines (170 loc) · 8.08 KB
/
NatsHeaderTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System.Buffers;
using System.IO.Pipelines;
using System.Text;
namespace NATS.Client.Core.Tests;
public class NatsHeaderTest
{
private readonly ITestOutputHelper _output;
public NatsHeaderTest(ITestOutputHelper output) => _output = output;
[Fact]
public async Task WriterTests()
{
var headers = new NatsHeaders
{
["k1"] = "v1",
["k2"] = new[] { "v2-0", "v2-1" },
["a-long-header-key"] = "value",
["key"] = "a-long-header-value",
};
var pipe = new Pipe(new PipeOptions(pauseWriterThreshold: 0));
var writer = new HeaderWriter(Encoding.UTF8);
var written = writer.Write(pipe.Writer, headers);
var text = "NATS/1.0\r\nk1: v1\r\nk2: v2-0\r\nk2: v2-1\r\na-long-header-key: value\r\nkey: a-long-header-value\r\n\r\n";
var expected = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(text));
Assert.Equal(expected.Length, written);
await pipe.Writer.FlushAsync();
var result = await pipe.Reader.ReadAtLeastAsync((int)written);
Assert.True(expected.ToSpan().SequenceEqual(result.Buffer.ToSpan()));
_output.WriteLine($"Buffer:\n{result.Buffer.First.Span.Dump()}");
}
[Fact]
public async Task WriterEmptyTests()
{
var headers = new NatsHeaders();
var pipe = new Pipe(new PipeOptions(pauseWriterThreshold: 0));
var writer = new HeaderWriter(Encoding.UTF8);
var written = writer.Write(pipe.Writer, headers);
var text = "NATS/1.0\r\n\r\n";
var expected = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(text));
Assert.Equal(expected.Length, written);
await pipe.Writer.FlushAsync();
var result = await pipe.Reader.ReadAtLeastAsync((int)written);
Assert.True(expected.ToSpan().SequenceEqual(result.Buffer.ToSpan()));
_output.WriteLine($"Buffer:\n{result.Buffer.First.Span.Dump()}");
}
[Fact]
public void ParserTests()
{
var parser = new NatsHeaderParser(Encoding.UTF8);
var text = "NATS/1.0 123 Test Message\r\nk1: v1\r\nk2: v2-0\r\nk2: v2-1\r\na-long-header-key: value\r\nkey: a-long-header-value\r\n\r\n";
var input = new SequenceReader<byte>(new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(text)));
var headers = new NatsHeaders();
parser.ParseHeaders(input, headers);
_output.WriteLine($"Headers:\n{headers.Dump()}");
Assert.Equal(1, headers.Version);
Assert.Equal(123, headers.Code);
Assert.Equal("Test Message", headers.MessageText);
Assert.Equal(4, headers.Count);
Assert.True(headers.ContainsKey("k1"));
Assert.Single(headers["k1"].ToArray());
Assert.Equal("v1", headers["k1"]);
Assert.True(headers.ContainsKey("k2"));
Assert.Equal(2, headers["k2"].ToArray().Length);
Assert.Equal("v2-0", headers["k2"][0]);
Assert.Equal("v2-1", headers["k2"][1]);
Assert.True(headers.ContainsKey("a-long-header-key"));
Assert.Single(headers["a-long-header-key"].ToArray());
Assert.Equal("value", headers["a-long-header-key"]);
Assert.True(headers.ContainsKey("key"));
Assert.Single(headers["key"].ToArray());
Assert.Equal("a-long-header-value", headers["key"]);
}
[Theory]
[InlineData("Idle Heartbeat", NatsHeaders.Messages.IdleHeartbeat)]
[InlineData("Bad Request", NatsHeaders.Messages.BadRequest)]
[InlineData("Consumer Deleted", NatsHeaders.Messages.ConsumerDeleted)]
[InlineData("Consumer is push based", NatsHeaders.Messages.ConsumerIsPushBased)]
[InlineData("No Messages", NatsHeaders.Messages.NoMessages)]
[InlineData("Request Timeout", NatsHeaders.Messages.RequestTimeout)]
[InlineData("Message Size Exceeds MaxBytes", NatsHeaders.Messages.MessageSizeExceedsMaxBytes)]
[InlineData("test message", NatsHeaders.Messages.Text)]
public void ParserMessageEnumTests(string message, NatsHeaders.Messages result)
{
var parser = new NatsHeaderParser(Encoding.UTF8);
var text = $"NATS/1.0 100 {message}\r\n\r\n";
var input = new SequenceReader<byte>(new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(text)));
var headers = new NatsHeaders();
parser.ParseHeaders(input, headers);
Assert.Equal(result, headers.Message);
Assert.Equal(message, headers.MessageText);
}
[Fact]
public void ParserVersionErrorTests()
{
var exception = Assert.Throws<NatsException>(() =>
{
var parser = new NatsHeaderParser(Encoding.UTF8);
var text = "NATS/2.0\r\n\r\n";
var input = new SequenceReader<byte>(new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(text)));
var headers = new NatsHeaders();
parser.ParseHeaders(input, headers);
});
Assert.Equal("Protocol error: header version mismatch", exception.Message);
}
[Fact]
public void ParserCodeErrorTests()
{
var exception = Assert.Throws<NatsException>(() =>
{
var parser = new NatsHeaderParser(Encoding.UTF8);
var text = "NATS/1.0 x\r\n\r\n";
var input = new SequenceReader<byte>(new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(text)));
var headers = new NatsHeaders();
parser.ParseHeaders(input, headers);
});
Assert.Equal("Protocol error: header code is not a number", exception.Message);
}
[Theory]
[InlineData("NATS/1.0\r\n\r\n", 0, "", 0)]
[InlineData("NATS/1.0\r\nk:v\r\n\r\n", 0, "", 1)]
[InlineData("NATS/1.0 42\r\n\r\n", 42, "", 0)]
[InlineData("NATS/1.0 42\r\nk:v\r\n\r\n", 42, "", 1)]
[InlineData("NATS/1.0 123 test\r\nk:v\r\n\r\n", 123, "test", 1)]
[InlineData("NATS/1.0 123 test\r\n\r\n", 123, "test", 0)]
[InlineData("NATS/1.0 456 test 2\r\n\r\n", 456, "test 2", 0)]
[InlineData("NATS/1.0 123456 test test 3\r\n\r\n", 123456, "test test 3", 0)]
[InlineData("NATS/1.0 123456 test test 3\r\nk:v\r\n\r\n", 123456, "test test 3", 1)]
public void ParserHeaderVersionOnlyTests(string text, int code, string message, int headerCount)
{
var parser = new NatsHeaderParser(Encoding.UTF8);
var input = new SequenceReader<byte>(new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(text)));
var headers = new NatsHeaders();
parser.ParseHeaders(input, headers);
Assert.Equal(1, headers.Version);
Assert.Equal(code, headers.Code);
Assert.Equal(NatsHeaders.Messages.Text, headers.Message);
Assert.Equal(message, headers.MessageText);
Assert.Equal(headerCount, headers.Count);
}
[Fact]
public void ParserMultiSpanTests()
{
const string text1 = "NATS/1.0 123 Test ";
const string text2 = "Message\r\n\r\n";
var parser = new NatsHeaderParser(Encoding.UTF8);
var builder = new SeqeunceBuilder();
builder.Append(Encoding.UTF8.GetBytes(text1));
builder.Append(Encoding.UTF8.GetBytes(text2));
var input = new SequenceReader<byte>(builder.ToReadOnlySequence());
var headers = new NatsHeaders();
parser.ParseHeaders(input, headers);
Assert.Equal(1, headers.Version);
Assert.Equal(123, headers.Code);
Assert.Equal("Test Message", headers.MessageText);
}
[Theory]
[InlineData("NATS/1.0 123 Test Message\r\nk2: v2-0\r\nk2: v2-1\r\n\r\n", true, "v2-1")]
[InlineData("NATS/1.0 123 Test Message\r\nk2: v2-0\r\n\r\n", true, "v2-0")]
[InlineData("NATS/1.0 123 Test Message\r\nk3: v2-0\r\n\r\n", false, null)]
[InlineData("NATS/1.0 123 Test Message\r\n\r\n", false, null)]
public void GetLastValueTests(string text, bool expectedResult, string? expectedLastValue)
{
var parser = new NatsHeaderParser(Encoding.UTF8);
var input = new SequenceReader<byte>(new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(text)));
var headers = new NatsHeaders();
parser.ParseHeaders(input, headers);
var result = headers.TryGetLastValue("k2", out var lastValue);
Assert.Equal(result, expectedResult);
Assert.Equal(lastValue, expectedLastValue);
}
}