private async Task CreateFilterTestWithMultipleRequests(bool typed)
        {
            Log(string.Format(CultureInfo.InvariantCulture, "### Filter which maps one requests to many - {0} client", typed ? "typed" : "untyped"));

            var client           = this.GetClient();
            int numberOfRequests = new Random().Next(2, 5);
            var handler          = new HandlerWithMultipleRequests(this, numberOfRequests);

            Log("Created a filter which will replay the request {0} times", numberOfRequests);
            var filteredClient = new MobileServiceClient(client.ApplicationUri, client.ApplicationKey, handler);

            var typedTable   = filteredClient.GetTable <RoundTripTableItem>();
            var untypedTable = filteredClient.GetTable("RoundTripTable");
            var uniqueId     = Guid.NewGuid().ToString("N");

            if (typed)
            {
                var item = new RoundTripTableItem {
                    Name = uniqueId
                };
                await typedTable.InsertAsync(item);
            }
            else
            {
                var item = new JObject(new JProperty("name", uniqueId));
                await untypedTable.InsertAsync(item);
            }

            if (handler.TestFailed)
            {
                Assert.Fail("Filter reported a test failure. Aborting.");
            }

            Log("Inserted the data; now retrieving it to see how many items we have inserted.");
            handler.NumberOfRequests = 1; // no need to send it multiple times anymore

            var items = await untypedTable.ReadAsync("$select=name,id&$filter=name eq '" + uniqueId + "'");

            var  array = (JArray)items;
            bool passed;

            if (array.Count == numberOfRequests)
            {
                Log("Filter inserted correct number of items.");
                passed = true;
            }
            else
            {
                Log("Error, filtered client should have inserted {0} items, but there are {1}", numberOfRequests, array.Count);
                passed = false;
            }

            // Cleanup
            foreach (var item in array)
            {
                await untypedTable.DeleteAsync(item as JObject);
            }

            Log("Cleanup: removed added items.");
            if (!passed)
            {
                Assert.Fail("");
            }
        }
        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");
        }