/// <summary> /// При успешном коде ответа, возвращает объект типа <see cref="ResultOf{T}" /> в состоянии 'Success'. /// При ответе <see cref="HttpStatusCode.NotFound"/> возвращает <see cref="ResultOf{T}" /> в состоянии 'Empty'. /// При ответе <see cref="HttpStatusCode.BadRequest"/> возвращает <see cref="ResultOf{T}" /> в состоянии 'Invalid'. /// </summary> /// <exception cref="InvalidOperationException">При неизвестных ответах</exception> /// <typeparam name="T"></typeparam> /// <param name="response"></param> /// <returns>Объект типа <see cref="ResultOf{T}"/>.</returns> public static async Task <ResultOf <T> > HandleAsync <T>(this HttpResponseMessage response) where T : class { if (response.IsSuccessStatusCode) { var dto = await response.Content.ReadAsAsync <T>(formatters); return(ResultOf <T> .CreateSuccess(dto)); } switch (response.StatusCode) { case HttpStatusCode.NotFound: return(ResultOf <T> .CreateEmpty()); case HttpStatusCode.BadRequest: var error = await response.Content.ReadAsStringAsync(); var message = GetBadRequestMessage(error); return(ResultOf <T> .CreateInvalid(message)); default: var exception = await response.Content.ReadAsStringAsync(); throw new InvalidOperationException(exception); } }
public void CreateEmpty_IsOk() { var actual = ResultOf <FromTestClass> .CreateEmpty(); Assert.AreEqual(ResultState.Empty, actual.State); Assert.IsNull(actual.InvalidMessage); Assert.IsNull(actual.Result); }
public void CloneFailedAs_CreateEmpty_IsOk() { var from = ResultOf <FromTestClass> .CreateEmpty(); var actual = from.CloneFailedAs <ToTestClass>(); Assert.AreEqual(ResultState.Empty, actual.State); Assert.IsNull(actual.InvalidMessage); Assert.IsNull(actual.Result); Assert.AreEqual(typeof(ResultOf <ToTestClass>), actual.GetType()); }
public void CloneFailedAs_CreateEmpty_IsOk() { var from = ResultOf <FromTestClass> .CreateEmpty(); var actual = from.CloneFailedAs <ToTestClass>(); Assert.Multiple(() => { Assert.AreEqual(ResultState.Empty, actual.State); Assert.IsNull(actual.InvalidMessage); Assert.IsNull(actual.Result); Assert.That(actual, Is.InstanceOf <ResultOf <ToTestClass> >()); }); }
public void Equals_Empty_IsOk_Test() { // arrange var result1 = ResultOf <FromTestClass> .CreateEmpty(); var result2 = ResultOf <FromTestClass> .CreateEmpty(); // act var actual1 = result1.Equals(result2); var actual2 = result2.Equals(result1); // assert Assert.IsTrue(actual1); Assert.IsTrue(actual2); }