Gallatin.Core.Service.ProxyFilter.EvaluateResponseFiltersWithBody C# (CSharp) Method

EvaluateResponseFiltersWithBody() public method

public EvaluateResponseFiltersWithBody ( IHttpResponse args, string connectionId, byte body ) : byte[]
args IHttpResponse
connectionId string
body byte
return byte[]
        public byte[] EvaluateResponseFiltersWithBody( IHttpResponse args, string connectionId, byte[] body )
        {
            byte[] filteredBody = null;

            if ( body != null && body.Length > 0 )
            {
                ServiceLog.Logger.Verbose( "Evaluating body filters" );

                // The body has been re-assembled. Remove the "chunked" header.
                args.Headers.RemoveKeyValue("transfer-encoding", "chunked");

                // Decompress the body so filters don't have to repeatedly do this themselves
                body = DecompressBody(args, body);

                ServiceLog.Logger.Verbose( () => Encoding.UTF8.GetString(body));

                foreach ( Func<IHttpResponse, string, byte[], byte[]> callback in _callbackList )
                {
                    filteredBody = callback(args, connectionId, body);

                    if ( filteredBody != null )
                    {
                        break;
                    }
                }
            }

            // Update the HTTP headers by adding the content length for the new body.
            // If the body was not modified by the filters then reset this to the
            // body that was passed in, which has possibly been decompressed.
            if ( filteredBody != null )
            {
                ServiceLog.Logger.Verbose("Response body has been modified by proxy server");
                args.Headers.UpsertKeyValue( "Content-Length", filteredBody.Length.ToString() );
            }
            else
            {
                ServiceLog.Logger.Verbose("Proxy server did not modify response body");
                filteredBody = body;
            }

            byte[] header = args.GetBuffer();

            byte[] returnBuffer = new byte[header.Length + filteredBody.Length];
            Array.Copy( header, returnBuffer, header.Length );
            Array.Copy( filteredBody, 0, returnBuffer, header.Length, filteredBody.Length );

            return returnBuffer;
        }

Usage Example

        public void VerifyResponseFilterWithCallbackWithBody([Values(true, false)] bool filterEnabled)
        {
            bool delegateHasBeenCalled = false;

            byte[] body = Encoding.UTF8.GetBytes( "this is the body" );
            byte[] compressedBody = Compress( body );
            byte[] outBody = Encoding.UTF8.GetBytes( "this is the modified body" );

            Mock<IHttpHeaders> mockHeaders = new Mock<IHttpHeaders>();
            mockHeaders.Setup( s => s["transfer-encoding"] ).Returns( "chunked" );
            mockHeaders.Setup( s => s["content-encoding"] ).Returns( "gzip" );

            Mock<ICoreSettings> settings = new Mock<ICoreSettings>();
            settings.SetupGet(m => m.FilteringEnabled).Returns(filterEnabled);

            ProxyFilter filter = new ProxyFilter(settings.Object);
            List<IResponseFilter> filters = new List<IResponseFilter>();

            Mock<IHttpResponse> response = new Mock<IHttpResponse>();
            response.SetupAllProperties();
            response.SetupGet( s => s.HasBody ).Returns( true );
            response.SetupGet( s => s.Headers ).Returns( mockHeaders.Object );

            Mock<IResponseFilter> mockFilter = new Mock<IResponseFilter>();
            mockFilter.SetupGet( s => s.FilterSpeedType ).Returns( FilterSpeedType.Remote );
            Func<IHttpResponse, string, byte[], byte[]> outParm =
                ( r, c, i ) =>
                {
                    delegateHasBeenCalled = true;
                    Assert.That( r, Is.SameAs( response.Object ) );
                    Assert.That( c, Is.EqualTo( "connectionid" ) );
                    Assert.That( i, Is.EqualTo( body ), "This should be the uncompressed body" );
                    return outBody;
                };
            mockFilter.Setup( s => s.EvaluateFilter( response.Object, "connectionid", out outParm ) ).Returns( null as string );

            filters.Add( mockFilter.Object );
            filter.ResponseFilters = filters;

            bool isBodyNeeded;
            byte[] filterResponse = filter.EvaluateResponseFilters( response.Object, "connectionid", out isBodyNeeded );

            if (filterEnabled)
            {
                Assert.That(isBodyNeeded, Is.True);
                Assert.That(filterResponse, Is.Null);

                byte[] responseMessage = filter.EvaluateResponseFiltersWithBody(response.Object, "connectionid", compressedBody);

                Assert.That(responseMessage, Is.EqualTo(outBody));

                mockHeaders.Verify(s => s.RemoveKeyValue("transfer-encoding", "chunked"), Times.Once());
                mockHeaders.Verify(s => s.UpsertKeyValue("Content-Length", "25"), Times.Once());
                mockHeaders.Verify(s => s.RemoveKeyValue("content-encoding", "gzip"), Times.Once());
                mockHeaders.Verify(s => s.UpsertKeyValue("Content-Length", "16"),
                                    Times.Once(),
                                    "The header should have been updated with the uncompressed body size");

                Assert.That(delegateHasBeenCalled, Is.True);
            }
            else
            {
                Assert.That(isBodyNeeded, Is.False);
                Assert.That(filterResponse, Is.Null);

            }
        }