2 // HttpRequestMessageTest.cs
5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2011 Xamarin, Inc (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
31 using System.Collections.Generic;
32 using NUnit.Framework;
33 using System.Net.Http;
35 using System.Net.Http.Headers;
38 namespace MonoTests.System.Net.Http
41 public class HttpRequestMessageTest
44 public void Ctor_Invalid ()
47 new HttpRequestMessage (null, "");
49 } catch (ArgumentNullException) {
54 public void Ctor_Default ()
56 var m = new HttpRequestMessage ();
57 Assert.IsNull (m.Content, "#1");
58 Assert.IsNotNull (m.Headers, "#2");
59 Assert.AreEqual (HttpMethod.Get, m.Method, "#3");
60 Assert.IsNotNull (m.Properties, "#4");
61 Assert.IsNull (m.RequestUri, "#5");
62 Assert.AreEqual (new Version (1, 1), m.Version, "#6");
64 Assert.AreEqual ("Method: GET, RequestUri: '<null>', Version: 1.1, Content: <null>, Headers:\r\n{\r\n}", m.ToString (), "#7");
68 public void Ctor_Uri ()
70 var c = new HttpRequestMessage (HttpMethod.Get, new Uri ("http://xamarin.com"));
71 Assert.AreEqual ("http://xamarin.com/", c.RequestUri.AbsoluteUri, "#1");
73 c = new HttpRequestMessage (HttpMethod.Get, new Uri ("https://xamarin.com"));
74 Assert.AreEqual ("https://xamarin.com/", c.RequestUri.AbsoluteUri, "#2");
76 c = new HttpRequestMessage (HttpMethod.Get, new Uri ("HTTP://xamarin.com:8080"));
77 Assert.AreEqual ("http://xamarin.com:8080/", c.RequestUri.AbsoluteUri, "#3");
79 var a = Uri.UriSchemeHttps;
80 var b = new Uri ("http://xamarin.com");
83 new HttpRequestMessage (HttpMethod.Get, new Uri ("ftp://xamarin.com"));
85 } catch (ArgumentException) {
90 public void Headers ()
92 HttpRequestMessage message = new HttpRequestMessage ();
93 HttpRequestHeaders headers = message.Headers;
95 headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
96 headers.AcceptCharset.Add (new StringWithQualityHeaderValue ("str-v", 0.002));
97 headers.AcceptEncoding.Add (new StringWithQualityHeaderValue ("str-enc", 0.44));
98 headers.AcceptLanguage.Add (new StringWithQualityHeaderValue ("str-lang", 0.41));
99 headers.Authorization = new AuthenticationHeaderValue ("sh-aut", "par");
100 headers.CacheControl = new CacheControlHeaderValue () {
101 MaxAge = TimeSpan.MaxValue
103 headers.Connection.Add ("test-value");
104 headers.ConnectionClose = true;
105 headers.Date = new DateTimeOffset (DateTime.Today);
106 headers.Expect.Add (new NameValueWithParametersHeaderValue ("en", "ev"));
107 headers.ExpectContinue = true;
108 headers.From = "webmaster@w3.org";
109 headers.Host = "host";
110 headers.IfMatch.Add (new EntityTagHeaderValue ("\"tag\"", true));
111 headers.IfModifiedSince = new DateTimeOffset (DateTime.Today);
112 headers.IfNoneMatch.Add (new EntityTagHeaderValue ("\"tag2\"", true));
113 headers.IfRange = new RangeConditionHeaderValue (new DateTimeOffset (DateTime.Today));
114 headers.IfUnmodifiedSince = new DateTimeOffset? (DateTimeOffset.Now);
115 headers.MaxForwards = 0x15b3;
116 headers.Pragma.Add (new NameValueHeaderValue ("name", "value"));
117 headers.ProxyAuthorization = new AuthenticationHeaderValue ("s", "p");
118 headers.Range = new RangeHeaderValue (5L, 30L);
119 headers.Referrer = new Uri ("http://xamarin.com");
120 headers.TE.Add (new TransferCodingWithQualityHeaderValue ("TE", 0.3));
121 headers.Trailer.Add ("value");
122 headers.TransferEncoding.Add (new TransferCodingHeaderValue ("tchv"));
123 headers.TransferEncodingChunked = true;
124 headers.Upgrade.Add (new ProductHeaderValue ("prod", "ver"));
125 headers.UserAgent.Add (new ProductInfoHeaderValue ("(comment)"));
126 headers.Via.Add (new ViaHeaderValue ("protocol", "rec-by"));
127 headers.Warning.Add (new WarningHeaderValue (5, "agent", "\"txt\""));
130 headers.Add ("authorization", "");
131 Assert.Fail ("Authorization");
132 } catch (FormatException) {
136 headers.Add ("connection", "extra ß ");
137 Assert.Fail ("Date");
138 } catch (FormatException) {
142 headers.Add ("date", "");
143 Assert.Fail ("Date");
144 } catch (FormatException) {
148 headers.Add ("from", "a@w3.org");
149 Assert.Fail ("From");
150 } catch (FormatException) {
154 headers.Add ("hOst", "host");
155 Assert.Fail ("Host");
156 } catch (FormatException) {
160 headers.Add ("if-modified-since", "");
161 Assert.Fail ("if-modified-since");
162 } catch (FormatException) {
166 headers.Add ("if-range", "");
167 Assert.Fail ("if-range");
168 } catch (FormatException) {
172 headers.Add ("if-unmodified-since", "");
173 Assert.Fail ("if-unmodified-since");
174 } catch (FormatException) {
178 headers.Add ("max-forwards", "");
179 Assert.Fail ("max-forwards");
180 } catch (FormatException) {
184 headers.Add ("proxy-authorization", "");
185 Assert.Fail ("proxy-authorization");
186 } catch (FormatException) {
190 headers.Add ("range", "");
191 } catch (FormatException) {
195 headers.Add ("referer", "");
196 Assert.Fail ("referer");
197 } catch (FormatException) {
200 headers.Add ("accept", "audio/y");
201 headers.Add ("accept-charset", "achs");
202 headers.Add ("accept-encoding", "aenc");
203 headers.Add ("accept-language", "alan");
204 headers.Add ("expect", "exp");
205 headers.Add ("if-match", "\"v\"");
206 headers.Add ("if-none-match", "\"v2\"");
207 headers.Add ("pragma", "p");
208 headers.Add ("TE", "0.8");
209 headers.Add ("trailer", "value2");
210 headers.Add ("transfer-encoding", "ttt");
211 headers.Add ("upgrade", "uuu");
212 headers.Add ("user-agent", "uaua");
213 headers.Add ("via", "prot v");
214 headers.Add ("warning", "4 ww \"t\"");
216 Assert.IsTrue (headers.Accept.SequenceEqual (
218 new MediaTypeWithQualityHeaderValue ("audio/x"),
219 new MediaTypeWithQualityHeaderValue ("audio/y")
223 Assert.IsTrue (headers.AcceptCharset.SequenceEqual (
225 new StringWithQualityHeaderValue ("str-v", 0.002),
226 new StringWithQualityHeaderValue ("achs")
230 Assert.IsTrue (headers.AcceptEncoding.SequenceEqual (
232 new StringWithQualityHeaderValue ("str-enc", 0.44),
233 new StringWithQualityHeaderValue ("aenc")
237 Assert.IsTrue (headers.AcceptLanguage.SequenceEqual (
239 new StringWithQualityHeaderValue ("str-lang", 0.41),
240 new StringWithQualityHeaderValue ("alan")
244 Assert.AreEqual (new AuthenticationHeaderValue ("sh-aut", "par"), headers.Authorization);
246 var cch = new CacheControlHeaderValue () {
247 MaxAge = TimeSpan.MaxValue,
250 Assert.AreEqual (cch, headers.CacheControl);
252 Assert.IsTrue (headers.Connection.SequenceEqual (
253 new string[] { "test-value", "close" }));
255 Assert.AreEqual (headers.Date, new DateTimeOffset (DateTime.Today));
257 Assert.IsTrue (headers.Expect.SequenceEqual (
259 new NameValueWithParametersHeaderValue ("en", "ev"),
260 new NameValueWithParametersHeaderValue ("100-continue"),
261 new NameValueWithParametersHeaderValue ("exp")
264 Assert.AreEqual (headers.From, "webmaster@w3.org");
266 Assert.IsTrue (headers.IfMatch.SequenceEqual (
267 new EntityTagHeaderValue[] {
268 new EntityTagHeaderValue ("\"tag\"", true),
269 new EntityTagHeaderValue ("\"v\"", false)
273 Assert.AreEqual (headers.IfModifiedSince, new DateTimeOffset (DateTime.Today));
274 Assert.IsTrue (headers.IfNoneMatch.SequenceEqual (new EntityTagHeaderValue[] { new EntityTagHeaderValue ("\"tag2\"", true), new EntityTagHeaderValue ("\"v2\"", false) }));
275 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.IfRange.Date);
276 Assert.AreEqual (headers.MaxForwards, 0x15b3);
277 Assert.IsTrue (headers.Pragma.SequenceEqual (new NameValueHeaderValue[] { new NameValueHeaderValue ("name", "value"), new NameValueHeaderValue ("p", null) }));
278 Assert.AreEqual ("p", headers.ProxyAuthorization.Parameter);
279 Assert.AreEqual ("s", headers.ProxyAuthorization.Scheme);
280 Assert.AreEqual (5, headers.Range.Ranges.First ().From);
281 Assert.AreEqual (30, headers.Range.Ranges.First ().To);
282 Assert.AreEqual ("bytes", headers.Range.Unit);
283 Assert.AreEqual (headers.Referrer, new Uri ("http://xamarin.com"));
284 Assert.IsTrue (headers.TE.SequenceEqual (new TransferCodingWithQualityHeaderValue[] { new TransferCodingWithQualityHeaderValue ("TE", 0.3), new TransferCodingWithQualityHeaderValue ("0.8") }), "29");
285 Assert.IsTrue (headers.Trailer.SequenceEqual (
290 Assert.IsTrue (headers.TransferEncoding.SequenceEqual (
292 new TransferCodingHeaderValue ("tchv"),
293 new TransferCodingHeaderValue ("chunked"),
294 new TransferCodingHeaderValue ("ttt")
298 Assert.IsTrue (headers.Upgrade.SequenceEqual (
300 new ProductHeaderValue ("prod", "ver"),
301 new ProductHeaderValue ("uuu")
305 Assert.IsTrue (headers.UserAgent.SequenceEqual (
307 new ProductInfoHeaderValue ("(comment)"),
308 new ProductInfoHeaderValue ("uaua", null)
312 Assert.IsTrue (headers.Via.SequenceEqual (
314 new ViaHeaderValue ("protocol", "rec-by"),
315 new ViaHeaderValue ("prot", "v")
319 Assert.IsTrue (headers.Warning.SequenceEqual (
321 new WarningHeaderValue (5, "agent", "\"txt\""),
322 new WarningHeaderValue (4, "ww", "\"t\"")
329 public void Header_BaseImplementation ()
331 HttpRequestMessage message = new HttpRequestMessage ();
332 HttpRequestHeaders headers = message.Headers;
334 headers.Add ("a", "a-value");
335 headers.Add ("b", new List<string> { "v1", "v2" });
336 headers.Add ("c", null as string);
337 headers.Add ("d", new string[0]);
339 Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#0");
341 Assert.IsFalse (headers.Contains ("nn"), "#1a");
342 Assert.IsTrue (headers.Contains ("b"), "#1b");
344 var values = headers.GetValues ("b").ToList ();
345 Assert.AreEqual ("v1", values[0], "#2a");
346 Assert.AreEqual ("v2", values[1], "#2b");
348 Assert.IsFalse (headers.Remove ("p"), "#3a");
349 Assert.IsTrue (headers.Remove ("b"), "#3b");
350 Assert.IsFalse (headers.Contains ("b"), "#3b-c");
352 IEnumerable<string> values2;
353 Assert.IsTrue (headers.TryGetValues ("c", out values2));
354 values = values2.ToList ();
355 Assert.AreEqual ("", values[0], "#4a");
358 foreach (var i in headers) {
362 Assert.AreEqual (3, counter, "#5");
366 headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
367 Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#55");
369 values = headers.GetValues ("accept").ToList ();
370 Assert.AreEqual (2, values.Count, "#6");
371 Assert.AreEqual ("audio/x", values[0], "#6a");
372 Assert.AreEqual ("audio", values[1], "#6b");
373 Assert.AreEqual (1, headers.Accept.Count, "#6c");
377 Assert.IsTrue (headers.TryAddWithoutValidation ("from", new[] { "a@a.com", "ssss@oo.com" }), "#70");
378 values = headers.GetValues ("from").ToList ();
380 Assert.AreEqual (2, values.Count, "#7");
381 Assert.AreEqual ("a@a.com", values[0], "#7a");
382 Assert.AreEqual ("ssss@oo.com", values[1], "#7b");
383 Assert.AreEqual ("a@a.com", headers.From, "#7c");
387 Assert.IsTrue (headers.TryAddWithoutValidation ("Date", "wrong date"), "#8-0");
388 var value = headers.Date;
389 Assert.IsNull (headers.Date, "#8");
393 public void Headers_Invalid ()
395 HttpRequestMessage message = new HttpRequestMessage ();
396 HttpRequestHeaders headers = message.Headers;
399 headers.Add ("Allow", "");
401 } catch (InvalidOperationException) {
405 headers.Add (null, "");
407 } catch (ArgumentException) {
411 headers.Add ("mm", null as IEnumerable<string>);
413 } catch (ArgumentNullException) {
417 headers.Add ("accept", "audio");
419 } catch (FormatException) {
422 Assert.IsFalse (headers.TryAddWithoutValidation ("Allow", ""), "#3"); ;
424 Assert.IsFalse (headers.TryAddWithoutValidation (null, ""), "#4");
427 headers.Contains (null);
429 } catch (ArgumentException) {
433 headers.GetValues (null);
435 } catch (ArgumentException) {
439 headers.GetValues ("bbbb");
441 } catch (InvalidOperationException) {
445 headers.Add ("from", new[] { "a@a.com", "ssss@oo.com" });
447 } catch (FormatException) {
450 Assert.IsTrue (headers.TryAddWithoutValidation ("from", "a@a.com"), "#7-0");
452 headers.Add ("from", "valid@w3.org");
454 } catch (FormatException) {
459 public void Headers_Response ()
461 HttpRequestMessage message = new HttpRequestMessage ();
462 HttpRequestHeaders headers = message.Headers;
464 headers.Add ("Age", "vv");
465 Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#1");
468 headers.TryAddWithoutValidation ("Age", "vv");
469 Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#2");
471 Assert.AreEqual ("Method: GET, RequestUri: '<null>', Version: 1.1, Content: <null>, Headers:\r\n{\r\nAge: vv\r\n}", message.ToString (), "#3");
475 public void Headers_ExpectContinue ()
477 HttpRequestMessage message = new HttpRequestMessage ();
478 HttpRequestHeaders headers = message.Headers;
479 Assert.IsNull (headers.ExpectContinue, "#1");
481 headers.ExpectContinue = false;
482 Assert.IsFalse (headers.ExpectContinue.Value, "#2");
486 headers.ExpectContinue = true;
487 headers.ExpectContinue = true;
488 headers.ExpectContinue = true;
489 headers.ExpectContinue = true;
490 Assert.IsTrue (headers.ExpectContinue.Value, "#3");
491 Assert.AreEqual (1, headers.GetValues ("expect").ToList ().Count, "#4");
494 headers.Expect.Add (new NameValueWithParametersHeaderValue ("100-conTinuE"));
495 Assert.IsTrue (headers.ExpectContinue.Value, "#5");
499 public void Headers_ConnectionClose ()
501 HttpRequestMessage message = new HttpRequestMessage ();
502 HttpRequestHeaders headers = message.Headers;
503 Assert.IsNull (headers.ConnectionClose, "#1");
505 headers.ConnectionClose = false;
506 Assert.IsFalse (headers.ConnectionClose.Value, "#2");
510 headers.ConnectionClose = true;
511 Assert.IsTrue (headers.ConnectionClose.Value, "#3");
514 headers.Connection.Add ("Close");
515 Assert.IsTrue (headers.ConnectionClose.Value, "#4");
519 public void Headers_From_Invalid ()
521 HttpRequestMessage message = new HttpRequestMessage ();
522 HttpRequestHeaders headers = message.Headers;
528 } catch (FormatException) {
531 headers.From = "test";
533 } catch (FormatException) {
538 public void Headers_TransferEncodingChunked ()
540 HttpRequestMessage message = new HttpRequestMessage ();
541 HttpRequestHeaders headers = message.Headers;
542 Assert.IsNull (headers.TransferEncodingChunked, "#1");
544 headers.TransferEncodingChunked = false;
545 Assert.IsFalse (headers.TransferEncodingChunked.Value, "#2");
549 headers.TransferEncodingChunked = true;
550 Assert.IsTrue (headers.TransferEncodingChunked.Value, "#3");
551 Assert.AreEqual (1, headers.TransferEncoding.Count, "#3b");
555 public void Properties ()
557 var c = new HttpRequestMessage ();
559 c.Method = HttpMethod.Post;
560 c.Properties.Add ("a", "test");
562 c.Version = HttpVersion.Version10;
564 Assert.IsNull (c.Content, "#1");
565 Assert.AreEqual (HttpMethod.Post, c.Method, "#2");
566 Assert.AreEqual ("test", c.Properties["a"], "#3");
567 Assert.IsNull (c.RequestUri, "#4");
568 Assert.AreEqual (HttpVersion.Version10, c.Version, "#5");
572 public void Properties_Invalid ()
574 var c = new HttpRequestMessage ();
578 } catch (ArgumentNullException) {
582 c.RequestUri = new Uri ("ftp://xamarin.com");
584 } catch (ArgumentException) {
590 } catch (ArgumentNullException) {