示例#1
0
        public void Action_ComplexRow(WebClient wc)
        {
            var initalRow = new TestComplexRow();

            initalRow.ID = 777;

            initalRow.Row1 = new TestRow()
            {
                ID = 101, Name = "Test Row 1", Date = DateTime.Now
            };
            initalRow.Row2 = new TestRow()
            {
                ID = 102, Name = "Test Row 2", Date = DateTime.Now
            };

            initalRow.ErrorRows = new TestRow[] {
                new TestRow()
                {
                    ID = 201, Name = "Err Row 1", Date = DateTime.Now
                },
                new TestRow()
                {
                    ID = 202, Name = "Err Row 2", Date = DateTime.Now
                },
                new TestRow()
                {
                    ID = 203, Name = "Err Row 3", Date = DateTime.Now
                }
            };

            var str = initalRow.ToJSON(JSONWritingOptions.CompactRowsAsMap);

            //using (var wc = CreateWebClient())
            {
                wc.Headers[HttpRequestHeader.ContentType] = NFX.Web.ContentType.JSON;
                var res = wc.UploadString(m_ServerURI + "ComplexRowSet", str);

                var map    = JSONReader.DeserializeDataObject(res) as JSONDataMap;
                var gotRow = JSONReader.ToRow <TestComplexRow>(map);

                if (initalRow.ID + 1 != gotRow.ID)
                {
                    throw new Exception();
                }
                if (initalRow.Row1.ID + 2 != gotRow.Row1.ID)
                {
                    throw new Exception();
                }
                if (gotRow.ErrorRows[2].Date - initalRow.ErrorRows[2].Date.AddDays(-2) >= TimeSpan.FromMilliseconds(1))
                {
                    throw new Exception();
                }
            }
        }
示例#2
0
        public void TypeRowConversion()
        {
            var r = new WaveTestSite.Controllers.IntegrationTester.SpanRow()
            {
                Span = TimeSpan.FromTicks(1)
            };

            var str = r.ToJSON(JSONWritingOptions.CompactRowsAsMap);

            var map    = JSONReader.DeserializeDataObject(str) as JSONDataMap;
            var gotRow = JSONReader.ToRow <WaveTestSite.Controllers.IntegrationTester.SpanRow>(map);
        }
示例#3
0
        public void ToDynamicRow_FromString()
        {
            var str = @"{name: ""Orlov"", dob: ""02/12/2007 6:45 PM"", certified: true, serviceyears: 12, salary: 145000}";

            var row = new DynamicRow(Schema.GetForTypedRow(typeof(MySimpleData)));

            JSONReader.ToRow(row, str.JSONToDataObject() as JSONDataMap);

            Assert.AreEqual("Orlov", row["Name"]);
            Assert.AreEqual(new DateTime(2007, 2, 12, 18, 45, 0), row["DOB"]);
            Assert.AreEqual(true, row["Certified"]);
            Assert.AreEqual(12, row["ServiceYears"]);
            Assert.AreEqual(145000m, row["Salary"]);
        }
示例#4
0
        public void Action_RowGet_TypeRow()
        {
            DateTime start = DateTime.Now;

            using (var wc = CreateWebClient())
            {
                string str = wc.DownloadString(INTEGRATION_HTTP_ADDR + "RowGet");


                Assert.AreEqual("application/json", wc.ResponseHeaders[HttpResponseHeader.ContentType]);

                var map    = JSONReader.DeserializeDataObject(str) as JSONDataMap;
                var gotRow = JSONReader.ToRow <TestRow>(map);
            }
        }
示例#5
0
        public void Action_ComplexRow()
        {
            var initalRow = new TestComplexRow();

            initalRow.ID = 777;

            initalRow.Row1 = new TestRow()
            {
                ID = 101, Name = "Test Row 1", Date = DateTime.Now
            };
            initalRow.Row2 = new TestRow()
            {
                ID = 102, Name = "Test Row 2", Date = DateTime.Now
            };

            initalRow.ErrorRows = new TestRow[] {
                new TestRow()
                {
                    ID = 201, Name = "Err Row 1", Date = DateTime.Now
                },
                new TestRow()
                {
                    ID = 202, Name = "Err Row 2", Date = DateTime.Now
                },
                new TestRow()
                {
                    ID = 203, Name = "Err Row 3", Date = DateTime.Now
                }
            };

            var str = initalRow.ToJSON(JSONWritingOptions.CompactRowsAsMap);

            Console.WriteLine(str);

            using (var wc = CreateWebClient())
            {
                wc.Headers[HttpRequestHeader.ContentType] = NFX.Web.ContentType.JSON;
                var res = wc.UploadString(INTEGRATION_HTTP_ADDR + "ComplexRowSet", str);

                var map    = JSONReader.DeserializeDataObject(res) as JSONDataMap;
                var gotRow = JSONReader.ToRow <TestComplexRow>(map);

                Assert.AreEqual(initalRow.ID + 1, gotRow.ID);
                Assert.AreEqual(initalRow.Row1.ID + 2, gotRow.Row1.ID);
                Assert.IsTrue(gotRow.ErrorRows[2].Date - initalRow.ErrorRows[2].Date.AddDays(-2) < TimeSpan.FromMilliseconds(1)); // dlat 20140617: date string format preservs 3 signs after decimal second instead of 7 digits preserved by .NET DateTime type
            }
        }
示例#6
0
        public void Action_RowGet_TypeRow(WebClient wc)
        {
            var start = DateTime.Now;

            //using (var wc = CreateWebClient())
            {
                string str = wc.DownloadString(m_ServerURI + "RowGet");

                if ("application/json" != wc.ResponseHeaders[HttpResponseHeader.ContentType])
                {
                    throw new Exception();
                }

                var map    = JSONReader.DeserializeDataObject(str) as JSONDataMap;
                var gotRow = JSONReader.ToRow <TestRow>(map);
            }
        }
示例#7
0
        public void ToAmorphousDynamicRow_FromString()
        {
            var str = @"{name: ""Orlov"", StringArray: [""a"", null, ""b""]}";

            var row = new RowWithArrays();

            JSONReader.ToRow(row, str.JSONToDataObject() as JSONDataMap);

            Assert.AreEqual("Orlov", row.Name);

            Assert.IsNotNull(row.StringArray);
            Assert.AreEqual(3, row.StringArray.Length);

            Assert.AreEqual("a", row.StringArray[0]);
            Assert.AreEqual(null, row.StringArray[1]);
            Assert.AreEqual("b", row.StringArray[2]);
        }
示例#8
0
        public void ToAmorphousDynamicRow_FromString()
        {
            var str = @"{name: ""Orlov"", dob: ""02/12/2007 6:45 PM"", certified: true, serviceyears: 12, salary: 145000, extra: -1, yes: true}";

            var row = new AmorphousDynamicRow(Schema.GetForTypedRow(typeof(MySimpleData)));

            JSONReader.ToRow(row, str.JSONToDataObject() as JSONDataMap);

            Aver.AreObjectsEqual("Orlov", row["Name"]);
            Aver.AreObjectsEqual(new DateTime(2007, 2, 12, 18, 45, 0), row["DOB"]);
            Aver.AreObjectsEqual(true, row["Certified"]);
            Aver.AreObjectsEqual(12, row["ServiceYears"]);
            Aver.AreObjectsEqual(145000m, row["Salary"]);
            Aver.AreObjectsEqual(2, row.AmorphousData.Count);
            Aver.AreObjectsEqual(-1, row.AmorphousData["extra"]);
            Aver.AreObjectsEqual(true, row.AmorphousData["yes"]);
        }
示例#9
0
        /// <summary>
        /// Fills method invocation param array with args doing some interpretation for widely used types like JSONDataMaps, Rows etc..
        /// </summary>
        protected virtual void BindParameters(Controller controller, string action, ActionAttribute attrAction, MethodInfo method, WorkContext work, out object[] args)
        {
            var mpars = method.GetParameters();

            args = new object[mpars.Length];

            if (mpars.Length == 0)
            {
                return;
            }

            var requested = work.WholeRequestAsJSONDataMap;

            var strictParamBinding = attrAction.StrictParamBinding;

            //check for complex type
            for (var i = 0; i < mpars.Length; i++)
            {
                var ctp = mpars[i].ParameterType;
                if (ctp == typeof(object) || ctp == typeof(JSONDataMap) || ctp == typeof(Dictionary <string, object>))
                {
                    args[i] = requested;
                    continue;
                }
                if (typeof(TypedRow).IsAssignableFrom(ctp))
                {
                    try
                    {
                        args[i] = JSONReader.ToRow(ctp, requested);
                        continue;
                    }
                    catch (Exception error)
                    {
                        throw new HTTPStatusException(WebConsts.STATUS_400,
                                                      WebConsts.STATUS_400_DESCRIPTION,
                                                      error.ToMessageWithType(),
                                                      error);
                    }
                }
            }

            for (var i = 0; i < args.Length; i++)
            {
                if (args[i] != null)
                {
                    continue;
                }

                var mp = mpars[i];

                var got = requested[mp.Name];

                if (got == null)
                {
                    if (mp.HasDefaultValue)
                    {
                        args[i] = mp.DefaultValue;
                    }
                    continue;
                }

                if (got is byte[])
                {
                    if (mp.ParameterType == typeof(byte[]))
                    {
                        args[i] = got;
                        continue;
                    }
                    if (mp.ParameterType == typeof(Stream) || mp.ParameterType == typeof(MemoryStream))
                    {
                        args[i] = new MemoryStream((byte[])got, false);
                        continue;
                    }
                    if (strictParamBinding)
                    {
                        throw new HTTPStatusException(Web.WebConsts.STATUS_400,
                                                      Web.WebConsts.STATUS_400_DESCRIPTION,
                                                      StringConsts.MVCCONTROLLER_ACTION_PARAM_BINDER_ERROR
                                                      .Args(
                                                          controller.GetType().DisplayNameWithExpandedGenericArgs(),
                                                          strictParamBinding ? "strict" : "relaxed",
                                                          action,
                                                          mp.Name,
                                                          mp.ParameterType.DisplayNameWithExpandedGenericArgs(), "byte[]"));
                    }
                }//got byte[]

                var strVal = got.AsString();
                try
                {
                    args[i] = strVal.AsType(mp.ParameterType, strictParamBinding);
                }
                catch
                {
                    const int MAX_LEN = 30;
                    if (strVal.Length > MAX_LEN)
                    {
                        strVal = strVal.Substring(0, MAX_LEN) + "...";
                    }
                    throw new HTTPStatusException(WebConsts.STATUS_400,
                                                  WebConsts.STATUS_400_DESCRIPTION,
                                                  StringConsts.MVCCONTROLLER_ACTION_PARAM_BINDER_ERROR
                                                  .Args(
                                                      controller.GetType().DisplayNameWithExpandedGenericArgs(),
                                                      strictParamBinding ? "strict" : "relaxed",
                                                      action,
                                                      mp.Name,
                                                      mp.ParameterType.DisplayNameWithExpandedGenericArgs(), strVal));
                }
            }
        }