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 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 BeginReply_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.BeginReply(message, timeout, null, null);
     Assert.IsTrue(innerRequestContext.BeginReplyCalled, "BeginReply should have been called on the inner RequestContext.");
     Assert.AreEqual(timeout, innerRequestContext.Timeout, "HttpMessageEncodingRequestContext.BeginReply should have passed along the timeout instance to the inner RequestContext.");
 }
        public void BeginReply_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.BeginReply(message, null, null);
                });
        }
        public void BeginReply_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.BeginReply(message, null, null);
            Assert.IsTrue(innerRequestContext.BeginReplyCalled, "HttpMessageEncodingRequestContext.BeginReply should have called BeginReply on the inner RequestContext.");
        }
        public void BeginReply_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.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.");

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

            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_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.BeginReply(message, null, null);
            Assert.IsTrue(innerRequestContext.BeginReplyCalled, "HttpMessageEncodingRequestContext.BeginReply should have called BeginReply on the inner RequestContext.");
        }
        public void BeginReply_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.BeginReply(message, null, null);
            Assert.IsTrue(innerRequestContext.BeginReplyCalled, "HttpMessageEncodingRequestContext.BeginReply should have called BeginReply on the inner RequestContext.");
        }