public static async Task testStuff() { HttpClientWrapper httpClient = new HttpClientWrapper(new HttpClient()); KegbotHttpRESTClient c = new KegbotHttpRESTClient("http://192.168.1.101:30080/api/", "5b94a95f58e4e371c8ee4c466740e2cc", httpClient); try { User adminUser = await c.GetUser("admin"); List<Drink> adminDrinks = await c.GetUserDrinks("admin"); //List<Drink> allDrinks = await c.GetDrinks(); List<Keg> allKegs = await c.GetKegs(); foreach(Keg k in allKegs) { Keg k1 = await c.GetKeg(k.Id); List<Drink> k1Drinks = await c.GetKegDrinks(k.Id); List<Session> k1Sessions = await c.GetKegSessions(k.Id); } List<KegTap> taps = await c.GetTaps(); foreach(KegTap t in taps) { KegTap k = await c.GetTap(t.MeterName); RecordedDrink rd = await c.RecordDrink(t.MeterName, 0, 568.26, "admin", null, null, null, "test"); Drink d = await c.GetDrink(rd.Id); //bool success = await c.CancelDrink(d.Id, false); } } catch (KegbotAPIException e) { } }
public async Task TestGetTapsErrorResponse() { // Arrange string testURL = "http://test.org:8080/api/"; string getURL = "taps"; string testAPIKey = "test12345"; HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError); ErrorResponse payload = new ErrorResponse(); payload.MetaData = new Meta() { Result = "error" }; payload.Error = new Error() { Code = "ServerError", Message = "Something bad happened!" }; string content = JsonConvert.SerializeObject(payload); Mock<IHttpClient> httpClient = SetupClient(testURL, testAPIKey); responseMessage.Content = new StringContent(content, System.Text.Encoding.UTF8, "application/json"); httpClient.Setup(c => c.GetAsync(It.Is<string>(s => s == getURL))).Returns(Task.FromResult(responseMessage)).Verifiable(); KegbotHttpRESTClient client = new KegbotHttpRESTClient(testURL, testAPIKey, httpClient.Object); // Act try { List<KegTap> kegs = await client.GetTaps(); } catch (KegbotAPIException e) { // Assert Assert.IsNotNull(e.Error); Assert.AreEqual(e.Error.Code.ToString(), payload.Error.Code); Assert.AreEqual(e.Error.Message.ToString(), payload.Error.Message); } httpClient.VerifyAll(); }
public async Task TestGetTapsSuccess() { // Arrange string testURL = "http://test.org:8080/api/"; string getURL = "taps"; string testAPIKey = "test12345"; HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.OK); KegTapsPayload payload = new KegTapsPayload(); payload.MetaData = new Meta() { Result = "ok" }; payload.KegTaps = new List<KegTap>(); payload.KegTaps.Add(new KegTap()); payload.KegTaps.Add(new KegTap()); string content = JsonConvert.SerializeObject(payload); responseMessage.Content = new StringContent(content, System.Text.Encoding.UTF8, "application/json"); Mock<IHttpClient> httpClient = SetupClient(testURL, testAPIKey); httpClient.Setup(c => c.GetAsync(It.Is<string>(s => s == getURL))).Returns(Task.FromResult(responseMessage)).Verifiable(); KegbotHttpRESTClient client = new KegbotHttpRESTClient(testURL, testAPIKey, httpClient.Object); // Act List<KegTap> kegs = await client.GetTaps(); // Assert Assert.AreEqual(payload.KegTaps.Count, kegs.Count); for (int i = 0; i < kegs.Count; i++) { Assert.AreEqual(kegs[i], payload.KegTaps[i]); } httpClient.VerifyAll(); }