Selaa lähdekoodia

[wcf] UriTemplate must trim the first leading slash in the rendered template.

When using BindBy{Name,Position} UriTemplate must remove the the first slash from
the rendered URI. This fixes the 4 failing tests.
Marek Habersack 15 vuotta sitten
vanhempi
sitoutus
6005f0a7df

+ 13 - 2
mcs/class/System.ServiceModel.Web/System/UriTemplate.cs

@@ -128,6 +128,17 @@ namespace System
 			return BindByNameCommon (baseAddress, null, parameters, omitDefaults);
 		}
 
+		string TrimRenderedUri (StringBuilder sb)
+		{
+			if (sb.Length == 0)
+				return String.Empty;
+			
+			if (sb [0] == '/')
+				sb.Remove (0, 1);
+
+			return sb.ToString ();
+		}
+		
 		Uri BindByNameCommon (Uri baseAddress, NameValueCollection nvc, IDictionary<string,string> dic, bool omitDefaults)
 		{
 			CheckBaseAddress (baseAddress);
@@ -141,7 +152,7 @@ namespace System
 			BindByName (ref src, sb, path, nvc, dic, omitDefaults, false);
 			BindByName (ref src, sb, query, nvc, dic, omitDefaults, true);
 			sb.Append (template.Substring (src));
-			return new Uri (baseAddress.ToString () + sb.ToString ());
+			return new Uri (baseAddress.ToString () + TrimRenderedUri (sb));
 		}
 
 		void BindByName (ref int src, StringBuilder sb, ReadOnlyCollection<string> names, NameValueCollection nvc, IDictionary<string,string> dic, bool omitDefaults, bool query)
@@ -184,7 +195,7 @@ namespace System
 			BindByPosition (ref src, sb, path, values, ref index);
 			BindByPosition (ref src, sb, query, values, ref index);
 			sb.Append (template.Substring (src));
-			return new Uri (baseAddress.ToString () + sb.ToString ());
+			return new Uri (baseAddress.ToString () + TrimRenderedUri (sb));
 		}
 
 		void BindByPosition (ref int src, StringBuilder sb, ReadOnlyCollection<string> names, string [] values, ref int index)

+ 22 - 0
mcs/class/System.ServiceModel.Web/Test/System/UriTemplateTest.cs

@@ -217,6 +217,28 @@ namespace MonoTests.System
 			Assert.AreEqual ("http://localhost/value2/value1/", u.ToString ());
 		}
 
+		[Test]
+		public void BindByNameManySlashes ()
+		{
+			var t = new UriTemplate ("////{foo}/{bar}/");
+			var n = new NameValueCollection ();
+			n.Add ("Bar", "value1"); // case insensitive
+			n.Add ("FOO", "value2"); // case insensitive
+			var u = t.BindByName (new Uri ("http://localhost/"), n);
+			Assert.AreEqual ("http://localhost////value2/value1/", u.ToString ());
+		}
+
+		[Test]
+		public void BindByNameManySlashes2 ()
+		{
+			var t = new UriTemplate ("////{foo}/{bar}/");
+			var n = new NameValueCollection ();
+			n.Add ("Bar", "value1"); // case insensitive
+			n.Add ("FOO", "value2"); // case insensitive
+			var u = t.BindByName (new Uri ("http://localhost//"), n);
+			Assert.AreEqual ("http://localhost/////value2/value1/", u.ToString ());
+		}
+		
 		[Test]
 		public void BindByNameWithDefaults ()
 		{