Merge branch 'patch-1' of https://github.com/ReubenBond/mono into ReubenBond-patch-1
[mono.git] / mcs / class / System.Net.Http / Test / System.Net.Http / HttpRequestMessageTest.cs
index 0d331b735c6db06e95f4cac252b717d4b1993853..8b4d6bf394e4e8aa12bdd0842d18d148280b726c 100644 (file)
 using System;
 using System.Collections;
 using System.Collections.Generic;
+using System.Text.RegularExpressions;
 using NUnit.Framework;
 using System.Net.Http;
 using System.Net;
 using System.Net.Http.Headers;
 using System.Linq;
+using System.IO;
 
 namespace MonoTests.System.Net.Http
 {
@@ -60,6 +62,8 @@ namespace MonoTests.System.Net.Http
                        Assert.IsNotNull (m.Properties, "#4");
                        Assert.IsNull (m.RequestUri, "#5");
                        Assert.AreEqual (new Version (1, 1), m.Version, "#6");
+
+                       Assert.AreEqual ("Method: GET, RequestUri: '<null>', Version: 1.1, Content: <null>, Headers:\r\n{\r\n}", m.ToString (), "#7");
                }
 
                [Test]
@@ -84,6 +88,27 @@ namespace MonoTests.System.Net.Http
                        }
                }
 
+               [Test]
+               public void Ctor_RelativeUri ()
+               {
+                       var client = new HttpClient ();
+                       client.BaseAddress = new Uri ("http://en.wikipedia.org/wiki/");
+                       var uri = new Uri ("Computer", UriKind.Relative);
+                       var req = new HttpRequestMessage (HttpMethod.Get, uri);
+                       // HttpRequestMessage does not rewrite it here.
+                       Assert.AreEqual (req.RequestUri, uri);
+               }
+
+               [Test]
+               public void Ctor_RelativeUriString ()
+               {
+                       var client = new HttpClient ();
+                       client.BaseAddress = new Uri ("http://en.wikipedia.org/wiki/");
+                       var req = new HttpRequestMessage (HttpMethod.Get, "Computer");
+                       // HttpRequestMessage does not rewrite it here.
+                       Assert.IsFalse (req.RequestUri.IsAbsoluteUri);
+               }
+
                [Test]
                public void Headers ()
                {
@@ -195,15 +220,19 @@ namespace MonoTests.System.Net.Http
                        } catch (FormatException) {
                        }
 
+                       try {
+                               headers.Add ("pragma", "nocache,RequestID=1,g=");
+                               Assert.Fail ("pragma");
+                       } catch (FormatException) {                             
+                       }
+
                        headers.Add ("accept", "audio/y");
                        headers.Add ("accept-charset", "achs");
                        headers.Add ("accept-encoding", "aenc");
                        headers.Add ("accept-language", "alan");
-                       headers.Add ("cache-control", "cc");
                        headers.Add ("expect", "exp");
                        headers.Add ("if-match", "\"v\"");
                        headers.Add ("if-none-match", "\"v2\"");
-                       headers.Add ("pragma", "p");
                        headers.Add ("TE", "0.8");
                        headers.Add ("trailer", "value2");
                        headers.Add ("transfer-encoding", "ttt");
@@ -211,6 +240,7 @@ namespace MonoTests.System.Net.Http
                        headers.Add ("user-agent", "uaua");
                        headers.Add ("via", "prot v");
                        headers.Add ("warning", "4 ww \"t\"");
+                       headers.Add ("pragma", "nocache,R=1,g");
 
                        Assert.IsTrue (headers.Accept.SequenceEqual (
                                new[] {
@@ -245,7 +275,6 @@ namespace MonoTests.System.Net.Http
                        var cch = new CacheControlHeaderValue () {
                                        MaxAge = TimeSpan.MaxValue,
                                };
-                       cch.Extensions.Add (new NameValueHeaderValue ("cc"));
 
                        Assert.AreEqual (cch, headers.CacheControl);
 
@@ -274,7 +303,6 @@ namespace MonoTests.System.Net.Http
                        Assert.IsTrue (headers.IfNoneMatch.SequenceEqual (new EntityTagHeaderValue[] { new EntityTagHeaderValue ("\"tag2\"", true), new EntityTagHeaderValue ("\"v2\"", false) }));
                        Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.IfRange.Date);
                        Assert.AreEqual (headers.MaxForwards, 0x15b3);
-                       Assert.IsTrue (headers.Pragma.SequenceEqual (new NameValueHeaderValue[] { new NameValueHeaderValue ("name", "value"), new NameValueHeaderValue ("p", null) }));
                        Assert.AreEqual ("p", headers.ProxyAuthorization.Parameter);
                        Assert.AreEqual ("s", headers.ProxyAuthorization.Scheme);
                        Assert.AreEqual (5, headers.Range.Ranges.First ().From);
@@ -323,6 +351,92 @@ namespace MonoTests.System.Net.Http
                                }
                        ));
 
+                       Assert.IsTrue (headers.Pragma.SequenceEqual (
+                               new[] {
+                                       new NameValueHeaderValue ("name", "value"),
+                                       new NameValueHeaderValue ("nocache", null),
+                                       new NameValueHeaderValue ("R", "1"),
+                                       new NameValueHeaderValue ("g", null)
+                               }
+                       ));                     
+               }
+
+               [Test]
+               public void Headers_MultiValues ()
+               {
+                       HttpRequestMessage message = new HttpRequestMessage ();
+                       HttpRequestHeaders headers = message.Headers;
+
+                       headers.Add ("Accept", "application/vnd.citrix.requesttokenresponse+xml, application/vnd.citrix.requesttokenchoices+xml");
+                       headers.Add ("Accept-Charset", "aa ;Q=0,bb;Q=1");
+                       headers.Add ("Expect", "x=1; v, y=5");
+                       headers.Add ("If-Match", "\"a\",*, \"b\",*");
+                       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");
+
+                       Assert.AreEqual (2, headers.Accept.Count, "#1a");
+                       Assert.IsTrue (headers.Accept.SequenceEqual (
+                               new[] {
+                                       new MediaTypeWithQualityHeaderValue ("application/vnd.citrix.requesttokenresponse+xml"),
+                                       new MediaTypeWithQualityHeaderValue ("application/vnd.citrix.requesttokenchoices+xml"),
+                               }
+                       ), "#1b");
+
+                       Assert.AreEqual (2, headers.AcceptCharset.Count, "#2a");
+                       Assert.IsTrue (headers.AcceptCharset.SequenceEqual (
+                               new[] {
+                                       new StringWithQualityHeaderValue ("aa", 0),
+                                       new StringWithQualityHeaderValue ("bb", 1),
+                               }
+                       ), "#2b");
+
+                       Assert.AreEqual (2, headers.Expect.Count, "#3a");
+                       var expect_expected = new[] {
+                                       new NameValueWithParametersHeaderValue ("x", "1") {
+                                       },
+                                       new NameValueWithParametersHeaderValue ("y", "5"),
+                               };
+                       expect_expected [0].Parameters.Add (new NameValueHeaderValue ("v"));
+                       Assert.IsTrue (headers.Expect.SequenceEqual (
+                               expect_expected
+                       ), "#3b");
+
+                       Assert.AreEqual (4, headers.IfMatch.Count, "#4a");
+                       Assert.IsTrue (headers.IfMatch.SequenceEqual (
+                               new[] {
+                                       new EntityTagHeaderValue ("\"a\""),
+                                       EntityTagHeaderValue.Any,
+                                       new EntityTagHeaderValue ("\"b\""),
+                                       EntityTagHeaderValue.Any
+                               }
+                       ), "#4b");
+
+                       Assert.AreEqual (6, headers.UserAgent.Count, "#10a");
+
+                       Assert.IsTrue (headers.UserAgent.SequenceEqual (
+                               new[] {
+                                       new ProductInfoHeaderValue ("Mozilla", "5.0"),
+                                       new ProductInfoHeaderValue ("(Macintosh; Intel Mac OS X 10_8_4)"),
+                                       new ProductInfoHeaderValue ("AppleWebKit", "537.36"),
+                                       new ProductInfoHeaderValue ("(KHTML, like Gecko)"),
+                                       new ProductInfoHeaderValue ("Chrome", "29.0.1547.62"),
+                                       new ProductInfoHeaderValue ("Safari", "537.36")
+                               }
+                       ), "#10b");
+               }
+
+               [Test]
+               public void Headers_UserAgentExtra ()
+               {
+                       HttpRequestMessage message = new HttpRequestMessage ();
+                       var headers = message.Headers;
+
+                       headers.Add ("User-Agent", "MonoDevelop (Unix 3.13.0; amd64; en-US; Octokit 0.3.4)");
+
+                       var se = headers.UserAgent.SequenceEqual (
+                               new[] {
+                                               new ProductInfoHeaderValue ("MonoDevelop", null),
+                                               new ProductInfoHeaderValue ("(Unix 3.13.0; amd64; en-US; Octokit 0.3.4)")
+                               });
                }
 
                [Test]
@@ -336,7 +450,7 @@ namespace MonoTests.System.Net.Http
                        headers.Add ("c", null as string);
                        headers.Add ("d", new string[0]);
 
-                       headers.AddWithoutValidation ("accept", "audio");
+                       Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#0");
 
                        Assert.IsFalse (headers.Contains ("nn"), "#1a");
                        Assert.IsTrue (headers.Contains ("b"), "#1b");
@@ -364,7 +478,7 @@ namespace MonoTests.System.Net.Http
                        headers.Clear ();
 
                        headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
-                       headers.AddWithoutValidation ("accept", "audio");
+                       Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#55");
 
                        values = headers.GetValues ("accept").ToList ();
                        Assert.AreEqual (2, values.Count, "#6");
@@ -374,7 +488,7 @@ namespace MonoTests.System.Net.Http
 
                        headers.Clear ();
 
-                       headers.AddWithoutValidation ("from", new[] { "a@a.com", "ssss@oo.com" });
+                       Assert.IsTrue (headers.TryAddWithoutValidation ("from", new[] { "a@a.com", "ssss@oo.com" }), "#70");
                        values = headers.GetValues ("from").ToList ();
 
                        Assert.AreEqual (2, values.Count, "#7");
@@ -384,7 +498,7 @@ namespace MonoTests.System.Net.Http
 
                        headers.Clear ();
 
-                       headers.AddWithoutValidation ("Date", "wrong date");
+                       Assert.IsTrue (headers.TryAddWithoutValidation ("Date", "wrong date"), "#8-0");
                        var value = headers.Date;
                        Assert.IsNull (headers.Date, "#8");
                }
@@ -396,7 +510,7 @@ namespace MonoTests.System.Net.Http
                        HttpRequestHeaders headers = message.Headers;
 
                        try {
-                               headers.Add ("Age", "");
+                               headers.Add ("Allow", "");
                                Assert.Fail ("#1");
                        } catch (InvalidOperationException) {
                        }
@@ -419,17 +533,9 @@ namespace MonoTests.System.Net.Http
                        } catch (FormatException) {
                        }
 
-                       try {
-                               headers.AddWithoutValidation ("Age", "");
-                               Assert.Fail ("#3");
-                       } catch (InvalidOperationException) {
-                       }
+                       Assert.IsFalse (headers.TryAddWithoutValidation ("Allow", ""), "#3"); ;
 
-                       try {
-                               headers.AddWithoutValidation (null, "");
-                               Assert.Fail ("#4");
-                       } catch (ArgumentException) {
-                       }
+                       Assert.IsFalse (headers.TryAddWithoutValidation (null, ""), "#4");
 
                        try {
                                headers.Contains (null);
@@ -455,7 +561,7 @@ namespace MonoTests.System.Net.Http
                        } catch (FormatException) {
                        }
 
-                       headers.AddWithoutValidation ("from", "a@a.com");
+                       Assert.IsTrue (headers.TryAddWithoutValidation ("from", "a@a.com"), "#7-0");
                        try {
                                headers.Add ("from", "valid@w3.org");
                                Assert.Fail ("#7b");
@@ -463,6 +569,24 @@ namespace MonoTests.System.Net.Http
                        }
                }
 
+               [Test]
+               public void Headers_Response ()
+               {
+                       HttpRequestMessage message = new HttpRequestMessage ();
+                       HttpRequestHeaders headers = message.Headers;
+
+                       headers.Add ("Age", "vv");
+                       Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#1");
+
+                       headers.Clear ();
+                       headers.TryAddWithoutValidation ("Age", "vv");
+                       Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#2");
+
+                       // .NET encloses the "Age: vv" with two whitespaces.
+                       var normalized = Regex.Replace (message.ToString (), @"\s", "");
+                       Assert.AreEqual ("Method:GET,RequestUri:'<null>',Version:1.1,Content:<null>,Headers:{Age:vv}", normalized, "#3");
+               }
+
                [Test]
                public void Headers_ExpectContinue ()
                {