public async void ShouldReturnErrorIfAddUserRequestIsUnsuccessful() { const string errorMessage = "This is an error";; const string sampleErrorResponse = "{\"error\": \"" + errorMessage + "\", \"statusCode\": 400}"; var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(sampleErrorResponse) }; httpHandlerMock.Setup(handler => handler.PostAsync(It.IsAny <string>(), It.IsAny <HttpContent>())) .Returns(Task.FromResult(httpResponseMessage)); var userApi = new UserApi(configMock.Object, () => httpHandlerMock.Object); var response = await userApi.AddUserAsync(string.Empty); Assert.Equal(response.Error, errorMessage); Assert.Null(response.Body); Assert.False(response.IsSuccessful()); }
public async void ShouldReturnUserIfAddUserRequestIsSuccessful() { var username = "******"; string sampleSuccessResponseString = "{\"data\": {\"username\": \"" + username + "\"},\"statusCode\": 200}"; var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(sampleSuccessResponseString) }; httpHandlerMock.Setup(handler => handler.PostAsync(It.IsAny <string>(), It.IsAny <HttpContent>())) .Returns(Task.FromResult(httpResponseMessage)); var userApi = new UserApi(configMock.Object, () => httpHandlerMock.Object); var response = await userApi.AddUserAsync(username); Assert.Equal(response.Body, new User() { Username = username }); Assert.Null(response.Error); Assert.True(response.IsSuccessful()); }