UriExtensions

We found 10 examples in language CSharp for this search. You will see 46 fragments of code.

		[TestMethod]
		public void AppendTrailingSlash_String()
		{
			Assert.AreEqual("http://www.google.com/", UriExtensions.AppendTrailingSlash("http://www.google.com"));
			Assert.AreEqual("http://www.google.com/", UriExtensions.AppendTrailingSlash("http://www.google.com/"));
			Assert.AreEqual("test/", UriExtensions.AppendTrailingSlash("test"));
			Assert.AreEqual("test/", UriExtensions.AppendTrailingSlash("test/"));
		}

		[TestMethod]
		public void RemoveTrailingSlash_String()
		{
			Assert.AreEqual("http://www.google.com", UriExtensions.RemoveTrailingSlash("http://www.google.com"));
			Assert.AreEqual("http://www.google.com", UriExtensions.RemoveTrailingSlash("http://www.google.com/"));
		}

		[TestMethod]
		public void RemoveHeadSlash_String()
		{
			Assert.AreEqual("test", UriExtensions.RemoveHeadSlash("/test"));
			Assert.AreEqual("test", UriExtensions.RemoveHeadSlash("test"));
		}

		[TestMethod]
		public void AppRelativeUrlToRelativeUrl()
		{
			Assert.AreEqual("/base/test", UriExtensions.AppRelativeUrlToRelativeUrl("~/test", "/base"));
			Assert.AreEqual("/base/test", UriExtensions.AppRelativeUrlToRelativeUrl("~/test", "base"));

			Assert.AreEqual("/base/test", UriExtensions.AppRelativeUrlToRelativeUrl("test", "/base"));
			Assert.AreEqual("/test", UriExtensions.AppRelativeUrlToRelativeUrl("/test", "/base"));
		}

		[TestMethod()]
		[Description("Append(Uri, String) method when there is a trailing slash on 'instance'.")]
		public void UriExtensions_Unit_Append_TrailingSlash() {
			Uri instance = new Uri("http://www.google.com/path/");
			String relativePath = "folder1/folder2";
			Uri actual = UriExtensions.Append(instance, relativePath);
			Assert.AreEqual(new Uri("http://www.google.com/path/folder1/folder2"), actual);
		}
		[TestMethod()]
		[Description("Append(Uri, String) method when there is a leading slash on 'relativePath'.")]
		public void UriExtensions_Unit_Append_LeadingSlash() {
			Uri instance = new Uri("http://www.google.com");
			String relativePath = "/folder1/folder2";
			Uri actual = UriExtensions.Append(instance, relativePath);
			Assert.AreEqual(new Uri("http://www.google.com/folder1/folder2"), actual);
		}
		[TestMethod()]
		[Description("Append(Uri, String) method when there is a trailing slash on 'instance' and a leading slash on 'relativePath'.")]
		public void UriExtensions_Unit_Append_TwoSlashes() {
			Uri instance = new Uri("http://www.google.com/");
			String relativePath = "/folder1/folder2";
			Uri actual = UriExtensions.Append(instance, relativePath);
			Assert.AreEqual(new Uri("http://www.google.com/folder1/folder2"), actual);
		}
		[TestMethod()]
		[Description("Append(Uri, String) method when there is query information.")]
		public void UriExtensions_Unit_Append_WithQuery() {
			Uri instance = new Uri("http://www.google.com/?key=value");
			String relativePath = "folder1/folder2";
			Uri actual = UriExtensions.Append(instance, relativePath);
			Assert.AreEqual(new Uri("http://www.google.com/folder1/folder2?key=value"), actual);
		}
		[TestMethod()]
		[Description("Append(Uri, String) method when 'instance' is a null reference.")]
		[ExpectedException(typeof(ArgumentNullException))]
		public void UriExtensions_Unit_Append_InstanceNull() {
			Uri instance = null;
			String relativePath = "folder1/folder2";
			UriExtensions.Append(instance, relativePath);
		}
		[TestMethod()]
		[Description("Append(Uri, String) method when 'relativePath' is a null reference.")]
		[ExpectedException(typeof(ArgumentNullException))]
		public void UriExtensions_Unit_Append_RelativePathNull() {
			Uri instance = new Uri("http://www.google.com");
			String relativePath = null;
			UriExtensions.Append(instance, relativePath);
		}

        [Test]
        public void AssertThatUriSchemaAssertionWorksForHttpAndHttps()
        {
            UriExtensions.AssertUriIsHttpOrHttpsSchema(new Uri("http://somehost.com:23/"));
            UriExtensions.AssertUriIsHttpOrHttpsSchema(new Uri("https://someotherhost.com"));
        }

        [Test]
        public void AssertThatNonHttpAndHttpsSchemasThrow()
        {
            Assert.Throws<ArgumentException>(() => UriExtensions.AssertUriIsHttpOrHttpsSchema(new Uri("file://somehost.com")));
            Assert.Throws<ArgumentException>(() => UriExtensions.AssertUriIsHttpOrHttpsSchema(new Uri("ftp://somehost.com")));
        }

        [Test]
        public void AssertThatUriSchemaAssertionWorksForFile()
        {
            UriExtensions.AssertUriIsFileSchema(new Uri("file://localhost/etc/fstab"));
            UriExtensions.AssertUriIsFileSchema(new Uri("file:///c:/WINDOWS/clock.avi"));
        }

        [Test]
        public void AssertThatNonFileSchemasThrow()
        {
            Assert.Throws<ArgumentException>(() => UriExtensions.AssertUriIsFileSchema(new Uri("http://somehost.com")));
            Assert.Throws<ArgumentException>(() => UriExtensions.AssertUriIsFileSchema(new Uri("ftp://somehost.com")));
        }

        [Test]
        public void AssertThatUriSchemaAssertionWorksForHttpAndHttps()
        {
            UriExtensions.AssertUriIsHttpOrHttpsSchema(new Uri("http://somehost.com:23/"));
            UriExtensions.AssertUriIsHttpOrHttpsSchema(new Uri("https://someotherhost.com"));
        }

        [Test]
        public void AssertThatNonHttpAndHttpsSchemasThrow()
        {
            Assert.Throws<ArgumentException>(() => UriExtensions.AssertUriIsHttpOrHttpsSchema(new Uri("file://somehost.com")));
            Assert.Throws<ArgumentException>(() => UriExtensions.AssertUriIsHttpOrHttpsSchema(new Uri("ftp://somehost.com")));
        }

        [Test]
        public void AssertThatUriSchemaAssertionWorksForFile()
        {
            UriExtensions.AssertUriIsFileSchema(new Uri("file://localhost/etc/fstab"));
            UriExtensions.AssertUriIsFileSchema(new Uri("file:///c:/WINDOWS/clock.avi"));
        }

        [Test]
        public void AssertThatNonFileSchemasThrow()
        {
            Assert.Throws<ArgumentException>(() => UriExtensions.AssertUriIsFileSchema(new Uri("http://somehost.com")));
            Assert.Throws<ArgumentException>(() => UriExtensions.AssertUriIsFileSchema(new Uri("ftp://somehost.com")));
        }


        [TestMethod]
        public void BuildQueryString_WithEmptyInitialQueryString_ReturnsOnlyParameters()
        {
            var actual = UriExtensions.BuildQueryString("", false, new Dictionary<string, object>
            {
                { "x", 1 },
                { "y", 2 },
                { "z", 3 },
            });

            Assert.AreEqual("x=1&y=2&z=3", actual);
        }

        [TestMethod]
        public void BuildQueryString_WithEmptyParameters_ReturnsOnlyInitialQueryString()
        {
            var actual = UriExtensions.BuildQueryString("a=4&b=5&c=6", false, new Dictionary<string, object>());

            Assert.AreEqual("a=4&b=5&c=6", actual);
        }

        [TestMethod]
        public void BuildQueryString_WithNullParameter_IgnoresTheParameter()
        {
            var actual = UriExtensions.BuildQueryString("", false, new Dictionary<string, object>
            {
                { "x", 1 },
                { "y", null },
                { "z", 3 },
            });

            Assert.AreEqual("x=1&z=3", actual);
        }

        [TestMethod]
        public void BuildQueryString_WithInitialQueryStringAndParameters_ReturnsBoth()
        {
            var actual = UriExtensions.BuildQueryString("a=4&b=5&c=6", false, new Dictionary<string, object>
            {
                { "x", 1 },
                { "y", 2 },
                { "z", 3 },
            });

            Assert.AreEqual("a=4&b=5&c=6&x=1&y=2&z=3", actual);
        }

        [TestMethod]
        public void BuildQueryString_WithoutShortArraySerialization_ReturnsLongQueryString()
        {
            var actual = UriExtensions.BuildQueryString("", false, new Dictionary<string, object>
            {
                { "x", new [] {1, 2, 3 } },
            });

            Assert.AreEqual("x=1&x=2&x=3", actual);
        }

        [TestMethod]
        public void BuildQueryString_WithShortArraySerialization_ReturnsShortQueryString()
        {
            var actual = UriExtensions.BuildQueryString("", true, new Dictionary<string, object>
            {
                { "x", new [] {1, 2, 3 } },
            });

            Assert.AreEqual("x=1%2c2%2c3", actual);
        }


        private static String FormatParameterKey(String key)
        {
            return String.Concat(UriExtensions.ParameterPrefix, key);
        }

        public static Uri RemoveAddedParameters(this Uri uri)
        {
            NameValueCollection parameters = uri.GetParameters();
            parameters.AllKeys.Where(k => k.StartsWith(UriExtensions.ParameterPrefix, StringComparison.InvariantCulture)).ToList().ForEach(k => parameters.Remove(k));
            return uri.Update(parameters);
        }

        public static Uri AddParameters(this Uri uri, NameValueCollection parameters)
        {
            NameValueCollection currentParameters = uri.RemoveAddedParameters().GetParameters();
            foreach (String key in parameters)
            {
                currentParameters.Add(UriExtensions.FormatParameterKey(key), parameters[key]);
            }
            return uri.Update(currentParameters);
        }

        public static String GetAddedParameter(this Uri uri, String key)
        {
            NameValueCollection parameters = uri.GetParameters();
            String formatedKey = UriExtensions.FormatParameterKey(key);
            String existingKey = parameters.AllKeys.FirstOrDefault(k => k.EqualsInvariantCultureIgnoreCase(formatedKey));
            return String.IsNullOrEmpty(existingKey) ? null : parameters[existingKey];
        }

        [Fact]
        public void UriExtensions_ExtractUris_SimpleUri()
        {
            string input = "http://www.google.com";
            Uri expected = new Uri(input);

            UriExtensions.ExtractUris(input).Single().Should().Be(expected);
        }

        [Fact]
        public void UriExtensions_ExtractUris_FromHtml()
        {
            string input = "<a href=\"http://www.google.com\"><other";
            Uri expected = new Uri("http://www.google.com");

            UriExtensions.ExtractUris(input).Single().Should().Be(expected);
        }

        [Fact]
        public void UriExtensions_ExtractUris()
        {
            string input = " Support the awesome drummer Eddie Fisher of OneRepublic! Go to the Facebook page and click \"I like\"! http://www.facebook.com/pages/Eddie-Fisher-OneRepublic-F ...";
            Uri expected = new Uri("http://www.facebook.com/pages/Eddie-Fisher-OneRepublic-F");

            UriExtensions.ExtractUris(input).Single().Should().Be(expected);
        }

        [Fact]
        public void UriExtensions_ExtractUris_WithoutHost()
        {
            string input = "{asd:}";
            UriExtensions.ExtractUris(input).Any().Should().BeFalse();
        }

        [Fact]
        public void UriExtensions_IsWeb()
        {
            UriExtensions.IsWeb(new Uri("http://www.google.com")).Should().BeTrue();
            UriExtensions.IsWeb(new Uri("https://www.google.com")).Should().BeTrue();
            UriExtensions.IsWeb(new Uri("ftp://www.google.com")).Should().BeFalse();
            UriExtensions.IsWeb(new Uri("nttp://www.google.com")).Should().BeFalse();
        }

        [Fact]
        public void UriExtensions_ResolveRedirects_Facebook()
        {
            Uri uri = new Uri("http://t.co/6VyBZOO");
            Uri expected = new Uri("https://www.facebook.com/photo.php?pid=585685&l=588142298a&id=100399403341183");

            UriExtensions.ResolveRedirects(uri).Should().Be(expected);
        }


        private static String FormatParameterKey(String key)
        {
            return String.Concat(UriExtensions.ParameterPrefix, key);
        }

        public static Uri RemoveAddedParameters(this Uri uri)
        {
            NameValueCollection parameters = uri.GetParameters();
            parameters.AllKeys.Where(k => k.StartsWith(UriExtensions.ParameterPrefix, StringComparison.InvariantCulture)).ToList().ForEach(k => parameters.Remove(k));
            return uri.Update(parameters);
        }

        public static Uri AddParameters(this Uri uri, NameValueCollection parameters)
        {
            NameValueCollection currentParameters = uri.RemoveAddedParameters().GetParameters();

            foreach (String key in parameters)
            {
                currentParameters.Add(UriExtensions.FormatParameterKey(key), parameters[key]);
            }

            return uri.Update(currentParameters);
        }

        public static String GetAddedParameter(this Uri uri, String key)
        {
            NameValueCollection parameters = uri.GetParameters();
            String formatedKey = UriExtensions.FormatParameterKey(key);
            String existingKey = parameters.AllKeys.FirstOrDefault(k => k.EqualsOrdinalIgnoreCase(formatedKey));

            return String.IsNullOrEmpty(existingKey) ? null : parameters[existingKey];
        }


        private static String FormatParameterKey(String key)
        {
            return String.Concat(UriExtensions.ParameterPrefix, key);
        }

        public static Uri RemoveAddedParameters(this Uri uri)
        {
            NameValueCollection parameters = uri.GetParameters();
            parameters.AllKeys.Where(k => k.StartsWith(UriExtensions.ParameterPrefix, StringComparison.InvariantCulture)).ToList().ForEach(k => parameters.Remove(k));
            return uri.Update(parameters);
        }

        public static Uri AddParameters(this Uri uri, NameValueCollection parameters)
        {
            NameValueCollection currentParameters = uri.RemoveAddedParameters().GetParameters();

            foreach (String key in parameters)
            {
                currentParameters.Add(UriExtensions.FormatParameterKey(key), parameters[key]);
            }

            return uri.Update(currentParameters);
        }

        public static String GetAddedParameter(this Uri uri, String key)
        {
            NameValueCollection parameters = uri.GetParameters();
            String formatedKey = UriExtensions.FormatParameterKey(key);
            String existingKey = parameters.AllKeys.FirstOrDefault(k => k.EqualsOrdinalIgnoreCase(formatedKey));

            return String.IsNullOrEmpty(existingKey) ? null : parameters[existingKey];
        }


        private static String FormatParameterKey(String key)
        {
            return String.Concat(UriExtensions.ParameterPrefix, key);
        }

        public static Uri RemoveAddedParameters(this Uri uri)
        {
            NameValueCollection parameters = uri.GetParameters();
            parameters.AllKeys.Where(k => k.StartsWith(UriExtensions.ParameterPrefix, StringComparison.InvariantCulture)).ToList().ForEach(k => parameters.Remove(k));
            return uri.Update(parameters);
        }

        public static Uri AddParameters(this Uri uri, NameValueCollection parameters)
        {
            NameValueCollection currentParameters = uri.RemoveAddedParameters().GetParameters();

            foreach (String key in parameters)
            {
                currentParameters.Add(UriExtensions.FormatParameterKey(key), parameters[key]);
            }

            return uri.Update(currentParameters);
        }

        public static String GetAddedParameter(this Uri uri, String key)
        {
            NameValueCollection parameters = uri.GetParameters();
            String formatedKey = UriExtensions.FormatParameterKey(key);
            String existingKey = parameters.AllKeys.FirstOrDefault(k => k.EqualsOrdinalIgnoreCase(formatedKey));

            return String.IsNullOrEmpty(existingKey) ? null : parameters[existingKey];
        }

Uno.Extensions.UriExtensions : Object

Methods :

public static String EscapeDataString(String value = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()