/src/Manos.Tests/Manos.Http/HttpHeadersTest.cs

http://github.com/jacksonh/manos · C# · 344 lines · 246 code · 70 blank · 28 comment · 0 complexity · 69bf4a48e7eab74cd36680bed2f6228e MD5 · raw file

  1. //
  2. // Copyright (C) 2010 Jackson Harper (jackson@manosdemono.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. //
  24. using System;
  25. using System.IO;
  26. using NUnit.Framework;
  27. using Manos.ShouldExt;
  28. namespace Manos.Http.Tests
  29. {
  30. [TestFixture()]
  31. public class HttpHeadersTest
  32. {
  33. [Test()]
  34. public void TestCtor ()
  35. {
  36. HttpHeaders headers = new HttpHeaders ();
  37. Assert.IsNull (headers.ContentLength, "a1");
  38. }
  39. [Test()]
  40. public void TestSingleValueParse ()
  41. {
  42. HttpHeaders headers = new HttpHeaders ();
  43. string str = "Key: Value";
  44. headers.Parse (new StringReader (str));
  45. Assert.AreEqual ("Value", headers ["Key"], "a1");
  46. Assert.AreEqual (1, headers.Count, "a2");
  47. }
  48. [Test()]
  49. public void TestSingleValueParseTrailingWhiteSpace ()
  50. {
  51. HttpHeaders headers = new HttpHeaders ();
  52. string str = "Key: Value ";
  53. headers.Parse (new StringReader (str));
  54. Assert.AreEqual ("Value", headers ["Key"], "a1");
  55. Assert.AreEqual (1, headers.Count, "a2");
  56. str = "Key: Value\t";
  57. headers.Parse (new StringReader (str));
  58. Assert.AreEqual ("Value", headers ["Key"], "a1");
  59. Assert.AreEqual (1, headers.Count, "a2");
  60. str = "Key: Value ";
  61. headers.Parse (new StringReader (str));
  62. Assert.AreEqual ("Value", headers ["Key"], "a1");
  63. Assert.AreEqual (1, headers.Count, "a2");
  64. }
  65. [Test()]
  66. public void TestValueIsJustWhiteSpace ()
  67. {
  68. HttpHeaders headers = new HttpHeaders ();
  69. string str = "Key: ";
  70. Should.Throw<HttpException> (() => headers.Parse (new StringReader (str)));
  71. Assert.AreEqual (0, headers.Count, "a2");
  72. }
  73. [Test()]
  74. public void TestWhiteSpaceStartsFirstLine ()
  75. {
  76. HttpHeaders headers = new HttpHeaders ();
  77. string str = " Key: Value";
  78. Should.Throw<HttpException> (() => headers.Parse (new StringReader (str)));
  79. Assert.AreEqual (0, headers.Count, "a2");
  80. }
  81. [Test()]
  82. public void TestMultipleValueParse ()
  83. {
  84. HttpHeaders headers = new HttpHeaders ();
  85. string str = "Key1: Value1\nKey2: Value2\nKey3: Value3";
  86. headers.Parse (new StringReader (str));
  87. Assert.AreEqual ("Value1", headers ["Key1"], "a1");
  88. Assert.AreEqual ("Value2", headers ["Key2"], "a2");
  89. Assert.AreEqual ("Value3", headers ["Key3"], "a3");
  90. Assert.AreEqual (3, headers.Count, "a4");
  91. }
  92. [Test()]
  93. public void TestMultilineParse ()
  94. {
  95. //
  96. // multiline values are acceptable if the next
  97. // line starts with spaces
  98. //
  99. string header = @"HeaderName: Some multiline
  100. value";
  101. HttpHeaders headers = new HttpHeaders ();
  102. headers.Parse (new StringReader (header));
  103. Assert.AreEqual ("Some multiline value", headers ["HeaderName"], "a1");
  104. header = @"HeaderName: Some multiline
  105. value
  106. that spans
  107. a bunch of lines";
  108. headers = new HttpHeaders ();
  109. headers.Parse (new StringReader (header));
  110. Assert.AreEqual ("Some multiline value that spans a bunch of lines", headers ["HeaderName"], "a2");
  111. }
  112. [Test]
  113. public void TestParseNoValue ()
  114. {
  115. HttpHeaders headers = new HttpHeaders ();
  116. string str = "Key:\n";
  117. Should.Throw<HttpException> (() => headers.Parse (new StringReader (str)));
  118. Assert.AreEqual (0, headers.Count, "a2");
  119. Assert.IsNull (headers.ContentLength, "a3");
  120. }
  121. [Test]
  122. public void TestParseNoColon ()
  123. {
  124. HttpHeaders headers = new HttpHeaders ();
  125. string str = "Key value";
  126. Should.Throw<HttpException> (() => headers.Parse (new StringReader (str)));
  127. Assert.AreEqual (0, headers.Count, "a2");
  128. Assert.IsNull (headers.ContentLength, "a3");
  129. }
  130. [Test()]
  131. public void TestNormalizeNoDash ()
  132. {
  133. Assert.AreEqual ("Foo", HttpHeaders.NormalizeName ("foo"));
  134. Assert.AreEqual ("Foo", HttpHeaders.NormalizeName ("FOO"));
  135. Assert.AreEqual ("Foo", HttpHeaders.NormalizeName ("FOo"));
  136. Assert.AreEqual ("Foo", HttpHeaders.NormalizeName ("foO"));
  137. }
  138. [Test()]
  139. public void TestNormalizeDashedName ()
  140. {
  141. Assert.AreEqual ("Foo-Bar", HttpHeaders.NormalizeName ("foo-bar"));
  142. Assert.AreEqual ("Foo-Bar", HttpHeaders.NormalizeName ("FOO-BAR"));
  143. Assert.AreEqual ("Foo-Bar", HttpHeaders.NormalizeName ("Foo-bar"));
  144. Assert.AreEqual ("Foo-Bar", HttpHeaders.NormalizeName ("foo-BAR"));
  145. Assert.AreEqual ("Foo-Bar", HttpHeaders.NormalizeName ("Foo-BaR"));
  146. }
  147. [Test()]
  148. public void TestNormalizeDoubleDash ()
  149. {
  150. Assert.AreEqual ("Foo--Bar", HttpHeaders.NormalizeName ("foo--bar"));
  151. Assert.AreEqual ("Foo--Bar", HttpHeaders.NormalizeName ("FOO--BAR"));
  152. Assert.AreEqual ("Foo--Bar", HttpHeaders.NormalizeName ("Foo--bar"));
  153. Assert.AreEqual ("Foo--Bar", HttpHeaders.NormalizeName ("foo--BAR"));
  154. Assert.AreEqual ("Foo--Bar", HttpHeaders.NormalizeName ("Foo--BaR"));
  155. }
  156. [Test()]
  157. public void TestNormalizeStartDash ()
  158. {
  159. Assert.AreEqual ("-Foo-Bar", HttpHeaders.NormalizeName ("-foo-bar"));
  160. Assert.AreEqual ("-Foo-Bar", HttpHeaders.NormalizeName ("-FOO-BAR"));
  161. Assert.AreEqual ("-Foo-Bar", HttpHeaders.NormalizeName ("-Foo-bar"));
  162. Assert.AreEqual ("-Foo-Bar", HttpHeaders.NormalizeName ("-foo-BAR"));
  163. Assert.AreEqual ("-Foo-Bar", HttpHeaders.NormalizeName ("-Foo-BaR"));
  164. }
  165. [Test()]
  166. public void TestNormalizeEndDash ()
  167. {
  168. Assert.AreEqual ("Foo-Bar-", HttpHeaders.NormalizeName ("foo-bar-"));
  169. Assert.AreEqual ("Foo-Bar-", HttpHeaders.NormalizeName ("FOO-BAR-"));
  170. Assert.AreEqual ("Foo-Bar-", HttpHeaders.NormalizeName ("Foo-bar-"));
  171. Assert.AreEqual ("Foo-Bar-", HttpHeaders.NormalizeName ("foo-BAR-"));
  172. Assert.AreEqual ("Foo-Bar-", HttpHeaders.NormalizeName ("Foo-BaR-"));
  173. }
  174. [Test]
  175. public void TestSetValue ()
  176. {
  177. HttpHeaders headers = new HttpHeaders ();
  178. headers.SetHeader ("Foo", "Bar");
  179. Assert.AreEqual ("Bar", headers ["Foo"], "a1");
  180. // Keys should get normalized
  181. headers.SetHeader ("foo", "bar");
  182. Assert.AreEqual ("bar", headers ["foo"], "a2");
  183. Assert.AreEqual ("bar", headers ["Foo"], "a3");
  184. }
  185. [Test]
  186. public void TestSetValueNullRemovesKey ()
  187. {
  188. string dummy = null;
  189. HttpHeaders headers = new HttpHeaders ();
  190. headers.SetHeader ("Foo", "Bar");
  191. Assert.AreEqual ("Bar", headers ["Foo"], "a1");
  192. headers.SetHeader ("Foo", null);
  193. Assert.IsFalse (headers.TryGetValue ("Foo", out dummy), "a2");
  194. Assert.AreEqual (0, headers.Count, "a3");
  195. }
  196. [Test]
  197. public void TestSetContentLength ()
  198. {
  199. HttpHeaders headers = new HttpHeaders ();
  200. headers.SetContentLength ("100");
  201. Assert.AreEqual (100, headers.ContentLength, "a1");
  202. headers.SetContentLength ("1");
  203. Assert.AreEqual (1, headers.ContentLength, "a2");
  204. headers.SetContentLength ("0");
  205. Assert.AreEqual (0, headers.ContentLength, "a3");
  206. }
  207. [Test]
  208. public void TestSetContentLengthNegative ()
  209. {
  210. HttpHeaders headers = new HttpHeaders ();
  211. Should.Throw<ArgumentException> (() => headers.SetContentLength ("-1"));
  212. }
  213. [Test]
  214. public void TestSetContentLengthInvalid ()
  215. {
  216. HttpHeaders headers = new HttpHeaders ();
  217. Should.Throw<ArgumentException> (() => headers.SetContentLength ("foobar"));
  218. Should.Throw<ArgumentException> (() => headers.SetContentLength ("-1 foobar"));
  219. Should.Throw<ArgumentException> (() => headers.SetContentLength ("1foo"));
  220. Should.Throw<ArgumentException> (() => headers.SetContentLength ("1FA"));
  221. Should.Throw<ArgumentException> (() => headers.SetContentLength ("1."));
  222. Should.Throw<ArgumentException> (() => headers.SetContentLength ("1.0"));
  223. }
  224. [Test]
  225. public void TestSetContentLengthNullClearsValue ()
  226. {
  227. HttpHeaders headers = new HttpHeaders ();
  228. headers.SetContentLength ("100");
  229. Assert.AreEqual (100, headers.ContentLength, "a1");
  230. headers.SetContentLength (null);
  231. Assert.IsNull (headers.ContentLength, "a2");
  232. }
  233. [Test]
  234. public void TestSetContentLengthProperty ()
  235. {
  236. string cl;
  237. HttpHeaders headers = new HttpHeaders ();
  238. headers.ContentLength = 10;
  239. Assert.AreEqual (10, headers.ContentLength, "a1");
  240. Assert.IsTrue (headers.TryGetValue ("Content-Length", out cl), "a2");
  241. Assert.AreEqual ("10", cl, "a3");
  242. headers.ContentLength = 100;
  243. Assert.AreEqual (100, headers.ContentLength, "a4");
  244. Assert.IsTrue (headers.TryGetValue ("Content-Length", out cl), "a5");
  245. Assert.AreEqual ("100", cl, "a6");
  246. headers.ContentLength = 0;
  247. Assert.AreEqual (0, headers.ContentLength, "a7");
  248. Assert.IsTrue (headers.TryGetValue ("Content-Length", out cl), "a8");
  249. Assert.AreEqual ("0", cl, "a9");
  250. }
  251. [Test]
  252. public void TestSetContentLengthPropertyNull ()
  253. {
  254. string dummy = null;
  255. HttpHeaders headers = new HttpHeaders ();
  256. headers.ContentLength = 100;
  257. Assert.AreEqual (100, headers.ContentLength, "a1");
  258. headers.ContentLength = null;
  259. Assert.IsNull (headers.ContentLength, "a2");
  260. Assert.IsFalse (headers.TryGetValue ("Content-Length", out dummy), "a3");
  261. Assert.IsNull (dummy, "a4");
  262. }
  263. [Test]
  264. public void TestSetContentLengthPropertyNegative ()
  265. {
  266. string dummy = null;
  267. HttpHeaders headers = new HttpHeaders ();
  268. Should.Throw<ArgumentException> (() => headers.ContentLength = -1, "a1");
  269. Assert.IsNull (headers.ContentLength, "a2");
  270. Assert.IsFalse (headers.TryGetValue ("Content-Length", out dummy), "a3");
  271. Assert.IsNull (dummy, "a4");
  272. }
  273. }
  274. }