public void TestDouble(string before, object value, string after)
        {
            Assert.AreEqual((double?)value, InvariantConvert.TryParseDouble(before));

            try
            {
                double parsed = InvariantConvert.ParseDouble(before);
                Assert.AreEqual((double)value, parsed);
            }
            catch (FormatException)
            {
                Assert.IsNull(value);
            }

            if (after != null)
            {
                Assert.AreEqual(after, ((double)value).ToInvariantString());
            }
        }
        public void TestBoolean(string before, object value, string after)
        {
            Assert.AreEqual((bool?)value, InvariantConvert.TryParseBoolean(before));

            try
            {
                bool parsed = InvariantConvert.ParseBoolean(before);
                Assert.AreEqual((bool)value, parsed);
            }
            catch (FormatException)
            {
                Assert.IsNull(value);
            }

            if (after != null)
            {
                Assert.AreEqual(after, ((bool)value).ToInvariantString());
            }
        }
        public void TestInt64(string before, object value, string after)
        {
            Assert.AreEqual((long?)value, InvariantConvert.TryParseInt64(before));

            try
            {
                long parsed = InvariantConvert.ParseInt64(before);
                Assert.AreEqual((long)value, parsed);
            }
            catch (FormatException)
            {
                Assert.IsNull(value);
            }

            if (after != null)
            {
                Assert.AreEqual(after, ((long)value).ToInvariantString());
            }
        }
        public void TestTimeSpan(string before, string after)
        {
            TimeSpan?value = after != null?TimeSpan.Parse(after) : default(TimeSpan?);

            Assert.AreEqual(value, InvariantConvert.TryParseTimeSpan(before));

            try
            {
                TimeSpan parsed = InvariantConvert.ParseTimeSpan(before);
                Assert.AreEqual(value !.Value, parsed);
            }
            catch (FormatException)
            {
                Assert.IsNull(value);
            }

            if (after != null)
            {
                Assert.AreEqual(after, value !.Value.ToInvariantString());
            }
        }
 public void TestObject(object value, string after)
 {
     Assert.AreEqual(after, InvariantConvert.ToInvariantString(value));
 }
        /// <summary>
        /// Overrides HandleResponseCore.
        /// </summary>
        protected override async Task <bool> HandleResponseCoreAsync(WebServiceResponseHandlerInfo <TResponse> info)
        {
            TResponse           response     = CreateResponse();
            Type                responseType = response.GetType();
            HttpResponseMessage webResponse  = info.WebResponse;

            // find specific status code property
            bool           isStatusCodeHandled = false;
            HttpStatusCode statusCode          = webResponse.StatusCode;
            string         statusCodeText      = statusCode.ToString();
            PropertyInfo   resultProperty      = GetProperty(responseType, statusCodeText);
            object         content             = null;

            if (resultProperty != null && resultProperty.CanWrite)
            {
                Type resultPropertyType = resultProperty.PropertyType;
                content = await ReadContentAsAsync(info, resultPropertyType).ConfigureAwait(false);

                resultProperty.SetValue(response, content, null);
                isStatusCodeHandled = true;
            }

            PropertyInfo statusCodeProperty = GetProperty(responseType, "StatusCode");

            if (statusCodeProperty != null && statusCodeProperty.CanWrite)
            {
                Type statusCodePropertyType = statusCodeProperty.PropertyType;
                if (statusCodePropertyType == typeof(HttpStatusCode))
                {
                    statusCodeProperty.SetValue(response, webResponse.StatusCode, null);
                }
                else if (statusCodePropertyType == typeof(int))
                {
                    statusCodeProperty.SetValue(response, (int)webResponse.StatusCode, null);
                }
                else
                {
                    throw await CreateExceptionAsync(info, "Web response status code cannot be read as {0}.".FormatInvariant(statusCodePropertyType));
                }

                isStatusCodeHandled = true;
            }

            // make sure status code is handled
            if (!isStatusCodeHandled)
            {
                throw await CreateExceptionAsync(info, "Status code not handled.").ConfigureAwait(false);
            }

            // read headers
            foreach (var header in webResponse.Headers)
            {
                string headerName = header.Key;
                // remove hyphens before looking for property setter by name
                string       propertyName   = headerName.Replace("-", "");
                PropertyInfo headerProperty = GetProperty(responseType, propertyName);
                if (headerProperty != null && headerProperty.CanWrite)
                {
                    // get header text
                    string headerText = header.Value.Join("; ");

                    // convert header text to supported types
                    Type headerPropertyType = headerProperty.PropertyType;
                    if (headerPropertyType == typeof(string))
                    {
                        headerProperty.SetValue(response, headerText, null);
                    }
                    else if (headerPropertyType == typeof(int) || headerPropertyType == typeof(int?))
                    {
                        headerProperty.SetValue(response, InvariantConvert.ParseInt32(headerText), null);
                    }
                    else if (headerPropertyType == typeof(long) || headerPropertyType == typeof(long?))
                    {
                        headerProperty.SetValue(response, InvariantConvert.ParseInt64(headerText), null);
                    }
                    else if (headerPropertyType == typeof(Uri))
                    {
                        headerProperty.SetValue(response, new Uri(webResponse.RequestMessage.RequestUri, headerText), null);
                    }
                    else if (headerPropertyType == typeof(DateTime) || headerPropertyType == typeof(DateTime?))
                    {
                        headerProperty.SetValue(response, DateTime.ParseExact(headerText, "R", CultureInfo.InvariantCulture), null);
                    }
                    else if (headerPropertyType == typeof(byte[]))
                    {
                        headerProperty.SetValue(response, Convert.FromBase64String(headerText), null);
                    }
                    else
                    {
                        throw await CreateExceptionAsync(info, "Web response header cannot be read as {0}. {1}: {2}".FormatInvariant(headerPropertyType, headerName, headerText)).ConfigureAwait(false);
                    }
                }
            }

            // allow response to read extra data
            AutoWebServiceResponse autoWebServiceResponse = response as AutoWebServiceResponse;

            if (autoWebServiceResponse != null)
            {
                await autoWebServiceResponse.OnResponseHandledAsync(info).ConfigureAwait(false);
            }

            // detach response if necessary
            if (content is WebResponseStream)
            {
                info.DetachWebResponse();
            }

            // success
            info.Response = response;
            return(true);
        }