Exemplo n.º 1
0
        public async Task PostTest()
        {
            using (var webserver = new WebServer(_defaultPort))
            {
                const string status = "OK";

                webserver.RegisterModule(new FallbackModule((ctx, ct) =>
                {
                    var obj = ctx.ParseJson <BasicJson>();
                    Assert.IsNotNull(obj);
                    obj.StringData = status;
                    ctx.JsonResponse(obj);

                    return(true);
                }));

                webserver.RunAsync();
                await Task.Delay(100);

                var data = await JsonClient.Post <BasicJson>(_defaultHttp, BasicJson.GetDefault());

                Assert.IsNotNull(data);
                Assert.AreEqual(status, data.StringData);
            }
        }
Exemplo n.º 2
0
        public void SerializeBasicObjectTest()
        {
            var data = BasicJson.GetDefault().ToJson(false);

            Assert.IsNotNull(data);
            Assert.AreEqual(BasicStr, data);
        }
Exemplo n.º 3
0
        public void CompareEqualArrayWithObjects()
        {
            var first  = new[] { BasicJson.GetDefault(), BasicJson.GetDefault() };
            var second = new[] { BasicJson.GetDefault(), BasicJson.GetDefault() };

            Assert.IsTrue(ObjectComparer.AreEnumsEqual(first, second));
        }
Exemplo n.º 4
0
        public void CompareDifferentObjectsTest()
        {
            var left  = BasicJson.GetDefault();
            var right = new BasicJson();

            Assert.IsFalse(ObjectComparer.AreObjectsEqual(left, right));
        }
Exemplo n.º 5
0
        public void EqualArraysWithObjects_ReturnsTrue()
        {
            var leftArrayObject  = new[] { BasicJson.GetDefault(), BasicJson.GetDefault() };
            var rightArrayObject = new[] { BasicJson.GetDefault(), BasicJson.GetDefault() };

            Assert.IsTrue(ObjectComparer.AreEnumerationsEquals(leftArrayObject, rightArrayObject));
        }
Exemplo n.º 6
0
        public void WithJsonAsParam_ReturnsStringifiedJson()
        {
            var objectInfoLines = BasicJson.GetDefault().Stringify().ToLines();

            Assert.GreaterOrEqual(8, objectInfoLines.Length);
            Assert.AreEqual("$type           : Swan.Test.Mocks.BasicJson", objectInfoLines[0]);
            Assert.AreEqual("    string,", objectInfoLines[2]);
        }
Exemplo n.º 7
0
        public void WithInvalidParams_ThrowsJsonRequestException()
        {
            var exception = Assert.ThrowsAsync <JsonRequestException>(async() =>
            {
                await JsonClient.Post <BasicJson>(new Uri($"{DefaultHttp}/404"), BasicJson.GetDefault());
            });

            Assert.AreEqual(404, exception.HttpErrorCode);
        }
Exemplo n.º 8
0
        public void CheckJsonFormat()
        {
            Assert.AreEqual(BasicStr, BasicJson.GetDefault().ToJson(false));
            Assert.AreNotEqual(BasicStr, BasicJson.GetDefault().ToJson());

            object nullObj = null;

            Assert.AreEqual(string.Empty, nullObj.ToJson());
        }
Exemplo n.º 9
0
        public async Task WithValidParams_ReturnsTrue()
        {
            const string status    = "OK";
            var          basicJson = BasicJson.GetDefault();

            var data = await JsonClient.Post <BasicJson>(new Uri($"{DefaultHttp}{Api}/WithValidParams"), basicJson);

            Assert.IsNotNull(data);
            Assert.AreEqual(status, data.StringData);
        }
Exemplo n.º 10
0
        public void StringifyObjectTest()
        {
            var objectInfoLines = BasicJson.GetDefault().Stringify().ToLines();

            Assert.Greater(7, objectInfoLines.Length);

            Assert.AreEqual("$type           : Unosquare.Swan.Test.Mocks.BasicJson", objectInfoLines[0]);
            Assert.AreEqual("StringData      : string", objectInfoLines[1]);
            Assert.AreEqual("IntData         : 1", objectInfoLines[2]);
        }
Exemplo n.º 11
0
        public void WithValidBasicJson_CopyOnlyPropertiesToNewBasicJson()
        {
            var source      = BasicJson.GetDefault();
            var destination = source.CopyOnlyPropertiesToNew <BasicJson>(nameof(BasicJson.BoolData), nameof(BasicJson.DecimalData));

            Assert.IsNotNull(destination);
            Assert.AreSame(source.GetType(), destination.GetType());

            Assert.AreEqual(source.BoolData, destination.BoolData);
            Assert.AreEqual(source.DecimalData, destination.DecimalData);
        }
Exemplo n.º 12
0
        public void WithValidBasicJson_CopyNotIgnoredPropertiesToTarget()
        {
            var source      = BasicJson.GetDefault();
            var destination = new BasicJson();

            source.CopyPropertiesTo(destination, nameof(BasicJson.NegativeInt), nameof(BasicJson.BoolData));

            Assert.AreNotEqual(source.BoolData, destination.BoolData);
            Assert.AreNotEqual(source.NegativeInt, destination.NegativeInt);
            Assert.AreEqual(source.StringData, destination.StringData);
        }
Exemplo n.º 13
0
        public void ArrayOfObjects_ReturnsArrayOfObjectsSerialized()
        {
            var data = Json.Serialize(new List <BasicJson>
            {
                BasicJson.GetDefault(),
                BasicJson.GetDefault()
            });

            Assert.IsNotNull(data);
            Assert.AreEqual("[" + BasicStr + "," + BasicStr + "]", data);
        }
Exemplo n.º 14
0
        public async Task WithValidParamsAndAuthorizationToken_ReturnsTrue()
        {
            var data = await JsonClient.Post(
                new Uri($"{DefaultHttp}{Api}/WithValidParamsAndAuthorizationToken"),
                BasicJson.GetDefault(),
                AuthorizationToken);

            Assert.IsNotNull(data);
            Assert.IsTrue(data !.ContainsKey(Authorization));
            Assert.AreEqual($"Bearer {AuthorizationToken}", data[Authorization]);
        }
Exemplo n.º 15
0
        public void CompareEqualObjectsWithArrayProperty()
        {
            var first = new AdvArrayJson {
                Id = 1, Properties = new[] { BasicJson.GetDefault() }
            };
            var second = new AdvArrayJson {
                Id = 1, Properties = new[] { BasicJson.GetDefault() }
            };

            Assert.IsTrue(ObjectComparer.AreEqual(first, second));
        }
Exemplo n.º 16
0
        public void WithArrayOfObjects_ReturnsArrayOfObjectsSerialized()
        {
            var data = Json.Serialize(new List <BasicJson>
            {
                BasicJson.GetDefault(),
                BasicJson.GetDefault(),
            });

            Assert.IsNotNull(data);
            Assert.AreEqual($"[{BasicStr},{BasicStr}]", data);
        }
Exemplo n.º 17
0
        public void EqualObjectsWithArrayProperties_ReturnsTrue()
        {
            var leftObject = new AdvArrayJson {
                Id = 1, Properties = new[] { BasicJson.GetDefault() }
            };
            var rightObject = new AdvArrayJson {
                Id = 1, Properties = new[] { BasicJson.GetDefault() }
            };

            Assert.IsTrue(ObjectComparer.AreEqual(leftObject, rightObject));
        }
Exemplo n.º 18
0
        public void WithStringsArrayAndWeakReference_ReturnsArraySerialized()
        {
            var instance  = BasicJson.GetDefault();
            var reference = new List <WeakReference> {
                new(instance)
            };

            var data = Json.Serialize(instance, false, null, false, null, null, reference, JsonSerializerCase.None);

            Assert.IsTrue(data.StartsWith("{ \"$circref\":"));
        }
Exemplo n.º 19
0
        public void WithBasicObject_ReturnsObjectDeserialized()
        {
            var obj = Json.Deserialize <BasicJson>(BasicStr);

            Assert.IsNotNull(obj);
            Assert.AreEqual(obj.StringData, BasicJson.GetDefault().StringData);
            Assert.AreEqual(obj.IntData, BasicJson.GetDefault().IntData);
            Assert.AreEqual(obj.NegativeInt, BasicJson.GetDefault().NegativeInt);
            Assert.AreEqual(obj.BoolData, BasicJson.GetDefault().BoolData);
            Assert.AreEqual(obj.DecimalData, BasicJson.GetDefault().DecimalData);
            Assert.AreEqual(obj.StringNull, BasicJson.GetDefault().StringNull);
        }
Exemplo n.º 20
0
        public void WithValidBasicJson_CopyPropertiesToTarget()
        {
            var source      = BasicJson.GetDefault();
            var destination = new BasicJson();

            source.CopyPropertiesTo(destination);

            Assert.AreEqual(source.BoolData, destination.BoolData);
            Assert.AreEqual(source.DecimalData, destination.DecimalData);
            Assert.AreEqual(source.StringData, destination.StringData);
            Assert.AreEqual(source.StringNull, destination.StringNull);
        }
Exemplo n.º 21
0
        public void CopyPropertiesToNewTest()
        {
            var source      = BasicJson.GetDefault();
            var destination = source.CopyPropertiesToNew <BasicJson>();

            Assert.IsNotNull(destination);
            Assert.AreSame(source.GetType(), destination.GetType());

            Assert.AreEqual(source.BoolData, destination.BoolData);
            Assert.AreEqual(source.DecimalData, destination.DecimalData);
            Assert.AreEqual(source.StringData, destination.StringData);
            Assert.AreEqual(source.StringNull, destination.StringNull);
        }
Exemplo n.º 22
0
        public void WithValidBasicJson_CopyOnlyPropertiesToTarget()
        {
            var source      = BasicJson.GetDefault();
            var destination = new BasicJson {
                NegativeInt = 800, BoolData = false
            };

            source.CopyOnlyPropertiesTo(destination, new[] { nameof(BasicJson.NegativeInt), nameof(BasicJson.BoolData) });

            Assert.AreEqual(source.BoolData, destination.BoolData);
            Assert.AreEqual(source.NegativeInt, destination.NegativeInt);
            Assert.AreNotEqual(source.StringData, destination.StringData);
        }
Exemplo n.º 23
0
        public void WithObject_ReturnsObjectSerializedExcludingProps()
        {
            var excludeNames = new[]
            {
                nameof(BasicJson.StringData),
                nameof(BasicJson.IntData),
                nameof(BasicJson.NegativeInt),
            };

            var dataSerialized = Json.SerializeExcluding(BasicJson.GetDefault(), false, excludeNames);

            Assert.AreEqual(
                "{\"DecimalData\": 10.33,\"BoolData\": true,\"StringNull\": null}",
                dataSerialized);
        }
Exemplo n.º 24
0
        public void WithObject_ReturnsObjectSerialized()
        {
            var includeNames = new[]
            {
                nameof(BasicJson.StringData),
                nameof(BasicJson.IntData),
                nameof(BasicJson.NegativeInt),
            };

            var dataSerialized = Json.SerializeOnly(BasicJson.GetDefault(), false, includeNames);

            Assert.AreEqual(
                "{\"StringData\": \"string,\\r\\ndata\\\\\",\"IntData\": 1,\"NegativeInt\": -1}",
                dataSerialized);
        }
Exemplo n.º 25
0
        public void WithValidParamsAndNewProperty_CopyPropertiesToTarget()
        {
            var source = BasicJson.GetDefault();

            source.StringNull = "1";

            var destination = new BasicJsonWithNewProperty();

            source.CopyPropertiesTo(destination);

            Assert.AreEqual(source.BoolData, destination.BoolData);
            Assert.AreEqual(source.DecimalData, destination.DecimalData);
            Assert.AreEqual(source.StringData, destination.StringData);
            Assert.AreEqual(source.StringNull, destination.StringNull.ToString());
        }
Exemplo n.º 26
0
        public void JsonRequestErrorTest()
        {
            var exception = Assert.ThrowsAsync <JsonRequestException>(async() =>
            {
                using (var webserver = new WebServer(_defaultPort))
                {
                    webserver.RegisterModule(new FallbackModule((ctx, ct) => false));

                    webserver.RunAsync();
                    await Task.Delay(100);

                    await JsonClient.Post <BasicJson>(_defaultHttp, BasicJson.GetDefault());
                }
            });

            Assert.AreEqual(404, exception.HttpErrorCode, "EmebedIO should return 404 error code");
        }
Exemplo n.º 27
0
        public async Task PostWithAuthenticationTest()
        {
            using (var webserver = new WebServer(_defaultPort))
            {
                webserver.RegisterModule(new FallbackModule((ctx, ct) =>
                {
                    ctx.JsonResponse(new Dictionary <string, string> {
                        { Authorization, ctx.RequestHeader(Authorization) }
                    });

                    return(true);
                }));

                webserver.RunAsync();
                await Task.Delay(500);

                var data = await JsonClient.Post(_defaultHttp, BasicJson.GetDefault(), AuthorizationToken);

                Assert.IsNotNull(data);
                Assert.IsTrue(data.ContainsKey(Authorization));
                Assert.AreEqual($"Bearer {AuthorizationToken}", data[Authorization]);
            }
        }
Exemplo n.º 28
0
        public void EqualsObjects_ReturnsTrue()
        {
            var left = new BasicJson
            {
                BoolData    = true,
                DecimalData = 1,
                IntData     = 1,
                NegativeInt = -1,
                StringData  = "A",
                StringNull  = null,
            };

            var right = new BasicJson
            {
                BoolData    = true,
                DecimalData = 1,
                IntData     = 1,
                NegativeInt = -1,
                StringData  = "A",
                StringNull  = null,
            };

            Assert.IsTrue(ObjectComparer.AreObjectsEqual(left, right));
        }
Exemplo n.º 29
0
 public void CheckJsonFormat_ValidatesIfObjectsAreNotEqual()
 {
     Assert.AreNotEqual(BasicStr, BasicJson.GetDefault().ToJson());
 }
Exemplo n.º 30
0
 public void WithNullUrl_ThrowsArgumentNullException()
 {
     Assert.ThrowsAsync <ArgumentNullException>(async() =>
                                                await JsonClient.Post <BasicJson>(null, BasicJson.GetDefault()));
 }