Beispiel #1
0
        public void GetObjectFromString_Null_ReturnError()
        {
            Action action = () => ServiceHelpers.GetObjectFromString(null);

            action.Should()
            .Throw <ArgumentNullException>();
        }
Beispiel #2
0
        public void GetStringContent_JObject_ReturnStringContent()
        {
            string str  = "{'Test': 'Test'}";
            var    type = ServiceHelpers.GetObjectFromString(str);

            var result = ServiceHelpers.GetStringContent(type, str);

            result.Headers.ContentType.ToString().Should().Be("application/json; charset=utf-8");
        }
Beispiel #3
0
        public void GetStringContent_XDoc_ReturnStringContent()
        {
            string str  = "<p>Test</p>";
            var    type = ServiceHelpers.GetObjectFromString(str);

            var result = ServiceHelpers.GetStringContent(type, str);

            result.Headers.ContentType.ToString().Should().Be("text/xml; charset=utf-8");
        }
Beispiel #4
0
        public void GetStringContent_String_ReturnStringContent()
        {
            string str  = "test";
            var    type = ServiceHelpers.GetObjectFromString(str);

            var result = ServiceHelpers.GetStringContent(type, str);

            result.Headers.ContentType.ToString().Should().Be("text/plain; charset=utf-8");
            result.Headers.ContentLength.Should().Be(4);
        }
Beispiel #5
0
        public RequestDto TableToRequestDTO(Table table)
        {
            var headers = table.CreateSet <Header>().ToList();

            var requestDto = new RequestDto(headers, variableController);

            if (requestDto.Body.Any())
            {
                //получаем тип контента
                var doc = ServiceHelpers.GetObjectFromString(requestDto.Body.Values.First());
                //получаем StringContent для формирования RequestInfo
                requestDto.Content = ServiceHelpers.GetStringContent(doc, requestDto.Body.Values.First());
            }

            return(requestDto);
        }
Beispiel #6
0
        public void GetObjectFromString_EmptyString_ReturnString(string str)
        {
            var result = ServiceHelpers.GetObjectFromString(str);

            result.GetType().Name.Should().Be("String");
        }
Beispiel #7
0
        public void GetObjectFromString_CorrectString_ReturnXDoc(string str)
        {
            var result = ServiceHelpers.GetObjectFromString(str);

            result.GetType().Name.Should().Be("XDocument");
        }
        public ResponceInfo SendMessage(RequestInfo request)
        {
            var(isValid, results) = Validate.ValidateModel(request);
            if (isValid)
            {
                try
                {
                    Log.Logger().LogInformation("Request: " + Environment.NewLine +
                                                request.Url + Environment.NewLine +
                                                request.Method + Environment.NewLine +
                                                request.Content.ReadAsStringAsync().Result);

                    var resp    = fprovider.SendRequest(request);
                    var content = ServiceHelpers.GetObjectFromString(resp.Result.Content.ReadAsStringAsync().Result);

                    Log.Logger().LogInformation("Responce: " + Environment.NewLine +
                                                resp.Result.StatusCode + Environment.NewLine +
                                                resp.Result.Content.ReadAsStringAsync().Result);

                    return(new ResponceInfo
                    {
                        Headers = resp.Result.Headers,
                        Content = content,
                        Request = request,
                        StatusCode = resp.Result.StatusCode
                    });
                }
                catch (FlurlHttpTimeoutException)
                {
                    Log.Logger().LogError("Request:" + request.Url + Environment.NewLine +
                                          request.Method + Environment.NewLine +
                                          request.Content.Headers.ToString() + Environment.NewLine +
                                          "timed out.");

                    return(new ResponceInfo
                    {
                        Headers = null,
                        Content = new StringContent(string.Empty),
                        StatusCode = System.Net.HttpStatusCode.GatewayTimeout,
                        Request = request
                    });
                }
                catch (Exception ex)
                {
                    if (ex.InnerException is FlurlHttpException fex)
                    {
                        if (fex.Call == null)
                        {
                            Log.Logger().LogError("Request:" + request.Url + Environment.NewLine +
                                                  request.Method + Environment.NewLine +
                                                  request.Content.Headers.ToString() + Environment.NewLine +
                                                  "failed:" + fex.Message);

                            Log.Logger().LogInformation("Responce status code: " + Environment.NewLine +
                                                        System.Net.HttpStatusCode.BadRequest);

                            return(new ResponceInfo
                            {
                                Headers = null,
                                Content = new StringContent(string.Empty),
                                StatusCode = System.Net.HttpStatusCode.BadRequest,
                                Request = request
                            });
                        }
                        else
                        {
                            string fexResult = string.Empty;

                            if (fex.Call.Response != null)
                            {
                                fexResult = fex.Call.Response.ResponseMessage.Content.ReadAsStringAsync().Result;

                                Log.Logger().LogInformation("Responce: " + Environment.NewLine +
                                                            fex.Call.Response.StatusCode + Environment.NewLine +
                                                            fexResult);
                            }
                            else
                            {
                                Log.Logger().LogInformation("Responce is empty. Check request parameters.");

                                Log.Logger().LogError("Request:" + request.Url + Environment.NewLine +
                                                      request.Method + Environment.NewLine +
                                                      request.Content.Headers.ToString() + Environment.NewLine +
                                                      "failed:" + fex.Message);

                                return(null);
                            }

                            Log.Logger().LogError("Request:" + request.Url + Environment.NewLine +
                                                  request.Method + Environment.NewLine +
                                                  request.Content.Headers.ToString() + Environment.NewLine +
                                                  "failed:" + fex.Message);


                            var content = ServiceHelpers.GetObjectFromString(fexResult);

                            return(new ResponceInfo
                            {
                                Headers = fex.Call.Response.ResponseMessage.Headers,
                                Content = content,
                                StatusCode = fex.Call.Response.ResponseMessage.StatusCode,
                                Request = request
                            });
                        }
                    }
                    Log.Logger().LogError("Request:" + request.Url + Environment.NewLine +
                                          request.Method + Environment.NewLine +
                                          request.Content.Headers.ToString() + Environment.NewLine +
                                          "failed:" + ex.Message);

                    Log.Logger().LogInformation("Responce: " + Environment.NewLine +
                                                System.Net.HttpStatusCode.BadRequest);

                    return(new ResponceInfo
                    {
                        Headers = null,
                        Content = new StringContent(string.Empty),
                        StatusCode = System.Net.HttpStatusCode.BadRequest,
                        Request = request
                    });
                }
            }
            else
            {
                Log.Logger().LogInformation($"Request is not valid " + Environment.NewLine +
                                            request.Url + Environment.NewLine +
                                            request.Method + Environment.NewLine +
                                            request.Content.ReadAsStringAsync().Result);

                return(null);
            }
        }