コード例 #1
0
        public void HttpMessageEncoder_Is_Nested_Private_Class()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            Type type = encoder.GetType();
            Assert.IsTrue(type.IsNestedPrivate, "HttpMessageEncoder should be nested and private.");
            Assert.IsFalse(type.IsVisible, "HttpMessageEncoder should not be visible.");
            Assert.IsFalse(type.IsAbstract, "HttpMessageEncoder should not be abstract.");
            Assert.IsTrue(type.IsClass, "HttpMessageEncoder should be a class.");
        }
コード例 #2
0
        public void IsContentTypeSupported_Throws()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            ExceptionAssert.ThrowsArgumentNull(
                "HttpMessageEncoder.IsContentTypeSupported should have throw because the content type parameter was null.",
                "contentType",
                () =>
                {
                    encoder.IsContentTypeSupported(null);
                });
        }
コード例 #3
0
        public void WriteMessage_That_Takes_A_Stream_Writes_Nothing_If_Message_Content_Is_Null()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            HttpResponseMessage response = new HttpResponseMessage();
            HttpMessage message = new HttpMessage(response);

            using (MemoryStream stream = new MemoryStream())
            {
                encoder.WriteMessage(message, stream);
                stream.Flush();
                stream.Seek(0, SeekOrigin.Begin);
                Assert.AreEqual(0, stream.Length, "HttpMessageEncoder.WriteMessage should not have written anything to the stream since the content was null.");
            }
        }
コード例 #4
0
        public void ReadMessage_Returns_HttpMessage_With_HttpContent_Null_Content_Type()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            byte[] bytes = new byte[5] { 0, 1, 2, 3, 4 };
            ArraySegment<byte> buffer = new ArraySegment<byte>(bytes);

            HttpMessage message = encoder.ReadMessage(buffer, new MockBufferManager(), null) as HttpMessage;
            HttpRequestMessage request = message.ToHttpRequestMessage();
            Assert.IsNull(request.Content.Headers.ContentType, "HttpMessageEncoder.ReadMessage should have returned an HttpMessage with a null content type.");
        }
コード例 #5
0
        public void WriteMessage_That_Takes_A_Stream_Writes_Message_Content_To_The_Stream()
        {
            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);
            HttpMessage message = new HttpMessage(response);

            using (MemoryStream stream = new MemoryStream())
            {
                encoder.WriteMessage(message, stream);
                stream.Flush();
                stream.Seek(0, SeekOrigin.Begin);
                byte[] writtenBytes = new byte[stream.Length];
                stream.Read(writtenBytes, 0, writtenBytes.Length);
                CollectionAssert.AreEquivalent(bytes, writtenBytes, "HttpMessageEncoder.WriteMessage should have written the content to the stream.");
            }
        }
コード例 #6
0
        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());
                    });
        }
コード例 #7
0
 public void MessageVersion_Is_None()
 {
     HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
     MessageEncoder encoder = factory.Encoder;
     Assert.AreEqual(MessageVersion.None, encoder.MessageVersion, "HttpMessageEncoder.MessageVersion should have returned MessageVersion.None.");
 }
コード例 #8
0
        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());
                });
        }
コード例 #9
0
        public void ReadMessage_Returns_HttpMessage_With_No_Headers()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            byte[] bytes = new byte[5] { 0, 1, 2, 3, 4 };
            ArraySegment<byte> buffer = new ArraySegment<byte>(bytes);

            HttpMessage message = encoder.ReadMessage(buffer, new MockBufferManager(), "someType/someSubType") as HttpMessage;
            Assert.AreEqual(0, message.Headers.Count, "HttpMessageEncoder.ReadMessage should have returned an HttpMessage with no headers.");
        }
コード例 #10
0
        public void ReadMessage_With_Stream_Throws_If_Stream_Parameter_Is_Null()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            ExceptionAssert.ThrowsArgumentNull(
                "HttpMessageEncoder.ReadMessage should have thrown since the stream parameter was null.",
                "stream",
                () =>
                {
                    encoder.ReadMessage(null, 5, "someContentType");
                });
        }
コード例 #11
0
        public void WriteMessage_Returns_An_ArraySegment_With_Size_Of_Content_Byte_Count()
        {
            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);

            ArraySegment<byte> buffer = encoder.WriteMessage(message, int.MaxValue, new MockBufferManager());
            Assert.AreEqual(5, buffer.Count, "HttpMessageEncoder.WriteMessage should have returned an ArraySegment with the size limited by the HttpContent byte count.");
        }
コード例 #12
0
        public void ReadMessage_With_Stream_Returns_HttpMessage()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            byte[] bytes = new byte[5] { 0, 1, 2, 3, 4 };
            using (MemoryStream stream = new MemoryStream(bytes))
            {
                Message message = encoder.ReadMessage(stream, 5, "type/subType");
                Assert.IsNotNull(message, "HttpMessageEncoder.ReadMessage should not have returned null.");
                Assert.IsInstanceOfType(message, typeof(HttpMessage), "HttpMessageEncoder.ReadMessage should have returned an HttpMessage.");
            }
        }
コード例 #13
0
        public void ReadMessage_With_Stream_Returns_HttpMessage_With_IsRequest_True()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            byte[] bytes = new byte[5] { 0, 1, 2, 3, 4 };
            using (MemoryStream stream = new MemoryStream(bytes))
            {
                HttpMessage message = encoder.ReadMessage(stream, 5, "type/subType") as HttpMessage;
                Assert.IsTrue(message.IsRequest, "HttpMessageEncoder.ReadMessage should have returned an HttpMessage with IsRequest set to 'true'.");
            }
        }
コード例 #14
0
        public void ReadMessage_With_Stream_Does_Not_Advance_The_Stream()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            byte[] bytes = new byte[5] { 0, 1, 2, 3, 4 };
            using (MemoryStream stream = new MemoryStream(bytes))
            {
                HttpMessage message = encoder.ReadMessage(stream, 5, "type/subType") as HttpMessage;
                Assert.AreEqual(0, stream.Position, "HttpMessageEncoder.ReadMessage should not have advanced the stream parameter.");
            }
        }
コード例 #15
0
        public void ReadMessage_Throws_If_The_BufferManager_Parameter_Is_Null()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            byte[] bytes = new byte[5] { 0, 1, 2, 3, 4 };
            ArraySegment<byte> buffer = new ArraySegment<byte>(bytes);

            ExceptionAssert.ThrowsArgumentNull(
                "HttpMessageEncoder.ReadMessage should have thrown since the bufferManager parameter was null.",
                "bufferManager",
                () =>
                {
                    encoder.ReadMessage(buffer, null, "someContentType");
                });
        }
コード例 #16
0
        public void ReadMessage_Returns_The_Buffer_To_The_Buffer_Manager()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            byte[] bytes = new byte[5] { 0, 1, 2, 3, 4 };
            ArraySegment<byte> buffer = new ArraySegment<byte>(bytes);

            MockBufferManager bufferManager = new MockBufferManager();
            HttpMessage message = encoder.ReadMessage(buffer, bufferManager, "someType/someSubType") as HttpMessage;
            Assert.IsTrue(bufferManager.ReturnBufferCalled, "HttpMessageEncoder.ReadMessage should have returned the buffer to the buffer manager.");
            Assert.AreSame(bytes, bufferManager.BufferReturned, "HttpMessageEncoder.ReadMessage should have returned the original buffer instance to the buffer manager.");
        }
コード例 #17
0
        public void WriteMessage_Throws_If_BufferManager_Parameter_Is_Null()
        {
            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.ThrowsArgumentNull(
                "HttpMessageEncoder.WriteMessage should have thrown since bufferManager parameter was null.",
                "bufferManager",
                () =>
                {
                    encoder.WriteMessage(message, 5, null);
                });
        }
コード例 #18
0
        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);
                });
        }
コード例 #19
0
 public void IsContentTypeSupported_Is_True()
 {
     HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
     MessageEncoder encoder = factory.Encoder;
     Assert.IsTrue(encoder.IsContentTypeSupported("someContentType"), "HttpMessageEncoder.IsContentTypeSupported should have returned 'true'.");
 }
コード例 #20
0
        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);
                });
        }
コード例 #21
0
        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());
                });
        }
コード例 #22
0
        public void ReadMessage_Returns_HttpMessage_With_IsRequest_True()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            byte[] bytes = new byte[5] { 0, 1, 2, 3, 4};
            ArraySegment<byte> buffer = new ArraySegment<byte>(bytes);

            HttpMessage message = encoder.ReadMessage(buffer, new MockBufferManager(), "someType/someSubType") as HttpMessage;
            Assert.IsTrue(message.IsRequest, "HttpMessageEncoder.ReadMessage should have returned an HttpMessage with IsRequest set to 'true'.");
        }
コード例 #23
0
        public void WriteMessage_Returns_An_ArraySegment_With_The_Content_From_The_HttpMessage()
        {
            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);
            HttpMessage message = new HttpMessage(response);

            ArraySegment<byte> buffer = encoder.WriteMessage(message, int.MaxValue, new MockBufferManager());
            Assert.IsNotNull(buffer, "HttpMessageEncoder.WriteMessage should not have returned null.");
            CollectionAssert.AreEquivalent(bytes, buffer.Array, "HttpMessageEncoder.WriteMessage should have returned the content of the HttpMessage.");
        }
コード例 #24
0
        public void WriteMessage_Returns_Empty_ArraySegment_If_HttpMessage_Content_Is_Null()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            HttpResponseMessage response = new HttpResponseMessage();
            HttpMessage message = new HttpMessage(response);

            ArraySegment<byte> buffer = encoder.WriteMessage(message, int.MaxValue, new MockBufferManager());
            Assert.AreEqual(0, buffer.Count, "HttpMessageEncoder.WriteMessage should have returned an empty ArraySegment.");
        }
コード例 #25
0
        public void WriteMessage_Throws_If_Message_Parameter_Is_Null()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            ExceptionAssert.ThrowsArgumentNull(
                "HttpMessageEncoder.WriteMessage should have thrown since message parameter was null.",
                "message",
                () =>
                {
                    encoder.WriteMessage(null, 5, new MockBufferManager());
                });
        }
コード例 #26
0
        public void WriteMessage_Takes_A_Buffer_From_The_BufferManager()
        {
            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);

            MockBufferManager bufferManager = new MockBufferManager();
            ArraySegment<byte> buffer = encoder.WriteMessage(message, int.MaxValue, bufferManager);
            Assert.IsTrue(bufferManager.TakeBufferCalled, "HttpMessageEncoder.WriteMessage should have taken a buffer from the bufferManager.");
            Assert.AreSame(buffer.Array, bufferManager.BufferTaken, "HttpMessageEncoder.WriteMessage should have returned the array taken from the bufferManager.");
        }
コード例 #27
0
 public void MediaType_Is_Empty_String()
 {
     HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
     MessageEncoder encoder = factory.Encoder;
     Assert.AreEqual(string.Empty, encoder.MediaType, "HttpMessageEncoder.MediaType should have returned an empty string.");
 }
コード例 #28
0
        public void WriteMessage_That_Takes_A_Stream_Throws_If_The_Message_Parameter_Is_Null()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            using (MemoryStream stream = new MemoryStream())
            {
                ExceptionAssert.ThrowsArgumentNull(
                    "HttpMessageEncoder.WriteMessage should have thrown since message parameter was null.",
                    "message",
                    () =>
                    {
                        encoder.WriteMessage(null, stream);
                    });
            }
        }
コード例 #29
0
        public void ReadMessage_Returns_HttpMessage()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            byte[] bytes = new byte[5] { 0, 1, 2, 3, 4};
            ArraySegment<byte> buffer = new ArraySegment<byte>(bytes);

            Message message = encoder.ReadMessage(buffer, new MockBufferManager(), "someType/someSubType");
            Assert.IsNotNull(message, "HttpMessageEncoder.ReadMessage should not have returned null.");
            Assert.IsInstanceOfType(message, typeof(HttpMessage), "HttpMessageEncoder.ReadMessage should have returned an HttpMessage.");
        }
コード例 #30
0
        public void ReadMessage_Returns_HttpMessage_With_HttpContent_Given_By_ContentType_Parameter()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder encoder = factory.Encoder;

            byte[] bytes = new byte[5] { 0, 1, 2, 3, 4 };
            ArraySegment<byte> buffer = new ArraySegment<byte>(bytes);

            HttpMessage message = encoder.ReadMessage(buffer, new MockBufferManager(), "someType/someSubType") as HttpMessage;
            HttpRequestMessage request = message.ToHttpRequestMessage();
            Assert.AreEqual("someType/someSubType", request.Content.Headers.ContentType.MediaType, "HttpMessageEncoder.ReadMessage should have returned an HttpMessage with a content type of 'someType/someSubType'.");
        }