A class that encodes and decodes the body of an HTTP request or an HTTP response for a specified HTTP content encoding method.
Example #1
0
        public void Decode_WithNull_ReturnsNull()
        {
            var encoding  = new ContentEncoder("gzip");
            var nullBytes = encoding.Decode(null);

            Assert.IsNull(nullBytes);
        }
Example #2
0
        public void Decode_WithNull_ReturnsNull()
        {
            var encoding = new ContentEncoder("gzip");
            var nullBytes = encoding.Decode(null);

            Assert.IsNull(nullBytes);
        }
Example #3
0
        public void Constructor_WithValue_MethodUpdated()
        {
            const string Expected = "gzip";

            var encoding = new ContentEncoder(Expected);
            Assert.AreEqual(Expected, encoding.Method);
        }
Example #4
0
        public void Decode_UsingGzip_ReturnsDecompressed()
        {
            var encoding = new ContentEncoder("gzip");
            var actual   = encoding.Decode(_helloWorldGZip);

            CollectionAssert.AreEqual(_helloWorldUtf8, actual);
        }
Example #5
0
        public void Decode_UsingUnknownMethod_ReturnsSame()
        {
            var encoding = new ContentEncoder("badmethod");
            var actual   = encoding.Decode(_helloWorldGZip);

            CollectionAssert.AreEqual(_helloWorldGZip, actual);
        }
Example #6
0
        public void Decode_UsingDeflate_ReturnsDecompressed()
        {
            var encoding = new ContentEncoder("deflate");
            var actual   = encoding.Decode(_helloWorldDeflate);

            CollectionAssert.AreEqual(_helloWorldUtf8, actual);
        }
Example #7
0
        public void Constructor_WithValue_MethodUpdated()
        {
            const string Expected = "gzip";

            var encoding = new ContentEncoder(Expected);

            Assert.AreEqual(Expected, encoding.Method);
        }
Example #8
0
        public void Encode_UsingDeflate_ReturnsCompressed()
        {
            var encoding = new ContentEncoder("deflate");
            var actual   = encoding.Encode(_helloWorldUtf8);

            // assert that we got back something different
            CollectionAssert.AreNotEqual(_helloWorldUtf8, actual);

            var roundTrip = encoding.Decode(actual);

            CollectionAssert.AreEqual(_helloWorldUtf8, roundTrip);
        }
Example #9
0
        public void Encode_UsingGzip_ReturnsCompressed()
        {
            var encoding = new ContentEncoder("gzip");
            var actual   = encoding.Encode(_helloWorldUtf8);

            CollectionAssert.AreNotEqual(_helloWorldUtf8, actual);

            // assert that the gzip header is present
            Assert.AreEqual(actual[0], 31);
            Assert.AreEqual(actual[1], 139);

            var roundTrip = encoding.Decode(actual);

            CollectionAssert.AreEqual(_helloWorldUtf8, roundTrip);
        }
Example #10
0
 public void Decode_UsingUnknownMethod_ReturnsSame()
 {
     var encoding = new ContentEncoder("badmethod");
     var actual = encoding.Decode(_helloWorldGZip);
     CollectionAssert.AreEqual(_helloWorldGZip, actual);
 }
Example #11
0
 public void Decode_UsingGzip_ReturnsDecompressed()
 {
     var encoding = new ContentEncoder("gzip");
     var actual = encoding.Decode(_helloWorldGZip);
     CollectionAssert.AreEqual(_helloWorldUtf8, actual);
 }
Example #12
0
 public void Decode_UsingDeflate_ReturnsDecompressed()
 {
     var encoding = new ContentEncoder("deflate");
     var actual = encoding.Decode(_helloWorldDeflate);
     CollectionAssert.AreEqual(_helloWorldUtf8, actual);
 }
Example #13
0
 public void Constructor_WithNullMethod_MethodIsStringEmpty()
 {
     var encoding = new ContentEncoder(null);
     Assert.AreEqual(string.Empty, encoding.Method);
 }
Example #14
0
        public void Constructor_WithNullMethod_MethodIsStringEmpty()
        {
            var encoding = new ContentEncoder(null);

            Assert.AreEqual(string.Empty, encoding.Method);
        }
Example #15
0
        public void Encode_UsingDeflate_ReturnsCompressed()
        {
            var encoding = new ContentEncoder("deflate");
            var actual = encoding.Encode(_helloWorldUtf8);

            // assert that we got back something different
            CollectionAssert.AreNotEqual(_helloWorldUtf8, actual);

            var roundTrip = encoding.Decode(actual);

            CollectionAssert.AreEqual(_helloWorldUtf8, roundTrip);
        }
Example #16
0
        public void Encode_UsingGzipUppercase_ReturnsCompressed()
        {
            var encoding = new ContentEncoder("GZIP");
            var actual = encoding.Encode(_helloWorldUtf8);

            // assert that we got back something different
            CollectionAssert.AreNotEqual(_helloWorldUtf8, actual);

            // assert that the gzip header is present
            Assert.AreEqual(actual[0], 31);
            Assert.AreEqual(actual[1], 139);

            var roundTrip = encoding.Decode(actual);
            CollectionAssert.AreEqual(_helloWorldUtf8, roundTrip);
        }
Example #17
0
        /// <summary>
        ///     Populates the response of the HTTP context from the Stump.
        /// </summary>
        /// <param name="incommingHttpContext">The incomming HTTP context.</param>
        /// <param name="stump">The <see cref="T:Stumps.Stump"/> used to populate the response.</param>
        private void PopulateResponse(IStumpsHttpContext incommingHttpContext, Stump stump)
        {

            // Write the status code information
            incommingHttpContext.Response.StatusCode = stump.Response.StatusCode;
            incommingHttpContext.Response.StatusDescription = stump.Response.StatusDescription;

            // Write the headers
            incommingHttpContext.Response.Headers.Clear();

            stump.Response.Headers.CopyTo(incommingHttpContext.Response.Headers);

            // Write the body
            incommingHttpContext.Response.ClearBody();
            
            if (stump.Response.BodyLength > 0)
            {
                var buffer = stump.Response.GetBody();
                if (stump.Response.Headers["Content-Encoding"] != null)
                {
                    var encoder = new ContentEncoder(stump.Response.Headers["Content-Encoding"]);
                    buffer = encoder.Encode(buffer);
                }

                incommingHttpContext.Response.AppendToBody(buffer);
            }

        }