public void CanAddRangeHeaders()
        {
            var headers = new HttpHeadersAbstraction();

            var rspMsg = new HttpResponseMessage();
            rspMsg.Headers.Add("Test", "Value");

            headers.AddRange(rspMsg.Headers);
            Assert.IsTrue(headers.Contains("Test"));
            Assert.AreEqual("Value", headers["Test"].First());
        }
        public async Task <IHttpResponseAbstraction> SendAsync()
        {
            var requestMessage = new HttpRequestMessage {
                Method = this.Method, RequestUri = this.Uri
            };

            if (this.Method == HttpMethod.Post || this.Method == HttpMethod.Put)
            {
                if (this.Content != null)
                {
                    requestMessage.Content = new StreamContent(this.Content);
                    if (this.ContentType != string.Empty)
                    {
                        requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(this.ContentType);
                    }
                }
            }

            requestMessage.Headers.Clear();
            foreach (var header in this.Headers)
            {
                requestMessage.Headers.Add(header.Key, header.Value);
            }

            var startTime = DateTime.Now;

            try
            {
                var result = await this._client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, this._cancellationToken);

                var headers = new HttpHeadersAbstraction(result.Headers);

                Stream content = null;
                if (result.Content != null)
                {
                    headers.AddRange(result.Content.Headers);
                    content = this.WaitForResult(result.Content.ReadAsStreamAsync(), new TimeSpan(0, 0, 0, 0, int.MaxValue));
                }

                var retval = new HttpResponseAbstraction(content, headers, result.StatusCode);

                //TODO: Add logging code

                return(retval);
            }
            catch (Exception ex)
            {
                //TODO: Add logging code

                var tcex = ex as TaskCanceledException;
                if (tcex == null)
                {
                    throw;
                }

                if (this._cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException("The operation was canceled by user request.", tcex, this._cancellationToken);
                }

                if (DateTime.Now - startTime > this.Timeout)
                {
                    throw new TimeoutException(string.Format(CultureInfo.InvariantCulture, "The task failed to complete in the given timeout period ({0}).", this.Timeout));
                }

                throw;
            }
        }