示例#1
0
        public void TestEncodeLikeUrlEncode()
        {
            const string chars    = ".:#&?=!$%'()*+,/;@[]-_~{} ";
            var          expected = HttpUtility.UrlEncode(chars);
            var          test     = UrlUtility.Escape(chars, UrlEscapeFlags.LikeUrlEncode);

            Assert.Equal(expected, test);
        }
示例#2
0
        public void TestEncodeLikeEscapeUriString()
        {
            const string chars    = ".:#&?=!$%'()*+,/;@[]-_~{} ";
            var          expected = Uri.EscapeUriString(chars);
            var          test     = UrlUtility.Escape(chars, UrlEscapeFlags.AllowLikeEscapeUriString);

            Assert.Equal(expected, test);
        }
示例#3
0
        public void TestUrlEncodeComplianceUmlaut()
        {
            const string chars    = "äöüßÄÖÜ\u007F";
            var          expected = HttpUtility.UrlEncode(chars);
            var          test     = UrlUtility.Escape(chars, UrlEscapeFlags.LikeUrlEncode);

            Assert.Equal(expected, test);
        }
示例#4
0
        public void TestUrlEncodeComplianceASCII()
        {
            var chars    = new string(Enumerable.Range(32, 95).Select(x => (char)x).ToArray());
            var expected = HttpUtility.UrlEncode(chars);
            var test     = UrlUtility.Escape(chars, UrlEscapeFlags.LikeUrlEncode);

            Assert.Equal(expected, test);
        }
示例#5
0
        public void TestEscapeDataStringComplianceUmlaut()
        {
            const string chars    = "äöüßÄÖÜ\u007F";
            var          expected = Uri.EscapeDataString(chars);
            var          test     = UrlUtility.Escape(chars);

            Assert.Equal(expected, test);
        }
示例#6
0
        public void TestEscapeDataStringComplianceASCII()
        {
            var chars    = new string(Enumerable.Range(32, 95).Select(x => (char)x).ToArray());
            var expected = Uri.EscapeDataString(chars);
            var test     = UrlUtility.Escape(chars);

            Assert.Equal(expected, test);
        }
示例#7
0
        /// <summary>
        /// URL encodes a string based on section 5.1 of the OAuth spec.
        /// Namely, percent encoding with [RFC3986], avoiding unreserved characters,
        /// upper-casing hexadecimal characters, and UTF-8 encoding for text value pairs.
        /// </summary>
        /// <param name="value">The value to escape.</param>
        /// <returns>The escaped value.</returns>
        /// <remarks>
        /// The <see cref="Uri.EscapeDataString"/> method is <i>supposed</i> to take on
        /// RFC 3986 behavior if certain elements are present in a .config file. Even if this
        /// actually worked (which in my experiments it <i>doesn't</i>), we can't rely on every
        /// host actually having this configuration element present.
        /// </remarks>
        /// <a href="http://oauth.net/core/1.0#encoding_parameters" />
        /// <a href="http://stackoverflow.com/questions/846487/how-to-get-uri-escapedatastring-to-comply-with-rfc-3986" />
        public static string UrlEncodeRelaxed(string value)
        {
            // Start with RFC 2396 escaping by calling the .NET method to do the work.
            // This MAY sometimes exhibit RFC 3986 behavior (according to the documentation).
            // If it does, the escaping we do that follows it will be a no-op since the
            // characters we search for to replace can't possibly exist in the string.
            var escaped = new StringBuilder(UrlUtility.Escape(value));

            // Upgrade the escaping to RFC 3986, if necessary.
            for (int i = 0; i < _uriRfc3986CharsToEscape.Length; i++)
            {
                string t = _uriRfc3986CharsToEscape[i];
                escaped.Replace(t, _uriRfc3968EscapedHex[i]);
            }
            // Return the fully-RFC3986-escaped string.
            return(escaped.ToString());
        }