Exemple #1
0
        public virtual void Deserialize(string content)
        {
            // remove heading and trailing white spaces
            content = content.Trim();

            // make sure that the parameter starts with the keyword followed by '='
            if (!content.StartsWith($"{ ParameterKeyword }="))
            {
                throw new ArgumentException($"Invalid { ParameterKeyword } content detected! Property parameter needs to start with { ParameterKeyword } keyword!");
            }

            // extract the uri part from content
            string uriPart = content.Substring(content.IndexOf('=') + 1).Trim();

            // make sure that the uri is surrounded by double quotes
            if (uriPart.First() != '"' || uriPart.Last() != '"')
            {
                throw new ArgumentException($"Invalid uri ({ uriPart }) detected! Uri needs to be surrounded with double quotes!");
            }

            // create a new uri value instance from it
            string uriContent = uriPart.Substring(1, uriPart.Length - 2);

            Uri = ObjectSerializer.Deserialize <UriValue>(uriContent);
        }
Exemple #2
0
        public void ToUriTest()
        {
            var uriValue = new UriValue("http://www.webapiclient.com");
            var uri      = uriValue.ToUri();

            Assert.Equal(new Uri("http://www.webapiclient.com"), uri);

            uriValue = new UriValue(new Uri("http://www.webapiclient.com"));
            uri      = uriValue.ToUri();
            Assert.Equal(new Uri("http://www.webapiclient.com"), uri);
        }
        /// <summary>
        /// 创建新的uri
        /// </summary>
        /// <param name="uri">原始uri</param>
        /// <param name="keyValues">键值对</param>
        /// <returns></returns>
        protected virtual Uri CreateUri(Uri uri, IEnumerable <KeyValue> keyValues)
        {
            var uriValue = new UriValue(uri);

            foreach (var keyValue in keyValues)
            {
                uriValue = uriValue.Replace(keyValue.Key, keyValue.Value, out var replaced);
                if (replaced == false)
                {
                    uriValue = uriValue.AddQuery(keyValue.Key, keyValue.Value);
                }
            }
            return(uriValue.ToUri());
        }
Exemple #4
0
        public void ReplaceTest()
        {
            var uriValue = new UriValue("http://www.webapiclient.com");

            uriValue.Replace("a", "a", out var replaced);
            Assert.False(replaced);

            uriValue = new UriValue("http://www.webapiclient.com/path/?x={x}&y={Y}");
            uriValue = uriValue.Replace("x", "x", out replaced);
            Assert.True(replaced);
            Assert.Equal("http://www.webapiclient.com/path/?x=x&y={Y}", uriValue.ToString());

            uriValue = new UriValue("http://www.webapiclient.com/path/?x={x}&y={Y}");
            uriValue = uriValue.Replace("y", "y", out replaced);
            Assert.True(replaced);
            Assert.Equal("http://www.webapiclient.com/path/?x={x}&y=y", uriValue.ToString());
        }
Exemple #5
0
        public void ReadRow_AutoMappedUri_ReturnsExpected()
        {
            using (var importer = Helpers.GetImporter("Uris.xlsx"))
            {
                ExcelSheet sheet = importer.ReadSheet();
                sheet.ReadHeading();

                UriValue row1 = sheet.ReadRow <UriValue>();
                Assert.Equal(new Uri("http://google.com"), row1.Uri);

                // Defaults to null if empty.
                UriValue row2 = sheet.ReadRow <UriValue>();
                Assert.Null(row2.Uri);

                // Defaults to throw if invalid.
                Assert.Throws <ExcelMappingException>(() => sheet.ReadRow <UriValue>());
            }
        }
Exemple #6
0
        public void ReadRow_UriWithCustomFallback_ReturnsExpected()
        {
            using (var importer = Helpers.GetImporter("Uris.xlsx"))
            {
                importer.Configuration.RegisterClassMap <UriValueFallbackMap>();

                ExcelSheet sheet = importer.ReadSheet();
                sheet.ReadHeading();

                UriValue row1 = sheet.ReadRow <UriValue>();
                Assert.Equal(new Uri("http://google.com"), row1.Uri);

                UriValue row2 = sheet.ReadRow <UriValue>();
                Assert.Equal(new Uri("http://empty.com"), row2.Uri);

                UriValue row3 = sheet.ReadRow <UriValue>();
                Assert.Equal(new Uri("http://invalid.com"), row3.Uri);
            }
        }
Exemple #7
0
        public void AddQueryTest()
        {
            var uriValue = new UriValue("http://www.webapiclient.com");

            uriValue = uriValue.AddQuery("a", "a");
            Assert.Equal("http://www.webapiclient.com/?a=a", uriValue.ToString());

            uriValue = new UriValue("http://www.webapiclient.com/path");
            uriValue = uriValue.AddQuery("a", "a");
            Assert.Equal("http://www.webapiclient.com/path?a=a", uriValue.ToString());

            uriValue = new UriValue("http://www.webapiclient.com/path/");
            uriValue = uriValue.AddQuery("a", "a");
            Assert.Equal("http://www.webapiclient.com/path/?a=a", uriValue.ToString());


            uriValue = new UriValue("http://www.webapiclient.com/path/?");
            uriValue = uriValue.AddQuery("a", "a");
            Assert.Equal("http://www.webapiclient.com/path/?a=a", uriValue.ToString());

            uriValue = new UriValue("http://www.webapiclient.com/path?x=1");
            uriValue = uriValue.AddQuery("a", "a");
            Assert.Equal("http://www.webapiclient.com/path?x=1&a=a", uriValue.ToString());


            uriValue = new UriValue("http://www.webapiclient.com/path?x=1&");
            uriValue = uriValue.AddQuery("a", "a");
            Assert.Equal("http://www.webapiclient.com/path?x=1&a=a", uriValue.ToString());

            uriValue = new UriValue("http://www.webapiclient.com/path?x=1&");
            uriValue = uriValue.AddQuery("a", "我");
            Assert.Equal($"http://www.webapiclient.com/path?x=1&a={Uri.EscapeDataString("我")}", uriValue.ToString());


            uriValue = new UriValue("http://www.webapiclient.com/path/?x=1&");
            uriValue = uriValue.AddQuery("a", "我");
            Assert.True(uriValue.ToString() == $"http://www.webapiclient.com/path/?x=1&a={Uri.EscapeDataString("我")}");
        }
Exemple #8
0
 public int CompareTo(Udi?other) => string.Compare(UriValue.ToString(), other?.UriValue.ToString(), StringComparison.OrdinalIgnoreCase);
Exemple #9
0
 public override int GetHashCode() => UriValue.GetHashCode();
Exemple #10
0
 public int CompareTo(Udi other)
 {
     return(string.Compare(UriValue.ToString(), other.UriValue.ToString(), StringComparison.InvariantCultureIgnoreCase));
 }
Exemple #11
0
 public override int GetHashCode()
 {
     return(UriValue.GetHashCode());
 }
Exemple #12
0
 public int CompareTo(Udi other)
 {
     return(string.Compare(UriValue.ToString(), other.UriValue.ToString(), StringComparison.OrdinalIgnoreCase));
 }
        public override bool Equals(object obj)
        {
            if (!(obj is ModelWithAllTypes to))
            {
                return(false);
            }

            if (ByteValue != to.ByteValue)
            {
                return(false);
            }
            if (CharValue != to.CharValue)
            {
                return(false);
            }
            if (CustomException.Message != to.CustomException.Message)
            {
                return(false);
            }
            if (DateTimeValue != to.DateTimeValue)
            {
                return(false);
            }
            if (DecimalValue != to.DecimalValue)
            {
                return(false);
            }
            if (DoubleValue != to.DoubleValue)
            {
                return(false);
            }
            if (Exception.Message != to.Exception.Message)
            {
                return(false);
            }
            if (FloatValue != to.FloatValue)
            {
                return(false);
            }
            if (IntValue != to.IntValue)
            {
                return(false);
            }
            if (LongValue != to.LongValue)
            {
                return(false);
            }
            if (SByteValue != to.SByteValue)
            {
                return(false);
            }
            if (ShortValue != to.ShortValue)
            {
                return(false);
            }
            if (TimeSpanValue != to.TimeSpanValue)
            {
                return(false);
            }
            if (TypeValue != to.TypeValue)
            {
                return(false);
            }
            if (UIntValue != to.UIntValue)
            {
                return(false);
            }
            if (ULongValue != to.ULongValue)
            {
                return(false);
            }
            if (UriValue.ToString() != to.UriValue.ToString())
            {
                return(false);
            }
            if (UShortValue != to.UShortValue)
            {
                return(false);
            }
            if (GuidValue != to.GuidValue)
            {
                return(false);
            }

            return(true);
        }
Exemple #14
0
 public UriBasedPropertyParameter(Uri uri)
 {
     Uri = new UriValue(uri);
 }