Exemplo n.º 1
0
        public void SelectResponseMediaTypeMatchesWithMediaTypeMapping(MediaTypeWithQualityHeaderValue mediaRangeWithQuality)
        {
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter()
            {
                CallBase = true
            };
            MediaTypeHeaderValue mediaRangeWithoutQuality = new MediaTypeHeaderValue(mediaRangeWithQuality.MediaType);
            MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("application/xml");
            MediaRangeMapping    mapping   = new MediaRangeMapping(mediaRangeWithoutQuality, mediaType);

            formatter.MediaTypeMappings.Add(mapping);

            HttpRequestMessage request = new HttpRequestMessage();

            request.Headers.Accept.Add(mediaRangeWithQuality);
            ResponseMediaTypeMatch match = formatter.SelectResponseMediaType(typeof(string), request);

            Assert.NotNull(match);
            Assert.Equal(ResponseFormatterSelectionResult.MatchOnRequestWithMediaTypeMapping, match.ResponseFormatterSelectionResult);
            double quality = mediaRangeWithQuality.Quality.Value;

            Assert.Equal(quality, match.MediaTypeMatch.Quality);
            Assert.NotNull(match.MediaTypeMatch.MediaType);
            Assert.Equal(mediaType.MediaType, match.MediaTypeMatch.MediaType.MediaType);
        }
Exemplo n.º 2
0
        public void SelectResponseMediaTypeMatchesRequestContentType(MediaTypeHeaderValue mediaType)
        {
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter()
            {
                CallBase = true
            };

            formatter.SupportedMediaTypes.Add(mediaType);
            HttpRequestMessage request = new HttpRequestMessage()
            {
                Content = new StringContent("fred")
            };

            request.Content.Headers.ContentType = mediaType;
            HttpResponseMessage response = new HttpResponseMessage()
            {
                RequestMessage = request
            };
            ResponseMediaTypeMatch match = formatter.SelectResponseMediaType(typeof(string), request);

            Assert.NotNull(match);
            Assert.Equal(ResponseFormatterSelectionResult.MatchOnRequestContentType, match.ResponseFormatterSelectionResult);
            Assert.NotNull(match.MediaTypeMatch.MediaType);
            Assert.Equal(mediaType.MediaType, match.MediaTypeMatch.MediaType.MediaType);
        }
Exemplo n.º 3
0
        public void SelectResponseMediaTypeMatchesType()
        {
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter()
            {
                CallBase = true
            };
            HttpRequestMessage     request = new HttpRequestMessage();
            ResponseMediaTypeMatch match   = formatter.SelectResponseMediaType(typeof(string), request);

            Assert.NotNull(match);
            Assert.Equal(ResponseFormatterSelectionResult.MatchOnCanWriteType, match.ResponseFormatterSelectionResult);
            Assert.Null(match.MediaTypeMatch.MediaType);
        }
Exemplo n.º 4
0
        public void SelectResponseMediaTypeMatchesAcceptHeaderToSupportedMediaTypes(MediaTypeWithQualityHeaderValue mediaTypeWithQuality)
        {
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter()
            {
                CallBase = true
            };
            MediaTypeHeaderValue mediaTypeWithoutQuality = new MediaTypeHeaderValue(mediaTypeWithQuality.MediaType);

            formatter.SupportedMediaTypes.Add(mediaTypeWithoutQuality);

            HttpRequestMessage request = new HttpRequestMessage();

            request.Headers.Accept.Add(mediaTypeWithQuality);
            ResponseMediaTypeMatch match = formatter.SelectResponseMediaType(typeof(string), request);

            Assert.NotNull(match);
            Assert.Equal(ResponseFormatterSelectionResult.MatchOnRequestAcceptHeader, match.ResponseFormatterSelectionResult);
            double quality = mediaTypeWithQuality.Quality.Value;

            Assert.Equal(quality, match.MediaTypeMatch.Quality);
            Assert.NotNull(match.MediaTypeMatch.MediaType);
            Assert.Equal(mediaTypeWithoutQuality.MediaType, match.MediaTypeMatch.MediaType.MediaType);
        }
Exemplo n.º 5
0
        private static MediaTypeFormatter RunNegotiation(Type type, HttpRequestMessage request, IEnumerable <MediaTypeFormatter> formatters, out MediaTypeHeaderValue mediaType)
        {
            // Asking to serialize a response.   This is the nominal code path.
            // We ask all formatters for their best kind of match, and then we
            // choose the best among those.
            MediaTypeFormatter     formatterMatchOnType = null;
            ResponseMediaTypeMatch mediaTypeMatchOnType = null;

            MediaTypeFormatter     formatterMatchOnAcceptHeader = null;
            ResponseMediaTypeMatch mediaTypeMatchOnAcceptHeader = null;

            MediaTypeFormatter     formatterMatchWithMapping = null;
            ResponseMediaTypeMatch mediaTypeMatchWithMapping = null;

            MediaTypeFormatter     formatterMatchOnRequestContentType = null;
            ResponseMediaTypeMatch mediaTypeMatchOnRequestContentType = null;

            foreach (MediaTypeFormatter formatter in formatters)
            {
                ResponseMediaTypeMatch match = formatter.SelectResponseMediaType(type, request);
                if (match == null)
                {
                    // Null signifies no match
                    continue;
                }

                ResponseFormatterSelectionResult matchResult = match.ResponseFormatterSelectionResult;
                switch (matchResult)
                {
                case ResponseFormatterSelectionResult.MatchOnCanWriteType:

                    // First match by type trumps all other type matches
                    if (formatterMatchOnType == null)
                    {
                        formatterMatchOnType = formatter;
                        mediaTypeMatchOnType = match;
                    }

                    break;

                case ResponseFormatterSelectionResult.MatchOnResponseContentType:

                    // Match on response content trumps all other choices
                    mediaType = match.MediaTypeMatch.MediaType;
                    return(formatter);

                case ResponseFormatterSelectionResult.MatchOnRequestAcceptHeader:

                    // Matches on accept headers must choose the highest quality match
                    double thisQuality = match.MediaTypeMatch.Quality;
                    if (formatterMatchOnAcceptHeader != null)
                    {
                        double bestQualitySeen = mediaTypeMatchOnAcceptHeader.MediaTypeMatch.Quality;
                        if (thisQuality <= bestQualitySeen)
                        {
                            continue;
                        }
                    }

                    formatterMatchOnAcceptHeader = formatter;
                    mediaTypeMatchOnAcceptHeader = match;

                    break;

                case ResponseFormatterSelectionResult.MatchOnRequestWithMediaTypeMapping:

                    // Matches on accept headers using mappings must choose the highest quality match
                    double thisMappingQuality = match.MediaTypeMatch.Quality;
                    if (mediaTypeMatchWithMapping != null)
                    {
                        double bestMappingQualitySeen = mediaTypeMatchWithMapping.MediaTypeMatch.Quality;
                        if (thisMappingQuality <= bestMappingQualitySeen)
                        {
                            continue;
                        }
                    }

                    formatterMatchWithMapping = formatter;
                    mediaTypeMatchWithMapping = match;

                    break;

                case ResponseFormatterSelectionResult.MatchOnRequestContentType:

                    // First match on request content type trumps other request content matches
                    if (formatterMatchOnRequestContentType == null)
                    {
                        formatterMatchOnRequestContentType = formatter;
                        mediaTypeMatchOnRequestContentType = match;
                    }

                    break;
                }
            }

            // If we received matches based on both supported media types and from media type mappings,
            // we want to give precedence to the media type mappings, but only if their quality is >= that of the supported media type.
            // We do this because media type mappings are the user's extensibility point and must take precedence over normal
            // supported media types in the case of a tie.   The 99% case is where both have quality 1.0.
            if (mediaTypeMatchWithMapping != null && mediaTypeMatchOnAcceptHeader != null)
            {
                if (mediaTypeMatchOnAcceptHeader.MediaTypeMatch.Quality > mediaTypeMatchWithMapping.MediaTypeMatch.Quality)
                {
                    formatterMatchWithMapping = null;
                }
            }

            // now select the formatter and media type
            // A MediaTypeMapping is highest precedence -- it is an extensibility point
            // allowing the user to override normal accept header matching
            if (formatterMatchWithMapping != null)
            {
                mediaType = mediaTypeMatchWithMapping.MediaTypeMatch.MediaType;
                return(formatterMatchWithMapping);
            }
            else if (formatterMatchOnAcceptHeader != null)
            {
                mediaType = mediaTypeMatchOnAcceptHeader.MediaTypeMatch.MediaType;
                return(formatterMatchOnAcceptHeader);
            }
            else if (formatterMatchOnRequestContentType != null)
            {
                mediaType = mediaTypeMatchOnRequestContentType.MediaTypeMatch.MediaType;
                return(formatterMatchOnRequestContentType);
            }
            else if (formatterMatchOnType != null)
            {
                mediaType = mediaTypeMatchOnType.MediaTypeMatch.MediaType;
                return(formatterMatchOnType);
            }

            mediaType = null;
            return(null);
        }