Esempio n. 1
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]);
        }
Esempio n. 2
0
        private void InitHeader(byte[] serialized, int len, Token tCrlf, bool hadStatus)
        {
            // REGULAR HEADER
            Token peek = new Token(serialized, len, tCrlf, null);

            while (peek.IsType(TokenType.Text))
            {
                Token tKey = new Token(serialized, len, tCrlf, TokenType.Key);
                Token tVal = new Token(serialized, len, tKey, null);
                if (tVal.IsType(TokenType.Space))
                {
                    tVal = new Token(serialized, len, tVal, null);
                }
                if (tVal.IsType(TokenType.Text))
                {
                    tCrlf = new Token(serialized, len, tVal, TokenType.Crlf);
                }
                else
                {
                    tVal.MustBe(TokenType.Crlf);
                    tCrlf = tVal;
                }
                _header.Add(tKey.Value(), tVal.Value());
                peek = new Token(serialized, len, tCrlf, null);
            }
            peek.MustBe(TokenType.Crlf);

            if (_header.Count == 0 && !hadStatus)
            {
                throw new NATSInvalidHeaderException(InvalidHeaderComposition);
            }
        }
        public void TestGetAndDeleteMessage()
        {
            Context.RunInJsServer(c => {
                CreateDefaultTestStream(c);
                IJetStream js = c.CreateJetStreamContext();

                MsgHeader h = new MsgHeader();
                h.Add("foo", "bar");

                DateTime beforeCreated = DateTime.Now;
                js.Publish(new Msg(SUBJECT, null, h, DataBytes(1)));
                js.Publish(new Msg(SUBJECT, null));

                IJetStreamManagement jsm = c.CreateJetStreamManagementContext();

                MessageInfo mi = jsm.GetMessage(STREAM, 1);
                Assert.Equal(SUBJECT, mi.Subject);
                Assert.Equal(Data(1), System.Text.Encoding.ASCII.GetString(mi.Data));
                Assert.Equal(1U, mi.Sequence);
                Assert.True(mi.Time >= beforeCreated);
                Assert.NotNull(mi.Headers);
                Assert.Equal("bar", mi.Headers["foo"]);

                mi = jsm.GetMessage(STREAM, 2);
                Assert.Equal(SUBJECT, mi.Subject);
                Assert.Null(mi.Data);
                Assert.Equal(2U, mi.Sequence);
                Assert.True(mi.Time >= beforeCreated);
                Assert.Null(mi.Headers);

                Assert.True(jsm.DeleteMessage(STREAM, 1));
                Assert.Throws <NATSJetStreamException>(() => jsm.DeleteMessage(STREAM, 1));
                Assert.Throws <NATSJetStreamException>(() => jsm.GetMessage(STREAM, 1));
                Assert.Throws <NATSJetStreamException>(() => jsm.GetMessage(STREAM, 3));
                Assert.Throws <NATSJetStreamException>(() => jsm.DeleteMessage(Stream(999), 1));
                Assert.Throws <NATSJetStreamException>(() => jsm.GetMessage(Stream(999), 1));
            });
        }