public void GetBodyAttribute_Throws()
        {
            HttpRequestMessage request = new HttpRequestMessage();
            HttpMessage message = new HttpMessage(request);

            ExceptionAssert.Throws(
                typeof(NotSupportedException),
                "HttpMessage.GetBodyAttribute should have thrown because body attributes don't apply.",
                () =>
                {
                    message.GetBodyAttribute("localName", "nameSpace");
                });
        }
        public void CreateBufferedCopy_Always_Returns_An_HttpMessageBuffer()
        {
            HttpRequestMessage request = new HttpRequestMessage();
            HttpMessage message = new HttpMessage(request);

            ExceptionAssert.Throws(
                typeof(NotSupportedException),
                "CreateBufferedCopy should have thrown because copying is not supported.",
                () =>
                {
                    MessageBuffer buffer = message.CreateBufferedCopy(5);
                });
        }
        public void Closing_Disposes_Of_The_HttpResponseMessage()
        {
            HttpResponseMessage response = new HttpResponseMessage();
            response.Content = new ByteArrayContent(new byte[0]);
            HttpMessage message = new HttpMessage(response);
            message.Close();
            Assert.AreEqual(MessageState.Closed, message.State, "HttpMessage.State should be closed.");

            ExceptionAssert.Throws(
                typeof(ObjectDisposedException),
                "HttpRequestMessage.Content.ReadAsByteArray should have thrown because the HttpResponseMessage was disposed.",
                () =>
                {
                    response.Content.ReadAsByteArray();
                });
        }
        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 Properties_Has_Only_AllowOutputBatching_MessageProperty()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     HttpMessage message = new HttpMessage(request);
     Assert.AreEqual(1, message.Properties.Count, "HttpMessage.Properties.Count should be 1.");
     Assert.IsFalse(message.Properties.AllowOutputBatching, "The value of the 'AllowOutputBatching' property should be false.");
 }
 public void MessageVersion_Always_None()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     HttpMessage message = new HttpMessage(request);
     Assert.AreEqual(MessageVersion.None, message.Version, "HttpMessage.Version should always be MessageVersion.None.");
 }
 public void MessageState_Is_Created_After_Creation()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     HttpMessage message = new HttpMessage(request);
     Assert.AreEqual(MessageState.Created, message.State, "HttpMessage.State should be MessageState.Created.");
 }
        public void WriteMessage_Throws_If_MessageOffset_Is_Greater_Than_MaxMessageSize()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            HttpResponseMessage response = new HttpResponseMessage();
            response.Content = new ByteArrayContent(new byte[5] { 0, 1, 2, 3, 4 });
            HttpMessage message = new HttpMessage(response);

            ExceptionAssert.Throws(
                typeof(ArgumentException),
                "HttpMessageEncoder.WriteMessage should have thrown since messageOffset was greater than the maxMessageSize.",
                () =>
                {
                    encoder.WriteMessage(message, 5, new MockBufferManager(), 6);
                });
        }
        public void WriteStartEnvelope_Throws()
        {
            HttpRequestMessage request = new HttpRequestMessage();
            HttpMessage message = new HttpMessage(request);
            MockXmlDictionaryWriter writer = new MockXmlDictionaryWriter();

            ExceptionAssert.Throws(
                typeof(NotSupportedException),
                "HttpMessage.WriteStartEnvelope should have thrown because the message can't be read/written.",
                () =>
                {
                    message.WriteStartEnvelope(writer);
                });
        }
        public void WriteBodyContents_Throws()
        {
            HttpRequestMessage request = new HttpRequestMessage();
            HttpMessage message = new HttpMessage(request);

            ExceptionAssert.Throws(
                typeof(NotSupportedException),
                "HttpMessage.WriteBodyContents should have thrown because the message can't be read/written.",
                () =>
                {
                    message.WriteBodyContents(new MockXmlDictionaryWriter());
                });
        }
 public void ToString_With_A_Response_And_UnSeekable_HttpContent_Indicates_Unknown_Content_Length()
 {
     HttpResponseMessage response = new HttpResponseMessage();
     response.Content = new StreamContent(new MockUnseekableStream(new MemoryStream()));
     HttpMessage message = new HttpMessage(response);
     Assert.AreEqual("HTTP response message body with an undetermined content length.", message.ToString(), "HttpMessage.ToString() should have returned 'HTTP response message body with an undetermined content length.'.");
 }
 public void IsEmpty_Is_True_When_HttpContent_Length_Is_Zero()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     request.Content = new ByteArrayContent(new byte[0]);
     HttpMessage message = new HttpMessage(request);
     Assert.IsTrue(message.IsEmpty, "HttpMessage.IsEmpty should always be 'true' since the byte array was of zero length.");
 }
 public void IsEmpty_Is_True_When_HttpContent_Is_Null()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     HttpMessage message = new HttpMessage(request);
     Assert.IsTrue(message.IsEmpty, "HttpMessage.IsEmpty should be 'true' since there is no content.");
 }
 public void IsEmpty_Is_False_When_HttpContent_Length_Is_Unknown()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     request.Content =  new StreamContent(new MockUnseekableStream(new MemoryStream()));
     HttpMessage message = new HttpMessage(request);
     Assert.IsFalse(message.IsEmpty, "HttpMessage.IsEmpty should always be 'false' since the content length is unknown.");
 }
 public void IsEmpty_Is_False_When_HttpContent_Length_Is_Not_Zero()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     request.Content = new ByteArrayContent(new byte[1] { 0 });
     HttpMessage message = new HttpMessage(request);
     Assert.IsFalse(message.IsEmpty, "HttpMessage.IsEmpty should always be 'false' since the byte array was of length '1'.");
 }
 public void Headers_Is_Never_Null()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     HttpMessage message = new HttpMessage(request);
     Assert.IsNotNull(message.Headers, "HttpMessage.Headers should never be null.");
 }
 public void Headers_Count_Is_Zero_By_Default()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     HttpMessage message = new HttpMessage(request);
     Assert.AreEqual(0, message.Headers.Count, "HttpMessage.Headers.Count should be 0 by default.");
 }
        public void WriteMessage_Throws_If_The_Message_State_Is_Not_Created()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            HttpResponseMessage response = new HttpResponseMessage();
            response.Content = new ByteArrayContent(new byte[5] { 0, 1, 2, 3, 4 });
            HttpMessage message = new HttpMessage(response);
            message.Close();

            ExceptionAssert.Throws(
                typeof(InvalidOperationException),
                "HttpMessageEncoder.WriteMessage should have thrown since the message state was not MessageState.Created.",
                () =>
                    {
                        encoder.WriteMessage(message, 5, new MockBufferManager());
                    });
        }
 public void ToString_With_A_Request_And_UnSeekable_HttpContent_Indicates_Unknown_Content_Length()
 {
     HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
     request.Content = new StreamContent(new MockUnseekableStream(new MemoryStream()));
     HttpMessage message = new HttpMessage(request);
     Assert.AreEqual("HTTP request message body with an undetermined content length.", message.ToString(), "HttpMessage.ToString() should have returned 'HTTP request message body with an undetermined content length.'.");
 }
 public void ToString_With_A_Request_Indicates_Zero_Content_Length()
 {
     HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
     request.Content = new ByteArrayContent(new byte[0]);
     HttpMessage message = new HttpMessage(request);
     Assert.AreEqual("HTTP request message body with a content length of '0' bytes.", message.ToString(), "HttpMessage.ToString() should have returned 'HTTP request message body with a content length of '0' bytes.'.");
 }
 public void IsFault_Is_False()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     HttpMessage message = new HttpMessage(request);
     Assert.IsFalse(message.IsFault, "HttpMessage.IsFault should always be 'false'.");
 }
 public void ToString_With_A_Response_Indicates_Zero_Content_Length()
 {
     HttpResponseMessage response = new HttpResponseMessage();
     response.Content = new ByteArrayContent(new byte[0]);
     HttpMessage message = new HttpMessage(response);
     Assert.AreEqual("HTTP response message body with a content length of '0' bytes.", message.ToString(), "HttpMessage.ToString() should have returned 'HTTP response message body with a content length of '0' bytes.'.");
 }
        public void WriteMessage_Throws_If_MaxMessageSize_Is_Exceeded()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            HttpResponseMessage response = new HttpResponseMessage();
            byte[] bytes = new byte[5] { 0, 1, 2, 3, 4 };
            response.Content = new ByteArrayContent(bytes);
            response.Content.LoadIntoBuffer();
            HttpMessage message = new HttpMessage(response);

            ExceptionAssert.Throws(
                typeof(QuotaExceededException),
                "HttpMessageEncoder.WriteMessage should have thrown because the content byte count was greater than the maxMessageSize parameter.",
                () =>
                {
                    ArraySegment<byte> buffer2 = encoder.WriteMessage(message, 2, new MockBufferManager());
                });
        }
        public void WriteMessage_Throws_If_HttpMessage_IsRequest_Is_Not_False()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
            request.Content = new ByteArrayContent(new byte[5] { 0, 1, 2, 3, 4 });
            HttpMessage message = new HttpMessage(request);

            ExceptionAssert.Throws(
                typeof(InvalidOperationException),
                "HttpMessageEncoder.WriteMessage should have thrown since HttpMessage.IsRequest was not 'false'.",
                () =>
                {
                    encoder.WriteMessage(message, 5, new MockBufferManager());
                });
        }
 public void MessageState_Is_Closed_After_Calling_Closed()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     HttpMessage message = new HttpMessage(request);
     message.Close();
     Assert.AreEqual(MessageState.Closed, message.State, "HttpMessage.State should be MessageState.Closed.");
 }
 public void IsRequest_Is_False_If_HttpResponseMessage_Constructor_Was_Used()
 {
     HttpResponseMessage response = new HttpResponseMessage();
     HttpMessage message = new HttpMessage(response);
     Assert.IsFalse(message.IsRequest, "HttpMessage.IsRequest should be 'false'.");
 }
        public void WriteMessage_Throws_If_MessageOffsert_Parameter_Is_Negative()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            HttpResponseMessage response = new HttpResponseMessage();
            response.Content = new ByteArrayContent(new byte[5] { 0, 1, 2, 3, 4 });
            HttpMessage message = new HttpMessage(response);

            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "HttpMessageEncoder.WriteMessage should have thrown since messageOffset parameter was negative.",
                () =>
                {
                    encoder.WriteMessage(message, 5, new MockBufferManager(), -1);
                });
        }
        public void GetReaderAtBodyContents_Throws()
        {
            HttpRequestMessage request = new HttpRequestMessage();
            request.Content = new ByteArrayContent(new byte[1] { 0 });
            HttpMessage message = new HttpMessage(request);

            ExceptionAssert.Throws(
                typeof(NotSupportedException),
                "HttpMessage.GetReaderAtBodyContents should have thrown because the message can't be read/written.",
                () =>
                {
                    message.GetReaderAtBodyContents();
                });
        }
 public void IsRequest_Is_True_If_HttpRequestMessage_Constructor_Was_Used()
 {
     HttpRequestMessage request = new HttpRequestMessage();
     HttpMessage message = new HttpMessage(request);
     Assert.IsTrue(message.IsRequest, "HttpMessage.IsRequest should be 'true'.");
 }