public static async Task <TryResult <TResult> > TryReadResponse <TResult>(this HttpResponseMessage httpResponse, AhkResult ahkResult)
        {
            var requestMethodAndUrl = $"{httpResponse.RequestMessage.Method.Method.ToUpperInvariant()} {httpResponse.RequestMessage.RequestUri}";

            if (!httpResponse.IsSuccessStatusCode)
            {
                ahkResult.AddProblem($"{requestMethodAndUrl} hibas valaszkod {httpResponse.StatusCode}. {requestMethodAndUrl} yields invalid response {httpResponse.StatusCode}.");
                return(TryResult <TResult> .Failed());
            }

            try
            {
                var value = await httpResponse.Content.ReadAsAsync <TResult>();

                if (value == null)
                {
                    ahkResult.AddProblem($"{requestMethodAndUrl} valasz tartalma hibas. {requestMethodAndUrl} yields invalid content.");
                    return(TryResult <TResult> .Failed());
                }

                return(TryResult <TResult> .Ok(value));
            }
            catch (Exception ex)
            {
                ahkResult.AddProblem(ex, $"{requestMethodAndUrl} valasz tartalma hibas. {requestMethodAndUrl} yields invalid content.");
                return(TryResult <TResult> .Failed());
            }
        }
        public static async Task Execute(AhkResult ahkResult)
        {
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("###### Feladat 1 ###### Exercise 1 ######");

            try
            {
                await testGetWithNotFound(ahkResult);
                await testGetWithSuccess(ahkResult);

                // screenshot is mandatory
                if (!ScreenshotValidator.IsScreenshotPresent("screenshot.png", "screenshot.png", ahkResult))
                {
                    ahkResult.ResetPointToZero();
                }
            }
            catch (MissingMethodException ex) // expected problem, violates contract, solution evaluation ignored
            {
                ahkResult.AddProblem(ex, "Nem megengedett kodot valtoztattal. Changed code that you should not have.");
            }
            catch (TypeLoadException ex) // expected problem, violates contract, solution evaluation ignored
            {
                ahkResult.AddProblem(ex, "Nem megengedett kodot valtoztattal. Changed code that you should not have.");
            }
        }
        public static async Task <TryResult <T> > TryExecuteAndReadResponse <T>(this Task <HttpResponseMessage> send, string httpMethod, string url, AhkResult ahkResult,
                                                                                bool allowNotFound = false, Predicate <HttpResponseMessage> responseAdditionalCheck = null)
        {
            var requestMethodAndUrl = $"{httpMethod.ToUpperInvariant()} {url}";
            HttpResponseMessage responseMessage;

            try
            {
                responseMessage = await send;
            }
            catch (Exception ex)
            {
                ahkResult.AddProblem(ex, $"{requestMethodAndUrl} keres sikertelen. {requestMethodAndUrl} request unsuccessful.");
                return(TryResult <T> .Failed());
            }

            if (responseAdditionalCheck != null)
            {
                if (!responseAdditionalCheck(responseMessage))
                {
                    return(TryResult <T> .Failed());
                }
            }

            if (allowNotFound && responseMessage.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                return(TryResult <T> .Ok(default(T)));
            }

            return(await responseMessage.TryReadResponse <T>(ahkResult));
        }
Esempio n. 4
0
        public static async Task Execute(AhkResult ahkResult)
        {
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("###### Feladat 2 ###### Exercise 2 ######");

            try
            {
                await testDelete(ahkResult);
            }
            catch (MissingMethodException ex) // expected problem, violates contract, solution evaluation ignored
            {
                ahkResult.AddProblem(ex, "Nem megengedett kodot valtoztattal. Changed code that you should not have.");
            }
            catch (TypeLoadException ex) // expected problem, violates contract, solution evaluation ignored
            {
                ahkResult.AddProblem(ex, "Nem megengedett kodot valtoztattal. Changed code that you should not have.");
            }
        }
        public static async Task <TryResult <TResult> > TryPostWithReturnValue <TResult>(this HttpClient httpClient, string url, object value, AhkResult ahkResult, bool requireLocationHeader = false)
        {
            return(await httpClient.PostAsJsonAsync(url, value).TryExecuteAndReadResponse <TResult>("POST", url, ahkResult,
                                                                                                    responseAdditionalCheck: httpResponse =>
            {
                if (requireLocationHeader)
                {
                    if (!httpResponse.Headers.Contains(Microsoft.Net.Http.Headers.HeaderNames.Location) || httpResponse.Headers.Location == null)
                    {
                        ahkResult.AddProblem($"POST {url} valaszban hianyzo header. POST {url} reponse missing header.");
                        return false;
                    }
                }

                return true;
            }));
        }
Esempio n. 6
0
        /// <summary>
        /// DELETE /api/sample
        /// </summary>
        private static async Task testDelete(AhkResult ahkResult)
        {
            using (var scope = WebAppInit.GetRequestScope())
            {
                var deleteOkResponse = await scope.HttpClient.TryDelete($"/api/sample?input=apple", ahkResult);

                if (deleteOkResponse.Success)
                {
                    ahkResult.AddPoints(2);
                    ahkResult.Log("DELETE /api/sample ok");
                }
                else
                {
                    ahkResult.AddProblem("DELETE /api/sample valasz tartalma hibas. DELETE /api/sample yields invalid reponse.");
                }
            }
        }
        public static async Task <TryResult <bool> > TryDelete(this HttpClient httpClient, string url, AhkResult ahkResult)
        {
            var requestMethodAndUrl = $"DELETE {url}";

            try
            {
                var responseMessage = await httpClient.DeleteAsync(url);

                if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK || responseMessage.StatusCode == System.Net.HttpStatusCode.NoContent)
                {
                    return(TryResult <bool> .Ok(true));
                }
                else
                {
                    return(TryResult <bool> .Ok(false));
                }
            }
            catch (Exception ex)
            {
                ahkResult.AddProblem(ex, $"{requestMethodAndUrl} keres sikertelen. {requestMethodAndUrl} request unsuccessful.");
                return(TryResult <bool> .Failed());
            }
        }