Merge pull request #5714 from alexischr/update_bockbuild
[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                         Assert.IsTrue (se, "#1");
468                         Assert.AreEqual ("MonoDevelop (Unix 3.13.0; amd64; en-US; Octokit 0.3.4)", headers.UserAgent.ToString (), "#2");
469                 }
470
471                 [Test]
472                 public void Headers_UserAgentExtraWithTabs ()
473                 {
474                         HttpRequestMessage message = new HttpRequestMessage ();
475                         var headers = message.Headers;
476
477                         headers.Add ("User-Agent", "A \t  \t B");
478
479                         var se = headers.UserAgent.SequenceEqual (
480                                 new[] {
481                                                 new ProductInfoHeaderValue ("A", null),
482                                                 new ProductInfoHeaderValue ("B", null)
483                                 });
484
485                         Assert.IsTrue (se, "#1");
486                         Assert.AreEqual ("A B", headers.UserAgent.ToString (), "#2");
487                 }
488
489                 [Test]
490                 public void Headers_UserAgentInvalid ()
491                 {
492                         HttpRequestMessage message = new HttpRequestMessage ();
493                         var headers = message.Headers;
494
495                         try {
496                                 headers.Add ("User-Agent", "A(B)");
497                                 Assert.Fail ("#1");
498                         } catch (FormatException) {
499                         }
500                 }
501
502                 [Test]
503                 public void Headers_AcceptMulti ()
504                 {
505                         var request = new HttpRequestMessage();
506                         var headers = request.Headers;
507
508                         headers.Add("Accept", "application/vnd.github.moondragon+json; charset=utf-8,application/vnd.github.v3+json; charset=utf-8");
509
510                         var se = headers.Accept.SequenceEqual (
511                                 new[] {
512                                         new MediaTypeHeaderValue ("application/vnd.github.moondragon+json") {
513                                                 CharSet = "utf-8"
514                                         },
515                                         new MediaTypeHeaderValue ("application/vnd.github.v3+json") {
516                                                 CharSet = "utf-8"
517                                         }
518                                 });
519
520                         Assert.IsTrue (se, "#1");
521                         Assert.AreEqual ("application/vnd.github.moondragon+json; charset=utf-8, application/vnd.github.v3+json; charset=utf-8", headers.Accept.ToString (), "#2");
522                 }
523
524                 [Test]
525                 public void Header_BaseImplementation ()
526                 {
527                         HttpRequestMessage message = new HttpRequestMessage ();
528                         HttpRequestHeaders headers = message.Headers;
529
530                         headers.Add ("a", "a-value");
531                         headers.Add ("b", new List<string> { "v1", "v2" });
532                         headers.Add ("c", null as string);
533                         headers.Add ("d", new string[0]);
534
535                         Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#0");
536
537                         Assert.IsFalse (headers.Contains ("nn"), "#1a");
538                         Assert.IsTrue (headers.Contains ("b"), "#1b");
539
540                         var values = headers.GetValues ("b").ToList ();
541                         Assert.AreEqual ("v1", values[0], "#2a");
542                         Assert.AreEqual ("v2", values[1], "#2b");
543
544                         Assert.IsFalse (headers.Remove ("p"), "#3a");
545                         Assert.IsTrue (headers.Remove ("b"), "#3b");
546                         Assert.IsFalse (headers.Contains ("b"), "#3b-c");
547
548                         IEnumerable<string> values2;
549                         Assert.IsTrue (headers.TryGetValues ("c", out values2));
550                         values = values2.ToList ();
551                         Assert.AreEqual ("", values[0], "#4a");
552
553                         int counter = 0;
554                         foreach (var i in headers) {
555                                 ++counter;
556                         }
557
558                         Assert.AreEqual (3, counter, "#5");
559
560                         headers.Clear ();
561
562                         headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
563                         Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#55");
564
565                         values = headers.GetValues ("accept").ToList ();
566                         Assert.AreEqual (2, values.Count, "#6");
567                         Assert.AreEqual ("audio/x", values[0], "#6a");
568                         Assert.AreEqual ("audio", values[1], "#6b");
569                         Assert.AreEqual (1, headers.Accept.Count, "#6c");
570
571                         headers.Clear ();
572
573                         Assert.IsTrue (headers.TryAddWithoutValidation ("from", new[] { "a@a.com", "ssss@oo.com" }), "#70");
574                         values = headers.GetValues ("from").ToList ();
575
576                         Assert.AreEqual (2, values.Count, "#7");
577                         Assert.AreEqual ("a@a.com", values[0], "#7a");
578                         Assert.AreEqual ("ssss@oo.com", values[1], "#7b");
579                         Assert.AreEqual ("a@a.com", headers.From, "#7c");
580
581                         headers.Clear ();
582
583                         Assert.IsTrue (headers.TryAddWithoutValidation ("Date", "wrong date"), "#8-0");
584                         var value = headers.Date;
585                         Assert.IsNull (headers.Date, "#8");
586                 }
587
588                 [Test]
589                 public void Headers_Invalid ()
590                 {
591                         HttpRequestMessage message = new HttpRequestMessage ();
592                         HttpRequestHeaders headers = message.Headers;
593
594                         try {
595                                 headers.Add ("Allow", "");
596                                 Assert.Fail ("#1");
597                         } catch (InvalidOperationException) {
598                         }
599
600                         try {
601                                 headers.Add (null, "");
602                                 Assert.Fail ("#2");
603                         } catch (ArgumentException) {
604                         }
605
606                         try {
607                                 headers.Add ("mm", null as IEnumerable<string>);
608                                 Assert.Fail ("#2b");
609                         } catch (ArgumentNullException) {
610                         }
611
612                         try {
613                                 headers.Add ("accept", "audio");
614                                 Assert.Fail ("#2c");
615                         } catch (FormatException) {
616                         }
617
618                         Assert.IsFalse (headers.TryAddWithoutValidation ("Allow", ""), "#3"); ;
619
620                         Assert.IsFalse (headers.TryAddWithoutValidation (null, ""), "#4");
621
622                         try {
623                                 headers.Contains (null);
624                                 Assert.Fail ("#5");
625                         } catch (ArgumentException) {
626                         }
627
628                         try {
629                                 headers.GetValues (null);
630                                 Assert.Fail ("#6a");
631                         } catch (ArgumentException) {
632                         }
633
634                         try {
635                                 headers.GetValues ("bbbb");
636                                 Assert.Fail ("#6b");
637                         } catch (InvalidOperationException) {
638                         }
639
640                         try {
641                                 headers.Add ("from", new[] { "a@a.com", "ssss@oo.com" });
642                                 Assert.Fail ("#7a");
643                         } catch (FormatException) {
644                         }
645
646                         Assert.IsTrue (headers.TryAddWithoutValidation ("from", "a@a.com"), "#7-0");
647                         try {
648                                 headers.Add ("from", "valid@w3.org");
649                                 Assert.Fail ("#7b");
650                         } catch (FormatException) {
651                         }
652                 }
653
654                 [Test]
655                 public void Headers_Response ()
656                 {
657                         HttpRequestMessage message = new HttpRequestMessage ();
658                         HttpRequestHeaders headers = message.Headers;
659
660                         headers.Add ("Age", "vv");
661                         Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#1");
662
663                         headers.Clear ();
664                         headers.TryAddWithoutValidation ("Age", "vv");
665                         Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#2");
666
667                         // .NET encloses the "Age: vv" with two whitespaces.
668                         var normalized = Regex.Replace (message.ToString (), @"\s", "");
669                         Assert.AreEqual ("Method:GET,RequestUri:'<null>',Version:1.1,Content:<null>,Headers:{Age:vv}", normalized, "#3");
670                 }
671
672                 [Test]
673                 public void Headers_ExpectContinue ()
674                 {
675                         HttpRequestMessage message = new HttpRequestMessage ();
676                         HttpRequestHeaders headers = message.Headers;
677                         Assert.IsNull (headers.ExpectContinue, "#1");
678
679                         headers.ExpectContinue = false;
680                         Assert.IsFalse (headers.ExpectContinue.Value, "#2");
681
682                         headers.Clear ();
683
684                         headers.ExpectContinue = true;
685                         headers.ExpectContinue = true;
686                         headers.ExpectContinue = true;
687                         headers.ExpectContinue = true;
688                         Assert.IsTrue (headers.ExpectContinue.Value, "#3");
689                         Assert.AreEqual (1, headers.GetValues ("expect").ToList ().Count, "#4");
690
691                         headers.Clear ();
692                         headers.Expect.Add (new NameValueWithParametersHeaderValue ("100-conTinuE"));
693                         Assert.IsTrue (headers.ExpectContinue.Value, "#5");
694                 }
695
696                 [Test]
697                 public void Headers_ConnectionClose ()
698                 {
699                         HttpRequestMessage message = new HttpRequestMessage ();
700                         HttpRequestHeaders headers = message.Headers;
701                         Assert.IsNull (headers.ConnectionClose, "#1");
702
703                         headers.ConnectionClose = false;
704                         Assert.IsFalse (headers.ConnectionClose.Value, "#2");
705
706                         headers.Clear ();
707
708                         headers.ConnectionClose = true;
709                         Assert.IsTrue (headers.ConnectionClose.Value, "#3");
710
711                         headers.Clear ();
712                         headers.Connection.Add ("Close");
713                         Assert.IsTrue (headers.ConnectionClose.Value, "#4");
714                 }
715
716                 [Test]
717                 public void Headers_From_Invalid ()
718                 {
719                         HttpRequestMessage message = new HttpRequestMessage ();
720                         HttpRequestHeaders headers = message.Headers;
721                         headers.From = null;
722                         headers.From = "";
723                         try {
724                                 headers.From = " ";
725                                 Assert.Fail ("#1");
726                         } catch (FormatException) {
727                         }
728                         try {
729                                 headers.From = "test";
730                                 Assert.Fail ("#2");
731                         } catch (FormatException) {
732                         }
733                 }
734
735                 [Test]
736                 public void Headers_TransferEncodingChunked ()
737                 {
738                         HttpRequestMessage message = new HttpRequestMessage ();
739                         HttpRequestHeaders headers = message.Headers;
740                         Assert.IsNull (headers.TransferEncodingChunked, "#1");
741
742                         headers.TransferEncodingChunked = false;
743                         Assert.IsFalse (headers.TransferEncodingChunked.Value, "#2");
744
745                         headers.Clear ();
746
747                         headers.TransferEncodingChunked = true;
748                         Assert.IsTrue (headers.TransferEncodingChunked.Value, "#3");
749                         Assert.AreEqual (1, headers.TransferEncoding.Count, "#3b");
750                 }
751
752                 [Test]
753                 public void Properties ()
754                 {
755                         var c = new HttpRequestMessage ();
756                         c.Content = null;
757                         c.Method = HttpMethod.Post;
758                         c.Properties.Add ("a", "test");
759                         c.RequestUri = null;
760                         c.Version = HttpVersion.Version10;
761
762                         Assert.IsNull (c.Content, "#1");
763                         Assert.AreEqual (HttpMethod.Post, c.Method, "#2");
764                         Assert.AreEqual ("test", c.Properties["a"], "#3");
765                         Assert.IsNull (c.RequestUri, "#4");
766                         Assert.AreEqual (HttpVersion.Version10, c.Version, "#5");
767                 }
768
769                 [Test]
770                 public void Properties_Invalid ()
771                 {
772                         var c = new HttpRequestMessage ();
773                         try {
774                                 c.Method = null;
775                                 Assert.Fail ("#1");
776                         } catch (ArgumentNullException) {
777                         }
778
779                         try {
780                                 c.RequestUri = new Uri ("ftp://xamarin.com");
781                                 Assert.Fail ("#2");
782                         } catch (ArgumentException) {
783                         }
784
785                         try {
786                                 c.Version = null;
787                                 Assert.Fail ("#3");
788                         } catch (ArgumentNullException) {
789                         }
790                 }
791         }
792 }