public void TestMultipleCombining()
        {
            var subStream = new CombiningStream(", ");

            subStream.Add(GetStream("{\"id\": 1}"));
            subStream.Add(GetStream("{\"id\": 2}"));
            subStream.Add(GetStream("{\"id\": 3}"));
            subStream.Add(GetStream("{\"id\": 4}"));

            var stream = new CombiningStream();

            stream.Add(GetStream("{\"events\":["));
            stream.Add(subStream);
            stream.Add(GetStream("]}"));

            Assert.AreEqual(13 + 4 * 9 + 3 * 2, stream.Length);

            var buf  = new byte[stream.Length + 100];
            var read = stream.Read(buf, 0, buf.Length);

            Assert.AreEqual(stream.Length, read);

            var data = Encoding.UTF8.GetString(buf, 0, read);

            Assert.AreEqual("{\"events\":[{\"id\": 1}, {\"id\": 2}, {\"id\": 3}, {\"id\": 4}]}", data);
        }
        public void TestEmpty()
        {
            var stream = new CombiningStream();

            Assert.AreEqual(0, stream.Length);

            var buf  = new byte[100];
            var read = stream.Read(buf, 0, buf.Length);

            Assert.AreEqual(0, read);
        }
        public void TestNoCombining()
        {
            var stream = new CombiningStream(" & ");

            stream.Add(GetStream("test"));
            Assert.AreEqual(4, stream.Length);

            var buf  = new byte[100];
            var read = stream.Read(buf, 0, buf.Length);

            Assert.AreEqual(stream.Length, read);

            var data = Encoding.UTF8.GetString(buf, 0, read);

            Assert.AreEqual("test", data);
        }
        public void TestSingleCombining()
        {
            var stream = new CombiningStream(" bar ");

            stream.Add(GetStream("foo"));
            stream.Add(GetStream("baz"));
            Assert.AreEqual(11, stream.Length);

            var buf  = new byte[100];
            var read = stream.Read(buf, 0, buf.Length);

            Assert.AreEqual(stream.Length, read);

            var data = Encoding.UTF8.GetString(buf, 0, read);

            Assert.AreEqual("foo bar baz", data);
        }
Beispiel #5
0
        private Stream MakeNotification(Stream[] jsonEventStreams)
        {
            if (notifPrepend == null || notifAppend == null)
            {
                var json = JsonConvert.SerializeObject(new Notification()
                {
                    ApiKey   = ApiKey,
                    Notifier = Notifier,
                    Events   = new List <Event> (0),
                });

                // Find empty events array:
                var idx = json.IndexOf("[]");
                notifPrepend = json.Substring(0, idx + 1);
                notifAppend  = json.Substring(idx + 1);
            }

            var stream = new CombiningStream();

            stream.Add(notifPrepend);
            if (jsonEventStreams.Length > 1)
            {
                var eventsStream = new CombiningStream(", ");
                foreach (var eventStream in jsonEventStreams)
                {
                    eventsStream.Add(eventStream);
                }
                stream.Add(eventsStream);
            }
            else if (jsonEventStreams.Length == 1)
            {
                stream.Add(jsonEventStreams [0]);
            }
            stream.Add(notifAppend);

            return(stream);
        }