/// <summary>
        /// Convenience method to serialize a JArray to bytes, then
        /// decode it as a structured event with the default (System.Text.Json) JsonEventFormatter and no extension attributes.
        /// </summary>
        private static IReadOnlyList <CloudEvent> DecodeBatchModeMessage(Newtonsoft.Json.Linq.JArray array)
        {
            byte[] bytes     = Encoding.UTF8.GetBytes(array.ToString());
            var    formatter = new JsonEventFormatter();

            return(formatter.DecodeBatchModeMessage(bytes, s_jsonCloudEventBatchContentType, null));
        }
        public void DecodeBatchMode_NotArray()
        {
            var formatter = new JsonEventFormatter();
            var data      = Encoding.UTF8.GetBytes(CreateMinimalValidJObject().ToString());

            Assert.Throws <ArgumentException>(() => formatter.DecodeBatchModeMessage(data, s_jsonCloudEventBatchContentType, extensionAttributes: null));
        }
示例#3
0
        public async Task ToHttpContent_Batch()
        {
            var batch = CreateSampleBatch();

            var formatter = new JsonEventFormatter();
            var content   = batch.ToHttpContent(formatter);

            var bytes = await content.ReadAsByteArrayAsync();

            var parsedBatch = formatter.DecodeBatchModeMessage(bytes, MimeUtilities.ToContentType(content.Headers.ContentType), extensionAttributes: null);

            AssertBatchesEqual(batch, parsedBatch);
        }
        public void DecodeBatchMode_Minimal_WithStream()
        {
            var array = new JArray {
                CreateMinimalValidJObject()
            };

            byte[] bytes       = Encoding.UTF8.GetBytes(array.ToString());
            var    formatter   = new JsonEventFormatter();
            var    cloudEvents = formatter.DecodeBatchModeMessage(new MemoryStream(bytes), s_jsonCloudEventBatchContentType, null);
            var    cloudEvent  = Assert.Single(cloudEvents);

            Assert.Equal("event-type", cloudEvent.Type);
            Assert.Equal("event-id", cloudEvent.Id);
            Assert.Equal(new Uri("//event-source", UriKind.RelativeOrAbsolute), cloudEvent.Source);
        }