Esempio n. 1
0
        public void SupportedMediaTypes_AddThrowsWithMediaRange(MediaTypeHeaderValue mediaType)
        {
            MockMediaTypeFormatter            formatter           = new MockMediaTypeFormatter();
            Collection <MediaTypeHeaderValue> supportedMediaTypes = formatter.SupportedMediaTypes;

            Assert.ThrowsArgument(() => supportedMediaTypes.Add(mediaType), "item", Error.Format(Properties.Resources.CannotUseMediaRangeForSupportedMediaType, typeof(MediaTypeHeaderValue).Name, mediaType.MediaType));
        }
Esempio n. 2
0
        public void SelectResponseCharacterEncoding_ReturnsBestEncodingBasedOnAcceptCharsetMatch(string[] acceptCharsetValues, string bodyEncoding, string[] supportedEncodings, string expectedEncoding)
        {
            // Arrange
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter()
            {
                CallBase = true
            };
            HttpRequestMessage request = new HttpRequestMessage();

            foreach (var acceptCharsetValue in acceptCharsetValues)
            {
                request.Headers.AcceptCharset.Add(StringWithQualityHeaderValue.Parse(acceptCharsetValue));
            }

            foreach (var supportedEncoding in supportedEncodings)
            {
                Encoding supportedEnc = Encoding.GetEncoding(supportedEncoding);
                formatter.SupportedEncodings.Add(supportedEnc);
            }

            if (bodyEncoding != null)
            {
                Encoding bodyEnc = Encoding.GetEncoding(bodyEncoding);
                request.Method  = HttpMethod.Post;
                request.Content = new StringContent("Hello World", bodyEnc, "text/plain");
            }

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

            // Assert
            Encoding expectedEnc = expectedEncoding != null?Encoding.GetEncoding(expectedEncoding) : null;

            Assert.Equal(expectedEnc, actualEncoding);
        }
Esempio n. 3
0
        public void CanWriteAsThrowsWithNullContent()
        {
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();
            MediaTypeHeaderValue   mediaType = null;

            Assert.ThrowsArgumentNull(() => formatter.CanWriteAs(typeof(int), null, out mediaType), "mediaType");
        }
Esempio n. 4
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);
        }
Esempio n. 5
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);
        }
        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);
            }
        }
        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);
        }
Esempio n. 8
0
        public void WritingFormatters_FiltersOutCanWriteAnyTypesFalse()
        {
            // Arrange
            MockMediaTypeFormatter writableFormatter = new MockMediaTypeFormatter();
            MockMediaTypeFormatter readOnlyFormatter = new MockMediaTypeFormatter()
            {
                CanWriteAnyTypesReturn = false
            };
            List <MediaTypeFormatter> formatters = new List <MediaTypeFormatter>()
            {
                writableFormatter,
                readOnlyFormatter
            };
            MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(formatters);

            // Act
            MediaTypeFormatter[] writableFormatters = collection.WritingFormatters;

            // Assert
            MediaTypeFormatter[] expectedFormatters = new MediaTypeFormatter[]
            {
                writableFormatter
            };
            Assert.Equal(expectedFormatters, writableFormatters);
        }
Esempio n. 9
0
        public void FindWriter_ReturnsFormatterOnMatch(
            Type variationType,
            object testData,
            string mediaType
            )
        {
            // Arrange
            GC.KeepAlive(testData); // Mark parameter as used. See xUnit1026, [Theory] method doesn't use all parameters.
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter()
            {
                CallBase = true
            };

            foreach (string legalMediaType in HttpTestData.LegalMediaTypeStrings)
            {
                formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue(mediaType));
            }

            MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();

            collection.Clear();
            collection.Add(formatter);

            MediaTypeHeaderValue contentType = new MediaTypeHeaderValue(mediaType);

            // Act
            MediaTypeFormatter actualFormatter = collection.FindWriter(variationType, contentType);

            // Assert
            Assert.Same(formatter, actualFormatter);
        }
        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);
            }
        }
        private static MediaTypeFormatterMatch CreateMatch(double?quality, MediaTypeFormatterMatchRanking ranking)
        {
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();
            MediaTypeHeaderValue   mediaType = new MediaTypeHeaderValue("text/test");

            return(new MediaTypeFormatterMatch(formatter, mediaType, quality, ranking));
        }
        public void Negotiate_ForRequestReturnsFirstMatchingFormatter()
        {
            MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("application/myMediaType");

            MediaTypeFormatter formatter1 = new MockMediaTypeFormatter()
            {
                CanWriteTypeCallback = (Type t) => false
            };

            MediaTypeFormatter formatter2 = new MockMediaTypeFormatter()
            {
                CanWriteTypeCallback = (Type t) => true
            };

            formatter2.SupportedMediaTypes.Add(mediaType);

            MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(
                new MediaTypeFormatter[]
            {
                formatter1,
                formatter2
            });

            _request.Content = new StringContent("test", Encoding.UTF8, mediaType.MediaType);

            var result = _negotiator.Negotiate(typeof(string), _request, collection);

            Assert.Same(formatter2, result.Formatter);
            new MediaTypeAssert().AreEqual(mediaType, result.MediaType, "Expected the formatter's media type to be returned.");
        }
Esempio n. 13
0
        public void SupportedMediaTypes_AddThrowsWithNullMediaType()
        {
            MockMediaTypeFormatter            formatter           = new MockMediaTypeFormatter();
            Collection <MediaTypeHeaderValue> supportedMediaTypes = formatter.SupportedMediaTypes;

            Assert.ThrowsArgumentNull(() => supportedMediaTypes.Add(null), "item");
        }
        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);
            }
        }
Esempio n. 15
0
        public void SetDefaultContentHeaders_ThrowsOnNullHeaders()
        {
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();
            Type type = typeof(object);

            Assert.ThrowsArgumentNull(() => formatter.SetDefaultContentHeaders(type, null, TestMediaTypeHeader), "headers");
        }
        public void FindWriter_ReturnsFormatterOnMatch(Type variationType, object testData, string mediaType)
        {
            // Arrange
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter()
            {
                CallBase = true
            };

            foreach (string legalMediaType in HttpTestData.LegalMediaTypeStrings)
            {
                formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue(mediaType));
            }

            MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();

            collection.Clear();
            collection.Add(formatter);

            MediaTypeHeaderValue contentType = new MediaTypeHeaderValue(mediaType);

            // Act
            MediaTypeFormatter actualFormatter = collection.FindWriter(variationType, contentType);

            // Assert
            Assert.Same(formatter, actualFormatter);
        }
Esempio n. 17
0
        public void SelectCharacterEncoding_ReturnsBestEncoding(string bodyEncoding, string[] supportedEncodings, string expectedEncoding)
        {
            // Arrange
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter {
                CallBase = true
            };

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

            HttpContentHeaders contentHeaders = null;

            if (bodyEncoding != null)
            {
                Encoding    bodyEnc = Encoding.GetEncoding(bodyEncoding);
                HttpContent content = new StringContent("Hello World", bodyEnc, "text/plain");
                contentHeaders = content.Headers;
            }

            // Act
            Encoding actualEncoding = formatter.SelectCharacterEncoding(contentHeaders);

            // Assert
            Encoding expectedEnc = expectedEncoding != null?Encoding.GetEncoding(expectedEncoding) : null;

            Assert.Equal(expectedEnc, actualEncoding);
        }
Esempio n. 18
0
        public void SetDefaultContentHeaders_ThrowsOnNullType()
        {
            MockMediaTypeFormatter formatter      = new MockMediaTypeFormatter();
            HttpContentHeaders     contentHeaders = FormattingUtilities.CreateEmptyContentHeaders();

            Assert.ThrowsArgumentNull(() => formatter.SetDefaultContentHeaders(null, contentHeaders, TestMediaTypeHeader), "type");
        }
        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 SelectCharacterEncoding_ThrowsIfNoSupportedEncodings()
        {
            // Arrange
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter { CallBase = true };
            HttpContent content = new StringContent("Hello World", Encoding.UTF8, "text/plain");

            // Act
            Assert.Throws<InvalidOperationException>(() => formatter.SelectCharacterEncoding(content.Headers));
        }
        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 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 MediaTypeMappings_IsMutable()
        {
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();
            Collection<MediaTypeMapping> mappings = formatter.MediaTypeMappings;
            MediaTypeMapping[] standardMappings = HttpTestData.StandardMediaTypeMappings.ToArray();
            foreach (MediaTypeMapping mapping in standardMappings)
            {
                mappings.Add(mapping);
            }

            Assert.True(standardMappings.SequenceEqual(formatter.MediaTypeMappings));
        }
        public void SupportedMediaTypes_IsMutable()
        {
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();
            Collection<MediaTypeHeaderValue> supportedMediaTypes = formatter.SupportedMediaTypes;
            MediaTypeHeaderValue[] mediaTypes = HttpTestData.LegalMediaTypeHeaderValues.ToArray();
            foreach (MediaTypeHeaderValue mediaType in mediaTypes)
            {
                supportedMediaTypes.Add(mediaType);
            }

            Assert.True(mediaTypes.SequenceEqual(formatter.SupportedMediaTypes));
        }
Esempio n. 27
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);
        }
Esempio n. 28
0
        public void SetDefaultContentHeaders_UsesNonNullMediaType()
        {
            // Arrange
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();
            Type type = typeof(object);
            HttpContentHeaders contentHeaders = FormattingUtilities.CreateEmptyContentHeaders();

            // Act
            formatter.SetDefaultContentHeaders(type, contentHeaders, TestMediaType);

            // Assert
            Assert.Equal(TestMediaType, contentHeaders.ContentType.MediaType);
        }
        public void Ctor_ClonesMediaType()
        {
            // Arrange
            MockMediaTypeFormatter formatter = new MockMediaTypeFormatter();
            MediaTypeHeaderValue   mediaType = MediaTypeHeaderValue.Parse("text/test");

            // Act
            MediaTypeFormatterMatch match = new MediaTypeFormatterMatch(formatter, mediaType, null, MediaTypeFormatterMatchRanking.MatchOnCanWriteType);

            // Assert
            Assert.Equal(mediaType, match.MediaType);
            Assert.NotSame(mediaType, match.MediaType);
        }
Esempio n. 30
0
        public void Constructor()
        {
            MockMediaTypeFormatter            formatter           = new MockMediaTypeFormatter();
            Collection <MediaTypeHeaderValue> supportedMediaTypes = formatter.SupportedMediaTypes;

            Assert.NotNull(supportedMediaTypes);
            Assert.Equal(0, supportedMediaTypes.Count);

            Collection <MediaTypeMapping> mappings = formatter.MediaTypeMappings;

            Assert.NotNull(mappings);
            Assert.Equal(0, mappings.Count);
        }