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);
}
}