Пример #1
0
        /// <summary>
        /// Determine how well each formatter matches by associating a <see cref="MediaTypeFormatterMatchRanking"/>
        /// value with the formatter. Then associate the quality of the match based on q-factors and other parameters.
        /// The result of this method is a collection of the matches found categorized and assigned a quality value.
        /// </summary>
        /// <param name="type">The type to be serialized.</param>
        /// <param name="request">The request.</param>
        /// <param name="formatters">The set of <see cref="MediaTypeFormatter"/> objects from which to choose.</param>
        /// <returns>A collection containing all the matches.</returns>
        protected virtual Collection <MediaTypeFormatterMatch> ComputeFormatterMatches(
            [NotNull] Type type,
            [NotNull] HttpRequestMessage request,
            [NotNull] IEnumerable <MediaTypeFormatter> formatters)
        {
            IEnumerable <MediaTypeWithQualityHeaderValue> sortedAcceptValues = null;

            // Go through each formatter to find how well it matches.
            var matches =
                new ListWrapperCollection <MediaTypeFormatterMatch>();
            var writingFormatters = GetWritingFormatters(formatters);

            for (var i = 0; i < writingFormatters.Length; i++)
            {
                var formatter = writingFormatters[i];

                // Check first that formatter can write the actual type
                if (!formatter.CanWriteType(type))
                {
                    // Formatter can't even write the type so no match at all
                    continue;
                }

                // Match against the accept header values.
                if (sortedAcceptValues == null)
                {
                    // Sort the Accept header values in descending order based on q-factor
                    sortedAcceptValues = SortMediaTypeWithQualityHeaderValuesByQFactor(request.Headers.Accept);
                }

                var match = MatchAcceptHeader(sortedAcceptValues, formatter);
                if (match != null)
                {
                    matches.Add(match);
                    continue;
                }

                // Match against request's media type if any
                if ((match = MatchRequestMediaType(request, formatter)) != null)
                {
                    matches.Add(match);
                    continue;
                }

                // Check whether we should match on type or stop the matching process.
                // The latter is used to generate 406 (Not Acceptable) status codes.
                var shouldMatchOnType = ShouldMatchOnType(sortedAcceptValues);

                // Match against the type of object we are writing out
                if (shouldMatchOnType && (match = MatchType(type, formatter)) != null)
                {
                    matches.Add(match);
                    continue;
                }
            }

            return(matches);
        }
Пример #2
0
        public void ListWrapperCollection_ItemsList_IsPassedInList()
        {
            // Arrange
            List<object> list = new List<object> { new object(), new object() };
            ListWrapperCollection<object> listWrapper = new ListWrapperCollection<object>(list);

            // Act & Assert
            Assert.Same(list, listWrapper.ItemsList);
        }
        public void AsList_ListWrapperCollection_ReturnsSameInstance()
        {
            List <object> list = new List <object> {
                new object(), new object()
            };
            ListWrapperCollection <object> listWrapper = new ListWrapperCollection <object>(list);

            List <object> listWrapperAsList = ((IEnumerable <object>)listWrapper).AsList();

            Assert.Same(list, listWrapperAsList);
        }
Пример #4
0
        public void ListWrapperCollection_ItemsList_IsPassedInList()
        {
            // Arrange
            List <object> list = new List <object> {
                new object(), new object()
            };
            ListWrapperCollection <object> listWrapper = new ListWrapperCollection <object>(list);

            // Act & Assert
            Assert.Same(list, listWrapper.ItemsList);
        }
Пример #5
0
        public void ListWrapperCollection_ItemsList_HasSameContents()
        {
            // Arrange
            ListWrapperCollection<object> listWrapper = new ListWrapperCollection<object>();

            // Act
            listWrapper.Add(new object());
            listWrapper.Add(new object());

            // Assert
            Assert.Equal(listWrapper, listWrapper.ItemsList);
        }
Пример #6
0
        public void ListWrapperCollection_ItemsList_HasSameContents()
        {
            // Arrange
            ListWrapperCollection <object> listWrapper = new ListWrapperCollection <object>();

            // Act
            listWrapper.Add(new object());
            listWrapper.Add(new object());

            // Assert
            Assert.Equal(listWrapper, listWrapper.ItemsList);
        }
Пример #7
0
        /// <summary>
        /// Return the enumerable as a List of T, copying if required. Optimized for common case where it is an List of T
        /// or a ListWrapperCollection of T. Avoid mutating the return value.
        /// </summary>
        public static List <T> AsList <T>(this IEnumerable <T> enumerable)
        {
            Contract.Assert(enumerable != null);

            List <T> list = enumerable as List <T>;

            if (list != null)
            {
                return(list);
            }
            ListWrapperCollection <T> listWrapper = enumerable as ListWrapperCollection <T>;

            list = listWrapper == null ? new List <T>(enumerable) : listWrapper.ItemsList;
            return(list);
        }
        /// <summary>
        /// Return the enumerable as a List of T, copying if required. Optimized for common case where it is an List of
        /// T or a ListWrapperCollection of T. Avoid mutating the return value.
        /// </summary>
        public static List <T> AsList <T>(this IEnumerable <T> enumerable)
        {
            Debug.Assert(enumerable != null);

            List <T> list = enumerable as List <T>;

            if (list != null)
            {
                return(list);
            }
            ListWrapperCollection <T> listWrapper = enumerable as ListWrapperCollection <T>;

            if (listWrapper != null)
            {
                return(listWrapper.ItemsList);
            }
            return(new List <T>(enumerable));
        }
Пример #9
0
        /// <summary>
        /// Determine how well each formatter matches by associating a <see cref="MediaTypeFormatterMatchRanking"/>
        /// value with the formatter. Then associate the quality of the match based on q-factors and other parameters.
        /// The result of this method is a collection of the matches found categorized and assigned a quality value.
        /// </summary>
        /// <param name="type">The type to be serialized.</param>
        /// <param name="request">The request.</param>
        /// <param name="formatters">The set of <see cref="MediaTypeFormatter"/> objects from which to choose.</param>
        /// <returns>A collection containing all the matches.</returns>
        protected virtual Collection<MediaTypeFormatterMatch> ComputeFormatterMatches(
            [NotNull] Type type,
            [NotNull] HttpRequestMessage request,
            [NotNull] IEnumerable<MediaTypeFormatter> formatters)
        {
            IEnumerable<MediaTypeWithQualityHeaderValue> sortedAcceptValues = null;

            // Go through each formatter to find how well it matches.
            var matches =
                new ListWrapperCollection<MediaTypeFormatterMatch>();
            var writingFormatters = GetWritingFormatters(formatters);
            for (var i = 0; i < writingFormatters.Length; i++)
            {
                var formatter = writingFormatters[i];

                // Check first that formatter can write the actual type
                if (!formatter.CanWriteType(type))
                {
                    // Formatter can't even write the type so no match at all
                    continue;
                }

                // Match against the accept header values.
                if (sortedAcceptValues == null)
                {
                    // Sort the Accept header values in descending order based on q-factor
                    sortedAcceptValues = SortMediaTypeWithQualityHeaderValuesByQFactor(request.Headers.Accept);
                }

                var match = MatchAcceptHeader(sortedAcceptValues, formatter);
                if (match != null)
                {
                    matches.Add(match);
                    continue;
                }

                // Match against request's media type if any
                if ((match = MatchRequestMediaType(request, formatter)) != null)
                {
                    matches.Add(match);
                    continue;
                }

                // Check whether we should match on type or stop the matching process.
                // The latter is used to generate 406 (Not Acceptable) status codes.
                var shouldMatchOnType = ShouldMatchOnType(sortedAcceptValues);

                // Match against the type of object we are writing out
                if (shouldMatchOnType && (match = MatchType(type, formatter)) != null)
                {
                    matches.Add(match);
                    continue;
                }
            }

            return matches;
        }
        /// <summary>
        /// Determine how well each formatter matches by associating a <see cref="MediaTypeFormatterMatchRanking"/> value
        /// with the formatter. Then associate the quality of the match based on q-factors and other parameters. The result of this
        /// method is a collection of the matches found categorized and assigned a quality value.
        /// </summary>
        /// <param name="type">The type to be serialized.</param>
        /// <param name="request">The request.</param>
        /// <param name="formatters">The set of <see cref="MediaTypeFormatter"/> objects from which to choose.</param>
        /// <returns>A collection containing all the matches.</returns>
        protected virtual Collection <MediaTypeFormatterMatch> ComputeFormatterMatches(Type type, HttpRequestMessage request, IEnumerable <MediaTypeFormatter> formatters)
        {
            // Performance-sensitive
            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }
            if (formatters == null)
            {
                throw Error.ArgumentNull("formatters");
            }

            IEnumerable <MediaTypeWithQualityHeaderValue> sortedAcceptValues = null;

            // Go through each formatter to find how well it matches.
            ListWrapperCollection <MediaTypeFormatterMatch> matches = new ListWrapperCollection <MediaTypeFormatterMatch>();

            MediaTypeFormatter[] writingFormatters = GetWritingFormatters(formatters);
            for (int i = 0; i < writingFormatters.Length; i++)
            {
                MediaTypeFormatter      formatter = writingFormatters[i];
                MediaTypeFormatterMatch match     = null;

                // Check first that formatter can write the actual type
                if (!formatter.CanWriteType(type))
                {
                    // Formatter can't even write the type so no match at all
                    continue;
                }

                // Match against media type mapping.
                if ((match = MatchMediaTypeMapping(request, formatter)) != null)
                {
                    matches.Add(match);
                    continue;
                }

                // Match against the accept header values.
                if (sortedAcceptValues == null)
                {
                    // Sort the Accept header values in descending order based on q-factor
                    sortedAcceptValues = SortMediaTypeWithQualityHeaderValuesByQFactor(request.Headers.Accept);
                }
                if ((match = MatchAcceptHeader(sortedAcceptValues, formatter)) != null)
                {
                    matches.Add(match);
                    continue;
                }

                // Match against request's media type if any
                if ((match = MatchRequestMediaType(request, formatter)) != null)
                {
                    matches.Add(match);
                    continue;
                }

                // Check whether we should match on type or stop the matching process.
                // The latter is used to generate 406 (Not Acceptable) status codes.
                bool shouldMatchOnType = ShouldMatchOnType(sortedAcceptValues);

                // Match against the type of object we are writing out
                if (shouldMatchOnType && (match = MatchType(type, formatter)) != null)
                {
                    matches.Add(match);
                    continue;
                }
            }

            return(matches);
        }
        /// <summary>
        /// Determine how well each formatter matches by associating a <see cref="MediaTypeFormatterMatchRanking"/> value
        /// with the formatter. Then associate the quality of the match based on q-factors and other parameters. The result of this 
        /// method is a collection of the matches found categorized and assigned a quality value.
        /// </summary>
        /// <param name="type">The type to be serialized.</param>
        /// <param name="request">The request.</param>
        /// <param name="formatters">The set of <see cref="MediaTypeFormatter"/> objects from which to choose.</param>
        /// <returns>A collection containing all the matches.</returns>
        protected virtual Collection<MediaTypeFormatterMatch> ComputeFormatterMatches(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
        {
            // Performance-sensitive
            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }
            if (formatters == null)
            {
                throw Error.ArgumentNull("formatters");
            }

            IEnumerable<MediaTypeWithQualityHeaderValue> sortedAcceptValues = null;

            // Go through each formatter to find how well it matches.
            ListWrapperCollection<MediaTypeFormatterMatch> matches = new ListWrapperCollection<MediaTypeFormatterMatch>();
            MediaTypeFormatter[] writingFormatters = GetWritingFormatters(formatters);
            for (int i = 0; i < writingFormatters.Length; i++) 
            {
                MediaTypeFormatter formatter = writingFormatters[i];
                MediaTypeFormatterMatch match = null;

                // Check first that formatter can write the actual type
                if (!formatter.CanWriteType(type))
                {
                    // Formatter can't even write the type so no match at all
                    continue;
                }

                // Match against media type mapping.
                if ((match = this.MatchMediaTypeMapping(request, formatter)) != null)
                {
                    matches.Add(match);
                    continue;
                }

                // Match against the accept header values.
                if (sortedAcceptValues == null)
                {
                    // Sort the Accept header values in descending order based on q-factor
                    sortedAcceptValues = this.SortMediaTypeWithQualityHeaderValuesByQFactor(request.Headers.Accept);
                }
                if ((match = this.MatchAcceptHeader(sortedAcceptValues, formatter)) != null)
                {
                    matches.Add(match);
                    continue;
                }

                // Match against request's media type if any
                if ((match = this.MatchRequestMediaType(request, formatter)) != null)
                {
                    matches.Add(match);
                    continue;
                }

                // Check whether we should match on type or stop the matching process. 
                // The latter is used to generate 406 (Not Acceptable) status codes.
                bool shouldMatchOnType = this.ShouldMatchOnType(sortedAcceptValues);

                // Match against the type of object we are writing out
                if (shouldMatchOnType && (match = this.MatchType(type, formatter)) != null)
                {
                    matches.Add(match);
                    continue;
                }
            }

            return matches;
        }
        public void AsList_ListWrapperCollection_ReturnsSameInstance()
        {
            List<object> list = new List<object> { new object(), new object() };
            ListWrapperCollection<object> listWrapper = new ListWrapperCollection<object>(list);

            List<object> listWrapperAsList = ((IEnumerable<object>)listWrapper).AsList();

            Assert.Same(list, listWrapperAsList);
        }