Exemplo n.º 1
0
        public void ContentTypeGetEncoding_NoCharSet()
        {
            ContentType contentType = new ContentType("text/plain");
            Encoding    encoding    = MimeUtilities.GetEncoding(contentType);

            Assert.Equal(Encoding.UTF8, encoding);
        }
Exemplo n.º 2
0
        public void ContentTypeGetEncoding_NoContentType()
        {
            ContentType?contentType = null;
            Encoding    encoding    = MimeUtilities.GetEncoding(contentType);

            Assert.Equal(Encoding.UTF8, encoding);
        }
Exemplo n.º 3
0
        public void ContentTypeGetEncoding(string charSet)
        {
            var      contentType = new ContentType($"text/plain; charset={charSet}");
            Encoding encoding    = MimeUtilities.GetEncoding(contentType);

            Assert.Equal(charSet, encoding.WebName);
        }
 /// <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);
     }
Exemplo n.º 5
0
        public override IReadOnlyList <CloudEvent> DecodeBatchModeMessage(Stream data, ContentType contentType, IEnumerable <CloudEventAttribute> extensionAttributes)
        {
            Validation.CheckNotNull(data, nameof(data));

            var jsonReader = CreateJsonReader(data, MimeUtilities.GetEncoding(contentType));
            var array      = JArray.Load(jsonReader);

            return(DecodeJArray(array, extensionAttributes, nameof(data)));
        }
Exemplo n.º 6
0
        public override CloudEvent DecodeStructuredModeMessage(Stream data, ContentType contentType, IEnumerable <CloudEventAttribute> extensionAttributes)
        {
            Validation.CheckNotNull(data, nameof(data));

            var jsonReader = CreateJsonReader(data, MimeUtilities.GetEncoding(contentType));
            var jObject    = JObject.Load(jsonReader);

            return(DecodeJObject(jObject, extensionAttributes));
        }
Exemplo n.º 7
0
        public override async Task <CloudEvent> DecodeStructuredModeMessageAsync(Stream data, ContentType contentType, IEnumerable <CloudEventAttribute> extensionAttributes)
        {
            Validation.CheckNotNull(data, nameof(data));

            var jsonReader = CreateJsonReader(data, MimeUtilities.GetEncoding(contentType));
            var jObject    = await JObject.LoadAsync(jsonReader).ConfigureAwait(false);

            return(DecodeJObject(jObject, extensionAttributes));
        }
Exemplo n.º 8
0
        /// <inheritdoc />
        public override async Task <IReadOnlyList <CloudEvent> > DecodeBatchModeMessageAsync(Stream body, ContentType contentType, IEnumerable <CloudEventAttribute> extensionAttributes)
        {
            Validation.CheckNotNull(body, nameof(body));

            var jsonReader = CreateJsonReader(body, MimeUtilities.GetEncoding(contentType));
            var array      = await JArray.LoadAsync(jsonReader).ConfigureAwait(false);

            return(DecodeJArray(array, extensionAttributes, nameof(body)));
        }
Exemplo n.º 9
0
        private async Task <JsonDocument> ReadDocumentAsync(Stream data, ContentType contentType, bool async)
        {
            var encoding = MimeUtilities.GetEncoding(contentType);

            if (encoding is UTF8Encoding)
            {
                return(async
                    ? await JsonDocument.ParseAsync(data, DocumentOptions).ConfigureAwait(false)
                    : JsonDocument.Parse(data, DocumentOptions));
            }
            else
            {
                using var reader = new StreamReader(data, encoding);
                var json = async
                    ? await reader.ReadToEndAsync().ConfigureAwait(false)
                    : reader.ReadToEnd();

                return(JsonDocument.Parse(json, DocumentOptions));
            }
        }