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