Пример #1
0
        public void MatchAcceptHeader_ReturnsMatch(string[] acceptHeaders, string[] supportedMediaTypes, string expectedMediaType, double expectedQuality, int ranking)
        {
            // Arrange
            MockContentNegotiator negotiator = new MockContentNegotiator();

            List <MediaTypeWithQualityHeaderValue>        unsortedAcceptHeaders = acceptHeaders.Select(a => MediaTypeWithQualityHeaderValue.Parse(a)).ToList();
            IEnumerable <MediaTypeWithQualityHeaderValue> sortedAcceptHeaders   = negotiator.SortMediaTypeWithQualityHeaderValuesByQFactor(unsortedAcceptHeaders);

            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();

            foreach (string supportedMediaType in supportedMediaTypes)
            {
                formatter.SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse(supportedMediaType));
            }

            // Act
            MediaTypeFormatterMatch match = negotiator.MatchAcceptHeader(sortedAcceptHeaders, formatter);

            // Assert
            if (expectedMediaType == null)
            {
                Assert.Null(match);
            }
            else
            {
                Assert.Same(formatter, match.Formatter);
                Assert.Equal(MediaTypeHeaderValue.Parse(expectedMediaType), match.MediaType);
                Assert.Equal(expectedQuality, match.Quality);
                Assert.Equal(ranking, (int)match.Ranking);
            }
        }
Пример #2
0
        public void MatchRequestMediaType_ReturnsMatch(string requestMediaType, string[] supportedMediaTypes, string expectedMediaType)
        {
            // Arrange
            MockContentNegotiator negotiator = new MockContentNegotiator();

            HttpRequestMessage request = new HttpRequestMessage();

            request.Content = new StringContent(String.Empty);
            request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(requestMediaType);

            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();

            foreach (string supportedMediaType in supportedMediaTypes)
            {
                formatter.SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse(supportedMediaType));
            }

            // Act
            MediaTypeFormatterMatch match = negotiator.MatchRequestMediaType(request, formatter);

            // Assert
            if (expectedMediaType == null)
            {
                Assert.Null(match);
            }
            else
            {
                Assert.Same(formatter, match.Formatter);
                Assert.Equal(MediaTypeHeaderValue.Parse(expectedMediaType), match.MediaType);
                Assert.Equal(1.0, match.Quality);
                Assert.Equal(MediaTypeFormatterMatchRanking.MatchOnRequestMediaType, match.Ranking);
            }
        }
        public void MatchType_ReturnsMatch(bool excludeMatchOnTypeOnly, string[] supportedMediaTypes, string expectedMediaType)
        {
            // Arrange
            MockContentNegotiator negotiator = new MockContentNegotiator(excludeMatchOnTypeOnly);

            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();

            foreach (string supportedMediaType in supportedMediaTypes)
            {
                formatter.SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse(supportedMediaType));
            }

            // Act
            MediaTypeFormatterMatch match = negotiator.MatchType(typeof(object), formatter);

            // Assert
            if (expectedMediaType == null)
            {
                Assert.Null(match);
            }
            else
            {
                Assert.Same(formatter, match.Formatter);
                Assert.Equal(MediaTypeHeaderValue.Parse(expectedMediaType), match.MediaType);
                Assert.Equal(1.0, match.Quality);
                Assert.Equal(MediaTypeFormatterMatchRanking.MatchOnCanWriteType, match.Ranking);
            }
        }
Пример #4
0
        public void MatchMediaTypeMapping_ReturnsMatch()
        {
            // Arrange
            MockContentNegotiator negotiator = new MockContentNegotiator();

            HttpRequestMessage   request              = new HttpRequestMessage();
            MediaTypeHeaderValue mappingMediatype     = MediaTypeHeaderValue.Parse("application/other");
            MockMediaTypeMapping mockMediaTypeMapping = new MockMediaTypeMapping(mappingMediatype, 0.75);

            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();

            formatter.MediaTypeMappings.Add(mockMediaTypeMapping);

            // Act
            MediaTypeFormatterMatch match = negotiator.MatchMediaTypeMapping(request, formatter);

            // Assert
            Assert.True(mockMediaTypeMapping.WasInvoked);
            Assert.Same(request, mockMediaTypeMapping.Request);

            Assert.Same(formatter, match.Formatter);
            Assert.Equal(mockMediaTypeMapping.MediaType, match.MediaType);
            Assert.Equal(mockMediaTypeMapping.MatchQuality, match.Quality);
            Assert.Equal(MediaTypeFormatterMatchRanking.MatchOnRequestWithMediaTypeMapping, match.Ranking);
        }
        public void SelectResponseMediaTypeFormatter_ThrowsOnNull()
        {
            MockContentNegotiator  negotiator = new MockContentNegotiator();
            HttpRequestMessage     request    = new HttpRequestMessage();
            MockMediaTypeFormatter formatter  = new MockMediaTypeFormatter();

            Assert.ThrowsArgumentNull(() => negotiator.SelectResponseMediaTypeFormatter(matches: null), "matches");
        }
        public void SelectResponseCharacterEncoding_ThrowsOnNull()
        {
            MockContentNegotiator  negotiator = new MockContentNegotiator();
            HttpRequestMessage     request    = new HttpRequestMessage();
            MockMediaTypeFormatter formatter  = new MockMediaTypeFormatter();

            Assert.ThrowsArgumentNull(() => negotiator.SelectResponseCharacterEncoding(request: null, formatter: formatter), "request");
            Assert.ThrowsArgumentNull(() => negotiator.SelectResponseCharacterEncoding(request: request, formatter: null), "formatter");
        }
        public void MatchType_ThrowsOnNull()
        {
            MockContentNegotiator negotiator = new MockContentNegotiator();
            Type type = typeof(object);
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();

            Assert.ThrowsArgumentNull(() => negotiator.MatchType(type: null, formatter: formatter), "type");
            Assert.ThrowsArgumentNull(() => negotiator.MatchType(type: type, formatter: null), "formatter");
        }
        public void MatchRequestMediaType_ThrowsOnNull()
        {
            MockContentNegotiator  negotiator = new MockContentNegotiator();
            HttpRequestMessage     request    = new HttpRequestMessage();
            MockMediaTypeFormatter formatter  = new MockMediaTypeFormatter();

            Assert.ThrowsArgumentNull(() => negotiator.MatchRequestMediaType(request: null, formatter: formatter), "request");
            Assert.ThrowsArgumentNull(() => negotiator.MatchRequestMediaType(request: request, formatter: null), "formatter");
        }
        public void MatchAcceptHeader_ThrowsOnNull()
        {
            MockContentNegotiator  negotiator = new MockContentNegotiator();
            MockMediaTypeFormatter formatter  = new MockMediaTypeFormatter();
            List <MediaTypeWithQualityHeaderValue> sortedAcceptValues = new List <MediaTypeWithQualityHeaderValue>();

            Assert.ThrowsArgumentNull(() => negotiator.MatchAcceptHeader(sortedAcceptValues: null, formatter: formatter), "sortedAcceptValues");
            Assert.ThrowsArgumentNull(() => negotiator.MatchAcceptHeader(sortedAcceptValues: sortedAcceptValues, formatter: null), "formatter");
        }
        public void ComputeFormatterMatches_ThrowsOnNull()
        {
            MockContentNegotiator negotiator = new MockContentNegotiator();
            Type type = typeof(object);
            HttpRequestMessage        request    = new HttpRequestMessage();
            List <MediaTypeFormatter> formatters = new List <MediaTypeFormatter>();

            Assert.ThrowsArgumentNull(() => negotiator.ComputeFormatterMatches(type: null, request: request, formatters: formatters), "type");
            Assert.ThrowsArgumentNull(() => negotiator.ComputeFormatterMatches(type: type, request: null, formatters: formatters), "request");
            Assert.ThrowsArgumentNull(() => negotiator.ComputeFormatterMatches(type: type, request: request, formatters: null), "formatters");
        }
Пример #11
0
        public void SelectResponseMediaTypeFormatter_SelectsMediaType(ICollection <MediaTypeFormatterMatch> matches, MediaTypeFormatterMatch expectedWinner)
        {
            // Arrange
            MockContentNegotiator negotiator = new MockContentNegotiator();

            // Act
            MediaTypeFormatterMatch actualWinner = negotiator.SelectResponseMediaTypeFormatter(matches);

            // Assert
            Assert.Same(expectedWinner, actualWinner);
        }
Пример #12
0
        public void ShouldMatchOnType_ReturnsExpectedResult(bool excludeMatchOnType, string[] acceptHeaders, bool expectedResult)
        {
            // Arrange
            MockContentNegotiator negotiator = new MockContentNegotiator(excludeMatchOnType);
            List <MediaTypeWithQualityHeaderValue>        unsortedAcceptHeaders = acceptHeaders.Select(a => MediaTypeWithQualityHeaderValue.Parse(a)).ToList();
            IEnumerable <MediaTypeWithQualityHeaderValue> sortedAcceptHeaders   = negotiator.SortMediaTypeWithQualityHeaderValuesByQFactor(unsortedAcceptHeaders);

            // Act
            bool result = negotiator.ShouldMatchOnType(sortedAcceptHeaders);

            // Assert
            Assert.Equal(expectedResult, result);
        }
Пример #13
0
        public void SortStringWithQualityHeaderValuesByQFactor_SortsCorrectly(IEnumerable <string> unsorted, IEnumerable <string> expectedSorted)
        {
            // Arrange
            MockContentNegotiator negotiator = new MockContentNegotiator();

            List <StringWithQualityHeaderValue> unsortedValues =
                new List <StringWithQualityHeaderValue>(unsorted.Select(u => StringWithQualityHeaderValue.Parse(u)));

            List <StringWithQualityHeaderValue> expectedSortedValues =
                new List <StringWithQualityHeaderValue>(expectedSorted.Select(u => StringWithQualityHeaderValue.Parse(u)));

            // Act
            IEnumerable <StringWithQualityHeaderValue> actualSorted = negotiator.SortStringWithQualityHeaderValuesByQFactor(unsortedValues);

            // Assert
            Assert.True(expectedSortedValues.SequenceEqual(actualSorted));
        }
Пример #14
0
        public void UpdateBestMatch_SelectsCorrectly(MediaTypeFormatterMatch current, MediaTypeFormatterMatch replacement, bool currentWins)
        {
            // Arrange
            MockContentNegotiator negotiator = new MockContentNegotiator();

            // Act
            MediaTypeFormatterMatch actualResult = negotiator.UpdateBestMatch(current, replacement);

            // Assert
            if (currentWins)
            {
                Assert.Same(current, actualResult);
            }
            else
            {
                Assert.Same(replacement, actualResult);
            }
        }
Пример #15
0
        public void SelectResponseCharacterEncoding_SelectsEncoding(string[] acceptCharsetHeaders, string requestEncoding, string[] supportedEncodings, string expectedEncoding)
        {
            // Arrange
            MockContentNegotiator negotiator = new MockContentNegotiator();

            HttpRequestMessage request = new HttpRequestMessage();

            foreach (string acceptCharsetHeader in acceptCharsetHeaders)
            {
                request.Headers.AcceptCharset.Add(StringWithQualityHeaderValue.Parse(acceptCharsetHeader));
            }

            if (requestEncoding != null)
            {
                Encoding      reqEncoding = Encoding.GetEncoding(requestEncoding);
                StringContent content     = new StringContent("", reqEncoding, "text/plain");
                request.Content = content;
            }

            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter()
            {
                CallBase = true
            };

            foreach (string supportedEncoding in supportedEncodings)
            {
                formatter.SupportedEncodings.Add(Encoding.GetEncoding(supportedEncoding));
            }

            // Act
            Encoding actualEncoding = negotiator.SelectResponseCharacterEncoding(request, formatter);

            // Assert
            if (expectedEncoding == null)
            {
                Assert.Null(actualEncoding);
            }
            else
            {
                Assert.Equal(Encoding.GetEncoding(expectedEncoding), actualEncoding);
            }
        }
Пример #16
0
        public void Negotiate_ObservesExcludeMatchOnTypeOnly(bool excludeMatchOnTypeOnly)
        {
            // Arrange
            MockContentNegotiator negotiator = new MockContentNegotiator(excludeMatchOnTypeOnly);

            _request.Content = new StringContent("test");
            _request.Headers.Accept.ParseAdd("text/html");

            // Act
            var result = negotiator.Negotiate(typeof(string), _request, new MediaTypeFormatterCollection());

            // Assert
            if (excludeMatchOnTypeOnly)
            {
                Assert.Null(result);
            }
            else
            {
                Assert.NotNull(result);
                Assert.Equal("application/json", result.MediaType.MediaType);
            }
        }
        public void ShouldMatchOnType_ThrowsOnNull()
        {
            MockContentNegotiator negotiator = new MockContentNegotiator();

            Assert.ThrowsArgumentNull(() => negotiator.ShouldMatchOnType(sortedAcceptValues: null), "sortedAcceptValues");
        }
        public void SortStringWithQualityHeaderValuesByQFactor_ThrowsOnNull()
        {
            MockContentNegotiator negotiator = new MockContentNegotiator();

            Assert.ThrowsArgumentNull(() => negotiator.SortStringWithQualityHeaderValuesByQFactor((HttpHeaderValueCollection <StringWithQualityHeaderValue>)null), "headerValues");
        }