Beispiel #1
0
 private static HttpResponseMessage CreateResponseMessage(byte[] content, ContentType contentType) =>
 new HttpResponseMessage
 {
     Content = new ByteArrayContent(content)
     {
         Headers = { ContentType = MimeUtilities.ToMediaTypeHeaderValue(contentType) }
     }
 };
Beispiel #2
0
 internal static HttpResponseMessage CreateResponseMessage(ReadOnlyMemory <byte> content, ContentType contentType) =>
 new HttpResponseMessage
 {
     Content = new ByteArrayContent(content.ToArray())
     {
         Headers = { ContentType = MimeUtilities.ToMediaTypeHeaderValue(contentType) }
     }
 };
Beispiel #3
0
        /// <summary>
        /// Converts a CloudEvent to <see cref="HttpContent"/>.
        /// </summary>
        /// <param name="cloudEvent">The CloudEvent to convert. Must not be null, and must be a valid CloudEvent.</param>
        /// <param name="contentMode">Content mode. Structured or binary.</param>
        /// <param name="formatter">The formatter to use within the conversion. Must not be null.</param>
        public static HttpContent ToHttpContent(this CloudEvent cloudEvent, ContentMode contentMode, CloudEventFormatter formatter)
        {
            Validation.CheckCloudEventArgument(cloudEvent, nameof(cloudEvent));
            Validation.CheckNotNull(formatter, nameof(formatter));

            ReadOnlyMemory <byte> content;
            // The content type to include in the ContentType header - may be the data content type, or the formatter's content type.
            ContentType?contentType;

            switch (contentMode)
            {
            case ContentMode.Structured:
                content = formatter.EncodeStructuredModeMessage(cloudEvent, out contentType);
                break;

            case ContentMode.Binary:
                content     = formatter.EncodeBinaryModeEventData(cloudEvent);
                contentType = MimeUtilities.CreateContentTypeOrNull(formatter.GetOrInferDataContentType(cloudEvent));
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(contentMode), $"Unsupported content mode: {contentMode}");
            }
            ByteArrayContent ret = ToByteArrayContent(content);

            if (contentType is object)
            {
                ret.Headers.ContentType = MimeUtilities.ToMediaTypeHeaderValue(contentType);
            }
            else if (content.Length != 0)
            {
                throw new ArgumentException(Strings.ErrorContentTypeUnspecified, nameof(cloudEvent));
            }

            // Map headers in either mode.
            // Including the headers in structured mode is optional in the spec (as they're already within the body) but
            // can be useful.
            ret.Headers.Add(HttpUtilities.SpecVersionHttpHeader, HttpUtilities.EncodeHeaderValue(cloudEvent.SpecVersion.VersionId));
            foreach (var attributeAndValue in cloudEvent.GetPopulatedAttributes())
            {
                CloudEventAttribute attribute = attributeAndValue.Key;
                string headerName             = HttpUtilities.HttpHeaderPrefix + attribute.Name;
                object value = attributeAndValue.Value;

                // Skip the data content type attribute in binary mode, because it's already in the content type header.
                if (attribute == cloudEvent.SpecVersion.DataContentTypeAttribute && contentMode == ContentMode.Binary)
                {
                    continue;
                }
                else
                {
                    string headerValue = HttpUtilities.EncodeHeaderValue(attribute.Format(value));
                    ret.Headers.Add(headerName, headerValue);
                }
            }
            return(ret);
        }
Beispiel #4
0
        /// <summary>
        /// Converts a CloudEvent batch to <see cref="HttpContent"/>.
        /// </summary>
        /// <param name="cloudEvents">The CloudEvent batch to convert. Must not be null, and every element must be non-null reference to a valid CloudEvent.</param>
        /// <param name="formatter">The formatter to use within the conversion. Must not be null.</param>
        public static HttpContent ToHttpContent(this IReadOnlyList <CloudEvent> cloudEvents, CloudEventFormatter formatter)
        {
            Validation.CheckCloudEventBatchArgument(cloudEvents, nameof(cloudEvents));
            Validation.CheckNotNull(formatter, nameof(formatter));

            // TODO: Validate that all events in the batch have the same version?
            // See https://github.com/cloudevents/spec/issues/807

            ReadOnlyMemory <byte> content = formatter.EncodeBatchModeMessage(cloudEvents, out var contentType);

            // Note: we don't populate any other headers for batch mode.
            var ret = ToByteArrayContent(content);

            ret.Headers.ContentType = MimeUtilities.ToMediaTypeHeaderValue(contentType);
            return(ret);
        }
Beispiel #5
0
        public void ContentTypeConversions(string text)
        {
            var originalContentType = new ContentType(text);
            var header = MimeUtilities.ToMediaTypeHeaderValue(originalContentType);

            AssertEqualParts(text, header !.ToString());
            var convertedContentType = MimeUtilities.ToContentType(header);

            AssertEqualParts(originalContentType.ToString(), convertedContentType !.ToString());

            // Conversions can end up reordering the parameters. In reality we're only
            // likely to end up with a media type and charset, but our tests use more parameters.
            // This just makes them deterministic.
            void AssertEqualParts(string expected, string actual)
            {
                expected = string.Join(";", expected.Split(";").OrderBy(x => x));
                actual   = string.Join(";", actual.Split(";").OrderBy(x => x));
                Assert.Equal(expected, actual);
            }
        }
Beispiel #6
0
 public void ContentTypeConversions_Null()
 {
     Assert.Null(MimeUtilities.ToMediaTypeHeaderValue(default(ContentType)));
     Assert.Null(MimeUtilities.ToContentType(default(MediaTypeHeaderValue)));
 }