public void Abort_Calls_Inner_RequestContext()
 {
     MockRequestContext innerRequestContext = new MockRequestContext();
     HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
     requestContext.Abort();
     Assert.IsTrue(innerRequestContext.AbortCalled, "Abort should have been called on the inner RequestContext.");
 }
        public void BeginReply_Does_Not_Change_Message_State_When_Response_Is_HttpMessage()
        {
            HttpResponseMessage response = new HttpResponseMessage();
            HttpMessage message = new HttpMessage(response);
            response.StatusCode = HttpStatusCode.OK;
            MessageState originalState = message.State;

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.OnReplyReceived = innerMessage =>
            {
                Assert.AreEqual(originalState, innerMessage.State, "HttpMessageEncodingRequestContext.BeginReply should not have changed the message state.");
            };

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            requestContext.BeginReply(message, null, null);
            Assert.IsTrue(innerRequestContext.BeginReplyCalled, "HttpMessageEncodingRequestContext.BeginReply should have called BeginReply on the inner RequestContext.");
        }
        public void BeginReply_Ignores_HttpResponseMessageProperty_If_Contains_HttpRequestMessage()
        {
            HttpRequestMessage request = new HttpRequestMessage();
            HttpMessage message = new HttpMessage(request);

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.OnReplyReceived = innerMessage =>
            {
                Assert.IsNotNull(innerMessage, "HttpMessageEncodingRequestContext.BeginReply should not have returned null.");
                Assert.IsInstanceOfType(innerMessage, typeof(HttpMessage), "HttpMessageEncodingRequestContext.BeginReply should have returned an HttpMessage instance.");
                Assert.AreSame(message, innerMessage, "HttpMessageEncodingRequestContext.BeginReply should have provided the same message instance as the HttpMessageEncodingRequestContext received.");

                HttpResponseMessageProperty innerProperty = innerMessage.Properties[HttpResponseMessageProperty.Name] as HttpResponseMessageProperty;
                Assert.IsNotNull(innerProperty, "The inner HttpMessage instance should have had an HttpResponseMessageProperty.");

                Assert.AreEqual(HttpStatusCode.InternalServerError, innerProperty.StatusCode, "HttpResponseMessageProperty.StatusCode should have been HttpStatusCode.InternalServerError.");
                Assert.IsTrue(innerProperty.SuppressEntityBody, "HttpResponseMessageProperty.SuppressEntityBody should have been 'true'.");
                Assert.AreEqual(0, innerProperty.Headers.Count, "HttpResponseMessageProperty.Header.Count should have been zero.");
            };

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            requestContext.BeginReply(message, null, null);
            Assert.IsTrue(innerRequestContext.BeginReplyCalled, "HttpMessageEncodingRequestContext.BeginReply should have called BeginReply on the inner RequestContext.");
        }
        public void RequestMessage_Returns_HttpMessage_With_Only_To_Header()
        {
            HttpRequestMessage innerRequest = new HttpRequestMessage();
            innerRequest.Content = new ByteArrayContent(new byte[] { 0, 1, 2, 3, 4 });
            Message innerMessage = new HttpMessage(innerRequest);
            innerMessage.Headers.To = new Uri("http://someHost.org/someService");
            HttpRequestMessageProperty property = new HttpRequestMessageProperty();
            innerMessage.Properties.Add(HttpRequestMessageProperty.Name, property);

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            Message message = requestContext.RequestMessage;
            HttpRequestMessage request = message.ToHttpRequestMessage();

            Assert.AreEqual(1, message.Headers.Count, "HttpMessage.Headers.Count should have been 1.");
            Assert.AreEqual(new Uri("http://someHost.org/someService"), message.Headers.To, "HttpMessage.Headers.To should have been 'http://someHost.org/someService'.");
        }
        public void RequestMessage_Returns_HttpMessage_With_Empty_Content_When_Inner_Request_Is_Not_HttpMessage()
        {
            Message innerMessage = Message.CreateMessage(MessageVersion.None, string.Empty);
            innerMessage.Headers.To = new Uri("http://someHost.org/someService");
            innerMessage.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty());

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            Message message = requestContext.RequestMessage;
            HttpRequestMessage request = message.ToHttpRequestMessage();

            Assert.IsNotNull(request.Content, "HttpRequestMessage.Content should not have been null.");
            Assert.AreEqual(0, request.Content.Headers.ContentLength, "HttpRequestMessage.Content length should have been zero.");
        }
        public void RequestMessage_Returns_HttpMessage_With_HttpRequestHeaders_Only_From_The_HttpRequestMessageProperty()
        {
            HttpRequestMessage innerRequest = new HttpRequestMessage();
            innerRequest.Content = new ByteArrayContent(new byte[] { 0, 1, 2, 3, 4 });
            innerRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("notThisType/notThisSubType");
            Message innerMessage = new HttpMessage(innerRequest);
            innerMessage.Headers.To = new Uri("http://someHost.org/someService");
            HttpRequestMessageProperty property = new HttpRequestMessageProperty();
            property.Headers.Add(HttpRequestHeader.UserAgent, "SomeUserAgent");
            property.Headers.Add(HttpRequestHeader.ContentType, "someType/someSubType");
            innerMessage.Properties.Add(HttpRequestMessageProperty.Name, property);

            MessageState originalState = innerMessage.State;

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            Message message = requestContext.RequestMessage;
            HttpRequestMessage request = message.ToHttpRequestMessage();

            Assert.AreEqual(1, request.Headers.Count(), "HttpRequestMessage.Headers.Count should have been one.");
            Assert.AreEqual(1, request.Content.Headers.Count(), "Content.Headers.Count should have been one");
            Assert.AreEqual("SomeUserAgent", request.Headers.UserAgent.First().Product.ToString(), "The user agent header should have been 'SomeUserAgent'.");
            Assert.AreEqual("someType/someSubType", request.Content.Headers.ContentType.MediaType, "The content type header should have been 'someType/someSubType'.");
        }
        public void BeginReply_Ignores_HttpResponseMessageProperty_When_Response_Is_Not_HttpMessage()
        {
            Message message = Message.CreateMessage(MessageVersion.None, string.Empty, "some content");
            HttpResponseMessageProperty property = new HttpResponseMessageProperty();
            property.StatusCode = HttpStatusCode.OK;
            property.SuppressEntityBody = false;
            property.Headers.Add(HttpResponseHeader.ContentType, "someType/someSubType");
            message.Properties.Add(HttpResponseMessageProperty.Name, property);

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.OnReplyReceived = innerMessage =>
            {
                HttpResponseMessageProperty innerProperty = innerMessage.Properties[HttpResponseMessageProperty.Name] as HttpResponseMessageProperty;
                Assert.IsNotNull(innerProperty, "The inner HttpMessage instance should have had an HttpResponseMessageProperty.");
                Assert.AreNotSame(property, innerProperty, "The inner HttpResponseMessageProperty should have been a different instance from the one on the response message.");

                Assert.AreEqual(HttpStatusCode.InternalServerError, innerProperty.StatusCode, "HttpResponseMessageProperty.StatusCode should have been HttpStatusCode.InternalServerError.");
                Assert.IsTrue(innerProperty.SuppressEntityBody, "HttpResponseMessageProperty.SuppressEntityBody should have been 'true'.");
                Assert.AreEqual(0, innerProperty.Headers.Count, "HttpResponseMessageProperty.Header.Count should have been zero.");
            };

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            requestContext.BeginReply(message, null, null);
            Assert.IsTrue(innerRequestContext.BeginReplyCalled, "HttpMessageEncodingRequestContext.BeginReply should have called BeginReply on the inner RequestContext.");
        }
 public void Close_With_TimeOut_Calls_Inner_RequestContext()
 {
     MockRequestContext innerRequestContext = new MockRequestContext();
     HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
     TimeSpan timeout = new TimeSpan(0, 1, 0);
     requestContext.Close(timeout);
     Assert.IsTrue(innerRequestContext.CloseCalled, "Close should have been called on the inner RequestContext.");
     Assert.AreEqual(timeout, innerRequestContext.Timeout, "HttpMessageEncodingRequestContext.Close should have passed along the timeout instance to the inner RequestContext.");
 }
        public void RequestMessage_Returns_Same_HttpMessage_Sans_HttpRequestMessageProperty_Instance_When_Inner_Request_Is_HttpMessage()
        {
            HttpRequestMessage innerRequest = new HttpRequestMessage();
            Message innerMessage = new HttpMessage(innerRequest);
            innerMessage.Headers.To = new Uri("http://someHost.org/someService");
            innerMessage.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty());

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            Message message = requestContext.RequestMessage;

            Assert.IsFalse(message.Properties.Keys.Contains(HttpResponseMessageProperty.Name), "The HttpMessage instance should not have had an HttpRequestMessageProperty.");
        }
        public void RequestMessage_Throws_If_HttpRequestMessageProperty_Not_Present()
        {
            Message innerMessage = Message.CreateMessage(MessageVersion.None, string.Empty);
            innerMessage.Headers.To = new Uri("http://someHost.org/someService");

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);

            ExceptionAssert.Throws(
                typeof(InvalidOperationException),
                "HttpMessageEncodingRequestContext.RequestMessage should have thrown since there was no HttpRequestMessageProperty.",
                () =>
                {
                    Message message = requestContext.RequestMessage;
                });
        }
 public void Reply_With_TimeOut_Calls_Inner_RequestContext()
 {
     Message message = Message.CreateMessage(MessageVersion.None, string.Empty);
     MockRequestContext innerRequestContext = new MockRequestContext();
     HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
     TimeSpan timeout = new TimeSpan(0, 1, 0);
     requestContext.Reply(message, timeout);
     Assert.IsTrue(innerRequestContext.ReplyCalled, "Reply should have been called on the inner RequestContext.");
     Assert.AreEqual(timeout, innerRequestContext.Timeout, "HttpMessageEncodingRequestContext.Reply should have passed along the timeout instance to the inner RequestContext.");
 }
        public void RequestMessage_Does_Not_Change_The_Message_State_When_Inner_Request_Is_HttpMessage()
        {
            HttpRequestMessage innerRequest = new HttpRequestMessage();
            Message innerMessage = new HttpMessage(innerRequest);
            innerMessage.Headers.To = new Uri("http://someHost.org/someService");
            innerMessage.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty());

            MessageState originalState = innerMessage.State;

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            Message message = requestContext.RequestMessage;

            Assert.AreEqual(originalState, message.State, "The state of the message should not have been changed by the HttpMessageEncodingRequestContext.");
        }
        public void Reply_Sets_SuppressEntityBody_When_HttpContent_Of_HttpMessage_Is_Null()
        {
            HttpResponseMessage response = new HttpResponseMessage();
            response.Content = null;
            response.StatusCode = HttpStatusCode.OK;
            HttpMessage message = new HttpMessage(response);

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.OnReplyReceived = innerMessage =>
            {
                HttpResponseMessageProperty innerProperty = innerMessage.Properties[HttpResponseMessageProperty.Name] as HttpResponseMessageProperty;
                Assert.IsTrue(innerProperty.SuppressEntityBody, "HttpResponseMessageProperty.SuppressEntityBody should have been 'true'.");
            };

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            requestContext.Reply(message);
            Assert.IsTrue(innerRequestContext.ReplyCalled, "HttpMessageEncodingRequestContext.Reply should have called Reply on the inner RequestContext.");
        }
        public void Reply_Throws_If__Message_Is_Closed()
        {
            HttpResponseMessage response = new HttpResponseMessage();
            HttpMessage message = new HttpMessage(response);
            message.Close();

            MockRequestContext innerRequestContext = new MockRequestContext();
            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);

            ExceptionAssert.Throws(
                typeof(ObjectDisposedException),
                "HttpMessageEncodingRequestContext.Reply should have thrown since the message is disposed.",
                () =>
                {
                    requestContext.Reply(message);
                });
        }
        public void Reply_Returns_Same_HttpMessage_Instance_When_Response_Is_HttpMessage()
        {
            HttpResponseMessage response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.OK;
            HttpMessage message = new HttpMessage(response);
            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.OnReplyReceived = innerMessage =>
            {
                Assert.IsNotNull(innerMessage, "HttpMessageEncodingRequestContext.Reply should not have returned null.");
                Assert.IsInstanceOfType(innerMessage, typeof(HttpMessage), "HttpMessageEncodingRequestContext.Reply should have returned an HttpMessage instance.");
                Assert.AreSame(message, innerMessage, "HttpMessageEncodingRequestContext.Reply should have provided the same message instance as the HttpMessageEncodingRequestContext received.");

                HttpResponseMessage innerResponse = innerMessage.ToHttpResponseMessage();
                Assert.IsNotNull(innerResponse, "ToHttpResponseMessage should not have returned null.");
                Assert.AreSame(response, innerResponse, "HttpMessageEncodingRequestContext.Reply should have provided the same HttpResponseMessage instance as the HttpMessageEncodingRequestContext received.");
            };

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            requestContext.Reply(message);
            Assert.IsTrue(innerRequestContext.ReplyCalled, "HttpMessageEncodingRequestContext.Reply should have called Reply on the inner RequestContext.");
        }
        public void Reply_Replaces_HttpResponseMessageProperty_When_Response_Is_HttpMessage()
        {
            HttpResponseMessage response = new HttpResponseMessage();
            response.Content = new ByteArrayContent(new byte[5] { 1, 2, 3, 4, 5 });
            response.StatusCode = HttpStatusCode.Moved;
            response.Headers.Add("SomeHeader", "SomeHeaderValue");
            HttpMessage message = new HttpMessage(response);

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.OnReplyReceived = innerMessage =>
            {
                HttpResponseMessageProperty innerProperty = innerMessage.Properties[HttpResponseMessageProperty.Name] as HttpResponseMessageProperty;
                Assert.IsNotNull(innerProperty, "The inner HttpMessage instance should have had an HttpResponseMessageProperty.");

                Assert.AreEqual(HttpStatusCode.Moved, innerProperty.StatusCode, "HttpResponseMessageProperty.StatusCode should have been HttpStatusCode.Moved.");
                Assert.IsFalse(innerProperty.SuppressEntityBody, "HttpResponseMessageProperty.SuppressEntityBody should have been 'false'.");
                Assert.AreEqual(2, innerProperty.Headers.Count, "HttpResponseMessageProperty.Header.Count should have been 2.");
                Assert.AreEqual("SomeHeaderValue", innerProperty.Headers["SomeHeader"], "HttpResponseMessageProperty.Header 'SomeHeader' value should have been 'SomeHeaderValue'.");
            };

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            requestContext.Reply(message);
            Assert.IsTrue(innerRequestContext.ReplyCalled, "HttpMessageEncodingRequestContext.Reply should have called Reply on the inner RequestContext.");
        }
        public void Reply_Provides_Same_Message_Instance_To_Inner_RequestContext_When_Response_Is_Not_HttpMessage()
        {
            Message message = Message.CreateMessage(MessageVersion.None, string.Empty);
            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.OnReplyReceived = innerMessage =>
            {
                Assert.IsNotNull(innerMessage, "HttpMessageEncodingRequestContext.Reply should not have provided null.");
                Assert.AreSame(message, innerMessage, "HttpMessageEncodingRequestContext.Reply should have provided the same message instance.");
            };

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            requestContext.Reply(message);
            Assert.IsTrue(innerRequestContext.ReplyCalled, "HttpMessageEncodingRequestContext.Reply should have called Reply on the inner RequestContext.");
        }
        public void RequestMessage_Returns_Same_HttpContent_Instance_When_Inner_Request_Is_HttpMessage()
        {
            HttpRequestMessage innerRequest = new HttpRequestMessage();
            innerRequest.Content = new ByteArrayContent(new byte[5] { 1, 2, 3, 4, 5 });
            Message innerMessage = new HttpMessage(innerRequest);
            innerMessage.Headers.To = new Uri("http://someHost.org/someService");
            innerMessage.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty());

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            Message message = requestContext.RequestMessage;
            HttpRequestMessage request = message.ToHttpRequestMessage();

            Assert.AreSame(innerRequest.Content, request.Content, "The HttpRequestMessage.Content should not have been changed by the HttpMessageEncodingRequestContext.");
        }
        public void RequestMessage_Method_Determined_By_HttpRequestMessageProperty()
        {
            HttpRequestMessage innerRequest = new HttpRequestMessage();
            innerRequest.Method = HttpMethod.Get;
            Message innerMessage = new HttpMessage(innerRequest);
            HttpRequestMessageProperty property = new HttpRequestMessageProperty();
            property.Method = "PUT";
            innerMessage.Properties.Add(HttpRequestMessageProperty.Name, property);
            innerMessage.Headers.To = new Uri("http://thisUri.org");

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            Message message = requestContext.RequestMessage;

            HttpRequestMessage request = message.ToHttpRequestMessage();
            Assert.AreEqual("PUT", request.Method.Method, "HttpRequestMessage.Method should have been set to the the value of the HttpRequestMessageProperty.Method.");
        }
        public void RequestMessage_Returns_Same_HttpMessage_Instance_When_Inner_Request_Is_HttpMessage()
        {
            HttpRequestMessage innerRequest = new HttpRequestMessage();
            Message innerMessage = new HttpMessage(innerRequest);
            innerMessage.Headers.To = new Uri("http://someHost.org/someService");
            innerMessage.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty());

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            Message message = requestContext.RequestMessage;
            HttpRequestMessage request = message.ToHttpRequestMessage();

            Assert.IsNotNull(message, "HttpMessageEncodingRequestContext.RequestMessage should not have returned null.");
            Assert.IsInstanceOfType(message, typeof(HttpMessage), "HttpMessageEncodingRequestContext.RequestMessage should not returned an HttpMessage instance.");
            Assert.AreSame(innerMessage, message, "HttpMessageEncodingRequestContext.RequestMessage should have returned the same instance as the inner context's RequestMessage.");
            Assert.IsNotNull(request, "ToHttpRequestMessage should have returned an HttpRequestMessage instance.");
            Assert.AreSame(innerRequest, request, "HttpMessageEncodingRequestContext.RequestMessage should have returned the same HttpRequestMessage instance as the inner context.");
        }
 public void Close_Calls_Inner_RequestContext()
 {
     MockRequestContext innerRequestContext = new MockRequestContext();
     HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
     requestContext.Close();
     Assert.IsTrue(innerRequestContext.CloseCalled, "Close should have been called on the inner RequestContext.");
 }
        public void RequestMessage_Returns_Same_Instance_Everytime()
        {
            Message innerMessage = Message.CreateMessage(MessageVersion.None, string.Empty);
            innerMessage.Headers.To = new Uri("http://someHost.org/someService");
            innerMessage.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty());

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            Message message = requestContext.RequestMessage;
            Message message2 = requestContext.RequestMessage;

            Assert.AreSame(message, message2, "HttpMessageEncodingRequestContext.RequestMessage should have returned the same instance everytime.");
        }
        public void RequestMessage_Returns_HttpMessage_In_Created_State_When_Inner_Request_Is_Not_HttpMessage()
        {
            Message innerMessage = Message.CreateMessage(MessageVersion.None, string.Empty);
            innerMessage.Headers.To = new Uri("http://someHost.org/someService");
            innerMessage.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty());

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            Message message = requestContext.RequestMessage;

            Assert.AreEqual(MessageState.Created, message.State, "HttpMessage.State should have been MessageState.Created.");
        }
        public void RequestMessage_Throws_If_To_Header_Not_Present()
        {
            Message innerMessage = Message.CreateMessage(MessageVersion.None, string.Empty);
            innerMessage.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty());

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);

            ExceptionAssert.Throws(
                typeof(InvalidOperationException),
                "HttpMessageEncodingRequestContext.RequestMessage should have thrown since there was no To header.",
                () =>
                {
                    Message message = requestContext.RequestMessage;
                });
        }
        public void RequestMessage_Returns_HttpMessage_When_Inner_Request_Is_Not_HttpMessage()
        {
            Message innerMessage = Message.CreateMessage(MessageVersion.None, string.Empty);
            innerMessage.Headers.To = new Uri("http://someHost.org/someService");
            innerMessage.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty());

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            Message message = requestContext.RequestMessage;
            HttpRequestMessage request = message.ToHttpRequestMessage();

            Assert.IsNotNull(message, "HttpMessageEncodingRequestContext.RequestMessage should not have returned null.");
            Assert.IsInstanceOfType(message, typeof(HttpMessage), "HttpMessageEncodingRequestContext.RequestMessage should not returned an HttpMessage instance.");
            Assert.IsNotNull(request, "ToHttpRequestMessage should have returned an HttpRequestMessage instance.");
        }
        public void RequestMessage_Uri_Determined_By_To_Header()
        {
            HttpRequestMessage innerRequest = new HttpRequestMessage();
            innerRequest.RequestUri = new Uri("http://notThisUri.org");
            Message innerMessage = new HttpMessage(innerRequest);
            HttpRequestMessageProperty property = new HttpRequestMessageProperty();
            innerMessage.Properties.Add(HttpRequestMessageProperty.Name, property);
            innerMessage.Headers.To = new Uri("http://thisUri.org");

            MockRequestContext innerRequestContext = new MockRequestContext();
            innerRequestContext.SetRequestMessage(innerMessage);

            HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
            Message message = requestContext.RequestMessage;

            HttpRequestMessage request = message.ToHttpRequestMessage();
            Assert.AreEqual(new Uri("http://thisUri.org").ToString(), request.RequestUri.ToString(), "HttpRequestMessage.Uri should have been set to the 'To' header value of the inner RequestContext message.");
        }
 public void EndReply_Calls_Inner_RequestContext()
 {
     MockRequestContext innerRequestContext = new MockRequestContext();
     HttpMessageEncodingRequestContext requestContext = new HttpMessageEncodingRequestContext(innerRequestContext);
     requestContext.EndReply(null);
     Assert.IsTrue(innerRequestContext.EndReplyCalled, "EndReply should have been called on the inner RequestContext.");
 }