Skip to content

Instantly share code, notes, and snippets.

@jamesrcounts
Created July 20, 2012 13:49
Show Gist options
  • Select an option

  • Save jamesrcounts/3150831 to your computer and use it in GitHub Desktop.

Select an option

Save jamesrcounts/3150831 to your computer and use it in GitHub Desktop.
MvcReporter for MvcApprovals which use self-hosted webserver

Proof of concept only. Has no tests, no error checking, no design to speak of...

Turns out that CassiniDev is still running while the reporter is executing, so there is no need to even spin the server back up. A reference to the running server is passed through the server factory, because it has a nice method to turn a virtual path into a url. However, we could get rid of that reference and just build the URL ourselves the same way that MvcApprovals already does. This is probably preferred because we wont need a reference to CassiniDev in ApprovalTests if we build the URL without using the server.

The rest is just plumbing around rewriting the HTML with inline CSS. Only looked at doing CSS in this prototype. This doesn't handle images and other external resources.

Works on my machine.

namespace ApprovalTests.Reporters
{
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using ApprovalTests.Asp.CassiniDev;
using ApprovalTests.Core;
public class CassiniReporter : IApprovalFailureReporter
{
public void Report(string approved, string received)
{
string temp = WriteConvertedTempFile(received);
new FileLauncherReporter().Report(approved, temp);
}
private static string GetCssContent(string[] htmlLines)
{
var contents = htmlLines.Where(l => l.Contains("<link"));
var matches = Regex.Match(contents.Single(), @"href=\W([/\w]+\.css)\W");
var url = ServerFactory.Server.NormalizeUrl(matches.Groups[1].Value);
WebClient client = new WebClient();
string cssContent = client.DownloadString(url);
return cssContent;
}
private static string GetHtmlTempFile()
{
string tempFile = Path.GetTempFileName();
string htmlTempFile = Path.ChangeExtension(tempFile, "html");
File.Copy(tempFile, htmlTempFile, true);
File.Delete(tempFile);
return htmlTempFile;
}
private static string GetHtmlWithInlineCss(string[] htmlLines, string cssContent)
{
string styleTag = "<style type=\"text/css\">" + cssContent + "</style>";
StringBuilder sb = new StringBuilder();
foreach (var line in htmlLines)
{
if (line.Contains("<link") && line.Contains("stylesheet"))
{
sb.AppendLine(styleTag);
}
else
{
sb.AppendLine(line);
}
}
string htmlWithInlineCss = sb.ToString();
return htmlWithInlineCss;
}
private static string GetModifiedHtmlFile(string modifiedContent)
{
string modifiedHtmlFile = GetHtmlTempFile();
File.WriteAllText(modifiedHtmlFile, modifiedContent);
return modifiedHtmlFile;
}
private string WriteConvertedTempFile(string received)
{
string[] htmlLines = File.ReadAllLines(received);
string cssContent = GetCssContent(htmlLines);
string htmlWithInlineCss = GetHtmlWithInlineCss(htmlLines, cssContent);
return GetModifiedHtmlFile(htmlWithInlineCss);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment