コード例 #1
0
        public void TestHeaderSerialization()
        {
            string headers = $"NATS/1.0\r\nfoo:bar\r\n\r\n";

            byte[] headerBytes = System.Text.Encoding.UTF8.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 mh2 = new MsgHeader(bytes, bytes.Length);

            Assert.Equal("bar", mh2["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
            mh2 = new MsgHeader(bytes, bytes.Length);
            Assert.Equal("bar", mh2["foo"]);
            Assert.Equal("", mh2["Null-Value"]);
            Assert.Equal("", mh2["Empty-Value"]);
            Assert.Equal(lv, mh2["LargeValue"]);
        }
コード例 #2
0
        public void TestHeaderMultiValues()
        {
            var mh = new MsgHeader();

            mh.Add("foo", "bar");
            mh.Add("foo", "baz");

            Assert.Equal("bar,baz", mh["foo"]);

            // Test the GetValues API, don't make assumpions 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", results);

            byte[] bytes = mh.ToByteArray();
            var    mh2   = new MsgHeader(bytes, bytes.Length);

            Assert.Equal("bar,baz", mh2["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]);
        }
コード例 #3
0
        public void TestHeaderMultiValueSerialization()
        {
            string headers = $"NATS/1.0\r\nfoo:bar\r\nfoo:baz,comma\r\n\r\n";

            byte[] headerBytes = Encoding.UTF8.GetBytes(headers);
            var    mh          = new MsgHeader(headerBytes, headerBytes.Length);

            byte[] bytes = mh.ToByteArray();
            Assert.True(bytes.Length == headerBytes.Length);
            for (int i = 0; i < bytes.Length; i++)
            {
                Assert.True(headerBytes[i] == bytes[i]);
            }
        }