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