public async Task TestRecordDrinkErrorResponse() { // Arrange string testURL = "http://test.org:8080/api/"; string putURL = "taps/1"; string testAPIKey = "test12345"; string tap = "1"; int ticks = 2; int volume = 3; string username = "******"; int pourTime = 4; int now = 5; int duration = 6; string shout = "test"; 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.PostAsync(It.Is<string>(s => s == putURL),It.IsAny<HttpContent>())).Returns(Task.FromResult(responseMessage)).Verifiable(); KegbotHttpRESTClient client = new KegbotHttpRESTClient(testURL, testAPIKey, httpClient.Object); // Act try { RecordedDrink drink = await client.RecordDrink(tap, ticks, volume, username, pourTime, now, duration, shout); } 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 TestGetTapErrorResponse() { // Arrange string testURL = "http://test.org:8080/api/"; string getURL = "taps/test_tap"; string testAPIKey = "test12345"; string tapName = "test_tap"; HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.NotFound); ErrorResponse payload = new ErrorResponse(); payload.MetaData = new Meta() { Result = "error" }; payload.Error = new Error() { Code = "NotFoundError", Message = "No KegTap matches the given query."}; 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 { KegTap k = await client.GetTap(tapName); } 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 TestGetUserDrinksErrorResponse() { // Arrange string testURL = "http://test.org:8080/api/"; string getURL = "users/test_user/drinks"; string testAPIKey = "test12345"; string userName = "******"; 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.GetUserDrinks(userName); } 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(); }