public async Task SendHttpRequest()
        {

            // Arrange
            var httpRequestMessage = new HttpRequestMessage() {
                RequestUri = new Uri("http://example.com/foo")
            };

            var httpMessage = new HttpMessage()
            {
                IsRequest = true,
                HttpRequestMessage = httpRequestMessage
            };

            var consoleLogger = new ConsoleLogger();
            var fakeMessageHandler = new FakeMessageHandler();
            var httpClient = new HttpClient(fakeMessageHandler);
            httpClient.BaseAddress = new Uri("http://api.runscope.com/");

            var message = new RunscopeHttpMessageProcessor(httpClient,consoleLogger);

            // Act
            await message.ProcessHttpMessage(httpMessage);

            // Assert
            Assert.NotNull(fakeMessageHandler.LastResponseMessage);
            Assert.Equal("api.runscope.com", fakeMessageHandler.LastResponseMessage.RequestMessage.RequestUri.Host);
        }
        public async Task ProcessHttpMessage(HttpMessage message)
        {
            var runscopeMessage = new RunscopeMessage()
            {
                UniqueIdentifier = message.MessageId
            };

            if (message.IsRequest)
            {
                _Logger.LogInfo("Processing HTTP request " + message.MessageId.ToString());
                runscopeMessage.Request = await RunscopeRequest.CreateFromAsync(message.HttpRequestMessage);
            }
            else
            {
                _Logger.LogInfo("Processing HTTP response " + message.MessageId.ToString());
                runscopeMessage.Response = await RunscopeResponse.CreateFromAsync(message.HttpResponseMessage);
            }

            var messagesLink = new MessagesLink() { Method = HttpMethod.Post };
            messagesLink.BucketKey = _BucketKey;
            messagesLink.RunscopeMessage = runscopeMessage;
            var runscopeResponse = await _HttpClient.SendAsync(messagesLink.CreateRequest());
            if (runscopeResponse.IsSuccessStatusCode)
            {
                _Logger.LogDebug("Message forwarded to Runscope");
            } else
            {
                _Logger.LogDebug("Failed to send request");
            }
        }
        public static HttpMessage Parse(string data)
        {
            var httpMessage = new HttpMessage();

            HttpContent content;
            using (var sr = new StringReader(data))
            {
                // First line of data is (request|response) followed by a GUID to link request to response
                // Rest of data is in message/http format

                var firstLine = sr.ReadLine().Split(':');
                if (firstLine.Length < 2)
                {
                    throw new ArgumentException("Invalid formatted event :" + data);
                }
                httpMessage.IsRequest = firstLine[0] == "request";
                httpMessage.MessageId = Guid.Parse(firstLine[1]);

                var stream = new MemoryStream(Encoding.UTF8.GetBytes(sr.ReadToEnd()));
                stream.Position = 0;
                content = new StreamContent(stream);
            }

            var contentType = new MediaTypeHeaderValue("application/http");
            content.Headers.ContentType = contentType;

            if (httpMessage.IsRequest)
            {
                contentType.Parameters.Add(new NameValueHeaderValue("msgtype", "request"));

                // Using .Result isn't too evil because content is a locally buffered memory stream
                // Although if this were hosted in a System.Web based ASP.NET host it might block
                httpMessage.HttpRequestMessage = content.ReadAsHttpRequestMessageAsync().Result;
            }
            else
            {
                contentType.Parameters.Add(new NameValueHeaderValue("msgtype", "response"));
                httpMessage.HttpResponseMessage = content.ReadAsHttpResponseMessageAsync().Result;
            }
            return httpMessage;
        }