1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Net.Http.Formatting.DataSets;
using System.Net.Http.Headers;
using Microsoft.TestCommon;
using Xunit;
using Xunit.Extensions;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Net.Http.Formatting
{
public class MediaRangeMappingTests
{
[Fact]
[Trait("Description", "MediaRangeMappring is public, and concrete.")]
public void TypeIsCorrect()
{
Assert.Type.HasProperties(
typeof(MediaRangeMapping),
TypeAssert.TypeProperties.IsPublicVisibleClass,
typeof(MediaTypeMapping));
}
[Theory]
[TestDataSet(
typeof(HttpUnitTestDataSets), "LegalMediaRangeValues",
typeof(HttpUnitTestDataSets), "LegalMediaTypeHeaderValues")]
[Trait("Description", "MediaRangeMapping(MediaTypeHeaderValue, MediaTypeHeaderValue) sets public properties.")]
public void Constructor(MediaTypeHeaderValue mediaRange, MediaTypeHeaderValue mediaType)
{
MediaRangeMapping mapping = new MediaRangeMapping(mediaRange, mediaType);
Assert.MediaType.AreEqual(mediaRange, mapping.MediaRange, "MediaRange failed to set.");
Assert.MediaType.AreEqual(mediaType, mapping.MediaType, "MediaType failed to set.");
}
[Theory]
[TestDataSet(typeof(HttpUnitTestDataSets), "LegalMediaTypeHeaderValues")]
[Trait("Description", "MediaRangeMapping(MediaTypeHeaderValue, MediaTypeHeaderValue) throws if the MediaRange parameter is null.")]
public void ConstructorThrowsWithNullMediaRange(MediaTypeHeaderValue mediaType)
{
Assert.ThrowsArgumentNull(() => new MediaRangeMapping((MediaTypeHeaderValue)null, mediaType), "mediaRange");
}
[Theory]
[TestDataSet(typeof(HttpUnitTestDataSets), "LegalMediaRangeValues")]
[Trait("Description", "MediaRangeMapping(MediaTypeHeaderValue, MediaTypeHeaderValue) throws if the MediaType parameter is null.")]
public void ConstructorThrowsWithNullMediaType(MediaTypeHeaderValue mediaRange)
{
Assert.ThrowsArgumentNull(() => new MediaRangeMapping(mediaRange, (MediaTypeHeaderValue)null), "mediaType");
}
[Theory]
[TestDataSet(
typeof(HttpUnitTestDataSets), "IllegalMediaRangeValues",
typeof(HttpUnitTestDataSets), "LegalMediaTypeHeaderValues")]
[Trait("Description", "MediaRangeMapping(MediaTypeHeaderValue, MediaTypeHeaderValue) throws if the MediaRange parameter is not really a media range.")]
public void ConstructorThrowsWithIllegalMediaRange(MediaTypeHeaderValue mediaRange, MediaTypeHeaderValue mediaType)
{
string errorMessage = RS.Format(Properties.Resources.InvalidMediaRange, mediaRange.MediaType);
Assert.Throws<InvalidOperationException>(() => new MediaRangeMapping(mediaRange, mediaType), errorMessage);
}
[Theory]
[TestDataSet(
typeof(HttpUnitTestDataSets), "LegalMediaRangeStrings",
typeof(HttpUnitTestDataSets), "LegalMediaTypeStrings")]
[Trait("Description", "MediaRangeMapping(string, string) sets public properties.")]
public void Constructor1(string mediaRange, string mediaType)
{
MediaRangeMapping mapping = new MediaRangeMapping(mediaRange, mediaType);
Assert.MediaType.AreEqual(mediaRange, mapping.MediaRange, "MediaRange failed to set.");
Assert.MediaType.AreEqual(mediaType, mapping.MediaType, "MediaType failed to set.");
}
[Theory]
[TestDataSet(
typeof(CommonUnitTestDataSets), "EmptyStrings",
typeof(HttpUnitTestDataSets), "LegalMediaTypeStrings")]
[Trait("Description", "MediaRangeMapping(string, string) throws if the MediaRange parameter is empty.")]
public void Constructor1ThrowsWithEmptyMediaRange(string mediaRange, string mediaType)
{
Assert.ThrowsArgumentNull(() => new MediaRangeMapping(mediaRange, mediaType), "mediaRange");
}
[Theory]
[TestDataSet(
typeof(HttpUnitTestDataSets), "LegalMediaRangeStrings",
typeof(CommonUnitTestDataSets), "EmptyStrings")]
[Trait("Description", "MediaRangeMapping(string, string) throws if the MediaType parameter is empty.")]
public void Constructor1ThrowsWithEmptyMediaType(string mediaRange, string mediaType)
{
Assert.ThrowsArgumentNull(() => new MediaRangeMapping(mediaRange, mediaType), "mediaType");
}
[Theory]
[TestDataSet(
typeof(HttpUnitTestDataSets), "IllegalMediaRangeStrings",
typeof(HttpUnitTestDataSets), "LegalMediaTypeStrings")]
[Trait("Description", "MediaRangeMapping(string, string) throws if the MediaRange parameter is not really a media range.")]
public void Constructor1ThrowsWithIllegalMediaRange(string mediaRange, string mediaType)
{
string errorMessage = RS.Format(Properties.Resources.InvalidMediaRange, mediaRange);
Assert.Throws<InvalidOperationException>(() => new MediaRangeMapping(mediaRange, mediaType), errorMessage);
}
[Theory]
[TestDataSet(
typeof(HttpUnitTestDataSets), "LegalMediaRangeStrings",
typeof(HttpUnitTestDataSets), "LegalMediaTypeStrings")]
[Trait("Description", "TryMatchMediaType(HttpRequestMessage) throws with null HttpRequestMessage.")]
public void TryMatchMediaTypeThrowsWithNullHttpRequestMessage(string mediaRange, string mediaType)
{
MediaRangeMapping mapping = new MediaRangeMapping(mediaRange, mediaType);
Assert.ThrowsArgumentNull(() => mapping.TryMatchMediaType(request: null), "request");
}
[Theory]
[TestDataSet(
typeof(HttpUnitTestDataSets), "LegalMediaRangeStrings",
typeof(HttpUnitTestDataSets), "LegalMediaTypeStrings")]
[Trait("Description", "TryMatchMediaType(HttpRequestMessage) returns 1.0 when the MediaRange is in the accept headers.")]
public void TryMatchMediaTypeReturnsOneWithMediaRangeInAcceptHeader(string mediaRange, string mediaType)
{
MediaRangeMapping mapping = new MediaRangeMapping(mediaRange, mediaType);
HttpRequestMessage request = new HttpRequestMessage();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaRange));
Assert.Equal(1.0, mapping.TryMatchMediaType(request));
}
[Theory]
[TestDataSet(
typeof(HttpUnitTestDataSets), "MediaRangeValuesWithQuality",
typeof(HttpUnitTestDataSets), "LegalMediaTypeHeaderValues")]
[Trait("Description", "TryMatchMediaType(HttpRequestMessage) returns quality factor when a MediaRange with quality is in the accept headers.")]
public void TryMatchMediaTypeReturnsQualityWithMediaRangeWithQualityInAcceptHeader(MediaTypeWithQualityHeaderValue mediaRangeWithQuality, MediaTypeHeaderValue mediaType)
{
MediaTypeWithQualityHeaderValue mediaRangeWithNoQuality = new MediaTypeWithQualityHeaderValue(mediaRangeWithQuality.MediaType);
MediaRangeMapping mapping = new MediaRangeMapping(mediaRangeWithNoQuality, mediaType);
HttpRequestMessage request = new HttpRequestMessage();
request.Headers.Accept.Add(mediaRangeWithQuality);
double quality = mediaRangeWithQuality.Quality.Value;
Assert.Equal(quality, mapping.TryMatchMediaType(request));
}
[Theory]
[TestDataSet(
typeof(HttpUnitTestDataSets), "LegalMediaRangeStrings",
typeof(HttpUnitTestDataSets), "LegalMediaTypeStrings")]
[Trait("Description", "TryMatchMediaType(HttpRequestMessage) returns 0.0 when the MediaRange is not in the accept headers.")]
public void TryMatchMediaTypeReturnsFalseWithMediaRangeNotInAcceptHeader(string mediaRange, string mediaType)
{
MediaRangeMapping mapping = new MediaRangeMapping(mediaRange, mediaType);
HttpRequestMessage request = new HttpRequestMessage();
Assert.Equal(0.0, mapping.TryMatchMediaType(request));
}
}
}
|