internal Msg(MsgArg arg, Subscription s, byte[] payload, long totalLen) { subject = arg.subject; sid = arg.sid; _reply = arg.reply; sub = s; if (arg.hdr > 0) { HeaderStatusReader hsr = new HeaderStatusReader(payload, arg.hdr); header = hsr.Header; status = hsr.Status; } // make a deep copy of the bytes for this message. long payloadLen = totalLen - arg.hdr; if (payloadLen > 0) { data = new byte[payloadLen]; Array.Copy(payload, arg.hdr, data, 0, (int)(totalLen - arg.hdr)); } else { data = Empty; } }
public void TestHeaderMultiValues() { var mh = new MsgHeader(); mh.Add("foo", "bar"); mh.Add("foo", "baz,comma"); // Test the GetValues API, don't make assumptions about order. string [] values = mh.GetValues("foo"); Assert.True(values.Length == 2); List <string> results = new List <string>(values); Assert.Contains("bar", results); Assert.Contains("baz,comma", results); byte[] bytes = mh.ToByteArray(); var hsr = new HeaderStatusReader(bytes, bytes.Length); Assert.Equal("bar,baz,comma", hsr.Header["foo"]); // test the API on a single value key mh = new MsgHeader(); mh["foo"] = "bar"; values = mh.GetValues("foo"); Assert.True(values.Length == 1); Assert.Equal("bar", values[0]); }
public void TestHeaderMultiValueSerialization() { string headers = "NATS/1.0\r\nfoo:bar\r\nfoo:baz,comma\r\n\r\n"; byte[] headerBytes = Encoding.ASCII.GetBytes(headers); var mh = new HeaderStatusReader(headerBytes, headerBytes.Length).Header; byte[] bytes = mh.ToByteArray(); Assert.True(bytes.Length == headerBytes.Length); for (int i = 0; i < bytes.Length; i++) { Assert.True(headerBytes[i] == bytes[i]); } }
public void TestHeaderSerialization() { string headers = "NATS/1.0\r\nfoo:bar\r\n\r\n"; byte[] headerBytes = Encoding.ASCII.GetBytes(headers); // can only test with one because order isn't guaranteed var mh = new MsgHeader(); mh["foo"] = "bar"; byte[] bytes = mh.ToByteArray(); Assert.True(bytes.Length == headerBytes.Length); for (int i = 0; i < bytes.Length; i++) { Assert.True(headerBytes[i] == bytes[i]); } // now serialize back var hsr = new HeaderStatusReader(bytes, bytes.Length); Assert.Equal("bar", hsr.Header["foo"]); // large header StringBuilder sb = new StringBuilder(); for (int i = 0; i < 20480; i++) { sb.Append("N"); } string lv = sb.ToString(); mh["LargeValue"] = lv; // test null and empty values mh["Null-Value"] = null; mh["Empty-Value"] = ""; bytes = mh.ToByteArray(); // now serialize back hsr = new HeaderStatusReader(bytes, bytes.Length); Assert.Equal("bar", hsr.Header["foo"]); Assert.Equal("", hsr.Header["Null-Value"]); Assert.Equal("", hsr.Header["Empty-Value"]); Assert.Equal(lv, hsr.Header["LargeValue"]); }
public void TestHeaderDeserialization() { byte[] hb = Encoding.ASCII.GetBytes("NATS/1.0\r\nfoo:bar\r\nbaz:bam\r\n\r\n"); var hsr = new HeaderStatusReader(hb, hb.Length); Assert.Equal(2, hsr.Header.Count); Assert.True(hsr.Status == null); Assert.Equal("bar", hsr.Header["foo"]); Assert.Equal("bam", hsr.Header["baz"]); // Test inline status and description which will come from the server. hb = Encoding.ASCII.GetBytes("NATS/1.0 503 an error\r\n\r\n"); hsr = new HeaderStatusReader(hb, hb.Length); Assert.Equal(0, hsr.Header.Count); Assert.NotNull(hsr.Status);; Assert.Equal(NatsConstants.NoRespondersCode, hsr.Status.Code); Assert.Equal("an error", hsr.Status.Message); // Test inline status and description which will come from the server. hb = Encoding.ASCII.GetBytes("NATS/1.0 503 an error \r\n\r\n"); hsr = new HeaderStatusReader(hb, hb.Length); Assert.Equal(0, hsr.Header.Count); Assert.NotNull(hsr.Status);; Assert.Equal(NatsConstants.NoRespondersCode, hsr.Status.Code); Assert.Equal("an error", hsr.Status.Message); // Test inline status and description which will come from the server. hb = Encoding.ASCII.GetBytes("NATS/1.0 404 Not Found\r\n\r\n"); hsr = new HeaderStatusReader(hb, hb.Length); Assert.Equal(0, hsr.Header.Count); Assert.NotNull(hsr.Status);; Assert.Equal(NatsConstants.NotFoundCode, hsr.Status.Code); Assert.Equal("Not Found", hsr.Status.Message); // test quoted strings hb = Encoding.ASCII.GetBytes("NATS/1.0\r\nfoo:\"string:with:quotes\"\r\nbaz:no:quotes\r\n\r\n"); hsr = new HeaderStatusReader(hb, hb.Length); Assert.Equal(2, hsr.Header.Count); Assert.True(hsr.Status == null); Assert.Equal("\"string:with:quotes\"", hsr.Header["foo"]); Assert.Equal("no:quotes", hsr.Header["baz"]); // Test unquoted strings. Technically not to spec, but // support anyhow. hb = Encoding.ASCII.GetBytes("NATS/1.0\r\nfoo::::\r\n\r\n"); hsr = new HeaderStatusReader(hb, hb.Length); Assert.Equal(1, hsr.Header.Count); Assert.True(hsr.Status == null); Assert.Equal(":::", hsr.Header["foo"]); // Test inline status which will come from the server. hb = Encoding.ASCII.GetBytes("NATS/1.0 503\r\n\r\n"); hsr = new HeaderStatusReader(hb, hb.Length); Assert.Equal(0, hsr.Header.Count); Assert.NotNull(hsr.Status);; Assert.Equal(NatsConstants.NoRespondersCode, hsr.Status.Code); // Test inline status with kv pair. hb = Encoding.ASCII.GetBytes("NATS/1.0 123\r\nfoo:bar\r\n\r\n"); hsr = new HeaderStatusReader(hb, hb.Length); Assert.True(hsr.Header.Count == 1); Assert.NotNull(hsr.Status);; Assert.Equal("bar", hsr.Header["foo"]); Assert.Equal(123, hsr.Status.Code); Assert.Equal("Server Status Message: 123", hsr.Status.Message); // Test inline status with kv pair. hb = Encoding.ASCII.GetBytes("NATS/1.0 503 hello\r\nfoo:bar\r\n\r\n"); hsr = new HeaderStatusReader(hb, hb.Length); Assert.True(hsr.Header.Count == 1); Assert.NotNull(hsr.Status);; Assert.Equal("bar", hsr.Header["foo"]); Assert.Equal(NatsConstants.NoRespondersCode, hsr.Status.Code); Assert.Equal("hello", hsr.Status.Message); }