public async Task InvokeCustomAPIGetWithODataParams() { TestHttpHandler hijack = new TestHttpHandler(); hijack.SetResponseContent("{\"id\":3}"); MobileServiceClient service = new MobileServiceClient("http://www.test.com", "secret...", hijack); var myParams = new Dictionary <string, string>() { { "$select", "one,two" }, { "$take", "1" } }; IntType expected = await service.InvokeApiAsync <IntType>("calculator/add", HttpMethod.Get, myParams); Assert.Contains(hijack.Request.RequestUri.Query, "?%24select=one%2Ctwo&%24take=1"); }
public async Task InvokeGenericCustomAPIWithNullResponse_Success() { TestHttpHandler hijack = new TestHttpHandler(); hijack.Response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); hijack.Response.Content = null; MobileServiceClient service = new MobileServiceClient(MobileAppUriValidator.DummyMobileApp, hijack); MobileAppUriValidator mobileAppUriValidator = new MobileAppUriValidator(service); IntType expected = await service.InvokeApiAsync <IntType>("testapi"); Assert.AreEqual(hijack.Request.RequestUri.LocalPath, mobileAppUriValidator.GetApiUriPath("testapi")); Assert.AreEqual(expected, null); }
public async Task InvokeCustomAPIGetWithParams() { TestHttpHandler hijack = new TestHttpHandler(); hijack.SetResponseContent("{\"id\":3}"); MobileServiceClient service = new MobileServiceClient(MobileAppUriValidator.DummyMobileApp, hijack); var myParams = new Dictionary <string, string>() { { "a", "1" }, { "b", "2" } }; IntType expected = await service.InvokeApiAsync <IntType>("calculator/add", HttpMethod.Get, myParams); Assert.Contains(hijack.Request.RequestUri.Query, "?a=1&b=2"); }
public async Task InvokeCustomAPIPostWithBody() { TestHttpHandler hijack = new TestHttpHandler(); hijack.SetResponseContent("{\"id\":3}"); MobileServiceClient service = new MobileServiceClient("http://www.test.com", "secret...", hijack); var myParams = new Dictionary <string, string>() { { "a", "1" }, { "b", "2" } }; var body = "{\"test\" : \"one\"}"; IntType expected = await service.InvokeApiAsync <string, IntType>("calculator/add", body, HttpMethod.Post, myParams); Assert.Contains(hijack.Request.RequestUri.Query, "?a=1&b=2"); Assert.IsNotNull(hijack.Request.Content); }
public void IntDeserializationNegative() { List<Tuple<string, string>> testCases = new List<Tuple<string, string>>() { new Tuple<string, string>("{\"id\":2147483648}", "Error converting value 2147483648 to type 'System.Int32'. Path 'id', line 1, position 16."), new Tuple<string, string>("{\"id\":-2147483649}", "Error converting value -2147483649 to type 'System.Int32'. Path 'id', line 1, position 17."), }; foreach (var testCase in testCases) { var input = testCase.Item1; var expected = testCase.Item2; IntType actual = new IntType(); Exception actualError = null; try { // Need to ensure that the type is registered as a table to force the id property check DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(IntType)); DefaultSerializer.Deserialize(input, actual); } catch (Exception e) { actualError = e; } Assert.AreEqual(actualError.Message, expected); } }
public void IntDeserialization() { List<Tuple<IntType, string>> testCases = new List<Tuple<IntType, string>>() { new Tuple<IntType, string>(new IntType() { Id = 0 }, "{}"), new Tuple<IntType, string>(new IntType() { Id = 0 }, "{\"id\":null}"), new Tuple<IntType, string>(new IntType() { Id = 0 }, "{\"id\":0}"), new Tuple<IntType, string>(new IntType() { Id = 0 }, "{\"id\":false}"), new Tuple<IntType, string>(new IntType() { Id = 1 }, "{\"id\":true}"), new Tuple<IntType, string>(new IntType() { Id = -1 }, "{\"id\":-1}"), new Tuple<IntType, string>(new IntType() { Id = 1 }, "{\"id\":1}"), new Tuple<IntType, string>(new IntType() { Id = int.MaxValue }, "{\"id\":2147483647}"), new Tuple<IntType, string>(new IntType() { Id = int.MinValue }, "{\"id\":-2147483648}") }; // Need to ensure that the type is registered as a table to force the id property check DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(IntType)); foreach (var testCase in testCases) { var input = JToken.Parse(testCase.Item2); var expected = testCase.Item1; IntType actual = new IntType(); DefaultSerializer.Deserialize(input, actual); Assert.AreEqual(actual.Id, expected.Id); JArray json = JToken.Parse("[" + testCase.Item2 + "]") as JArray; actual = DefaultSerializer.Deserialize<IntType>(json).FirstOrDefault(); Assert.AreEqual(actual.Id, expected.Id); actual = (IntType)DefaultSerializer.Deserialize<IntType>(input); Assert.AreEqual(actual.Id, expected.Id); } }