2
0

ContentDispositionHeaderValueTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // ContentDispositionHeaderValueTest.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using NUnit.Framework;
  32. using System.Net.Http.Headers;
  33. using System.Linq;
  34. namespace MonoTests.System.Net.Http.Headers
  35. {
  36. [TestFixture]
  37. public class ContentDispositionHeaderValueTest
  38. {
  39. [Test]
  40. public void Ctor_InvalidArguments ()
  41. {
  42. try {
  43. new ContentDispositionHeaderValue (null);
  44. Assert.Fail ("#1");
  45. } catch (ArgumentException) {
  46. }
  47. try {
  48. new ContentDispositionHeaderValue (" ");
  49. Assert.Fail ("#2");
  50. } catch (FormatException) {
  51. }
  52. }
  53. [Test]
  54. /*
  55. * This fails on Windows with the .NET runtime:
  56. *
  57. * Test Case Failures:
  58. * 1) MonoTests.System.Net.Http.Headers.ContentDispositionHeaderValueTest.Equals : System.NullReferenceException : Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
  59. * bei System.Net.Http.Headers.ContentDispositionHeaderValue.set_Size(Nullable`1 value)
  60. * bei MonoTests.System.Net.Http.Headers.ContentDispositionHeaderValueTest.Equals()
  61. *
  62. */
  63. [Category ("NotWorking")]
  64. public void Equals ()
  65. {
  66. var value = new ContentDispositionHeaderValue ("x");
  67. Assert.AreEqual (value, new ContentDispositionHeaderValue ("x"), "#1");
  68. Assert.AreNotEqual (value, new ContentDispositionHeaderValue ("y"), "#2");
  69. value = new ContentDispositionHeaderValue ("attachment");
  70. value.Parameters.Add (new NameValueHeaderValue ("size", "66"));
  71. Assert.AreEqual (value, new ContentDispositionHeaderValue ("attachment") { Size = 66 }, "#3");
  72. Assert.AreNotEqual (value, new ContentDispositionHeaderValue ("attachment"), "#4");
  73. Assert.AreNotEqual (value, new ContentDispositionHeaderValue ("attachment") { FileName="g" }, "#5");
  74. }
  75. [Test]
  76. public void Parse ()
  77. {
  78. var res = ContentDispositionHeaderValue.Parse ("attachment");
  79. Assert.AreEqual ("attachment", res.DispositionType, "#1");
  80. Assert.AreEqual ("attachment", res.ToString (), "#2");
  81. res = ContentDispositionHeaderValue.Parse ("attachmen;filename=foo;size=44; name=n2; filename*=UTF-8''Na%C3%AFve%20file.txt; creation-date=\"Wed, 02 Oct 2002 15:00:00 +0200\";modification-date=\"Wed, 02 Oct 2002 13:00:00 GMT\";read-date=\"Wed, 02 Oct 2002 15:00:00 +0000\";other=1");
  82. Assert.AreEqual (new DateTimeOffset (2002, 10, 2, 15, 0, 0, TimeSpan.FromHours (2)), res.CreationDate, "#10");
  83. Assert.AreEqual ("attachmen", res.DispositionType, "#11");
  84. Assert.AreEqual ("foo", res.FileName, "#12");
  85. Assert.AreEqual ("Naïve file.txt", res.FileNameStar, "#13");
  86. Assert.AreEqual (new DateTimeOffset (2002, 10, 2, 13, 0, 0, TimeSpan.Zero), res.ModificationDate, "#14");
  87. Assert.AreEqual ("n2", res.Name, "#15");
  88. Assert.AreEqual (8, res.Parameters.Count, "#16");
  89. Assert.AreEqual (new DateTimeOffset (2002, 10, 2, 15, 0, 0, TimeSpan.Zero), res.ReadDate, "#17");
  90. Assert.AreEqual (44, res.Size, "#18");
  91. Assert.AreEqual ("attachmen; filename=foo; size=44; name=n2; filename*=UTF-8''Na%C3%AFve%20file.txt; creation-date=\"Wed, 02 Oct 2002 15:00:00 +0200\"; modification-date=\"Wed, 02 Oct 2002 13:00:00 GMT\"; read-date=\"Wed, 02 Oct 2002 15:00:00 +0000\"; other=1", res.ToString (), "#19");
  92. res = ContentDispositionHeaderValue.Parse ("attachment; filename='foo.bar'");
  93. Assert.AreEqual ("attachment", res.DispositionType, "#21");
  94. Assert.AreEqual ("'foo.bar'", res.FileName, "#22");
  95. Assert.AreEqual ("attachment; filename='foo.bar'", res.ToString (), "#23");
  96. ContentDispositionHeaderValue.Parse ("aa;size=a4");
  97. res = ContentDispositionHeaderValue.Parse ("att;filename=\"=?utf-8?B?xI0=?=\"");
  98. Assert.AreEqual ("č", res.FileName, "#31");
  99. Assert.IsNull (res.FileNameStar, "#32");
  100. res = ContentDispositionHeaderValue.Parse ("att;filename=\"=?utf-?B?xI0=?=\"");
  101. Assert.AreEqual ("\"=?utf-?B?xI0=?=\"", res.FileName, "#41");
  102. res = ContentDispositionHeaderValue.Parse ("att;filename=\"=?utf-16?b?xI0=?=\"");
  103. Assert.AreEqual ("跄", res.FileName, "#51");
  104. res = ContentDispositionHeaderValue.Parse ("att;filename=\"=?utf-8?B?x/I0=?=\"");
  105. Assert.AreEqual ("\"=?utf-8?B?x/I0=?=\"", res.FileName, "#61");
  106. res = ContentDispositionHeaderValue.Parse ("att;filename*=utf-8''%C4%8Eas");
  107. Assert.AreEqual ("Ďas", res.FileNameStar, "#71");
  108. res = ContentDispositionHeaderValue.Parse ("att;filename*=btf-8''%C4%8E");
  109. Assert.IsNull (res.FileNameStar, "#72");
  110. res = ContentDispositionHeaderValue.Parse ("att;filename*=utf-8''%T4%8O%");
  111. Assert.AreEqual ("%T4%8O%", res.FileNameStar, "#73");
  112. }
  113. [Test]
  114. public void Parse_Invalid ()
  115. {
  116. try {
  117. ContentDispositionHeaderValue.Parse (null);
  118. Assert.Fail ("#1");
  119. } catch (FormatException) {
  120. }
  121. try {
  122. ContentDispositionHeaderValue.Parse (" ");
  123. Assert.Fail ("#2");
  124. } catch (FormatException) {
  125. }
  126. try {
  127. ContentDispositionHeaderValue.Parse ("attachment; filename=foo.html ;");
  128. Assert.Fail ("#3");
  129. } catch (FormatException) {
  130. }
  131. try {
  132. ContentDispositionHeaderValue.Parse ("attachment; filename=foo bar.html");
  133. Assert.Fail ("#4");
  134. } catch (FormatException) {
  135. }
  136. try {
  137. ContentDispositionHeaderValue.Parse ("\"attachment\"");
  138. Assert.Fail ("#5");
  139. } catch (FormatException) {
  140. }
  141. try {
  142. ContentDispositionHeaderValue.Parse ("att;filename*=utf-8''%T4%8╗");
  143. Assert.Fail ("#6");
  144. } catch (FormatException) {
  145. }
  146. }
  147. [Test]
  148. public void Properties ()
  149. {
  150. var value = new ContentDispositionHeaderValue ("ttt");
  151. Assert.IsNull (value.CreationDate, "#1");
  152. Assert.AreEqual ("ttt", value.DispositionType, "#2");
  153. Assert.IsNull (value.FileName, "#3");
  154. Assert.IsNull (value.FileNameStar, "#4");
  155. Assert.IsNull (value.ModificationDate, "#5");
  156. Assert.IsNull (value.Name, "#6");
  157. Assert.AreEqual (0, value.Parameters.Count, "#7");
  158. Assert.IsNull (value.ReadDate, "#8");
  159. Assert.IsNull (value.Size, "#9");
  160. value.Parameters.Add (new NameValueHeaderValue ("creation-date", "\"20 Jun 82 11:34:11\""));
  161. value.Parameters.Add (new NameValueHeaderValue ("filename", "g*"));
  162. value.Parameters.Add (new NameValueHeaderValue ("filename*", "ag*"));
  163. value.Parameters.Add (new NameValueHeaderValue ("modification-date", "\"20 Jun 22 4:6:22\""));
  164. value.Parameters.Add (new NameValueHeaderValue ("name", "nnn"));
  165. value.Parameters.Add (new NameValueHeaderValue ("read-date", "\"1 Jun 01 1:1:1\""));
  166. value.Parameters.Add (new NameValueHeaderValue ("size", "5"));
  167. Assert.AreEqual (new DateTimeOffset (1982, 6, 20, 11, 34, 11, TimeSpan.Zero), value.CreationDate, "#11");
  168. Assert.AreEqual ("g*", value.FileName, "#12");
  169. Assert.IsNull (value.FileNameStar, "#13");
  170. Assert.AreEqual (new DateTimeOffset (2022, 6, 20, 4, 6, 22, TimeSpan.Zero), value.ModificationDate, "#14");
  171. Assert.AreEqual ("nnn", value.Name, "#15");
  172. Assert.AreEqual (new DateTimeOffset (2001, 6, 1, 1, 1, 1, TimeSpan.Zero), value.ReadDate, "#16");
  173. Assert.AreEqual (5, value.Size, "#17");
  174. }
  175. [Test]
  176. public void Properties_FileName ()
  177. {
  178. var value = new ContentDispositionHeaderValue ("a");
  179. value.FileName = "aa";
  180. Assert.AreEqual ("aa", value.FileName, "#1");
  181. Assert.AreEqual (new NameValueHeaderValue ("filename", "aa"), value.Parameters.First (), "#2");
  182. value.FileName = "č";
  183. Assert.AreEqual ("č", value.FileName, "#11");
  184. Assert.AreEqual (new NameValueHeaderValue ("filename", "\"=?utf-8?B?xI0=?=\""), value.Parameters.First (), "#12");
  185. value.FileName = "(@)";
  186. Assert.AreEqual ("\"(@)\"", value.FileName, "#21");
  187. Assert.AreEqual (new NameValueHeaderValue ("filename", "\"(@)\""), value.Parameters.First (), "#22");
  188. }
  189. [Test]
  190. public void Properties_FileNameStar ()
  191. {
  192. var value = new ContentDispositionHeaderValue ("a");
  193. value.FileNameStar = "aa";
  194. Assert.AreEqual ("aa", value.FileNameStar, "#1");
  195. Assert.AreEqual (new NameValueHeaderValue ("filename*", "utf-8''aa"), value.Parameters.First (), "#2");
  196. value.FileNameStar = "č";
  197. Assert.AreEqual ("č", value.FileNameStar, "#11");
  198. Assert.AreEqual (new NameValueHeaderValue ("filename*", "utf-8''%C4%8D"), value.Parameters.First (), "#12");
  199. }
  200. [Test]
  201. public void Properties_ModificationDate ()
  202. {
  203. var value = new ContentDispositionHeaderValue ("a");
  204. value.ModificationDate = new DateTimeOffset (2010, 12, 30, 22, 34, 2, TimeSpan.Zero); //.FromHours (-3));
  205. Assert.AreEqual (new DateTimeOffset (2010, 12, 30, 22, 34, 2, TimeSpan.Zero), value.ModificationDate, "#1");
  206. Assert.AreEqual (new NameValueHeaderValue ("modification-date", "\"Thu, 30 Dec 2010 22:34:02 GMT\""), value.Parameters.First (), "#2");
  207. }
  208. [Test]
  209. public void Properties_Invalid ()
  210. {
  211. var value = new ContentDispositionHeaderValue ("a");
  212. try {
  213. value.Size = -9;
  214. Assert.Fail ("#1");
  215. } catch (ArgumentOutOfRangeException) {
  216. }
  217. }
  218. [Test]
  219. public void TryParse ()
  220. {
  221. ContentDispositionHeaderValue res;
  222. Assert.IsTrue (ContentDispositionHeaderValue.TryParse ("attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4; filename*=ISO-8859-1''currency-sign%3d%a4", out res), "#1");
  223. Assert.AreEqual ("attachment", res.DispositionType, "#2");
  224. Assert.AreEqual ("currency-sign=¤", res.FileNameStar, "#3");
  225. }
  226. [Test]
  227. public void TryParse_Invalid ()
  228. {
  229. ContentDispositionHeaderValue res;
  230. Assert.IsFalse (ContentDispositionHeaderValue.TryParse ("", out res), "#1");
  231. Assert.IsNull (res, "#2");
  232. }
  233. }
  234. }