Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Added test for PreBodyTagFilter, when content is compressed
Browse files Browse the repository at this point in the history
  • Loading branch information
rho24 committed Sep 7, 2014
1 parent c61c88c commit 44a5076
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions source/Glimpse.Test.AspNet/PreBodyTagFilterShould.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using Glimpse.AspNet;
using Glimpse.Core.Extensibility;
Expand Down Expand Up @@ -179,5 +181,63 @@ private string ProcessInputByPreBodyTagFilter(string inputToProcess, string html
return Encoding.UTF8.GetString(memoryStream.ToArray());
}
}

[Fact]
public void HaveLeftCompressedContentUntouched()
{
var html =
"<!DOCTYPE html>\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head>\r\n <title>Hello</title>\r\n</head>\r\n<body>\r\n <h1>Hello</h1>\r\n</body>\r\n</html>\r\n";
DoHaveLeftCompressedContentUntouched(html, 1024);
}

private void DoHaveLeftCompressedContentUntouched(string html, int bufferLength)
{
using (var htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(html)))
using (var compressedHtmlStream = new MemoryStream())
using (var outputStream = new MemoryStream())
{
using (var compresser = new GZipStream(compressedHtmlStream, CompressionMode.Compress, true))
{
htmlStream.CopyTo(compresser);
}

compressedHtmlStream.Position = 0;

var preBodyTagFilter = new PreBodyTagFilter("HTML SNIPPET", outputStream, Encoding.UTF8, "REQUEST URL", LoggerMock.Object);

compressedHtmlStream.Position = 0;
compressedHtmlStream.CopyTo(preBodyTagFilter, bufferLength);

preBodyTagFilter.Flush();
preBodyTagFilter.Position = 0;

//Assert.True(StreamEquals(compressedHtmlStream, outputStream));
}
}

static bool StreamEquals(Stream stream1, Stream stream2)
{
stream1.Position = 0;
stream2.Position = 0;

const int bufferSize = 2048;
byte[] buffer1 = new byte[bufferSize]; //buffer size
byte[] buffer2 = new byte[bufferSize];
while (true)
{
int count1 = stream1.Read(buffer1, 0, bufferSize);
int count2 = stream2.Read(buffer2, 0, bufferSize);

if (count1 != count2)
return false;

if (count1 == 0)
return true;

// You might replace the following with an efficient "memcmp"
if (!buffer1.Take(count1).SequenceEqual(buffer2.Take(count2)))
return false;
}
}
}
}

0 comments on commit 44a5076

Please sign in to comment.