Пример #1
0
        public void EncodeStructuredModeMessage_V1Attributes()
        {
            var cloudEvent = new CloudEvent(CloudEventsSpecVersion.V1_0)
            {
                Data            = "text", // Just so that it's reasonable to have a DataContentType
                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"
            };

            byte[] encoded = new JsonEventFormatter().EncodeStructuredModeMessage(cloudEvent, out var contentType);
            Assert.Equal("application/cloudevents+json; charset=utf-8", contentType.ToString());
            JsonElement obj      = ParseJson(encoded);
            var         asserter = new JsonElementAsserter
            {
                { "data", JsonValueKind.String, "text" },
                { "datacontenttype", JsonValueKind.String, "text/plain" },
                { "dataschema", JsonValueKind.String, "https://data-schema" },
                { "id", JsonValueKind.String, "event-id" },
                { "source", JsonValueKind.String, "https://event-source" },
                { "specversion", JsonValueKind.String, "1.0" },
                { "subject", JsonValueKind.String, "event-subject" },
                { "time", JsonValueKind.String, "2021-02-19T12:34:56.789+01:00" },
                { "type", JsonValueKind.String, "event-type" },
            };

            asserter.AssertProperties(obj, assertCount: true);
        }
Пример #2
0
        public void EncodeStructuredModeMessage_AllAttributeTypes()
        {
            var cloudEvent = new CloudEvent(AllTypesExtensions)
            {
                ["binary"]       = SampleBinaryData,
                ["boolean"]      = true,
                ["integer"]      = 10,
                ["string"]       = "text",
                ["timestamp"]    = SampleTimestamp,
                ["uri"]          = SampleUri,
                ["urireference"] = SampleUriReference
            };

            // We're not going to check these.
            cloudEvent.PopulateRequiredAttributes();

            JsonElement element  = EncodeAndParseStructured(cloudEvent);
            var         asserter = new JsonElementAsserter
            {
                { "binary", JsonValueKind.String, SampleBinaryDataBase64 },
                { "boolean", JsonValueKind.True, true },
                { "integer", JsonValueKind.Number, 10 },
                { "string", JsonValueKind.String, "text" },
                { "timestamp", JsonValueKind.String, SampleTimestampText },
                { "uri", JsonValueKind.String, SampleUriText },
                { "urireference", JsonValueKind.String, SampleUriReferenceText },
            };

            asserter.AssertProperties(element, assertCount: false);
        }
Пример #3
0
        public void DecodeBinaryModeEventData_Json(string charset)
        {
            var encoding = Encoding.GetEncoding(charset);
            var bytes    = encoding.GetBytes(new JObject {
                ["test"] = "some text"
            }.ToString());
            var data     = DecodeBinaryModeEventData(bytes, $"application/json; charset={charset}");
            var element  = Assert.IsType <JsonElement>(data);
            var asserter = new JsonElementAsserter
            {
                { "test", JsonValueKind.String, "some text" }
            };

            asserter.AssertProperties(element, assertCount: true);
        }
Пример #4
0
        public void EncodeBinaryModeEventData_JsonDataType_ObjectSerialization()
        {
            var cloudEvent = new CloudEvent().PopulateRequiredAttributes();

            cloudEvent.Data            = new { Text = "simple text" };
            cloudEvent.DataContentType = "application/json";
            byte[]      bytes    = new JsonEventFormatter().EncodeBinaryModeEventData(cloudEvent);
            JsonElement data     = ParseJson(bytes);
            var         asserter = new JsonElementAsserter
            {
                { "Text", JsonValueKind.String, "simple text" }
            };

            asserter.AssertProperties(data, assertCount: true);
        }
Пример #5
0
        public void EncodeStructuredModeMessage_JsonDataType_ObjectSerialization()
        {
            var cloudEvent = new CloudEvent().PopulateRequiredAttributes();

            cloudEvent.Data            = new { Text = "simple text" };
            cloudEvent.DataContentType = "application/json";
            JsonElement element      = EncodeAndParseStructured(cloudEvent);
            JsonElement dataProperty = element.GetProperty("data");
            var         asserter     = new JsonElementAsserter
            {
                { "Text", JsonValueKind.String, "simple text" }
            };

            asserter.AssertProperties(dataProperty, assertCount: true);
        }
Пример #6
0
        public void EncodeBatchModeMessage_TwoEvents()
        {
            var event1 = new CloudEvent().PopulateRequiredAttributes();

            event1.Id              = "event1";
            event1.Data            = "simple text";
            event1.DataContentType = "text/plain";

            var event2 = new CloudEvent().PopulateRequiredAttributes();

            event2.Id = "event2";

            var cloudEvents = new[] { event1, event2 };
            var formatter   = new JsonEventFormatter();

            byte[] bytes = formatter.EncodeBatchModeMessage(cloudEvents, out var contentType);
            Assert.Equal("application/cloudevents-batch+json; charset=utf-8", contentType.ToString());
            var array = ParseJson(bytes).EnumerateArray().ToList();

            Assert.Equal(2, array.Count);

            var asserter1 = new JsonElementAsserter
            {
                { "specversion", JsonValueKind.String, "1.0" },
                { "id", JsonValueKind.String, event1.Id },
                { "type", JsonValueKind.String, event1.Type },
                { "source", JsonValueKind.String, "//test" },
                { "data", JsonValueKind.String, "simple text" },
                { "datacontenttype", JsonValueKind.String, event1.DataContentType }
            };

            asserter1.AssertProperties(array[0], assertCount: true);

            var asserter2 = new JsonElementAsserter
            {
                { "specversion", JsonValueKind.String, "1.0" },
                { "id", JsonValueKind.String, event2.Id },
                { "type", JsonValueKind.String, event2.Type },
                { "source", JsonValueKind.String, "//test" },
            };

            asserter2.AssertProperties(array[1], assertCount: true);
        }
Пример #7
0
        public void EncodeBinaryModeEventData_JsonDataType_CustomSerializer()
        {
            var cloudEvent = new CloudEvent().PopulateRequiredAttributes();

            cloudEvent.Data            = new { DateValue = new DateTime(2021, 2, 19, 12, 49, 34, DateTimeKind.Utc) };
            cloudEvent.DataContentType = "application/json";

            var serializerOptions = new JsonSerializerOptions
            {
                Converters = { new YearMonthDayConverter() },
            };
            var formatter = new JsonEventFormatter(serializerOptions, default);

            byte[]      bytes    = formatter.EncodeBinaryModeEventData(cloudEvent);
            JsonElement data     = ParseJson(bytes);
            var         asserter = new JsonElementAsserter
            {
                { "DateValue", JsonValueKind.String, "2021-02-19" }
            };

            asserter.AssertProperties(data, assertCount: true);
        }
Пример #8
0
        public void EncodeStructuredModeMessage_JsonDataType_CustomSerializerOptions()
        {
            var cloudEvent = new CloudEvent().PopulateRequiredAttributes();

            cloudEvent.Data            = new { DateValue = new DateTime(2021, 2, 19, 12, 49, 34, DateTimeKind.Utc) };
            cloudEvent.DataContentType = "application/json";

            var serializerOptions = new JsonSerializerOptions
            {
                Converters = { new YearMonthDayConverter() },
            };
            var formatter = new JsonEventFormatter(serializerOptions, default);

            byte[]      encoded      = formatter.EncodeStructuredModeMessage(cloudEvent, out _);
            JsonElement element      = ParseJson(encoded);
            JsonElement dataProperty = element.GetProperty("data");
            var         asserter     = new JsonElementAsserter
            {
                { "DateValue", JsonValueKind.String, "2021-02-19" }
            };

            asserter.AssertProperties(dataProperty, assertCount: true);
        }