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