public async Task TestGetDrinksErrorResponse() { // Arrange string testURL = "http://test.org:8080/api/"; string getURL = "drinks"; 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<Drink> drinks = await client.GetDrinks(); } 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 TestGetDrinksSuccess() { // Arrange string testURL = "http://test.org:8080/api/"; string getURL = "drinks"; string testAPIKey = "test12345"; HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.OK); DrinksPayload payload = new DrinksPayload(); payload.MetaData = new Meta() { Result = "ok" }; payload.Drinks = new List<Drink>(); payload.Drinks.Add(new Drink()); payload.Drinks.Add(new Drink()); 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<Drink> drinks = await client.GetDrinks(); // Assert Assert.AreEqual(payload.Drinks.Count, drinks.Count); for (int i = 0; i < drinks.Count; i++) { Assert.AreEqual(drinks[i], payload.Drinks[i]); } httpClient.VerifyAll(); }