Beispiel #1
0
        /// <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);
            }
        }
Beispiel #2
0
        public void CreateInvalid_IsOk()
        {
            var actual = ResultOf <FromTestClass> .CreateInvalid("Message!");

            Assert.AreEqual(ResultState.Invalid, actual.State);
            Assert.AreEqual("Message!", actual.InvalidMessage);
            Assert.IsNull(actual.Result);
        }
Beispiel #3
0
        public void CloneFailedAs_CreateInvalid_IsOk()
        {
            var from = ResultOf <FromTestClass> .CreateInvalid("Message!");

            var actual = from.CloneFailedAs <ToTestClass>();

            Assert.AreEqual(ResultState.Invalid, actual.State);
            Assert.AreEqual("Message!", actual.InvalidMessage);
            Assert.IsNull(actual.Result);
            Assert.AreEqual(typeof(ResultOf <ToTestClass>), actual.GetType());
        }
        public void CloneFailedAs_CreateInvalid_IsOk()
        {
            var from = ResultOf <FromTestClass> .CreateInvalid("Message!");

            var actual = from.CloneFailedAs <ToTestClass>();

            Assert.Multiple(() =>
            {
                Assert.AreEqual(ResultState.Invalid, actual.State);
                Assert.AreEqual("Message!", actual.InvalidMessage);
                Assert.IsNull(actual.Result);
                Assert.That(actual, Is.InstanceOf <ResultOf <ToTestClass> >());
            });
        }
Beispiel #5
0
        public void Equals_Invalid_IsOk_Test()
        {
            // arrange
            var result1 = ResultOf <FromTestClass> .CreateInvalid("Test");

            var result2 = ResultOf <FromTestClass> .CreateInvalid("Test");

            // act
            var actual1 = result1.Equals(result2);
            var actual2 = result2.Equals(result1);

            // assert
            Assert.IsTrue(actual1);
            Assert.IsTrue(actual2);
        }
        public void Equals_Invalid_IsFail_Test()
        {
            // arrange
            var result1 = ResultOf <FromTestClass> .CreateInvalid("Test1");

            var result2 = ResultOf <FromTestClass> .CreateInvalid("Test2");

            // act
            var actual1 = result1.Equals(result2);
            var actual2 = result2.Equals(result1);

            // assert
            Assert.Multiple(() =>
            {
                Assert.IsFalse(actual1);
                Assert.IsFalse(actual2);
            });
        }