public void DecodeBatchMode_Empty() { var batchProto = new V1.CloudEventBatch(); byte[] bytes = batchProto.ToByteArray(); var stream = new MemoryStream(bytes); var formatter = new ProtobufEventFormatter(); var cloudEvents = formatter.DecodeBatchModeMessage(stream, s_protobufCloudEventBatchContentType, null); Assert.Empty(cloudEvents); }
public void DecodeBatchMode_Multiple() { var batchProto = 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" } } }; byte[] bytes = batchProto.ToByteArray(); var stream = new MemoryStream(bytes); var formatter = new ProtobufEventFormatter(); var cloudEvents = formatter.DecodeBatchModeMessage(stream, s_protobufCloudEventBatchContentType, null); Assert.Equal(2, cloudEvents.Count); var event1 = cloudEvents[0]; Assert.Equal("type1", event1.Type); Assert.Equal("event1", event1.Id); Assert.Equal(new Uri("//event-source1", UriKind.RelativeOrAbsolute), event1.Source); Assert.Equal("simple text", event1.Data); Assert.Equal("text/plain", event1.DataContentType); var event2 = cloudEvents[1]; Assert.Equal("type2", event2.Type); Assert.Equal("event2", event2.Id); Assert.Equal(new Uri("//event-source2", UriKind.RelativeOrAbsolute), event2.Source); Assert.Null(event2.Data); Assert.Null(event2.DataContentType); }
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); }
public void DecodeBatchMode_Minimal() { var batchProto = new V1.CloudEventBatch { Events = { CreateMinimalCloudEventProto() } }; byte[] bytes = batchProto.ToByteArray(); var stream = new MemoryStream(bytes); var formatter = new ProtobufEventFormatter(); var cloudEvents = formatter.DecodeBatchModeMessage(stream, s_protobufCloudEventBatchContentType, null); var cloudEvent = Assert.Single(cloudEvents); Assert.Equal("test-type", cloudEvent.Type); Assert.Equal("test-id", cloudEvent.Id); Assert.Equal(SampleUri, cloudEvent.Source); }