Esempio n. 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);
            }
        }
Esempio n. 2
0
        public void CreateSuccess_IsOk()
        {
            var actual = ResultOf <FromTestClass> .CreateSuccess(new FromTestClass { FromField = "Test!" });

            Assert.AreEqual(ResultState.Success, actual.State);
            Assert.IsNull(actual.InvalidMessage);
            Assert.AreEqual("Test!", actual.Result.FromField);
        }
Esempio n. 3
0
        public void Equals_Success_IsFail_Test()
        {
            // arrange
            var result1 = ResultOf <FromTestClass> .CreateSuccess(new FromTestClass());

            var result2 = ResultOf <FromTestClass> .CreateSuccess(new FromTestClass());

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

            // assert
            Assert.IsFalse(actual1);
            Assert.IsFalse(actual2);
        }
Esempio n. 4
0
        public void Equals_Success_IsOk_Test()
        {
            // arrange
            var obj     = new FromTestClass();
            var result1 = ResultOf <FromTestClass> .CreateSuccess(obj);

            var result2 = ResultOf <FromTestClass> .CreateSuccess(obj);

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

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