public void AddUriPathExtensionMapping_MediaType_UpdatesMediaTypeMappingsCollection()
        {
            MediaTypeFormatter mockFormatter = new Mock <MediaTypeFormatter> {
                CallBase = true
            }.Object;

            mockFormatter.AddUriPathExtensionMapping("ext", "application/test");

            MediaTypeMapping        mediaTypeMapping        = Assert.Single(mockFormatter.MediaTypeMappings);
            UriPathExtensionMapping uriPathExtensionMapping = Assert.IsType <UriPathExtensionMapping>(mediaTypeMapping);

            Assert.Equal("ext", uriPathExtensionMapping.UriPathExtension);
            Assert.Equal("application/test", uriPathExtensionMapping.MediaType.MediaType);
        }
Esempio n. 2
0
        /// <summary>
        /// Match a request against the <see cref="MediaTypeMapping"/>s registered with the formatter.
        /// </summary>
        /// <param name="request">The request to match.</param>
        /// <param name="formatter">The formatter to match against.</param>
        /// <returns>A <see cref="MediaTypeFormatterMatch"/> indicating the quality of the match or null is no match.</returns>
        protected virtual MediaTypeFormatterMatch MatchMediaTypeMapping(
            HttpRequestMessage request,
            MediaTypeFormatter formatter
            )
        {
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }
            if (formatter == null)
            {
                throw Error.ArgumentNull("formatter");
            }

            List <MediaTypeMapping> mediaTypeMappings = formatter.MediaTypeMappingsInternal;

            for (int i = 0; i < mediaTypeMappings.Count; i++)
            {
                MediaTypeMapping mapping = mediaTypeMappings[i];
                double           quality;
                if (
                    mapping != null &&
                    (
                        (quality = mapping.TryMatchMediaType(request)) > FormattingUtilities.NoMatch
                    )
                    )
                {
                    return(new MediaTypeFormatterMatch(
                               formatter,
                               mapping.MediaType,
                               quality,
                               MediaTypeFormatterMatchRanking.MatchOnRequestWithMediaTypeMapping
                               ));
                }
            }

            return(null);
        }
 public CSVMediaTypeFormatter(MediaTypeMapping mediaTypeMapping)
     : this()
 {
     MediaTypeMappings.Add(mediaTypeMapping);
 }
 public ProtobufMediaTypeFormatter(MediaTypeMapping mediaTypeMapping)
     : this()
 {
     MediaTypeMappings.Add(mediaTypeMapping);
 }
 public CostComparisonCsvFormatter(MediaTypeMapping mediaTypeMapping)
     : this()
 {
     MediaTypeMappings.Add(mediaTypeMapping);
 }