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