Example #1
0
        /// <summary>
        /// url添加query或替换segment
        /// </summary>
        /// <param name="uri">url</param>
        /// <param name="keyValues">键值对</param>
        /// <returns></returns>
        protected Uri UsePathQuery(Uri uri, IEnumerable <KeyValuePair <string, string> > keyValues)
        {
            var editor = new UriEditor(uri, this.encoding);

            foreach (var keyValue in keyValues)
            {
                if (editor.Replace(keyValue.Key, keyValue.Value) == false)
                {
                    editor.AddQuery(keyValue.Key, keyValue.Value);
                }
            }
            return(editor.Uri);
        }
Example #2
0
        /// <summary>
        /// 创建新的uri
        /// </summary>
        /// <param name="uri">原始uri</param>
        /// <param name="keyValues">键值对</param>
        /// <returns></returns>
        protected virtual Uri CreateUri(Uri uri, IEnumerable <KeyValue> keyValues)
        {
            var editor = new UriEditor(uri);

            foreach (var keyValue in keyValues)
            {
                if (editor.Replace(keyValue.Key, keyValue.Value) == false)
                {
                    editor.AddQuery(keyValue.Key, keyValue.Value);
                }
            }
            return(editor.Uri);
        }
Example #3
0
        public void BuildTest()
        {
            var encoding = Encoding.UTF8;

            var url    = new Uri("http://www.webapiclient.com");
            var editor = new UriEditor(url, encoding);

            Assert.False(editor.Replace("a", "a"));
            editor.AddQuery("a", "a");
            Assert.True(editor.Uri.ToString() == "http://www.webapiclient.com/?a=a");

            url    = new Uri("http://www.webapiclient.com/path");
            editor = new UriEditor(url, encoding);
            editor.AddQuery("a", "a");
            Assert.True(editor.Uri.ToString() == "http://www.webapiclient.com/path?a=a");

            url    = new Uri("http://www.webapiclient.com/path/");
            editor = new UriEditor(url, encoding);
            editor.AddQuery("a", "a");
            Assert.True(editor.Uri.ToString() == "http://www.webapiclient.com/path/?a=a");


            url    = new Uri("http://www.webapiclient.com/path/?");
            editor = new UriEditor(url, encoding);
            editor.AddQuery("a", "a");
            Assert.True(editor.Uri.ToString() == "http://www.webapiclient.com/path/?a=a");

            url    = new Uri("http://www.webapiclient.com/path?x=1");
            editor = new UriEditor(url, encoding);
            editor.AddQuery("a", "a");
            Assert.True(editor.Uri.ToString() == "http://www.webapiclient.com/path?x=1&a=a");


            url    = new Uri("http://www.webapiclient.com/path?x=1&");
            editor = new UriEditor(url, encoding);
            editor.AddQuery("a", "a");
            Assert.True(editor.Uri.ToString() == "http://www.webapiclient.com/path?x=1&a=a");


            url    = new Uri("http://www.webapiclient.com/path?x=1&");
            editor = new UriEditor(url, encoding);
            editor.AddQuery("a", "我");
            Assert.True(editor.Uri.ToString() == "http://www.webapiclient.com/path?x=1&a=我");


            url    = new Uri("http://www.webapiclient.com/path/?x=1&");
            editor = new UriEditor(url, encoding);
            editor.AddQuery("a", "我");
            Assert.True(editor.Uri.ToString() == "http://www.webapiclient.com/path/?x=1&a=我");


            url    = new Uri("http://www.webapiclient.com/path/?x={x}&");
            editor = new UriEditor(url, encoding);

            editor.AddQuery("a", "我");
            editor.Replace("x", "你");
            Assert.True(editor.Uri.ToString() == "http://www.webapiclient.com/path/?x=你&a=我");

            url    = new Uri("http://www.webapiclient.com");
            editor = new UriEditor(url, encoding);
            editor.AddQuery("a", "我");
            editor.AddQuery("b", "你");
            Assert.True(editor.Uri.ToString() == "http://www.webapiclient.com/?a=我&b=你");
        }