public void WithFilter() { string appUrl = "http://www.test.com/"; string appKey = "secret..."; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(new Uri(appUrl), appKey) .WithFilter(hijack); // Ensure properties are copied over Assert.AreEqual(appUrl, service.ApplicationUri.ToString()); Assert.AreEqual(appKey, service.ApplicationKey); // Set the filter to return an empty array hijack.Response.Content = new JsonArray().Stringify(); service.GetTable("foo").ReadAsync("bar") .ContinueWith(t => { // Verify the filter was in the loop Assert.That(hijack.Request.Uri.ToString(), Is.StringStarting(appUrl)); Assert.Throws <ArgumentNullException>(() => service.WithFilter(null)); }).WaitOrFail(Timeout); }
public async Task UpdateAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); JsonObject obj = new JsonObject().Set("id", 12).Set("value", "new"); hijack.Response.Content = new JsonObject() .Set("id", 12) .Set("value", "new") .Set("other", "123") .Stringify(); IMobileServiceTable table = service.GetTable(collection); await table.UpdateAsync(obj); Assert.AreEqual("123", obj.Get("other").AsString()); Assert.Contains(hijack.Request.Uri.ToString(), collection); ThrowsAsync <ArgumentNullException>(async() => await table.UpdateAsync(null)); ThrowsAsync <ArgumentException>(async() => await table.UpdateAsync(new JsonObject())); }
public void WithFilter() { string appUrl = "http://www.test.com/"; string appKey = "secret..."; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(new Uri(appUrl), appKey) .WithFilter(hijack); // Ensure properties are copied over Assert.AreEqual(appUrl, service.ApplicationUri.ToString()); Assert.AreEqual(appKey, service.ApplicationKey); // Set the filter to return an empty array hijack.Response.Content = new JsonArray().Stringify(); service.GetTable("foo").ReadAsync("bar") .ContinueWith (t => { // Verify the filter was in the loop Assert.That (hijack.Request.Uri.ToString(), Is.StringStarting (appUrl)); Assert.Throws<ArgumentNullException>(() => service.WithFilter(null)); }).WaitOrFail (Timeout); }
public async Task InsertAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); JsonObject obj = new JsonObject().Set("value", "new"); hijack.Response.Content = new JsonObject().Set("id", 12).Set("value", "new").Stringify(); await service.GetTable(collection).InsertAsync(obj); Assert.AreEqual(12, obj.Get("id").AsInteger()); Assert.Contains(hijack.Request.Uri.ToString(), collection); ThrowsAsync <ArgumentNullException>( async() => await service.GetTable(collection).InsertAsync(null)); // Verify we throw if ID is set on both JSON and strongly typed // instances ThrowsAsync <ArgumentException>( async() => await service.GetTable(collection).InsertAsync( new JsonObject().Set("id", 15))); ThrowsAsync <ArgumentException>( async() => await service.GetTable <Person>().InsertAsync( new Person() { Id = 15 })); }
public void UpdateAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; var userDefinedParameters = new Dictionary <string, string>() { { "state", "FL" } }; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); JsonObject obj = new JsonObject().Set("id", 12).Set("value", "new"); hijack.Response.Content = new JsonObject() .Set("id", 12) .Set("value", "new") .Set("other", "123") .Stringify(); IMobileServiceTable table = service.GetTable(collection); table.UpdateAsync(obj, userDefinedParameters).WaitOrFail(Timeout); Assert.AreEqual("123", obj.Get("other").AsString()); Assert.That(hijack.Request.Uri.ToString(), Contains.Substring(collection)); ThrowsAsync <ArgumentNullException>(() => table.UpdateAsync(null)); ThrowsAsync <ArgumentException>(() => table.UpdateAsync(new JsonObject())); }
public async Task StandardRequestFormat() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; string query = "$filter=id eq 12"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); service.CurrentUser = new MobileServiceUser("someUser"); service.CurrentUser.MobileServiceAuthenticationToken = "Not rhubarb"; hijack.Response.Content = new JsonArray() .Append(new JsonObject().Set("id", 12).Set("value", "test")) .Stringify(); IJsonValue response = await service.GetTable(collection).ReadAsync(query); Assert.IsNotNull(hijack.Request.Headers["X-ZUMO-INSTALLATION-ID"]); Assert.AreEqual("secret...", hijack.Request.Headers["X-ZUMO-APPLICATION"]); Assert.AreEqual("application/json", hijack.Request.Accept); Assert.AreEqual("Not rhubarb", hijack.Request.Headers["X-ZUMO-AUTH"]); }
public async Task ReadAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; string query = "$filter=id eq 12"; var userDefinedParameters = new Dictionary <string, string>() { { "tags", "#pizza #beer" } }; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonArray() .Append(new JsonObject().Set("id", 12).Set("value", "test")) .Stringify(); IJsonValue response = await service.GetTable(collection).ReadAsync(query, userDefinedParameters); Assert.Contains(hijack.Request.Uri.ToString(), collection); Assert.Contains(hijack.Request.Uri.AbsoluteUri, "tags=%23pizza%20%23beer"); Assert.Contains(hijack.Request.Uri.ToString(), query); ThrowsAsync <ArgumentNullException>(async() => await service.GetTable(null).ReadAsync(query)); ThrowsAsync <ArgumentException>(async() => await service.GetTable("").ReadAsync(query)); var invalidUserDefinedParameters = new Dictionary <string, string>() { { "$this is invalid", "since it starts with a '$'" } }; ThrowsAsync <ArgumentException>(async() => await service.GetTable(collection).ReadAsync(query, invalidUserDefinedParameters)); }
public async Task DeleteAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; var userDefinedParameters = new Dictionary <string, string>() { { "state", "WY" } }; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); JsonObject obj = new JsonObject().Set("id", 12).Set("value", "new"); IMobileServiceTable table = service.GetTable(collection); await table.DeleteAsync(obj, userDefinedParameters); Assert.Contains(hijack.Request.Uri.ToString(), collection); Assert.IsNull(hijack.Request.Content); Assert.Contains(hijack.Request.Uri.Query, "state=WY"); ThrowsAsync <ArgumentNullException>(async() => await table.DeleteAsync(null)); ThrowsAsync <ArgumentException>(async() => await table.DeleteAsync(new JsonObject())); }
public async Task SendLoginAsync() { TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient client = new MobileServiceClient("http://www.test.com", "secret...").WithFilter(hijack); MobileServiceLogin login = new MobileServiceLogin(client, ignoreFilters: false); // Send back a successful login response hijack.Response.Content = new JsonObject() .Set("authenticationToken", "rhubarb") .Set("user", new JsonObject() .Set("userId", "123456")).Stringify(); MobileServiceUser current = await login.SendLoginAsync("donkey"); Assert.IsNotNull(current); Assert.AreEqual("123456", current.UserId); Assert.AreEqual("rhubarb", current.MobileServiceAuthenticationToken); Assert.EndsWith(hijack.Request.Uri.ToString(), "login"); string input = JsonValue.Parse(hijack.Request.Content).Get("authenticationToken").AsString(); Assert.AreEqual("donkey", input); Assert.AreEqual("POST", hijack.Request.Method); Assert.AreSame(current, client.CurrentUser); }
public async Task LookupAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonObject() .Set("id", 12) .Set("Name", "Bob") .Stringify(); IMobileServiceTable <Person> table = service.GetTable <Person>(); Person bob = await table.LookupAsync(12); Assert.AreEqual(12L, bob.Id); Assert.AreEqual("Bob", bob.Name); hijack.Response.StatusCode = 404; bool thrown = false; try { bob = await table.LookupAsync(12); } catch (InvalidOperationException) { thrown = true; } Assert.IsTrue(thrown, "Exception should be thrown on a 404!"); }
public async Task DateOffsetUri() { TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient client = new MobileServiceClient("http://www.test.com").WithFilter(hijack); IMobileServiceTable<DateOffsetExample> table = client.GetTable<DateOffsetExample>(); hijack.Response.StatusCode = 200; hijack.Response.Content = "[]"; DateTimeOffset date = new DateTimeOffset( new DateTime(2009, 11, 21, 14, 22, 59, 860, DateTimeKind.Utc).ToLocalTime()); await table.Where(b => b.Date == date).ToEnumerableAsync(); Assert.EndsWith(hijack.Request.Uri.ToString(), "$filter=(DateOffsetExampleDate eq datetime'2009-11-21T14:22:59.860Z')"); }
public async Task DateOffsetUri() { TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient client = new MobileServiceClient("http://www.test.com").WithFilter(hijack); IMobileServiceTable <DateOffsetExample> table = client.GetTable <DateOffsetExample>(); hijack.Response.StatusCode = 200; hijack.Response.Content = "[]"; DateTimeOffset date = new DateTimeOffset( new DateTime(2009, 11, 21, 14, 22, 59, 860, DateTimeKind.Utc).ToLocalTime()); await table.Where(b => b.Date == date).ToEnumerableAsync(); Assert.EndsWith(hijack.Request.Uri.ToString(), "$filter=(DateOffsetExampleDate eq datetime'2009-11-21T14:22:59.860Z')"); }
public void LoginAsync() { TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient("http://www.test.com", "secret...") .WithFilter(hijack); // Send back a successful login response hijack.Response.Content = new JsonObject() .Set("authenticationToken", "rhubarb") .Set("user", new JsonObject() .Set("userId", "123456")).Stringify(); service.LoginAsync("donkey").ContinueWith(t => { var current = t.Result; Assert.IsNotNull(current); Assert.AreEqual("123456", current.UserId); Assert.AreEqual("rhubarb", current.MobileServiceAuthenticationToken); Assert.That(hijack.Request.Uri.ToString(), Is.StringEnding("login")); string input = JsonValue.Parse(hijack.Request.Content).Get("authenticationToken").AsString(); Assert.AreEqual("donkey", input); Assert.AreEqual("POST", hijack.Request.Method); Assert.AreSame(current, service.CurrentUser); // Set the Auth Token service.CurrentUser.MobileServiceAuthenticationToken = "Not rhubarb"; // Verify that the user token is sent with each request service.GetTable("foo").ReadAsync("bar").ContinueWith(rt => { var response = rt.Result; Assert.AreEqual("Not rhubarb", hijack.Request.Headers["X-ZUMO-AUTH"]); // Verify error cases ThrowsAsync <ArgumentNullException>(() => service.LoginAsync(null)); ThrowsAsync <ArgumentException>(() => service.LoginAsync("")); // Send back a failure and ensure it throws hijack.Response.Content = new JsonObject().Set("error", "login failed").Stringify(); hijack.Response.StatusCode = 401; ThrowsAsync <InvalidOperationException>(() => service.LoginAsync("donkey")); }).WaitOrFail(Timeout); }).WaitOrFail(Timeout); }
public void LookupAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; var userDefinedParameters = new Dictionary <string, string>() { { "state", "CA" } }; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonObject() .Set("id", 12) .Set("Name", "Bob") .Stringify(); IMobileServiceTable <Person> table = service.GetTable <Person>(); table.LookupAsync(12, userDefinedParameters).ContinueWith(t => { var bob = t.Result; Assert.That(hijack.Request.Uri.Query, Contains.Substring("state=CA")); Assert.AreEqual(12L, bob.Id); Assert.AreEqual("Bob", bob.Name); hijack.Response.StatusCode = 404; bool thrown = false; try { Task <Person> lookup = table.LookupAsync(12); lookup.WaitOrFail(Timeout); bob = lookup.Result; } catch (AggregateException aex) { aex.AssertCaught <InvalidOperationException>(); thrown = true; } Assert.IsTrue(thrown, "Exception should be thrown on a 404!"); }).WaitOrFail(Timeout); }
public void SendLoginAsyncThrows() { TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient client = new MobileServiceClient("http://www.test.com", "secret...").WithFilter(hijack); MobileServiceLogin login = new MobileServiceLogin(client, ignoreFilters: false); // Verify error cases ThrowsAsync <ArgumentNullException>(async() => await login.SendLoginAsync(null)); ThrowsAsync <ArgumentException>(async() => await login.SendLoginAsync("")); // Send back a failure and ensure it throws hijack.Response.Content = new JsonObject().Set("error", "login failed").Stringify(); hijack.Response.StatusCode = 401; ThrowsAsync <InvalidOperationException>(async() => { await login.SendLoginAsync("donkey"); }); }
public void SendLoginAsyncThrows() { TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient client = new MobileServiceClient("http://www.test.com", "secret...").WithFilter(hijack); MobileServiceLogin login = new MobileServiceLogin(client, ignoreFilters: false); // Verify error cases ThrowsAsync<ArgumentNullException>(async () => await login.SendLoginAsync(null)); ThrowsAsync<ArgumentException>(async () => await login.SendLoginAsync("")); // Send back a failure and ensure it throws hijack.Response.Content = new JsonObject().Set("error", "login failed").Stringify(); hijack.Response.StatusCode = 401; ThrowsAsync<InvalidOperationException>(async () => { await login.SendLoginAsync("donkey"); }); }
public async Task Logout() { TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient("http://www.test.com", "secret...") .WithFilter(hijack); // Send back a successful login response hijack.Response.Content = new JsonObject() .Set("authenticationToken", "rhubarb") .Set("user", new JsonObject() .Set("userId", "123456")).Stringify(); MobileServiceUser current = await service.LoginAsync("donkey"); Assert.IsNotNull(service.CurrentUser); service.Logout(); Assert.IsNull(service.CurrentUser); }
public void DeleteAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); JsonObject obj = new JsonObject().Set("id", 12).Set("value", "new"); IMobileServiceTable table = service.GetTable(collection); table.DeleteAsync(obj).WaitOrFail(Timeout); Assert.That(hijack.Request.Uri.ToString(), Contains.Substring(collection)); ThrowsAsync <ArgumentNullException>(() => table.DeleteAsync(null)); ThrowsAsync <ArgumentException>(() => table.DeleteAsync(new JsonObject())); }
public async Task ReadAsyncGeneric() { string appUrl = "http://www.test.com"; string appKey = "secret..."; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonArray() .Append(new JsonObject().Set("id", 12).Set("Name", "Bob")) .Stringify(); IMobileServiceTable <Person> table = service.GetTable <Person>(); List <Person> people = await table.Where(p => p.Id == 12).ToListAsync(); Assert.AreEqual(1, people.Count); Assert.AreEqual(12L, people[0].Id); Assert.AreEqual("Bob", people[0].Name); }
public void InsertAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; var userDefinedParameters = new Dictionary <string, string>() { { "state", "AL" } }; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); JsonObject obj = new JsonObject().Set("value", "new"); hijack.Response.Content = new JsonObject().Set("id", 12).Set("value", "new").Stringify(); service.GetTable(collection).InsertAsync(obj, userDefinedParameters).WaitOrFail(Timeout); Assert.AreEqual(12, obj.Get("id").AsInteger()); Assert.That(hijack.Request.Uri.ToString(), Contains.Substring(collection)); Assert.That(hijack.Request.Uri.Query, Contains.Substring("state=AL")); ThrowsAsync <ArgumentNullException>( () => service.GetTable(collection).InsertAsync(null)); // Verify we throw if ID is set on both JSON and strongly typed // instances ThrowsAsync <ArgumentException>( () => service.GetTable(collection).InsertAsync( new JsonObject().Set("id", 15))); ThrowsAsync <ArgumentException>( () => service.GetTable <Person>().InsertAsync( new Person() { Id = 15 })); }
public void StandardRequestFormat() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; string query = "$filter=id eq 12"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonArray() .Append(new JsonObject().Set("id", 12).Set("value", "test")) .Stringify(); service.GetTable(collection).ReadAsync(query).ContinueWith(t => { Assert.IsNotNull(hijack.Request.Headers["X-ZUMO-INSTALLATION-ID"]); Assert.AreEqual("secret...", hijack.Request.Headers["X-ZUMO-APPLICATION"]); Assert.AreEqual("application/json", hijack.Request.Accept); }); }
public async Task ReadAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; string query = "$filter=id eq 12"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonArray() .Append(new JsonObject().Set("id", 12).Set("value", "test")) .Stringify(); IJsonValue response = await service.GetTable(collection).ReadAsync(query); Assert.Contains(hijack.Request.Uri.ToString(), collection); Assert.EndsWith(hijack.Request.Uri.ToString(), query); ThrowsAsync <ArgumentNullException>(async() => await service.GetTable(null).ReadAsync(query)); ThrowsAsync <ArgumentException>(async() => await service.GetTable("").ReadAsync(query)); }
public void ReadAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; string query = "$filter=id eq 12"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonArray() .Append(new JsonObject().Set("id", 12).Set("value", "test")) .Stringify(); service.GetTable(collection).ReadAsync(query).ContinueWith(t => { Assert.That(hijack.Request.Uri.ToString(), Contains.Substring(collection)); Assert.That(hijack.Request.Uri.ToString(), Is.StringEnding(query)); ThrowsAsync <ArgumentNullException>(() => service.GetTable(null).ReadAsync(query)); ThrowsAsync <ArgumentException>(() => service.GetTable("").ReadAsync(query)); }).WaitOrFail(Timeout); }
public async Task SendLoginAsync() { TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient client = new MobileServiceClient("http://www.test.com", "secret...").WithFilter(hijack); MobileServiceLogin login = new MobileServiceLogin(client, ignoreFilters:false); // Send back a successful login response hijack.Response.Content = new JsonObject() .Set("authenticationToken", "rhubarb") .Set("user", new JsonObject() .Set("userId", "123456")).Stringify(); MobileServiceUser current = await login.SendLoginAsync("donkey"); Assert.IsNotNull(current); Assert.AreEqual("123456", current.UserId); Assert.AreEqual("rhubarb", current.MobileServiceAuthenticationToken); Assert.EndsWith(hijack.Request.Uri.ToString(), "login"); string input = JsonValue.Parse(hijack.Request.Content).Get("authenticationToken").AsString(); Assert.AreEqual("donkey", input); Assert.AreEqual("POST", hijack.Request.Method); Assert.AreSame(current, client.CurrentUser); }
public void ErrorMessageConstruction() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; string query = "$filter=id eq 12"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); // Verify the error message is correctly pulled out hijack.Response.Content = new JsonObject() .Set("error", "error message") .Set("other", "donkey") .Stringify(); hijack.Response.StatusCode = 401; hijack.Response.StatusDescription = "YOU SHALL NOT PASS."; try { service.GetTable(collection).ReadAsync(query).WaitOrFail(Timeout); } catch (AggregateException aex) { var ex = aex.AssertCaught <InvalidOperationException>(); Assert.That(ex.Message, Is.EqualTo("error message")); } // Verify all of the exception parameters hijack.Response.Content = new JsonObject() .Set("error", "error message") .Set("other", "donkey") .Stringify(); hijack.Response.StatusCode = 401; hijack.Response.StatusDescription = "YOU SHALL NOT PASS."; try { service.GetTable(collection).ReadAsync(query).WaitOrFail(Timeout); } catch (AggregateException aex) { var ex = aex.AssertCaught <MobileServiceInvalidOperationException>(); Assert.That(ex.Message, Is.EqualTo("error message")); Assert.AreEqual((int)HttpStatusCode.Unauthorized, ex.Response.StatusCode); Assert.That(ex.Response.Content, Contains.Substring("donkey")); Assert.That(ex.Request.Uri.ToString(), Is.StringStarting(appUrl)); Assert.AreEqual("YOU SHALL NOT PASS.", ex.Response.StatusDescription); } // If no error message in the response, we'll use the // StatusDescription instead hijack.Response.Content = new JsonObject() .Set("other", "donkey") .Stringify(); try { service.GetTable(collection).ReadAsync(query).WaitOrFail(Timeout); } catch (AggregateException aex) { var ex = aex.AssertCaught <InvalidOperationException>(); Assert.AreEqual("The request could not be completed. (YOU SHALL NOT PASS.)", ex.Message); } }
public async Task LoginAsync() { TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient("http://www.test.com", "secret...") .WithFilter(hijack); // Send back a successful login response hijack.Response.Content = new JsonObject() .Set("authenticationToken", "rhubarb") .Set("user", new JsonObject() .Set("userId", "123456")).Stringify(); MobileServiceUser current = await service.LoginAsync("donkey"); Assert.IsNotNull(current); Assert.AreEqual("123456", current.UserId); Assert.AreEqual("rhubarb", current.MobileServiceAuthenticationToken); Assert.EndsWith(hijack.Request.Uri.ToString(), "login"); string input = JsonValue.Parse(hijack.Request.Content).Get("authenticationToken").AsString(); Assert.AreEqual("donkey", input); Assert.AreEqual("POST", hijack.Request.Method); Assert.AreSame(current, service.CurrentUser); // Set the Auth Token service.CurrentUser.MobileServiceAuthenticationToken = "Not rhubarb"; // Verify that the user token is sent with each request IJsonValue response = await service.GetTable("foo").ReadAsync("bar"); Assert.AreEqual("Not rhubarb", hijack.Request.Headers["X-ZUMO-AUTH"]); // Verify error cases ThrowsAsync<ArgumentNullException>(async () => await service.LoginAsync(null)); ThrowsAsync<ArgumentException>(async () => await service.LoginAsync("")); // Send back a failure and ensure it throws hijack.Response.Content = new JsonObject().Set("error", "login failed").Stringify(); hijack.Response.StatusCode = 401; ThrowsAsync<InvalidOperationException>(async () => { current = await service.LoginAsync("donkey"); }); }
public async Task DeleteAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; var userDefinedParameters = new Dictionary<string, string>() { { "state", "WY" } }; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); JsonObject obj = new JsonObject().Set("id", 12).Set("value", "new"); IMobileServiceTable table = service.GetTable(collection); await table.DeleteAsync(obj, userDefinedParameters); Assert.Contains(hijack.Request.Uri.ToString(), collection); Assert.IsNull(hijack.Request.Content); Assert.Contains(hijack.Request.Uri.Query, "state=WY"); ThrowsAsync<ArgumentNullException>(async () => await table.DeleteAsync(null)); ThrowsAsync<ArgumentException>(async () => await table.DeleteAsync(new JsonObject())); }
public async Task InsertAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; var userDefinedParameters = new Dictionary<string, string>() {{ "state", "AL" }}; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); JsonObject obj = new JsonObject().Set("value", "new"); hijack.Response.Content = new JsonObject().Set("id", 12).Set("value", "new").Stringify(); await service.GetTable(collection).InsertAsync(obj, userDefinedParameters); Assert.AreEqual(12, obj.Get("id").AsInteger()); Assert.Contains(hijack.Request.Uri.ToString(), collection); Assert.Contains(hijack.Request.Uri.Query, "state=AL"); ThrowsAsync<ArgumentNullException>( async () => await service.GetTable(collection).InsertAsync(null)); // Verify we throw if ID is set on both JSON and strongly typed // instances ThrowsAsync<ArgumentException>( async () => await service.GetTable(collection).InsertAsync( new JsonObject().Set("id", 15))); ThrowsAsync<ArgumentException>( async () => await service.GetTable<Person>().InsertAsync( new Person() { Id = 15 })); }
public async Task LookupAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; var userDefinedParameters = new Dictionary<string, string>() { { "state", "CA"} }; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonObject() .Set("id", 12) .Set("Name", "Bob") .Stringify(); IMobileServiceTable<Person> table = service.GetTable<Person>(); Person bob = await table.LookupAsync(12, userDefinedParameters); Assert.Contains(hijack.Request.Uri.Query, "state=CA"); Assert.AreEqual(12L, bob.Id); Assert.AreEqual("Bob", bob.Name); hijack.Response.StatusCode = 404; bool thrown = false; try { bob = await table.LookupAsync(12); } catch (InvalidOperationException) { thrown = true; } Assert.IsTrue(thrown, "Exception should be thrown on a 404!"); }
public async Task ReadAsyncGeneric() { string appUrl = "http://www.test.com"; string appKey = "secret..."; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonArray() .Append(new JsonObject().Set("id", 12).Set("Name", "Bob")) .Stringify(); IMobileServiceTable<Person> table = service.GetTable<Person>(); List<Person> people = await table.Where(p => p.Id == 12).ToListAsync(); Assert.AreEqual(1, people.Count); Assert.AreEqual(12L, people[0].Id); Assert.AreEqual("Bob", people[0].Name); }
public async Task DeleteAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); JsonObject obj = new JsonObject().Set("id", 12).Set("value", "new"); IMobileServiceTable table = service.GetTable(collection); await table.DeleteAsync(obj); Assert.Contains(hijack.Request.Uri.ToString(), collection); ThrowsAsync<ArgumentNullException>(async () => await table.DeleteAsync(null)); ThrowsAsync<ArgumentException>(async () => await table.DeleteAsync(new JsonObject())); }
public async Task ErrorMessageConstruction() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; string query = "$filter=id eq 12"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); // Verify the error message is correctly pulled out hijack.Response.Content = new JsonObject() .Set("error", "error message") .Set("other", "donkey") .Stringify(); hijack.Response.StatusCode = 401; hijack.Response.StatusDescription = "YOU SHALL NOT PASS."; try { IJsonValue response = await service.GetTable(collection).ReadAsync(query); } catch (InvalidOperationException ex) { Assert.AreEqual(ex.Message, "error message"); } // Verify all of the exception parameters hijack.Response.Content = new JsonObject() .Set("error", "error message") .Set("other", "donkey") .Stringify(); hijack.Response.StatusCode = 401; hijack.Response.StatusDescription = "YOU SHALL NOT PASS."; try { IJsonValue response = await service.GetTable(collection).ReadAsync(query); } catch (MobileServiceInvalidOperationException ex) { Assert.AreEqual(ex.Message, "error message"); Assert.AreEqual((int)HttpStatusCode.Unauthorized, ex.Response.StatusCode); Assert.Contains(ex.Response.Content, "donkey"); Assert.StartsWith(ex.Request.Uri.ToString(), appUrl); Assert.AreEqual("YOU SHALL NOT PASS.", ex.Response.StatusDescription); } // If no error message in the response, we'll use the // StatusDescription instead hijack.Response.Content = new JsonObject() .Set("other", "donkey") .Stringify(); try { IJsonValue response = await service.GetTable(collection).ReadAsync(query); } catch (InvalidOperationException ex) { Assert.AreEqual("The request could not be completed. (YOU SHALL NOT PASS.)", ex.Message); } }
public async Task StandardRequestFormat() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; string query = "$filter=id eq 12"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonArray() .Append(new JsonObject().Set("id", 12).Set("value", "test")) .Stringify(); IJsonValue response = await service.GetTable(collection).ReadAsync(query); Assert.IsNotNull(hijack.Request.Headers["X-ZUMO-INSTALLATION-ID"]); Assert.AreEqual("secret...", hijack.Request.Headers["X-ZUMO-APPLICATION"]); Assert.AreEqual("application/json", hijack.Request.Accept); }
public void DeleteAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; var userDefinedParameters = new Dictionary<string, string>() { { "state", "WY" } }; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); JsonObject obj = new JsonObject().Set("id", 12).Set("value", "new"); IMobileServiceTable table = service.GetTable(collection); table.DeleteAsync(obj, userDefinedParameters).WaitOrFail (Timeout); Assert.That (hijack.Request.Uri.ToString(), Contains.Substring (collection)); ThrowsAsync<ArgumentNullException>(() => table.DeleteAsync(null)); ThrowsAsync<ArgumentException>(() => table.DeleteAsync(new JsonObject())); }
public void LookupAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; var userDefinedParameters = new Dictionary<string, string>() { { "state", "CA" } }; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonObject() .Set("id", 12) .Set("Name", "Bob") .Stringify(); IMobileServiceTable<Person> table = service.GetTable<Person>(); table.LookupAsync(12, userDefinedParameters).ContinueWith (t => { var bob = t.Result; Assert.That(hijack.Request.Uri.Query, Contains.Substring("state=CA")); Assert.AreEqual(12L, bob.Id); Assert.AreEqual("Bob", bob.Name); hijack.Response.StatusCode = 404; bool thrown = false; try { Task<Person> lookup = table.LookupAsync (12); lookup.WaitOrFail (Timeout); bob = lookup.Result; } catch (AggregateException aex) { aex.AssertCaught<InvalidOperationException>(); thrown = true; } Assert.IsTrue(thrown, "Exception should be thrown on a 404!"); }).WaitOrFail (Timeout); }
public async Task ReadAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; string query = "$filter=id eq 12"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonArray() .Append(new JsonObject().Set("id", 12).Set("value", "test")) .Stringify(); IJsonValue response = await service.GetTable(collection).ReadAsync(query); Assert.Contains(hijack.Request.Uri.ToString(), collection); Assert.EndsWith(hijack.Request.Uri.ToString(), query); ThrowsAsync<ArgumentNullException>(async () => await service.GetTable(null).ReadAsync(query)); ThrowsAsync<ArgumentException>(async () => await service.GetTable("").ReadAsync(query)); }
public async Task ErrorMessageConstruction() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; string query = "$filter=id eq 12"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); // Verify the error message is correctly pulled out hijack.Response.Content = new JsonObject() .Set("error", "error message") .Set("other", "donkey") .Stringify(); hijack.Response.StatusCode = 401; hijack.Response.StatusDescription = "YOU SHALL NOT PASS."; try { IJsonValue response = await service.GetTable(collection).ReadAsync(query); } catch (InvalidOperationException ex) { Assert.Contains(ex.Message, "error message"); } // Verify all of the exception parameters hijack.Response.Content = new JsonObject() .Set("error", "error message") .Set("other", "donkey") .Stringify(); hijack.Response.StatusCode = 401; hijack.Response.StatusDescription = "YOU SHALL NOT PASS."; try { IJsonValue response = await service.GetTable(collection).ReadAsync(query); } catch (MobileServiceInvalidOperationException ex) { Assert.StartsWith(ex.Message, "error message"); Assert.AreEqual((int)HttpStatusCode.Unauthorized, ex.Response.StatusCode); Assert.Contains(ex.Response.Content, "donkey"); Assert.StartsWith(ex.Request.Uri.ToString(), appUrl); Assert.AreEqual("YOU SHALL NOT PASS.", ex.Response.StatusDescription); } // If no error message in the response, we'll use the // StatusDescription instead hijack.Response.Content = new JsonObject() .Set("other", "donkey") .Stringify(); try { IJsonValue response = await service.GetTable(collection).ReadAsync(query); } catch (InvalidOperationException ex) { Assert.StartsWith(ex.Message, "YOU SHALL NOT PASS."); } }
public async Task ReadAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; string query = "$filter=id eq 12"; var userDefinedParameters = new Dictionary<string, string>() { { "tags", "#pizza #beer" } }; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonArray() .Append(new JsonObject().Set("id", 12).Set("value", "test")) .Stringify(); IJsonValue response = await service.GetTable(collection).ReadAsync(query, userDefinedParameters); Assert.Contains(hijack.Request.Uri.ToString(), collection); Assert.Contains(hijack.Request.Uri.AbsoluteUri, "tags=%23pizza%20%23beer"); Assert.Contains(hijack.Request.Uri.ToString(), query); ThrowsAsync<ArgumentNullException>(async () => await service.GetTable(null).ReadAsync(query)); ThrowsAsync<ArgumentException>(async () => await service.GetTable("").ReadAsync(query)); var invalidUserDefinedParameters = new Dictionary<string, string>() { { "$this is invalid", "since it starts with a '$'" } }; ThrowsAsync<ArgumentException>(async () => await service.GetTable(collection).ReadAsync(query, invalidUserDefinedParameters)); }
public void ErrorMessageConstruction() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; string query = "$filter=id eq 12"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); // Verify the error message is correctly pulled out hijack.Response.Content = new JsonObject() .Set("error", "error message") .Set("other", "donkey") .Stringify(); hijack.Response.StatusCode = 401; hijack.Response.StatusDescription = "YOU SHALL NOT PASS."; try { service.GetTable(collection).ReadAsync(query).WaitOrFail (Timeout); } catch (AggregateException aex) { var ex = aex.AssertCaught<InvalidOperationException>(); Assert.That (ex.Message, Is.EqualTo ("error message")); } // Verify all of the exception parameters hijack.Response.Content = new JsonObject() .Set("error", "error message") .Set("other", "donkey") .Stringify(); hijack.Response.StatusCode = 401; hijack.Response.StatusDescription = "YOU SHALL NOT PASS."; try { service.GetTable(collection).ReadAsync(query).WaitOrFail (Timeout); } catch (AggregateException aex) { var ex = aex.AssertCaught<MobileServiceInvalidOperationException>(); Assert.That (ex.Message, Is.EqualTo ("error message")); Assert.AreEqual((int)HttpStatusCode.Unauthorized, ex.Response.StatusCode); Assert.That (ex.Response.Content, Contains.Substring ("donkey")); Assert.That (ex.Request.Uri.ToString(), Is.StringStarting (appUrl)); Assert.AreEqual("YOU SHALL NOT PASS.", ex.Response.StatusDescription); } // If no error message in the response, we'll use the // StatusDescription instead hijack.Response.Content = new JsonObject() .Set("other", "donkey") .Stringify(); try { service.GetTable(collection).ReadAsync(query).WaitOrFail (Timeout); } catch (AggregateException aex) { var ex = aex.AssertCaught<InvalidOperationException>(); Assert.AreEqual ("The request could not be completed. (YOU SHALL NOT PASS.)", ex.Message); } }
public void InsertAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); JsonObject obj = new JsonObject().Set("value", "new"); hijack.Response.Content = new JsonObject().Set("id", 12).Set("value", "new").Stringify(); service.GetTable(collection).InsertAsync(obj).WaitOrFail (Timeout); Assert.AreEqual(12, obj.Get("id").AsInteger()); Assert.That (hijack.Request.Uri.ToString(), Contains.Substring (collection)); ThrowsAsync<ArgumentNullException>( () => service.GetTable(collection).InsertAsync(null)); // Verify we throw if ID is set on both JSON and strongly typed // instances ThrowsAsync<ArgumentException>( () => service.GetTable(collection).InsertAsync( new JsonObject().Set("id", 15))); ThrowsAsync<ArgumentException>( () => service.GetTable<Person>().InsertAsync( new Person() { Id = 15 })); }
public void ReadAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; string query = "$filter=id eq 12"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); hijack.Response.Content = new JsonArray() .Append(new JsonObject().Set("id", 12).Set("value", "test")) .Stringify(); service.GetTable(collection).ReadAsync(query).ContinueWith (t => { Assert.That (hijack.Request.Uri.ToString(), Contains.Substring (collection)); Assert.That (hijack.Request.Uri.ToString(), Is.StringEnding (query)); ThrowsAsync<ArgumentNullException>(() => service.GetTable(null).ReadAsync(query)); ThrowsAsync<ArgumentException>(() => service.GetTable("").ReadAsync(query)); }).WaitOrFail (Timeout); }
public void UpdateAsync() { string appUrl = "http://www.test.com"; string appKey = "secret..."; string collection = "tests"; TestServiceFilter hijack = new TestServiceFilter(); MobileServiceClient service = new MobileServiceClient(appUrl, appKey) .WithFilter(hijack); JsonObject obj = new JsonObject().Set("id", 12).Set("value", "new"); hijack.Response.Content = new JsonObject() .Set("id", 12) .Set("value", "new") .Set("other", "123") .Stringify(); IMobileServiceTable table = service.GetTable(collection); table.UpdateAsync(obj).WaitOrFail (Timeout); Assert.AreEqual("123", obj.Get("other").AsString()); Assert.That (hijack.Request.Uri.ToString(), Contains.Substring (collection)); ThrowsAsync<ArgumentNullException>(() => table.UpdateAsync(null)); ThrowsAsync<ArgumentException>(() => table.UpdateAsync(new JsonObject())); }