コード例 #1
0
 /// <inheritdoc />
 public override void DecodeBinaryModeEventData(ReadOnlyMemory <byte> body, CloudEvent cloudEvent)
 {
     Validation.CheckNotNull(cloudEvent, nameof(cloudEvent));
     if (cloudEvent.DataContentType is string dataContentType && dataContentType.StartsWith("text/"))
     {
         Encoding encoding = MimeUtilities.GetEncoding(new ContentType(dataContentType));
         cloudEvent.Data = BinaryDataUtilities.GetString(body, encoding);
     }
コード例 #2
0
        public void EncodeBinaryModeEventData_TextType_String()
        {
            var cloudEvent = new CloudEvent().PopulateRequiredAttributes();

            cloudEvent.Data            = "some text";
            cloudEvent.DataContentType = "text/anything";
            var bytes = new JsonEventFormatter().EncodeBinaryModeEventData(cloudEvent);

            Assert.Equal("some text", BinaryDataUtilities.GetString(bytes, Encoding.UTF8));
        }
コード例 #3
0
        private static T ParseJsonImpl <T>(ReadOnlyMemory <byte> data)
        {
            string text       = BinaryDataUtilities.GetString(data, Encoding.UTF8);
            var    serializer = new JsonSerializer
            {
                DateParseHandling = DateParseHandling.None
            };

            return(serializer.Deserialize <T>(new JsonTextReader(new StringReader(text))) !);
        }
コード例 #4
0
        public void EncodeBinaryModeEventData_JsonDataType_JToken()
        {
            // This would definitely be an odd thing to do, admittedly...
            var cloudEvent = new CloudEvent().PopulateRequiredAttributes();

            cloudEvent.Data            = new JValue(100);
            cloudEvent.DataContentType = "application/json";
            var bytes = new JsonEventFormatter().EncodeBinaryModeEventData(cloudEvent);

            Assert.Equal("100", BinaryDataUtilities.GetString(bytes, Encoding.UTF8));
        }
コード例 #5
0
        public void EncodeBinaryModeEventData_NoContentType_ConvertsStringToJson()
        {
            var cloudEvent = new CloudEvent
            {
                Data = "some text"
            }.PopulateRequiredAttributes();

            // EncodeBinaryModeEventData doesn't actually populate the content type of the CloudEvent,
            // but treat the data as if we'd explicitly specified application/json.
            var    data = new JsonEventFormatter().EncodeBinaryModeEventData(cloudEvent);
            string text = BinaryDataUtilities.GetString(data, Encoding.UTF8);

            Assert.Equal("\"some text\"", text);
        }
コード例 #6
0
        public async Task ToCloudEvent_Invalid()
        {
            var cloudEvent   = new CloudEvent().PopulateRequiredAttributes();
            var formatter    = new JsonEventFormatter();
            var contentBytes = formatter.EncodeStructuredModeMessage(cloudEvent, out var contentType);
            // Remove the required 'id' attribute
            var obj = JObject.Parse(BinaryDataUtilities.GetString(contentBytes, Encoding.UTF8));

            obj.Remove("id");
            contentBytes = Encoding.UTF8.GetBytes(obj.ToString());

            await Assert.ThrowsAsync <ArgumentException>(() => CreateRequestMessage(contentBytes, contentType).ToCloudEventAsync(formatter));

            await Assert.ThrowsAsync <ArgumentException>(() => CreateResponseMessage(contentBytes, contentType).ToCloudEventAsync(formatter));
        }