public void ConvertFromProto_V1Attributes() { var proto = new V1.CloudEvent { SpecVersion = "1.0", Type = "test-type", Id = "test-id", TextData = "text", Source = "//event-source", Attributes = { { "datacontenttype", StringAttribute("text/plain") }, { "dataschema", UriAttribute("https://data-schema") }, { "subject", StringAttribute("event-subject") }, { "time", TimestampAttribute(SampleTimestamp) } } }; var cloudEvent = new ProtobufEventFormatter().ConvertFromProto(proto, null); Assert.Equal(CloudEventsSpecVersion.V1_0, cloudEvent.SpecVersion); Assert.Equal("test-type", cloudEvent.Type); Assert.Equal("test-id", cloudEvent.Id); Assert.Equal("text/plain", cloudEvent.DataContentType); Assert.Equal(new Uri("https://data-schema"), cloudEvent.DataSchema); Assert.Equal("event-subject", cloudEvent.Subject); Assert.Equal(new Uri("//event-source", UriKind.RelativeOrAbsolute), cloudEvent.Source); // The protobuf timestamp loses the offset information, but is still the correct instant. AssertTimestampsEqual(SampleTimestamp.ToUniversalTime(), cloudEvent.Time); }
public void ConvertToProto_V1Attributes() { var cloudEvent = new CloudEvent(CloudEventsSpecVersion.V1_0) { Data = "text", DataContentType = "text/plain", DataSchema = new Uri("https://data-schema"), Id = "event-id", Source = new Uri("https://event-source"), Subject = "event-subject", Time = new DateTimeOffset(2021, 2, 19, 12, 34, 56, 789, TimeSpan.FromHours(1)), Type = "event-type" }; var actualProto = new ProtobufEventFormatter().ConvertToProto(cloudEvent); var expectedProto = new V1.CloudEvent { SpecVersion = "1.0", Id = "event-id", Source = "https://event-source", Type = "event-type", Attributes = { { "datacontenttype", StringAttribute("text/plain") }, { "dataschema", UriAttribute("https://data-schema") }, { "subject", StringAttribute("event-subject") }, // Deliberately not reusing cloudEvent.Time: this demonstrates that only the instant in time // is relevant, not the UTC offset. { "time", TimestampAttribute(new DateTimeOffset(2021, 2, 19, 11, 34, 56, 789, TimeSpan.Zero))} }, TextData = "text" }; Assert.Equal(expectedProto, actualProto); }
public void EncodeStructuredModeMessage_Minimal() { var cloudEvent = new CloudEvent(CloudEventsSpecVersion.V1_0) { Id = "event-id", Source = new Uri("https://event-source"), Type = "event-type", }; var encoded = new ProtobufEventFormatter().EncodeStructuredModeMessage(cloudEvent, out var contentType); Assert.Equal("application/cloudevents+protobuf; charset=utf-8", contentType.ToString()); var actualProto = V1.CloudEvent.Parser.ParseFrom(encoded.ToArray()); var expectedProto = new V1.CloudEvent { SpecVersion = "1.0", Id = "event-id", Source = "https://event-source", Type = "event-type" }; Assert.Equal(expectedProto, actualProto); }