public void CanCorrectlyDeserializeASerializedException()
        {
            // arrange
            var exception = new ApiCommunicationException("Message", HttpStatusCode.Ambiguous,
                                                          new Uri("http://localhost:9000"), null, new[] { new Error()
                                                                                                          {
                                                                                                              AdditionalDetails = "AdditionalDetails"
                                                                                                          } }, "Information", Guid.NewGuid().ToString());

            // act
            var formatter = new BinaryFormatter();
            ApiCommunicationException deserialized;

            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, exception);
                stream.Seek(0, SeekOrigin.Begin);
                deserialized = (ApiCommunicationException)formatter.Deserialize(stream);
            }

            // assert
            Assert.AreNotEqual(exception, deserialized);
            Assert.AreEqual(exception.StatusCode, deserialized.StatusCode);
            Assert.AreEqual(exception.ErrorInformation, deserialized.ErrorInformation);
            Assert.AreEqual(exception.URI, deserialized.URI);
            Assert.AreEqual(exception.RequestId, deserialized.RequestId);
            Assert.AreEqual(exception.Errors.Count, deserialized.Errors.Count);
            Assert.AreEqual(exception.Errors[0].AdditionalDetails, deserialized.Errors[0].AdditionalDetails);
        }
Example #2
0
        public virtual IDictionary <string, object> Execute(OperationConfig config, OperationMetadata metadata, BaseObject requestMap)
        {
            RestyRequest            request;
            CryptographyInterceptor interceptor;
            IRestClient             restClient;

            try {
                request     = this.GetRequest(config, metadata, requestMap);
                interceptor = request.interceptor;
                if (this.restClient != null)
                {
                    restClient         = this.restClient;
                    restClient.BaseUrl = request.BaseUrl;
                }
                else
                {
                    restClient = new RestClient(request.BaseUrl);
                }
            } catch (Exception ex) {
                throw new ApiException(ex.Message, ex);
            }
            IRestResponse restResponse;

            try {
                ApiController.log.Debug(string.Concat(new object[]
                {
                    ">>Execute(action='",
                    config.Action,
                    "', resourcePaht='",
                    config.ResourcePath,
                    "', requestMap='",
                    requestMap,
                    "'"
                }));
                ApiController.log.Debug("excute(), request.Method='" + request.Method + "'");
                ApiController.log.Debug("excute(), request.URL=" + request.AbsoluteUrl.ToString());
                ApiController.log.Debug("excute(), request.Header=");
                ApiController.log.Debug(request.Parameters.Where(x => x.Type == ParameterType.HttpHeader));
                ApiController.log.Debug("excute(), request.Body=");
                ApiController.log.Debug(request.Parameters.Where(x => x.Type == ParameterType.RequestBody));


                restClient.UseSynchronizationContext = true;
#if NET461
                System.Net.ServicePointManager.Expect100Continue       = false;
                System.Net.ServicePointManager.MaxServicePointIdleTime = 5000;
                //System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;
                restResponse = restClient.Execute(request);
#elif NETSTANDARD1_6
                restResponse = AsyncHelpers.RunSync(async() => {
                    return(await restClient.ExecuteRequestAsync(request));
                },
                                                    () => {
                    this._apiConfig?.getDoEvents()?.Invoke();
                    return(Task.FromResult(0));
                });
#endif
                ApiController.log.Debug("Execute(), response.Header=");
                ApiController.log.Debug(restResponse.Headers);
                ApiController.log.Debug("Execute(), response.Body=");
                ApiController.log.Debug(restResponse.Content.ToString());
            } catch (Exception ex3) {
                Exception ex2 = new ApiCommunicationException(ex3.Message, ex3);
                ApiController.log.Error(ex2.Message, ex2);
                throw ex2;
            }
            if (restResponse.ErrorException != null || restResponse.Content == null)
            {
                Exception ex4 = new MasterCard.Core.Exceptions.SystemException(restResponse.ErrorMessage, restResponse.ErrorException);
                ApiController.log.Error(ex4.Message, ex4);
                throw ex4;
            }
            IDictionary <string, object> dictionary = null;
            if (restResponse.Content.StartsWith("{") || restResponse.Content.StartsWith("[") || restResponse.ContentType == "application/json")
            {
                try {
                    dictionary = RequestMap.AsDictionary(restResponse.Content);
                    if (interceptor != null)
                    {
                        dictionary = interceptor.Encrypt(dictionary);
                    }
                } catch (Exception) {
                    throw new MasterCard.Core.Exceptions.SystemException("Error: parsing JSON response", restResponse.Content);
                }
            }
            else if (restResponse.Content.StartsWith("<") || restResponse.ContentType == "application/xml")
            {
                try {
                    dictionary = RequestMap.AsDictionaryFromXml(restResponse.Content);
                    if (interceptor != null)
                    {
                        dictionary = interceptor.Encrypt(dictionary);
                    }
                } catch (Exception) {
                    throw new MasterCard.Core.Exceptions.SystemException("Error: parsing XML response", restResponse.Content);
                }
            }
            if (restResponse.StatusCode < HttpStatusCode.MultipleChoices)
            {
                ApiController.log.Debug("<<Execute()");
                return(dictionary);
            }
            try {
                ApiController.ThrowException(dictionary, restResponse);
            } catch (Exception ex5) {
                ApiController.log.Error(ex5.Message, ex5);
                throw ex5;
            }
            return(null);
        }