예제 #1
0
        public void EncodeBatchModeMessage_Invalid()
        {
            var formatter = new ProtobufEventFormatter();

            // Invalid CloudEvent
            Assert.Throws <ArgumentException>(() => formatter.EncodeBatchModeMessage(new[] { new CloudEvent() }, out _));
            // Null argument
            Assert.Throws <ArgumentNullException>(() => formatter.EncodeBatchModeMessage(null !, out _));
            // Null value within the argument. Arguably this should throw ArgumentException instead of
            // ArgumentNullException, but it's unlikely to cause confusion.
            Assert.Throws <ArgumentNullException>(() => formatter.EncodeBatchModeMessage(new CloudEvent[1], out _));
        }
예제 #2
0
        public void EncodeBatchModeMessage_Empty()
        {
            var formatter = new ProtobufEventFormatter();
            var bytes     = formatter.EncodeBatchModeMessage(new CloudEvent[0], out var contentType);

            Assert.Equal("application/cloudevents-batch+protobuf; charset=utf-8", contentType.ToString());
            var batch = V1.CloudEventBatch.Parser.ParseFrom(bytes.ToArray());

            Assert.Empty(batch.Events);
        }
예제 #3
0
        public void EncodeBatchModeMessage_TwoEvents()
        {
            var event1 = new CloudEvent
            {
                Id              = "event1",
                Type            = "type1",
                Source          = new Uri("//event-source1", UriKind.RelativeOrAbsolute),
                Data            = "simple text",
                DataContentType = "text/plain"
            };
            var event2 = new CloudEvent
            {
                Id     = "event2",
                Type   = "type2",
                Source = new Uri("//event-source2", UriKind.RelativeOrAbsolute),
            };

            var cloudEvents = new[] { event1, event2 };
            var formatter   = new ProtobufEventFormatter();
            var bytes       = formatter.EncodeBatchModeMessage(cloudEvents, out var contentType);

            Assert.Equal("application/cloudevents-batch+protobuf; charset=utf-8", contentType.ToString());
            var actualBatch   = V1.CloudEventBatch.Parser.ParseFrom(bytes.ToArray());
            var expectedBatch = new V1.CloudEventBatch
            {
                Events =
                {
                    new V1.CloudEvent
                    {
                        SpecVersion = "1.0",
                        Type        = "type1",
                        Id          = "event1",
                        Source      = "//event-source1",
                        TextData    = "simple text",
                        Attributes  = { { "datacontenttype", StringAttribute("text/plain") } }
                    },
                    new V1.CloudEvent
                    {
                        SpecVersion = "1.0",
                        Type        = "type2",
                        Id          = "event2",
                        Source      = "//event-source2"
                    }
                }
            };

            Assert.Equal(expectedBatch, actualBatch);
        }