Ejemplo n.º 1
0
        private async Task CreateUserAgentValidationTest()
        {
            var handler = new HandlerToCaptureHttpTraffic();
            MobileServiceClient client = new MobileServiceClient(MobileServiceRuntimeUrl, handler);
            var table = client.GetTable <RoundTripTableItem>();
            var item  = new RoundTripTableItem {
                Name = "hello"
            };
            await table.InsertAsync(item);

            Action <string> dumpAndValidateHeaders = delegate(string operation)
            {
                if (!handler.RequestHeaders.TryGetValue("User-Agent", out string userAgent))
                {
                    throw new InvalidOperationException("This will fail the test");
                }
                else
                {
                    Regex expected = new Regex(@"^ZUMO\/\d.\d");
                    if (!expected.IsMatch(userAgent))
                    {
                        throw new InvalidOperationException("This will fail the test");
                    }
                }
            };

            dumpAndValidateHeaders("Insert");

            item.Number = 123;
            await table.UpdateAsync(item);

            dumpAndValidateHeaders("Update");

            var item2 = await table.LookupAsync(item.Id);

            dumpAndValidateHeaders("Read");

            await table.DeleteAsync(item);

            dumpAndValidateHeaders("Delete");
        }
        private async Task CreateUserAgentValidationTest()
        {
            Log("Validation User-Agent header");

            var handler = new HandlerToCaptureHttpTraffic();
            MobileServiceClient client = new MobileServiceClient(
                this.GetTestSetting("MobileServiceRuntimeUrl"),
                handler);
            var table = client.GetTable<RoundTripTableItem>();
            var item = new RoundTripTableItem { Name = "hello" };
            await table.InsertAsync(item);
            Action<string> dumpAndValidateHeaders = delegate(string operation)
            {
                Log("Headers for {0}:", operation);
                Log("  Request:");
                foreach (var header in handler.RequestHeaders.Keys)
                {
                    Log("    {0}: {1}", header, handler.RequestHeaders[header]);
                }

                Log("  Response:");
                foreach (var header in handler.ResponseHeaders.Keys)
                {
                    Log("    {0}: {1}", header, handler.ResponseHeaders[header]);
                }

                string userAgent;
                if (!handler.RequestHeaders.TryGetValue("User-Agent", out userAgent))
                {
                    Log("No user-agent header in the request");
                    throw new InvalidOperationException("This will fail the test");
                }
                else
                {
                    Regex expected = new Regex(@"^ZUMO\/\d.\d");
                    if (expected.IsMatch(userAgent))
                    {
                        Log("User-Agent validated correclty");
                    }
                    else
                    {
                        Log("User-Agent didn't validate properly.");
                        throw new InvalidOperationException("This will fail the test");
                    }
                }
            };

            dumpAndValidateHeaders("Insert");

            item.Number = 123;
            await table.UpdateAsync(item);
            dumpAndValidateHeaders("Update");

            var item2 = await table.LookupAsync(item.Id);
            dumpAndValidateHeaders("Read");

            await table.DeleteAsync(item);
            dumpAndValidateHeaders("Delete");
        }
        private async Task CreateParameterPassingTest(bool useTypedTable)
        {
            Log("### Parameter passing test - " + (useTypedTable ? "typed" : "untyped") + " tables");

            var client = this.GetClient();
            var typed = client.GetTable<ParamsTestTableItem>();
            var untyped = client.GetTable("ParamsTestTable");
            var dict = new Dictionary<string, string>
                {
                    { "item", "simple" },
                    { "empty", "" },
                    { "spaces", "with spaces" },
                    { "specialChars", "`!@#$%^&*()-=[]\\;',./~_+{}|:\"<>?" },
                    { "latin", "ãéìôü ÇñÑ" },
                    { "arabic", "الكتاب على الطاولة" },
                    { "chinese", "这本书在桌子上" },
                    { "japanese", "本は机の上に" },
                    { "hebrew", "הספר הוא על השולחן" },
                    { "name+with special&chars", "should just work" }
                };

            var expectedParameters = new JObject();
            foreach (var key in dict.Keys)
            {
                expectedParameters.Add(key, dict[key]);
            }

            bool testPassed = true;

            ParamsTestTableItem typedItem = new ParamsTestTableItem();
            var untypedItem = new JObject();
            JObject actualParameters;

            dict["operation"] = "insert";
            expectedParameters.Add("operation", "insert");
            if (useTypedTable)
            {
                await typed.InsertAsync(typedItem, dict);
                actualParameters = JObject.Parse(typedItem.parameters);
            }
            else
            {
                var inserted = await untyped.InsertAsync(untypedItem, dict);
                untypedItem = inserted as JObject;
                actualParameters = JObject.Parse(untypedItem["parameters"].Value<string>());
            }

            testPassed = testPassed && ValidateParameters("insert", expectedParameters, actualParameters);

            dict["operation"] = "update";
            expectedParameters["operation"] = "update";
            if (useTypedTable)
            {
                await typed.UpdateAsync(typedItem, dict);
                actualParameters = JObject.Parse(typedItem.parameters);
            }
            else
            {
                var updated = await untyped.UpdateAsync(untypedItem, dict);
                actualParameters = JObject.Parse(updated["parameters"].Value<string>());
            }

            testPassed = testPassed && ValidateParameters("update", expectedParameters, actualParameters);

            dict["operation"] = "lookup";
            expectedParameters["operation"] = "lookup";
            if (useTypedTable)
            {
                var temp = await typed.LookupAsync(1, dict);
                actualParameters = JObject.Parse(temp.parameters);
            }
            else
            {
                var temp = await untyped.LookupAsync(1, dict);
                actualParameters = JObject.Parse(temp["parameters"].Value<string>());
            }

            testPassed = testPassed && ValidateParameters("lookup", expectedParameters, actualParameters);

            dict["operation"] = "read";
            expectedParameters["operation"] = "read";
            if (useTypedTable)
            {
                var temp = await typed.Where(t => t.Id >= 1).WithParameters(dict).ToListAsync();
                actualParameters = JObject.Parse(temp[0].parameters);
            }
            else
            {
                var temp = await untyped.ReadAsync("$filter=id ge 1", dict);
                actualParameters = JObject.Parse(temp[0]["parameters"].Value<string>());
            }

            testPassed = testPassed && ValidateParameters("read", expectedParameters, actualParameters);

            if (useTypedTable)
            {
                // Refresh operation only exists for typed tables
                dict["operation"] = "read";
                expectedParameters["operation"] = "read";
                typedItem.Id = 1;
                typedItem.parameters = "";
                await typed.RefreshAsync(typedItem, dict);
                actualParameters = JObject.Parse(typedItem.parameters);
                testPassed = testPassed && ValidateParameters("refresh", expectedParameters, actualParameters);
            }

            // Delete operation doesn't populate the object with the response, so we'll use a filter to capture that
            var handler = new HandlerToCaptureHttpTraffic();
            var filteredClient = new MobileServiceClient(client.MobileAppUri, handler);
            typed = filteredClient.GetTable<ParamsTestTableItem>();
            untyped = filteredClient.GetTable("ParamsTestTable");

            dict["operation"] = "delete";
            expectedParameters["operation"] = "delete";
            if (useTypedTable)
            {
                await typed.DeleteAsync(typedItem, dict);
            }
            else
            {
                await untyped.DeleteAsync(untypedItem, dict);
            }

            JObject response = JObject.Parse(handler.ResponseBody);
            actualParameters = JObject.Parse(response["parameters"].Value<string>());

            testPassed = testPassed && ValidateParameters("delete", expectedParameters, actualParameters);

            if (!testPassed)
            {
                Assert.Fail("");
            }
        }
        private static ZumoTest CreateUserAgentValidationTest()
        {
            return new ZumoTest("Validation User-Agent header", async delegate(ZumoTest test)
            {
                var handler = new HandlerToCaptureHttpTraffic();
                MobileServiceClient client = new MobileServiceClient(
                    ZumoTestGlobals.Instance.Client.ApplicationUri,
                    ZumoTestGlobals.Instance.Client.ApplicationKey,
                    handler);
                var table = client.GetTable<RoundTripTableItem>();
                var item = new RoundTripTableItem { String1 = "hello" };
                await table.InsertAsync(item);
                Action<string> dumpAndValidateHeaders = delegate(string operation)
                {
                    test.AddLog("Headers for {0}:", operation);
                    test.AddLog("  Request:");
                    foreach (var header in handler.RequestHeaders.Keys)
                    {
                        test.AddLog("    {0}: {1}", header, handler.RequestHeaders[header]);
                    }

                    test.AddLog("  Response:");
                    foreach (var header in handler.ResponseHeaders.Keys)
                    {
                        test.AddLog("    {0}: {1}", header, handler.ResponseHeaders[header]);
                    }

                    string userAgent;
                    if (!handler.RequestHeaders.TryGetValue("User-Agent", out userAgent))
                    {
                        test.AddLog("No user-agent header in the request");
                        throw new InvalidOperationException("This will fail the test");
                    }
                    else
                    {
                        Regex expected = new Regex(@"^ZUMO\/\d.\d");
                        if (expected.IsMatch(userAgent))
                        {
                            test.AddLog("User-Agent validated correclty");
                        }
                        else
                        {
                            test.AddLog("User-Agent didn't validate properly.");
                            throw new InvalidOperationException("This will fail the test");
                        }
                    }
                };

                dumpAndValidateHeaders("Insert");

                item.Double1 = 123;
                await table.UpdateAsync(item);
                dumpAndValidateHeaders("Update");

                var item2 = await table.LookupAsync(item.Id);
                dumpAndValidateHeaders("Read");

                await table.DeleteAsync(item);
                dumpAndValidateHeaders("Delete");

                return true;
            });
        }
Ejemplo n.º 5
0
        private static ZumoTest CreateUserAgentValidationTest()
        {
            return(new ZumoTest("Validation User-Agent header", async delegate(ZumoTest test)
            {
                var handler = new HandlerToCaptureHttpTraffic();
                MobileServiceClient client = new MobileServiceClient(
                    ZumoTestGlobals.Instance.Client.ApplicationUri,
                    ZumoTestGlobals.Instance.Client.ApplicationKey,
                    handler);
                var table = client.GetTable <RoundTripTableItem>();
                var item = new RoundTripTableItem {
                    String1 = "hello"
                };
                await table.InsertAsync(item);
                Action <string> dumpAndValidateHeaders = delegate(string operation)
                {
                    test.AddLog("Headers for {0}:", operation);
                    test.AddLog("  Request:");
                    foreach (var header in handler.RequestHeaders.Keys)
                    {
                        test.AddLog("    {0}: {1}", header, handler.RequestHeaders[header]);
                    }

                    test.AddLog("  Response:");
                    foreach (var header in handler.ResponseHeaders.Keys)
                    {
                        test.AddLog("    {0}: {1}", header, handler.ResponseHeaders[header]);
                    }

                    string userAgent;
                    if (!handler.RequestHeaders.TryGetValue("User-Agent", out userAgent))
                    {
                        test.AddLog("No user-agent header in the request");
                        throw new InvalidOperationException("This will fail the test");
                    }
                    else
                    {
                        Regex expected = new Regex(@"^ZUMO\/\d.\d");
                        if (expected.IsMatch(userAgent))
                        {
                            test.AddLog("User-Agent validated correclty");
                        }
                        else
                        {
                            test.AddLog("User-Agent didn't validate properly.");
                            throw new InvalidOperationException("This will fail the test");
                        }
                    }
                };

                dumpAndValidateHeaders("Insert");

                item.Double1 = 123;
                await table.UpdateAsync(item);
                dumpAndValidateHeaders("Update");

                var item2 = await table.LookupAsync(item.Id);
                dumpAndValidateHeaders("Read");

                await table.DeleteAsync(item);
                dumpAndValidateHeaders("Delete");

                return true;
            }));
        }
Ejemplo n.º 6
0
        private static ZumoTest CreateParameterPassingTest(bool useTypedTable)
        {
            return(new ZumoTest("Parameter passing test - " + (useTypedTable ? "typed" : "untyped") + " tables", async delegate(ZumoTest test)
            {
                var client = ZumoTestGlobals.Instance.Client;
                var typed = client.GetTable <ParamsTestTableItem>();
                var untyped = client.GetTable(ZumoTestGlobals.ParamsTestTableName);
                var dict = new Dictionary <string, string>
                {
                    { "item", "simple" },
                    { "empty", "" },
                    { "spaces", "with spaces" },
                    { "specialChars", "`!@#$%^&*()-=[]\\;',./~_+{}|:\"<>?" },
                    { "latin", "ãéìôü ÇñÑ" },
                    { "arabic", "الكتاب على الطاولة" },
                    { "chinese", "这本书在桌子上" },
                    { "japanese", "本は机の上に" },
                    { "hebrew", "הספר הוא על השולחן" },
                    { "name+with special&chars", "should just work" }
                };

                var expectedParameters = new JObject();
                foreach (var key in dict.Keys)
                {
                    expectedParameters.Add(key, dict[key]);
                }

                bool testPassed = true;

                ParamsTestTableItem typedItem = new ParamsTestTableItem();
                var untypedItem = new JObject();
                JObject actualParameters;

                dict["operation"] = "insert";
                expectedParameters.Add("operation", "insert");
                if (useTypedTable)
                {
                    await typed.InsertAsync(typedItem, dict);
                    actualParameters = JObject.Parse(typedItem.parameters);
                }
                else
                {
                    var inserted = await untyped.InsertAsync(untypedItem, dict);
                    untypedItem = inserted as JObject;
                    actualParameters = JObject.Parse(untypedItem["parameters"].Value <string>());
                }

                testPassed = testPassed && ValidateParameters(test, "insert", expectedParameters, actualParameters);

                dict["operation"] = "update";
                expectedParameters["operation"] = "update";
                if (useTypedTable)
                {
                    await typed.UpdateAsync(typedItem, dict);
                    actualParameters = JObject.Parse(typedItem.parameters);
                }
                else
                {
                    var updated = await untyped.UpdateAsync(untypedItem, dict);
                    actualParameters = JObject.Parse(updated["parameters"].Value <string>());
                }

                testPassed = testPassed && ValidateParameters(test, "update", expectedParameters, actualParameters);

                dict["operation"] = "lookup";
                expectedParameters["operation"] = "lookup";
                if (useTypedTable)
                {
                    var temp = await typed.LookupAsync(1, dict);
                    actualParameters = JObject.Parse(temp.parameters);
                }
                else
                {
                    var temp = await untyped.LookupAsync(1, dict);
                    actualParameters = JObject.Parse(temp["parameters"].Value <string>());
                }

                testPassed = testPassed && ValidateParameters(test, "lookup", expectedParameters, actualParameters);

                dict["operation"] = "read";
                expectedParameters["operation"] = "read";
                if (useTypedTable)
                {
                    var temp = await typed.Where(t => t.Id >= 1).WithParameters(dict).ToListAsync();
                    actualParameters = JObject.Parse(temp[0].parameters);
                }
                else
                {
                    var temp = await untyped.ReadAsync("$filter=id gt 1", dict);
                    actualParameters = JObject.Parse(temp[0]["parameters"].Value <string>());
                }

                testPassed = testPassed && ValidateParameters(test, "read", expectedParameters, actualParameters);

                if (useTypedTable)
                {
                    // Refresh operation only exists for typed tables
                    dict["operation"] = "read";
                    expectedParameters["operation"] = "read";
                    typedItem.Id = 1;
                    typedItem.parameters = "";
                    await typed.RefreshAsync(typedItem, dict);
                    actualParameters = JObject.Parse(typedItem.parameters);
                    testPassed = testPassed && ValidateParameters(test, "refresh", expectedParameters, actualParameters);
                }

                // Delete operation doesn't populate the object with the response, so we'll use a filter to capture that
                var handler = new HandlerToCaptureHttpTraffic();
                var filteredClient = new MobileServiceClient(client.ApplicationUri, client.ApplicationKey, handler);
                typed = filteredClient.GetTable <ParamsTestTableItem>();
                untyped = filteredClient.GetTable(ZumoTestGlobals.ParamsTestTableName);

                dict["operation"] = "delete";
                expectedParameters["operation"] = "delete";
                if (useTypedTable)
                {
                    await typed.DeleteAsync(typedItem, dict);
                }
                else
                {
                    await untyped.DeleteAsync(untypedItem, dict);
                }

                JObject response = JObject.Parse(handler.ResponseBody);
                actualParameters = JObject.Parse(response["parameters"].Value <string>());

                testPassed = testPassed && ValidateParameters(test, "delete", expectedParameters, actualParameters);

                return testPassed;
            }));
        }
        private async Task CreateUserAgentValidationTest()
        {
            Log("Validation User-Agent header");

            var handler = new HandlerToCaptureHttpTraffic();
            MobileServiceClient client = new MobileServiceClient(
                this.GetTestSetting("MobileServiceRuntimeUrl"),
                this.GetTestSetting("MobileServiceRuntimeKey"),
                handler);
            var table = client.GetTable <RoundTripTableItem>();
            var item  = new RoundTripTableItem {
                Name = "hello"
            };
            await table.InsertAsync(item);

            Action <string> dumpAndValidateHeaders = delegate(string operation)
            {
                Log("Headers for {0}:", operation);
                Log("  Request:");
                foreach (var header in handler.RequestHeaders.Keys)
                {
                    Log("    {0}: {1}", header, handler.RequestHeaders[header]);
                }

                Log("  Response:");
                foreach (var header in handler.ResponseHeaders.Keys)
                {
                    Log("    {0}: {1}", header, handler.ResponseHeaders[header]);
                }

                string userAgent;
                if (!handler.RequestHeaders.TryGetValue("User-Agent", out userAgent))
                {
                    Log("No user-agent header in the request");
                    throw new InvalidOperationException("This will fail the test");
                }
                else
                {
                    Regex expected = new Regex(@"^ZUMO\/\d.\d");
                    if (expected.IsMatch(userAgent))
                    {
                        Log("User-Agent validated correclty");
                    }
                    else
                    {
                        Log("User-Agent didn't validate properly.");
                        throw new InvalidOperationException("This will fail the test");
                    }
                }
            };

            dumpAndValidateHeaders("Insert");

            item.Number = 123;
            await table.UpdateAsync(item);

            dumpAndValidateHeaders("Update");

            var item2 = await table.LookupAsync(item.Id);

            dumpAndValidateHeaders("Read");

            await table.DeleteAsync(item);

            dumpAndValidateHeaders("Delete");
        }