Parse() public static method

public static Parse ( string input ) : MediaTypeHeaderValue
input string
return MediaTypeHeaderValue
Example #1
0
        public static HttpContentType CreateFromBase64(HttpContentHeaderData headerData, string base64)
        {
            var content = CreateBase64(base64);

            content.Headers.ContentType = HttpMediaTypeHeaderValue.Parse(headerData.ContentType);
            return(content);
        }
        private static MediaTypeHeaderValue GetContentTypeFromQueryString(IEdmModel model, Type type, string dollarFormat)
        {
            var formatters = CreateOutputFormatters(model);
            var path       = new ODataPath();
            var request    = string.IsNullOrEmpty(dollarFormat)
                ? RequestFactory.CreateFromModel(model, "http://any", "OData", path)
                : RequestFactory.CreateFromModel(model, "http://any/?$format=" + dollarFormat, "OData", path);

            var context = new OutputFormatterWriteContext(
                request.HttpContext,
                new TestHttpResponseStreamWriterFactory().CreateWriter,
                type,
                new MemoryStream());

            foreach (var formatter in formatters)
            {
                context.ContentType = new StringSegment();
                context.ContentTypeIsServerDefined = false;

                if (formatter.CanWriteResult(context))
                {
                    MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse(context.ContentType.ToString());

                    // We don't care what the charset is for these tests.
                    if (mediaType.Parameters.Where(p => p.Name == "charset").Any())
                    {
                        mediaType.Parameters.Remove(mediaType.Parameters.Single(p => p.Name == "charset"));
                    }

                    return(mediaType);
                }
            }

            return(null);
        }
        public void TestCreate_DollarFormat_Error(string dollarFormatValue, string expectedMediaType)
        {
            // Arrange
            IEdmModel model     = CreateModelWithEntity <SampleType>();
            Type      errorType = typeof(ODataError);

            // Act
            MediaTypeHeaderValue mediaType = GetContentTypeFromQueryString(model, errorType, dollarFormatValue);

            // Assert
            Assert.Equal(MediaTypeHeaderValue.Parse(expectedMediaType), mediaType);
        }
        public void TestCreate_DollarFormat_Collection(string dollarFormatValue, string expectedMediaType)
        {
            // Arrange
            IEdmModel model          = CreateModelWithEntity <SampleType>();
            Type      collectionType = typeof(IEnumerable <int>);

            // Act
            MediaTypeHeaderValue mediaType = GetContentTypeFromQueryString(model, collectionType, dollarFormatValue);

            // Assert
            Assert.Equal(MediaTypeHeaderValue.Parse(expectedMediaType), mediaType);
        }
        public void TestCreate_MetadataDocument_DefaultContentType()
        {
            // Arrange
            IEdmModel model = CreateModel();
            Type      serviceDocumentType = typeof(IEdmModel);

            // Act
            MediaTypeHeaderValue mediaType = GetDefaultContentType(model, serviceDocumentType);

            // Assert
            Assert.Equal(MediaTypeHeaderValue.Parse("application/xml"), mediaType);
        }
        public void TestCreate_ServiceDocument_DefaultContentType()
        {
            // Arrange
            IEdmModel model = CreateModel();
            Type      serviceDocumentType = typeof(ODataServiceDocument);

            // Act
            MediaTypeHeaderValue mediaType = GetDefaultContentType(model, serviceDocumentType);

            // Assert
            Assert.Equal(MediaTypeHeaderValue.Parse("application/json;odata.metadata=minimal;odata.streaming=true"), mediaType);
        }
        public void TestCreate_Property_DefaultContentType()
        {
            // Arrange
            IEdmModel model        = CreateModelWithEntity <SampleType>();
            Type      propertyType = typeof(int);

            // Act
            MediaTypeHeaderValue mediaType = GetDefaultContentType(model, propertyType);

            // Assert
            Assert.Equal(MediaTypeHeaderValue.Parse("application/json;odata.metadata=minimal;odata.streaming=true"), mediaType);
        }
Example #8
0
        public void TestCreate_MetadataDocument_DollarFormat(string dollarFormatValue, string expectedMediaType)
        {
            // Arrange
            IEdmModel model = CreateModel();
            Type      metadataDocumentType = typeof(IEdmModel);

            // Act
            MediaTypeHeaderValue mediaType = GetContentTypeFromQueryString(model, metadataDocumentType, dollarFormatValue);

            // Assert
            Assert.Equal(MediaTypeHeaderValue.Parse(expectedMediaType), mediaType);
        }
        public static HttpContentType CreateFromBody(HttpContentHeaderData headerData, string body)
        {
            if (headerData.ContentEncoding == "gzip")
            {
                var content = CreateGzip(body);
                content.Headers.ContentType = HttpMediaTypeHeaderValue.Parse(headerData.ContentType);
#if WINDOWS_UWP
                content.Headers.ContentEncoding.ParseAdd(headerData.ContentEncoding);
#else
                content.Headers.ContentEncoding.Add(headerData.ContentEncoding);
#endif
                return(content);
            }
            else
            {
                var content = CreateString(body);
                content.Headers.ContentType = HttpMediaTypeHeaderValue.Parse(headerData.ContentType);
                return(content);
            }
        }
 private static IEnumerable <MediaTypeHeaderValue> GetMediaTypes(string[] mediaTypes)
 {
     return(mediaTypes.Select(m => MediaTypeHeaderValue.Parse(m)));
 }