PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/System.Net.Http.Formatting.Test/Formatting/MediaTypeFormatterCollectionTests.cs

https://github.com/huyq2002/aspnetwebstack
C# | 462 lines | 366 code | 67 blank | 29 comment | 2 complexity | 741c19e5992d3dbf60424674f7fe0b23 MD5 | raw file
Possible License(s): Apache-2.0
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Net.Http.Formatting.DataSets;
  6. using System.Net.Http.Formatting.Mocks;
  7. using System.Net.Http.Headers;
  8. using System.Web.Http;
  9. using System.Xml;
  10. using System.Xml.Linq;
  11. using Microsoft.TestCommon;
  12. using Newtonsoft.Json.Linq;
  13. namespace System.Net.Http.Formatting
  14. {
  15. public class MediaTypeFormatterCollectionTests
  16. {
  17. [Fact]
  18. public void TypeIsCorrect()
  19. {
  20. Assert.Type.HasProperties(typeof(MediaTypeFormatterCollection), TypeAssert.TypeProperties.IsPublicVisibleClass, typeof(Collection<MediaTypeFormatter>));
  21. }
  22. [Fact]
  23. public void Constructor()
  24. {
  25. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
  26. #if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
  27. Assert.Equal(3, collection.Count);
  28. #else
  29. Assert.Equal(2, collection.Count);
  30. #endif
  31. Assert.NotNull(collection.XmlFormatter);
  32. Assert.NotNull(collection.JsonFormatter);
  33. #if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
  34. Assert.NotNull(collection.FormUrlEncodedFormatter);
  35. #endif
  36. }
  37. [Fact]
  38. public void Constructor1_AcceptsEmptyList()
  39. {
  40. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(new MediaTypeFormatter[0]);
  41. Assert.Equal(0, collection.Count);
  42. }
  43. [Theory]
  44. [TestDataSet(typeof(HttpTestData), "AllFormatterCollections")]
  45. public void Constructor1_SetsProperties(IEnumerable<MediaTypeFormatter> formatterCollection)
  46. {
  47. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(formatterCollection);
  48. if (collection.OfType<XmlMediaTypeFormatter>().Any())
  49. {
  50. Assert.NotNull(collection.XmlFormatter);
  51. }
  52. else
  53. {
  54. Assert.Null(collection.XmlFormatter);
  55. }
  56. if (collection.OfType<JsonMediaTypeFormatter>().Any())
  57. {
  58. Assert.NotNull(collection.JsonFormatter);
  59. }
  60. else
  61. {
  62. Assert.Null(collection.JsonFormatter);
  63. }
  64. }
  65. [Fact]
  66. public void Constructor1_SetsDerivedFormatters()
  67. {
  68. // force to array to get stable instances
  69. MediaTypeFormatter[] derivedFormatters = HttpTestData.DerivedFormatters.ToArray();
  70. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(derivedFormatters);
  71. Assert.True(derivedFormatters.SequenceEqual(collection));
  72. }
  73. [Fact]
  74. public void Constructor1_ThrowsWithNullFormatters()
  75. {
  76. Assert.ThrowsArgumentNull(() => new MediaTypeFormatterCollection(null), "formatters");
  77. }
  78. [Fact]
  79. public void Constructor1_ThrowsWithNullFormatterInCollection()
  80. {
  81. Assert.ThrowsArgument(
  82. () => new MediaTypeFormatterCollection(new MediaTypeFormatter[] { null }), "formatters",
  83. Error.Format(Properties.Resources.CannotHaveNullInList,
  84. typeof(MediaTypeFormatter).Name));
  85. }
  86. [Fact]
  87. public void Constructor1_AcceptsDuplicateFormatterTypes()
  88. {
  89. MediaTypeFormatter[] formatters = new MediaTypeFormatter[]
  90. {
  91. new XmlMediaTypeFormatter(),
  92. new JsonMediaTypeFormatter(),
  93. #if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
  94. new FormUrlEncodedMediaTypeFormatter(),
  95. #endif
  96. new XmlMediaTypeFormatter(),
  97. new JsonMediaTypeFormatter(),
  98. #if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
  99. new FormUrlEncodedMediaTypeFormatter(),
  100. #endif
  101. };
  102. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(formatters);
  103. Assert.True(formatters.SequenceEqual(collection));
  104. }
  105. [Fact]
  106. public void MediaTypeFormatterCollection_Changing_FiresOnClear()
  107. {
  108. TestChanging((collection) => collection.Clear(), 1);
  109. }
  110. [Fact]
  111. public void MediaTypeFormatterCollection_Changing_FiresOnInsert()
  112. {
  113. TestChanging((collection) => collection.Insert(0, new XmlMediaTypeFormatter()), 1);
  114. }
  115. [Fact]
  116. public void MediaTypeFormatterCollection_Changing_FiresOnRemove()
  117. {
  118. TestChanging((collection) => collection.RemoveAt(0), 1);
  119. }
  120. [Fact]
  121. public void MediaTypeFormatterCollection_Changing_FiresOnSet()
  122. {
  123. TestChanging((collection) => collection[0] = new XmlMediaTypeFormatter(), 1);
  124. }
  125. private static void TestChanging(Action<MediaTypeFormatterCollection> mutation, int expectedCount)
  126. {
  127. // Arrange
  128. MediaTypeFormatter formatter1 = new XmlMediaTypeFormatter();
  129. MediaTypeFormatter formatter2 = new JsonMediaTypeFormatter();
  130. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(new MediaTypeFormatter[] { formatter1, formatter2 });
  131. int changeCount = 0;
  132. collection.Changing += (source, args) => { changeCount++; };
  133. // Act
  134. mutation(collection);
  135. //Assert
  136. Assert.Equal(expectedCount, changeCount);
  137. }
  138. [Fact]
  139. public void XmlFormatter_SetByCtor()
  140. {
  141. XmlMediaTypeFormatter formatter = new XmlMediaTypeFormatter();
  142. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(new MediaTypeFormatter[] { formatter });
  143. Assert.Same(formatter, collection.XmlFormatter);
  144. }
  145. [Fact]
  146. public void XmlFormatter_ClearedByCtor()
  147. {
  148. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(new MediaTypeFormatter[0]);
  149. Assert.Null(collection.XmlFormatter);
  150. }
  151. [Fact]
  152. public void JsonFormatter_SetByCtor()
  153. {
  154. JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter();
  155. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(new MediaTypeFormatter[] { formatter });
  156. Assert.Same(formatter, collection.JsonFormatter);
  157. }
  158. [Fact]
  159. public void JsonFormatter_ClearedByCtor()
  160. {
  161. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(new MediaTypeFormatter[0]);
  162. Assert.Null(collection.JsonFormatter);
  163. }
  164. #if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
  165. [Fact]
  166. public void FormUrlEncodedFormatter_SetByCtor()
  167. {
  168. FormUrlEncodedMediaTypeFormatter formatter = new FormUrlEncodedMediaTypeFormatter();
  169. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(new MediaTypeFormatter[] { formatter });
  170. Assert.Same(formatter, collection.FormUrlEncodedFormatter);
  171. }
  172. [Fact]
  173. public void FormUrlEncodedFormatter_ClearedByCtor()
  174. {
  175. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(new MediaTypeFormatter[0]);
  176. Assert.Null(collection.FormUrlEncodedFormatter);
  177. }
  178. #endif
  179. [Fact]
  180. public void Remove_SetsXmlFormatter()
  181. {
  182. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
  183. int count = collection.Count;
  184. collection.Remove(collection.XmlFormatter);
  185. Assert.Null(collection.XmlFormatter);
  186. Assert.Equal(count - 1, collection.Count);
  187. }
  188. [Fact]
  189. public void Remove_SetsJsonFormatter()
  190. {
  191. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
  192. int count = collection.Count;
  193. collection.Remove(collection.JsonFormatter);
  194. Assert.Null(collection.JsonFormatter);
  195. Assert.Equal(count - 1, collection.Count);
  196. }
  197. [Fact]
  198. public void Insert_SetsXmlFormatter()
  199. {
  200. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
  201. int count = collection.Count;
  202. XmlMediaTypeFormatter formatter = new XmlMediaTypeFormatter();
  203. collection.Insert(0, formatter);
  204. Assert.Same(formatter, collection.XmlFormatter);
  205. Assert.Equal(count + 1, collection.Count);
  206. }
  207. [Fact]
  208. public void Insert_SetsJsonFormatter()
  209. {
  210. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
  211. int count = collection.Count;
  212. JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter();
  213. collection.Insert(0, formatter);
  214. Assert.Same(formatter, collection.JsonFormatter);
  215. Assert.Equal(count + 1, collection.Count);
  216. }
  217. [Fact]
  218. public void FindReader_ThrowsOnNullType()
  219. {
  220. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
  221. MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("text/test");
  222. Assert.ThrowsArgumentNull(() => collection.FindReader(type: null, mediaType: mediaType), "type");
  223. }
  224. [Fact]
  225. public void FindReader_ThrowsOnNullMediaType()
  226. {
  227. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
  228. Assert.ThrowsArgumentNull(() => collection.FindReader(type: typeof(object), mediaType: null), "mediaType");
  229. }
  230. [Fact]
  231. public void FindReader_ReturnsNullOnNoMatch()
  232. {
  233. // Arrange
  234. MockMediaTypeFormatter formatter = new MockMediaTypeFormatter() { CallBase = true };
  235. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
  236. collection.Clear();
  237. collection.Add(formatter);
  238. MediaTypeHeaderValue contentType = new MediaTypeHeaderValue("text/test");
  239. // Act
  240. MediaTypeFormatter actualFormatter = collection.FindReader(typeof(object), contentType);
  241. // Assert
  242. Assert.Null(actualFormatter);
  243. }
  244. [Theory]
  245. [TestDataSet(
  246. typeof(CommonUnitTestDataSets), "RepresentativeValueAndRefTypeTestDataCollection",
  247. typeof(HttpTestData), "LegalMediaTypeStrings")]
  248. public void FindReader_ReturnsFormatterOnMatch(Type variationType, object testData, string mediaType)
  249. {
  250. // Arrange
  251. MockMediaTypeFormatter formatter = new MockMediaTypeFormatter() { CallBase = true };
  252. foreach (string legalMediaType in HttpTestData.LegalMediaTypeStrings)
  253. {
  254. formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue(legalMediaType));
  255. }
  256. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
  257. collection.Clear();
  258. collection.Add(formatter);
  259. MediaTypeHeaderValue contentType = new MediaTypeHeaderValue(mediaType);
  260. // Act
  261. MediaTypeFormatter actualFormatter = collection.FindReader(variationType, contentType);
  262. // Assert
  263. Assert.Same(formatter, actualFormatter);
  264. }
  265. [Fact]
  266. public void FindWriter_ThrowsOnNullType()
  267. {
  268. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
  269. MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("text/test");
  270. Assert.ThrowsArgumentNull(() => collection.FindWriter(type: null, mediaType: mediaType), "type");
  271. }
  272. [Fact]
  273. public void FindWriter_ThrowsOnNullMediaType()
  274. {
  275. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
  276. Assert.ThrowsArgumentNull(() => collection.FindWriter(type: typeof(object), mediaType: null), "mediaType");
  277. }
  278. [Fact]
  279. public void FindWriter_ReturnsNullOnNoMatch()
  280. {
  281. // Arrange
  282. MockMediaTypeFormatter formatter = new MockMediaTypeFormatter() { CallBase = true };
  283. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
  284. collection.Clear();
  285. collection.Add(formatter);
  286. MediaTypeHeaderValue contentType = new MediaTypeHeaderValue("text/test");
  287. // Act
  288. MediaTypeFormatter actualFormatter = collection.FindWriter(typeof(object), contentType);
  289. // Assert
  290. Assert.Null(actualFormatter);
  291. }
  292. [Theory]
  293. [TestDataSet(
  294. typeof(CommonUnitTestDataSets), "RepresentativeValueAndRefTypeTestDataCollection",
  295. typeof(HttpTestData), "LegalMediaTypeStrings")]
  296. public void FindWriter_ReturnsFormatterOnMatch(Type variationType, object testData, string mediaType)
  297. {
  298. // Arrange
  299. MockMediaTypeFormatter formatter = new MockMediaTypeFormatter() { CallBase = true };
  300. foreach (string legalMediaType in HttpTestData.LegalMediaTypeStrings)
  301. {
  302. formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue(mediaType));
  303. }
  304. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
  305. collection.Clear();
  306. collection.Add(formatter);
  307. MediaTypeHeaderValue contentType = new MediaTypeHeaderValue(mediaType);
  308. // Act
  309. MediaTypeFormatter actualFormatter = collection.FindWriter(variationType, contentType);
  310. // Assert
  311. Assert.Same(formatter, actualFormatter);
  312. }
  313. [Theory]
  314. [InlineData(typeof(JObject))]
  315. [InlineData(typeof(XAttribute))]
  316. [InlineData(typeof(Type))]
  317. [InlineData(typeof(byte[]))]
  318. #if !NETFX_CORE
  319. [InlineData(typeof(XmlElement))]
  320. [InlineData(typeof(FormDataCollection))]
  321. #endif
  322. public void IsTypeExcludedFromValidation_ReturnsTrueForExcludedTypes(Type type)
  323. {
  324. Assert.True(MediaTypeFormatterCollection.IsTypeExcludedFromValidation(type));
  325. }
  326. [Fact]
  327. public void WritingFormatters_FiltersOutCanWriteAnyTypesFalse()
  328. {
  329. // Arrange
  330. MockMediaTypeFormatter writableFormatter = new MockMediaTypeFormatter();
  331. MockMediaTypeFormatter readOnlyFormatter = new MockMediaTypeFormatter() { CanWriteAnyTypesReturn = false };
  332. List<MediaTypeFormatter> formatters = new List<MediaTypeFormatter>() { writableFormatter, readOnlyFormatter };
  333. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(formatters);
  334. // Act
  335. MediaTypeFormatter[] writableFormatters = collection.WritingFormatters;
  336. // Assert
  337. MediaTypeFormatter[] expectedFormatters = new MediaTypeFormatter[] { writableFormatter };
  338. Assert.Equal(expectedFormatters, writableFormatters);
  339. }
  340. [Fact]
  341. public void WritingFormatters_FiltersOutNull()
  342. {
  343. // Arrange
  344. MockMediaTypeFormatter writableFormatter = new MockMediaTypeFormatter();
  345. List<MediaTypeFormatter> formatters = new List<MediaTypeFormatter>() { writableFormatter };
  346. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(formatters);
  347. collection.Add(null);
  348. // Act
  349. MediaTypeFormatter[] writableFormatters = collection.WritingFormatters;
  350. // Assert
  351. MediaTypeFormatter[] expectedFormatters = new MediaTypeFormatter[] { writableFormatter };
  352. Assert.Equal(expectedFormatters, writableFormatters);
  353. }
  354. [Fact]
  355. public void WritingFormatters_Caches()
  356. {
  357. // Arrange
  358. MockMediaTypeFormatter formatter1 = new MockMediaTypeFormatter();
  359. MockMediaTypeFormatter formatter2 = new MockMediaTypeFormatter();
  360. List<MediaTypeFormatter> formatters = new List<MediaTypeFormatter>() { formatter1, formatter2 };
  361. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(formatters);
  362. // Act
  363. MediaTypeFormatter[] writableFormatters1 = collection.WritingFormatters;
  364. MediaTypeFormatter[] writableFormatters2 = collection.WritingFormatters;
  365. // Assert
  366. MediaTypeFormatter[] expectedFormatters = formatters.ToArray();
  367. Assert.Equal(expectedFormatters, writableFormatters1);
  368. Assert.Same(writableFormatters1, writableFormatters2);
  369. }
  370. [Fact]
  371. public void WritingFormatters_Insert_ResetsCache()
  372. {
  373. TestWritingFormattersCacheReset((collection) => collection.Insert(0, new MockMediaTypeFormatter()));
  374. }
  375. [Fact]
  376. public void WritingFormatters_RemoveAt_ResetsCache()
  377. {
  378. TestWritingFormattersCacheReset((collection) => collection.RemoveAt(0));
  379. }
  380. private static void TestWritingFormattersCacheReset(Action<MediaTypeFormatterCollection> mutation)
  381. {
  382. // Arrange
  383. MockMediaTypeFormatter formatter1 = new MockMediaTypeFormatter();
  384. MockMediaTypeFormatter formatter2 = new MockMediaTypeFormatter();
  385. List<MediaTypeFormatter> formatters = new List<MediaTypeFormatter>() { formatter1, formatter2 };
  386. MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(formatters);
  387. // Act
  388. mutation(collection);
  389. MediaTypeFormatter[] expectedFormatters = collection.ToArray();
  390. MediaTypeFormatter[] writableFormatters = collection.WritingFormatters;
  391. // Assert
  392. Assert.Equal(expectedFormatters, writableFormatters);
  393. }
  394. }
  395. }