Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System.Net.Http / Test / System.Net.Http / HttpRequestMessageTest.cs
1 //
2 // HttpRequestMessageTest.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2011 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
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Text.RegularExpressions;
33 using NUnit.Framework;
34 using System.Net.Http;
35 using System.Net;
36 using System.Net.Http.Headers;
37 using System.Linq;
38 using System.IO;
39
40 namespace MonoTests.System.Net.Http
41 {
42         [TestFixture]
43         public class HttpRequestMessageTest
44         {
45                 [Test]
46                 public void Ctor_Invalid ()
47                 {
48                         try {
49                                 new HttpRequestMessage (null, "");
50                                 Assert.Fail ("#1");
51                         } catch (ArgumentNullException) {
52                         }
53                 }
54
55                 [Test]
56                 public void Ctor_Default ()
57                 {
58                         var m = new HttpRequestMessage ();
59                         Assert.IsNull (m.Content, "#1");
60                         Assert.IsNotNull (m.Headers, "#2");
61                         Assert.AreEqual (HttpMethod.Get, m.Method, "#3");
62                         Assert.IsNotNull (m.Properties, "#4");
63                         Assert.IsNull (m.RequestUri, "#5");
64                         Assert.AreEqual (new Version (1, 1), m.Version, "#6");
65
66                         Assert.AreEqual ("Method: GET, RequestUri: '<null>', Version: 1.1, Content: <null>, Headers:\r\n{\r\n}", m.ToString (), "#7");
67                 }
68
69                 [Test]
70                 public void Ctor_Uri ()
71                 {
72                         var c = new HttpRequestMessage (HttpMethod.Get, new Uri ("http://xamarin.com"));
73                         Assert.AreEqual ("http://xamarin.com/", c.RequestUri.AbsoluteUri, "#1");
74
75                         c = new HttpRequestMessage (HttpMethod.Get, new Uri ("https://xamarin.com"));
76                         Assert.AreEqual ("https://xamarin.com/", c.RequestUri.AbsoluteUri, "#2");
77
78                         c = new HttpRequestMessage (HttpMethod.Get, new Uri ("HTTP://xamarin.com:8080"));
79                         Assert.AreEqual ("http://xamarin.com:8080/", c.RequestUri.AbsoluteUri, "#3");
80
81                         var a = Uri.UriSchemeHttps;
82                         var b = new Uri ("http://xamarin.com");
83
84                         try {
85                                 new HttpRequestMessage (HttpMethod.Get, new Uri ("ftp://xamarin.com"));
86                                 Assert.Fail ("#4");
87                         } catch (ArgumentException) {
88                         }
89                 }
90
91                 [Test]
92                 public void Ctor_RelativeUri ()
93                 {
94                         var client = new HttpClient ();
95                         client.BaseAddress = new Uri ("http://en.wikipedia.org/wiki/");
96                         var uri = new Uri ("Computer", UriKind.Relative);
97                         var req = new HttpRequestMessage (HttpMethod.Get, uri);
98                         // HttpRequestMessage does not rewrite it here.
99                         Assert.AreEqual (req.RequestUri, uri);
100                 }
101
102                 [Test]
103                 public void Ctor_RelativeUriString ()
104                 {
105                         var client = new HttpClient ();
106                         client.BaseAddress = new Uri ("http://en.wikipedia.org/wiki/");
107                         var req = new HttpRequestMessage (HttpMethod.Get, "Computer");
108                         // HttpRequestMessage does not rewrite it here.
109                         Assert.IsFalse (req.RequestUri.IsAbsoluteUri);
110                 }
111
112                 [Test]
113                 public void Headers ()
114                 {
115                         HttpRequestMessage message = new HttpRequestMessage ();
116                         HttpRequestHeaders headers = message.Headers;
117
118                         headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
119                         headers.AcceptCharset.Add (new StringWithQualityHeaderValue ("str-v", 0.002));
120                         headers.AcceptEncoding.Add (new StringWithQualityHeaderValue ("str-enc", 0.44));
121                         headers.AcceptLanguage.Add (new StringWithQualityHeaderValue ("str-lang", 0.41));
122                         headers.Authorization = new AuthenticationHeaderValue ("sh-aut", "par");
123                         headers.CacheControl = new CacheControlHeaderValue () {
124                                 MaxAge = TimeSpan.MaxValue
125                         };
126                         headers.Connection.Add ("test-value");
127                         headers.ConnectionClose = true;
128                         headers.Date = new DateTimeOffset (DateTime.Today);
129                         headers.Expect.Add (new NameValueWithParametersHeaderValue ("en", "ev"));
130                         headers.ExpectContinue = true;
131                         headers.From = "webmaster@w3.org";
132                         headers.Host = "host";
133                         headers.IfMatch.Add (new EntityTagHeaderValue ("\"tag\"", true));
134                         headers.IfModifiedSince = new DateTimeOffset (DateTime.Today);
135                         headers.IfNoneMatch.Add (new EntityTagHeaderValue ("\"tag2\"", true));
136                         headers.IfRange = new RangeConditionHeaderValue (new DateTimeOffset (DateTime.Today));
137                         headers.IfUnmodifiedSince = new DateTimeOffset? (DateTimeOffset.Now);
138                         headers.MaxForwards = 0x15b3;
139                         headers.Pragma.Add (new NameValueHeaderValue ("name", "value"));
140                         headers.ProxyAuthorization = new AuthenticationHeaderValue ("s", "p");
141                         headers.Range = new RangeHeaderValue (5L, 30L);
142                         headers.Referrer = new Uri ("http://xamarin.com");
143                         headers.TE.Add (new TransferCodingWithQualityHeaderValue ("TE", 0.3));
144                         headers.Trailer.Add ("value");
145                         headers.TransferEncoding.Add (new TransferCodingHeaderValue ("tchv"));
146                         headers.TransferEncodingChunked = true;
147                         headers.Upgrade.Add (new ProductHeaderValue ("prod", "ver"));
148                         headers.UserAgent.Add (new ProductInfoHeaderValue ("(comment)"));
149                         headers.Via.Add (new ViaHeaderValue ("protocol", "rec-by"));
150                         headers.Warning.Add (new WarningHeaderValue (5, "agent", "\"txt\""));
151
152                         try {
153                                 headers.Add ("authorization", "");
154                                 Assert.Fail ("Authorization");
155                         } catch (FormatException) {
156                         }
157
158                         try {
159                                 headers.Add ("connection", "extra ÃŸ ");
160                                 Assert.Fail ("Date");
161                         } catch (FormatException) {
162                         }
163
164                         try {
165                                 headers.Add ("date", "");
166                                 Assert.Fail ("Date");
167                         } catch (FormatException) {
168                         }
169
170                         try {
171                                 headers.Add ("from", "a@w3.org");
172                                 Assert.Fail ("From");
173                         } catch (FormatException) {
174                         }
175
176                         try {
177                                 headers.Add ("hOst", "host");
178                                 Assert.Fail ("Host");
179                         } catch (FormatException) {
180                         }
181
182                         try {
183                                 headers.Add ("if-modified-since", "");
184                                 Assert.Fail ("if-modified-since");
185                         } catch (FormatException) {
186                         }
187
188                         try {
189                                 headers.Add ("if-range", "");
190                                 Assert.Fail ("if-range");
191                         } catch (FormatException) {
192                         }
193
194                         try {
195                                 headers.Add ("if-unmodified-since", "");
196                                 Assert.Fail ("if-unmodified-since");
197                         } catch (FormatException) {
198                         }
199
200                         try {
201                                 headers.Add ("max-forwards", "");
202                                 Assert.Fail ("max-forwards");
203                         } catch (FormatException) {
204                         }
205
206                         try {
207                                 headers.Add ("proxy-authorization", "");
208                                 Assert.Fail ("proxy-authorization");
209                         } catch (FormatException) {
210                         }
211
212                         try {
213                                 headers.Add ("range", "");
214                         } catch (FormatException) {
215                         }
216
217                         try {
218                                 headers.Add ("referer", "");
219                                 Assert.Fail ("referer");
220                         } catch (FormatException) {
221                         }
222
223                         try {
224                                 headers.Add ("pragma", "nocache,RequestID=1,g=");
225                                 Assert.Fail ("pragma");
226                         } catch (FormatException) {                             
227                         }
228
229                         headers.Add ("accept", "audio/y");
230                         headers.Add ("accept-charset", "achs");
231                         headers.Add ("accept-encoding", "aenc");
232                         headers.Add ("accept-language", "alan");
233                         headers.Add ("expect", "exp");
234                         headers.Add ("if-match", "\"v\"");
235                         headers.Add ("if-none-match", "\"v2\"");
236                         headers.Add ("TE", "0.8");
237                         headers.Add ("trailer", "value2");
238                         headers.Add ("transfer-encoding", "ttt");
239                         headers.Add ("upgrade", "uuu");
240                         headers.Add ("user-agent", "uaua");
241                         headers.Add ("via", "prot v");
242                         headers.Add ("warning", "4 ww \"t\"");
243                         headers.Add ("pragma", "nocache,R=1,g");
244
245                         Assert.IsTrue (headers.Accept.SequenceEqual (
246                                 new[] {
247                                         new MediaTypeWithQualityHeaderValue ("audio/x"),
248                                         new MediaTypeWithQualityHeaderValue ("audio/y")
249                                 }
250                         ));
251
252                         Assert.IsTrue (headers.AcceptCharset.SequenceEqual (
253                                 new[] {
254                                         new StringWithQualityHeaderValue ("str-v", 0.002),
255                                         new StringWithQualityHeaderValue ("achs")
256                                 }
257                         ));
258
259                         Assert.IsTrue (headers.AcceptEncoding.SequenceEqual (
260                                 new[] {
261                                         new StringWithQualityHeaderValue ("str-enc", 0.44),
262                                         new StringWithQualityHeaderValue ("aenc")
263                                 }
264                         ));
265
266                         Assert.IsTrue (headers.AcceptLanguage.SequenceEqual (
267                                 new[] {
268                                         new StringWithQualityHeaderValue ("str-lang", 0.41),
269                                         new StringWithQualityHeaderValue ("alan")
270                                 }
271                         ));
272
273                         Assert.AreEqual (new AuthenticationHeaderValue ("sh-aut", "par"), headers.Authorization);
274
275                         var cch = new CacheControlHeaderValue () {
276                                         MaxAge = TimeSpan.MaxValue,
277                                 };
278
279                         Assert.AreEqual (cch, headers.CacheControl);
280
281                         Assert.IsTrue (headers.Connection.SequenceEqual (
282                                 new string[] { "test-value", "close" }));
283
284                         Assert.AreEqual (headers.Date, new DateTimeOffset (DateTime.Today));
285
286                         Assert.IsTrue (headers.Expect.SequenceEqual (
287                                 new [] {
288                                         new NameValueWithParametersHeaderValue ("en", "ev"),
289                                         new NameValueWithParametersHeaderValue ("100-continue"),
290                                         new NameValueWithParametersHeaderValue ("exp")
291                                 }));
292
293                         Assert.AreEqual (headers.From, "webmaster@w3.org");
294
295                         Assert.IsTrue (headers.IfMatch.SequenceEqual (
296                                 new EntityTagHeaderValue[] {
297                                         new EntityTagHeaderValue ("\"tag\"", true),
298                                         new EntityTagHeaderValue ("\"v\"", false)
299                                 }
300                         ));
301
302                         Assert.AreEqual (headers.IfModifiedSince, new DateTimeOffset (DateTime.Today));
303                         Assert.IsTrue (headers.IfNoneMatch.SequenceEqual (new EntityTagHeaderValue[] { new EntityTagHeaderValue ("\"tag2\"", true), new EntityTagHeaderValue ("\"v2\"", false) }));
304                         Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.IfRange.Date);
305                         Assert.AreEqual (headers.MaxForwards, 0x15b3);
306                         Assert.AreEqual ("p", headers.ProxyAuthorization.Parameter);
307                         Assert.AreEqual ("s", headers.ProxyAuthorization.Scheme);
308                         Assert.AreEqual (5, headers.Range.Ranges.First ().From);
309                         Assert.AreEqual (30, headers.Range.Ranges.First ().To);
310                         Assert.AreEqual ("bytes", headers.Range.Unit);
311                         Assert.AreEqual (headers.Referrer, new Uri ("http://xamarin.com"));
312                         Assert.IsTrue (headers.TE.SequenceEqual (new TransferCodingWithQualityHeaderValue[] { new TransferCodingWithQualityHeaderValue ("TE", 0.3), new TransferCodingWithQualityHeaderValue ("0.8") }), "29");
313                         Assert.IsTrue (headers.Trailer.SequenceEqual (
314                                 new string[] {
315                                         "value", "value2"
316                                 }), "30");
317
318                         Assert.IsTrue (headers.TransferEncoding.SequenceEqual (
319                                 new[] {
320                                         new TransferCodingHeaderValue ("tchv"),
321                                         new TransferCodingHeaderValue ("chunked"),
322                                         new TransferCodingHeaderValue ("ttt")
323                                 }
324                         ));
325
326                         Assert.IsTrue (headers.Upgrade.SequenceEqual (
327                                 new[] {
328                                         new ProductHeaderValue ("prod", "ver"),
329                                         new ProductHeaderValue ("uuu")
330                                 }
331                         ));
332
333                         Assert.IsTrue (headers.UserAgent.SequenceEqual (
334                                 new[] {
335                                         new ProductInfoHeaderValue ("(comment)"),
336                                         new ProductInfoHeaderValue ("uaua", null)
337                                 }
338                         ));
339
340                         Assert.IsTrue (headers.Via.SequenceEqual (
341                                 new[] {
342                                         new ViaHeaderValue ("protocol", "rec-by"),
343                                         new ViaHeaderValue ("prot", "v")
344                                 }
345                         ));
346
347                         Assert.IsTrue (headers.Warning.SequenceEqual (
348                                 new[] {
349                                         new WarningHeaderValue (5, "agent", "\"txt\""),
350                                         new WarningHeaderValue (4, "ww", "\"t\"")
351                                 }
352                         ));
353
354                         Assert.IsTrue (headers.Pragma.SequenceEqual (
355                                 new[] {
356                                         new NameValueHeaderValue ("name", "value"),
357                                         new NameValueHeaderValue ("nocache", null),
358                                         new NameValueHeaderValue ("R", "1"),
359                                         new NameValueHeaderValue ("g", null)
360                                 }
361                         ));                     
362                 }
363
364                 [Test]
365                 public void Headers_MultiValues ()
366                 {
367                         HttpRequestMessage message = new HttpRequestMessage ();
368                         HttpRequestHeaders headers = message.Headers;
369
370                         headers.Add ("Accept", "application/vnd.citrix.requesttokenresponse+xml, application/vnd.citrix.requesttokenchoices+xml");
371                         headers.Add ("Accept-Charset", "aa ;Q=0,bb;Q=1");
372                         headers.Add ("Expect", "x=1; v, y=5");
373                         headers.Add ("If-Match", "\"a\",*, \"b\",*");
374                         headers.Add ("user-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36");
375
376                         Assert.AreEqual (2, headers.Accept.Count, "#1a");
377                         Assert.IsTrue (headers.Accept.SequenceEqual (
378                                 new[] {
379                                         new MediaTypeWithQualityHeaderValue ("application/vnd.citrix.requesttokenresponse+xml"),
380                                         new MediaTypeWithQualityHeaderValue ("application/vnd.citrix.requesttokenchoices+xml"),
381                                 }
382                         ), "#1b");
383
384                         Assert.AreEqual (2, headers.AcceptCharset.Count, "#2a");
385                         Assert.IsTrue (headers.AcceptCharset.SequenceEqual (
386                                 new[] {
387                                         new StringWithQualityHeaderValue ("aa", 0),
388                                         new StringWithQualityHeaderValue ("bb", 1),
389                                 }
390                         ), "#2b");
391
392                         Assert.AreEqual (2, headers.Expect.Count, "#3a");
393                         var expect_expected = new[] {
394                                         new NameValueWithParametersHeaderValue ("x", "1") {
395                                         },
396                                         new NameValueWithParametersHeaderValue ("y", "5"),
397                                 };
398                         expect_expected [0].Parameters.Add (new NameValueHeaderValue ("v"));
399                         Assert.IsTrue (headers.Expect.SequenceEqual (
400                                 expect_expected
401                         ), "#3b");
402
403                         Assert.AreEqual (4, headers.IfMatch.Count, "#4a");
404                         Assert.IsTrue (headers.IfMatch.SequenceEqual (
405                                 new[] {
406                                         new EntityTagHeaderValue ("\"a\""),
407                                         EntityTagHeaderValue.Any,
408                                         new EntityTagHeaderValue ("\"b\""),
409                                         EntityTagHeaderValue.Any
410                                 }
411                         ), "#4b");
412
413                         Assert.AreEqual (6, headers.UserAgent.Count, "#10a");
414
415                         Assert.IsTrue (headers.UserAgent.SequenceEqual (
416                                 new[] {
417                                         new ProductInfoHeaderValue ("Mozilla", "5.0"),
418                                         new ProductInfoHeaderValue ("(Macintosh; Intel Mac OS X 10_8_4)"),
419                                         new ProductInfoHeaderValue ("AppleWebKit", "537.36"),
420                                         new ProductInfoHeaderValue ("(KHTML, like Gecko)"),
421                                         new ProductInfoHeaderValue ("Chrome", "29.0.1547.62"),
422                                         new ProductInfoHeaderValue ("Safari", "537.36")
423                                 }
424                         ), "#10b");
425                 }
426
427                 [Test]
428                 public void Header_BaseImplementation ()
429                 {
430                         HttpRequestMessage message = new HttpRequestMessage ();
431                         HttpRequestHeaders headers = message.Headers;
432
433                         headers.Add ("a", "a-value");
434                         headers.Add ("b", new List<string> { "v1", "v2" });
435                         headers.Add ("c", null as string);
436                         headers.Add ("d", new string[0]);
437
438                         Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#0");
439
440                         Assert.IsFalse (headers.Contains ("nn"), "#1a");
441                         Assert.IsTrue (headers.Contains ("b"), "#1b");
442
443                         var values = headers.GetValues ("b").ToList ();
444                         Assert.AreEqual ("v1", values[0], "#2a");
445                         Assert.AreEqual ("v2", values[1], "#2b");
446
447                         Assert.IsFalse (headers.Remove ("p"), "#3a");
448                         Assert.IsTrue (headers.Remove ("b"), "#3b");
449                         Assert.IsFalse (headers.Contains ("b"), "#3b-c");
450
451                         IEnumerable<string> values2;
452                         Assert.IsTrue (headers.TryGetValues ("c", out values2));
453                         values = values2.ToList ();
454                         Assert.AreEqual ("", values[0], "#4a");
455
456                         int counter = 0;
457                         foreach (var i in headers) {
458                                 ++counter;
459                         }
460
461                         Assert.AreEqual (3, counter, "#5");
462
463                         headers.Clear ();
464
465                         headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
466                         Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#55");
467
468                         values = headers.GetValues ("accept").ToList ();
469                         Assert.AreEqual (2, values.Count, "#6");
470                         Assert.AreEqual ("audio/x", values[0], "#6a");
471                         Assert.AreEqual ("audio", values[1], "#6b");
472                         Assert.AreEqual (1, headers.Accept.Count, "#6c");
473
474                         headers.Clear ();
475
476                         Assert.IsTrue (headers.TryAddWithoutValidation ("from", new[] { "a@a.com", "ssss@oo.com" }), "#70");
477                         values = headers.GetValues ("from").ToList ();
478
479                         Assert.AreEqual (2, values.Count, "#7");
480                         Assert.AreEqual ("a@a.com", values[0], "#7a");
481                         Assert.AreEqual ("ssss@oo.com", values[1], "#7b");
482                         Assert.AreEqual ("a@a.com", headers.From, "#7c");
483
484                         headers.Clear ();
485
486                         Assert.IsTrue (headers.TryAddWithoutValidation ("Date", "wrong date"), "#8-0");
487                         var value = headers.Date;
488                         Assert.IsNull (headers.Date, "#8");
489                 }
490
491                 [Test]
492                 public void Headers_Invalid ()
493                 {
494                         HttpRequestMessage message = new HttpRequestMessage ();
495                         HttpRequestHeaders headers = message.Headers;
496
497                         try {
498                                 headers.Add ("Allow", "");
499                                 Assert.Fail ("#1");
500                         } catch (InvalidOperationException) {
501                         }
502
503                         try {
504                                 headers.Add (null, "");
505                                 Assert.Fail ("#2");
506                         } catch (ArgumentException) {
507                         }
508
509                         try {
510                                 headers.Add ("mm", null as IEnumerable<string>);
511                                 Assert.Fail ("#2b");
512                         } catch (ArgumentNullException) {
513                         }
514
515                         try {
516                                 headers.Add ("accept", "audio");
517                                 Assert.Fail ("#2c");
518                         } catch (FormatException) {
519                         }
520
521                         Assert.IsFalse (headers.TryAddWithoutValidation ("Allow", ""), "#3"); ;
522
523                         Assert.IsFalse (headers.TryAddWithoutValidation (null, ""), "#4");
524
525                         try {
526                                 headers.Contains (null);
527                                 Assert.Fail ("#5");
528                         } catch (ArgumentException) {
529                         }
530
531                         try {
532                                 headers.GetValues (null);
533                                 Assert.Fail ("#6a");
534                         } catch (ArgumentException) {
535                         }
536
537                         try {
538                                 headers.GetValues ("bbbb");
539                                 Assert.Fail ("#6b");
540                         } catch (InvalidOperationException) {
541                         }
542
543                         try {
544                                 headers.Add ("from", new[] { "a@a.com", "ssss@oo.com" });
545                                 Assert.Fail ("#7a");
546                         } catch (FormatException) {
547                         }
548
549                         Assert.IsTrue (headers.TryAddWithoutValidation ("from", "a@a.com"), "#7-0");
550                         try {
551                                 headers.Add ("from", "valid@w3.org");
552                                 Assert.Fail ("#7b");
553                         } catch (FormatException) {
554                         }
555                 }
556
557                 [Test]
558                 public void Headers_Response ()
559                 {
560                         HttpRequestMessage message = new HttpRequestMessage ();
561                         HttpRequestHeaders headers = message.Headers;
562
563                         headers.Add ("Age", "vv");
564                         Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#1");
565
566                         headers.Clear ();
567                         headers.TryAddWithoutValidation ("Age", "vv");
568                         Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#2");
569
570                         // .NET encloses the "Age: vv" with two whitespaces.
571                         var normalized = Regex.Replace (message.ToString (), @"\s", "");
572                         Assert.AreEqual ("Method:GET,RequestUri:'<null>',Version:1.1,Content:<null>,Headers:{Age:vv}", normalized, "#3");
573                 }
574
575                 [Test]
576                 public void Headers_ExpectContinue ()
577                 {
578                         HttpRequestMessage message = new HttpRequestMessage ();
579                         HttpRequestHeaders headers = message.Headers;
580                         Assert.IsNull (headers.ExpectContinue, "#1");
581
582                         headers.ExpectContinue = false;
583                         Assert.IsFalse (headers.ExpectContinue.Value, "#2");
584
585                         headers.Clear ();
586
587                         headers.ExpectContinue = true;
588                         headers.ExpectContinue = true;
589                         headers.ExpectContinue = true;
590                         headers.ExpectContinue = true;
591                         Assert.IsTrue (headers.ExpectContinue.Value, "#3");
592                         Assert.AreEqual (1, headers.GetValues ("expect").ToList ().Count, "#4");
593
594                         headers.Clear ();
595                         headers.Expect.Add (new NameValueWithParametersHeaderValue ("100-conTinuE"));
596                         Assert.IsTrue (headers.ExpectContinue.Value, "#5");
597                 }
598
599                 [Test]
600                 public void Headers_ConnectionClose ()
601                 {
602                         HttpRequestMessage message = new HttpRequestMessage ();
603                         HttpRequestHeaders headers = message.Headers;
604                         Assert.IsNull (headers.ConnectionClose, "#1");
605
606                         headers.ConnectionClose = false;
607                         Assert.IsFalse (headers.ConnectionClose.Value, "#2");
608
609                         headers.Clear ();
610
611                         headers.ConnectionClose = true;
612                         Assert.IsTrue (headers.ConnectionClose.Value, "#3");
613
614                         headers.Clear ();
615                         headers.Connection.Add ("Close");
616                         Assert.IsTrue (headers.ConnectionClose.Value, "#4");
617                 }
618
619                 [Test]
620                 public void Headers_From_Invalid ()
621                 {
622                         HttpRequestMessage message = new HttpRequestMessage ();
623                         HttpRequestHeaders headers = message.Headers;
624                         headers.From = null;
625                         headers.From = "";
626                         try {
627                                 headers.From = " ";
628                                 Assert.Fail ("#1");
629                         } catch (FormatException) {
630                         }
631                         try {
632                                 headers.From = "test";
633                                 Assert.Fail ("#2");
634                         } catch (FormatException) {
635                         }
636                 }
637
638                 [Test]
639                 public void Headers_TransferEncodingChunked ()
640                 {
641                         HttpRequestMessage message = new HttpRequestMessage ();
642                         HttpRequestHeaders headers = message.Headers;
643                         Assert.IsNull (headers.TransferEncodingChunked, "#1");
644
645                         headers.TransferEncodingChunked = false;
646                         Assert.IsFalse (headers.TransferEncodingChunked.Value, "#2");
647
648                         headers.Clear ();
649
650                         headers.TransferEncodingChunked = true;
651                         Assert.IsTrue (headers.TransferEncodingChunked.Value, "#3");
652                         Assert.AreEqual (1, headers.TransferEncoding.Count, "#3b");
653                 }
654
655                 [Test]
656                 public void Properties ()
657                 {
658                         var c = new HttpRequestMessage ();
659                         c.Content = null;
660                         c.Method = HttpMethod.Post;
661                         c.Properties.Add ("a", "test");
662                         c.RequestUri = null;
663                         c.Version = HttpVersion.Version10;
664
665                         Assert.IsNull (c.Content, "#1");
666                         Assert.AreEqual (HttpMethod.Post, c.Method, "#2");
667                         Assert.AreEqual ("test", c.Properties["a"], "#3");
668                         Assert.IsNull (c.RequestUri, "#4");
669                         Assert.AreEqual (HttpVersion.Version10, c.Version, "#5");
670                 }
671
672                 [Test]
673                 public void Properties_Invalid ()
674                 {
675                         var c = new HttpRequestMessage ();
676                         try {
677                                 c.Method = null;
678                                 Assert.Fail ("#1");
679                         } catch (ArgumentNullException) {
680                         }
681
682                         try {
683                                 c.RequestUri = new Uri ("ftp://xamarin.com");
684                                 Assert.Fail ("#2");
685                         } catch (ArgumentException) {
686                         }
687
688                         try {
689                                 c.Version = null;
690                                 Assert.Fail ("#3");
691                         } catch (ArgumentNullException) {
692                         }
693                 }
694         }
695 }