PageRenderTime 53ms CodeModel.GetById 30ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 25
 26using System;
 27using System.IO;
 28
 29using NUnit.Framework;
 30using Manos.ShouldExt;
 31
 32
 33
 34namespace Manos.Http.Tests
 35{
 36	[TestFixture()]
 37	public class HttpHeadersTest
 38	{
 39
 40		[Test()]
 41		public void TestCtor ()
 42		{
 43			HttpHeaders headers = new HttpHeaders ();
 44			
 45			Assert.IsNull (headers.ContentLength, "a1");
 46		}
 47		
 48		[Test()]
 49		public void TestSingleValueParse ()
 50		{
 51			HttpHeaders headers = new HttpHeaders ();
 52			
 53			string str = "Key: Value";
 54			
 55			headers.Parse (new StringReader (str));
 56			
 57			Assert.AreEqual ("Value", headers ["Key"], "a1");
 58			Assert.AreEqual (1, headers.Count, "a2");
 59		}
 60		
 61		[Test()]
 62		public void TestSingleValueParseTrailingWhiteSpace ()
 63		{
 64			HttpHeaders headers = new HttpHeaders ();
 65			
 66			string str = "Key: Value      ";
 67			headers.Parse (new StringReader (str));
 68			Assert.AreEqual ("Value", headers ["Key"], "a1");
 69			Assert.AreEqual (1, headers.Count, "a2");
 70			
 71			str = "Key: Value\t";
 72			headers.Parse (new StringReader (str));
 73			Assert.AreEqual ("Value", headers ["Key"], "a1");
 74			Assert.AreEqual (1, headers.Count, "a2");
 75			
 76			str = "Key: Value ";
 77			headers.Parse (new StringReader (str));
 78			Assert.AreEqual ("Value", headers ["Key"], "a1");
 79			Assert.AreEqual (1, headers.Count, "a2");
 80		}
 81		
 82		[Test()]
 83		public void TestValueIsJustWhiteSpace ()
 84		{
 85			HttpHeaders headers = new HttpHeaders ();
 86			
 87			string str = "Key: ";
 88			
 89			Should.Throw<HttpException> (() => headers.Parse (new StringReader (str)));
 90			Assert.AreEqual (0, headers.Count, "a2");
 91		}
 92		
 93		[Test()]
 94		public void TestWhiteSpaceStartsFirstLine ()
 95		{
 96			HttpHeaders headers = new HttpHeaders ();
 97			
 98			string str = " Key: Value";
 99			
100			Should.Throw<HttpException> (() => headers.Parse (new StringReader (str)));
101			Assert.AreEqual (0, headers.Count, "a2");
102		}
103		
104		[Test()]
105		public void TestMultipleValueParse ()
106		{
107			HttpHeaders headers = new HttpHeaders ();
108			
109			string str = "Key1: Value1\nKey2: Value2\nKey3: Value3";
110			
111			headers.Parse (new StringReader (str));
112			
113			Assert.AreEqual ("Value1", headers ["Key1"], "a1");
114			Assert.AreEqual ("Value2", headers ["Key2"], "a2");
115			Assert.AreEqual ("Value3", headers ["Key3"], "a3");
116			Assert.AreEqual (3, headers.Count, "a4");
117		}
118		
119		[Test()]
120		public void TestMultilineParse ()
121		{
122			//
123			// multiline values are acceptable if the next 
124			// line starts with spaces
125			//
126			string header = @"HeaderName: Some multiline
127  								value";
128		
129			HttpHeaders headers = new HttpHeaders ();
130			
131			headers.Parse (new StringReader (header));
132			
133			Assert.AreEqual ("Some multiline value", headers ["HeaderName"], "a1");
134			
135			header = @"HeaderName: Some multiline
136  								value
137	that spans
138	a bunch of lines";
139			
140			headers = new HttpHeaders ();
141			headers.Parse (new StringReader (header));
142			
143			Assert.AreEqual ("Some multiline value that spans a bunch of lines", headers ["HeaderName"], "a2");
144		}
145		
146		[Test]
147		public void TestParseNoValue ()
148		{
149			HttpHeaders headers = new HttpHeaders ();
150			
151			string str = "Key:\n";
152			
153			Should.Throw<HttpException> (() => headers.Parse (new StringReader (str)));
154			Assert.AreEqual (0, headers.Count, "a2");
155			Assert.IsNull (headers.ContentLength, "a3");
156		}
157		
158		[Test]
159		public void TestParseNoColon ()
160		{
161			HttpHeaders headers = new HttpHeaders ();
162			
163			string str = "Key value";
164			
165			Should.Throw<HttpException> (() => headers.Parse (new StringReader (str)));
166			Assert.AreEqual (0, headers.Count, "a2");
167			Assert.IsNull (headers.ContentLength, "a3");
168		}
169		
170		[Test()]
171		public void TestNormalizeNoDash ()
172		{	
173			Assert.AreEqual ("Foo", HttpHeaders.NormalizeName ("foo"));
174			Assert.AreEqual ("Foo", HttpHeaders.NormalizeName ("FOO"));
175			Assert.AreEqual ("Foo", HttpHeaders.NormalizeName ("FOo"));
176			Assert.AreEqual ("Foo", HttpHeaders.NormalizeName ("foO"));
177		}
178		
179		[Test()]
180		public void TestNormalizeDashedName ()
181		{
182			Assert.AreEqual ("Foo-Bar", HttpHeaders.NormalizeName ("foo-bar"));
183			Assert.AreEqual ("Foo-Bar", HttpHeaders.NormalizeName ("FOO-BAR"));
184			Assert.AreEqual ("Foo-Bar", HttpHeaders.NormalizeName ("Foo-bar"));
185			Assert.AreEqual ("Foo-Bar", HttpHeaders.NormalizeName ("foo-BAR"));
186			Assert.AreEqual ("Foo-Bar", HttpHeaders.NormalizeName ("Foo-BaR"));
187		}
188		
189		[Test()]
190		public void TestNormalizeDoubleDash ()
191		{
192			Assert.AreEqual ("Foo--Bar", HttpHeaders.NormalizeName ("foo--bar"));
193			Assert.AreEqual ("Foo--Bar", HttpHeaders.NormalizeName ("FOO--BAR"));
194			Assert.AreEqual ("Foo--Bar", HttpHeaders.NormalizeName ("Foo--bar"));
195			Assert.AreEqual ("Foo--Bar", HttpHeaders.NormalizeName ("foo--BAR"));
196			Assert.AreEqual ("Foo--Bar", HttpHeaders.NormalizeName ("Foo--BaR"));
197		}
198		
199		[Test()]
200		public void TestNormalizeStartDash ()
201		{
202			Assert.AreEqual ("-Foo-Bar", HttpHeaders.NormalizeName ("-foo-bar"));
203			Assert.AreEqual ("-Foo-Bar", HttpHeaders.NormalizeName ("-FOO-BAR"));
204			Assert.AreEqual ("-Foo-Bar", HttpHeaders.NormalizeName ("-Foo-bar"));
205			Assert.AreEqual ("-Foo-Bar", HttpHeaders.NormalizeName ("-foo-BAR"));
206			Assert.AreEqual ("-Foo-Bar", HttpHeaders.NormalizeName ("-Foo-BaR"));
207		}
208		
209		[Test()]
210		public void TestNormalizeEndDash ()
211		{
212			Assert.AreEqual ("Foo-Bar-", HttpHeaders.NormalizeName ("foo-bar-"));
213			Assert.AreEqual ("Foo-Bar-", HttpHeaders.NormalizeName ("FOO-BAR-"));
214			Assert.AreEqual ("Foo-Bar-", HttpHeaders.NormalizeName ("Foo-bar-"));
215			Assert.AreEqual ("Foo-Bar-", HttpHeaders.NormalizeName ("foo-BAR-"));
216			Assert.AreEqual ("Foo-Bar-", HttpHeaders.NormalizeName ("Foo-BaR-"));
217		}
218		
219		[Test]
220		public void TestSetValue ()
221		{
222			HttpHeaders headers = new HttpHeaders ();
223			
224			headers.SetHeader ("Foo", "Bar");
225			Assert.AreEqual ("Bar", headers ["Foo"], "a1");
226			
227			// Keys should get normalized
228			headers.SetHeader ("foo", "bar");
229			Assert.AreEqual ("bar", headers ["foo"], "a2");
230			Assert.AreEqual ("bar", headers ["Foo"], "a3");
231		}
232		
233		[Test]
234		public void TestSetValueNullRemovesKey ()
235		{
236			string dummy = null;
237			HttpHeaders headers = new HttpHeaders ();
238			
239			headers.SetHeader ("Foo", "Bar");
240			Assert.AreEqual ("Bar", headers ["Foo"], "a1");
241			
242			headers.SetHeader ("Foo", null);
243			Assert.IsFalse (headers.TryGetValue ("Foo", out dummy), "a2");
244			Assert.AreEqual (0, headers.Count, "a3");
245		}
246		
247		[Test]
248		public void TestSetContentLength ()
249		{
250			HttpHeaders headers = new HttpHeaders ();
251			
252			headers.SetContentLength ("100");
253			Assert.AreEqual (100, headers.ContentLength, "a1");
254			
255			headers.SetContentLength ("1");
256			Assert.AreEqual (1, headers.ContentLength, "a2");
257			
258			headers.SetContentLength ("0");
259			Assert.AreEqual (0, headers.ContentLength, "a3");
260		}
261		
262		[Test]
263		public void TestSetContentLengthNegative ()
264		{
265			HttpHeaders headers = new HttpHeaders ();
266			Should.Throw<ArgumentException> (() => headers.SetContentLength ("-1"));
267		}
268		
269		[Test]
270		public void TestSetContentLengthInvalid ()
271		{
272			HttpHeaders headers = new HttpHeaders ();
273			
274			Should.Throw<ArgumentException> (() => headers.SetContentLength ("foobar"));
275			Should.Throw<ArgumentException> (() => headers.SetContentLength ("-1 foobar"));
276			Should.Throw<ArgumentException> (() => headers.SetContentLength ("1foo"));
277			Should.Throw<ArgumentException> (() => headers.SetContentLength ("1FA"));
278			Should.Throw<ArgumentException> (() => headers.SetContentLength ("1."));
279			Should.Throw<ArgumentException> (() => headers.SetContentLength ("1.0"));
280		}
281		
282		[Test]
283		public void TestSetContentLengthNullClearsValue ()
284		{
285			HttpHeaders headers = new HttpHeaders ();
286			
287			headers.SetContentLength ("100");
288			Assert.AreEqual (100, headers.ContentLength, "a1");
289			
290			headers.SetContentLength (null);
291			Assert.IsNull (headers.ContentLength, "a2");
292		}
293		
294		[Test]
295		public void TestSetContentLengthProperty ()
296		{
297			string cl;
298			HttpHeaders headers = new HttpHeaders ();
299			
300			headers.ContentLength = 10;
301			Assert.AreEqual (10, headers.ContentLength, "a1");
302			Assert.IsTrue (headers.TryGetValue ("Content-Length", out cl), "a2");
303			Assert.AreEqual ("10", cl, "a3");
304			
305			headers.ContentLength = 100;
306			Assert.AreEqual (100, headers.ContentLength, "a4");
307			Assert.IsTrue (headers.TryGetValue ("Content-Length", out cl), "a5");
308			Assert.AreEqual ("100", cl, "a6");
309			
310			headers.ContentLength = 0;
311			Assert.AreEqual (0, headers.ContentLength, "a7");
312			Assert.IsTrue (headers.TryGetValue ("Content-Length", out cl), "a8");
313			Assert.AreEqual ("0", cl, "a9");
314		}
315		
316		[Test]
317		public void TestSetContentLengthPropertyNull ()
318		{
319			string dummy = null;
320			HttpHeaders headers = new HttpHeaders ();
321			
322			headers.ContentLength = 100;
323			Assert.AreEqual (100, headers.ContentLength, "a1");
324			
325			headers.ContentLength = null;
326			Assert.IsNull (headers.ContentLength, "a2");
327			Assert.IsFalse (headers.TryGetValue ("Content-Length", out dummy), "a3");
328			Assert.IsNull (dummy, "a4");
329			
330		}
331		
332		[Test]
333		public void TestSetContentLengthPropertyNegative ()
334		{
335			string dummy = null;
336			HttpHeaders headers = new HttpHeaders ();
337			
338			Should.Throw<ArgumentException> (() => headers.ContentLength = -1, "a1");
339			Assert.IsNull (headers.ContentLength, "a2");
340			Assert.IsFalse (headers.TryGetValue ("Content-Length", out dummy), "a3");
341			Assert.IsNull (dummy, "a4");
342		}
343	}
344}