Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
[Feature] Remove references to web assemblies (#7)
Browse files Browse the repository at this point in the history
Remove references to web assemblies
  • Loading branch information
robertcoltheart committed Mar 17, 2019
1 parent e8521f8 commit 291bde7
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 51 deletions.
47 changes: 44 additions & 3 deletions src/Machine.Specifications.Reporting/Generation/Spark/SparkView.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Web;
using System.Linq;
using System.Text;

using Machine.Specifications.Reporting.Model;

Expand All @@ -17,7 +18,7 @@ public Run Model

public string H(object value)
{
return HttpUtility.HtmlEncode(Convert.ToString(value));
return HtmlEncode(Convert.ToString(value));
}

public static string Pluralize(string caption, int count)
Expand All @@ -29,5 +30,45 @@ public static string Pluralize(string caption, int count)

return caption;
}

private string HtmlEncode(string value)
{
if (string.IsNullOrEmpty(value) || !ShouldEncode(value))
return value;

var encoded = new StringBuilder();

foreach (var c in value)
{
if (c == '&')
encoded.Append("&");
else if (c == '"')
encoded.Append(""");
else if (c == '<')
encoded.Append("&lt;");
else if (c == '>')
encoded.Append("&gt;");
else if (c == '\'')
encoded.Append("&#39;");
else if (c > 159 && c < 256)
encoded.Append($"&#{Convert.ToInt32(c).ToString()};");
else
encoded.Append(c);
}

return encoded.ToString();
}

private bool ShouldEncode(string value)
{
bool IsHtmlChar(char c)
{
var highChar = c > 159 && c < 256;

return c == '&' || c == '"' || c == '<' || c == '>' || c == '\'' || highChar;
}

return value.Any(IsHtmlChar);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Net;
using System.Text;

namespace Machine.Specifications.Reporting.Integration.AppVeyor
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;

public class AppVeyorBuildWorkerApiClient : IAppVeyorBuildWorkerApiClient
{
const string ApiResource = "/api/tests";
Expand All @@ -32,24 +30,22 @@ public void AddTest(string testName,
string stdOut,
string stdErr)
{
var body = new
{
TestName = testName,
TestFramework = testFramework,
FileName = fileName,
Outcome = outcome,
DurationMilliseconds = durationMilliseconds,
ErrorMessage = errorMessage,
ErrorStackTrace = errorStackTrace,
StdOut = TrimStdOut(stdOut),
StdErr = TrimStdOut(stdErr)
};

try
{
var json = Json(
testName,
testFramework,
fileName,
outcome,
durationMilliseconds,
errorMessage,
errorStackTrace,
TrimStdOut(stdOut),
TrimStdOut(stdErr));

using (WebClient wc = GetClient())
{
wc.UploadData(ApiResource, "POST", Json(body));
wc.UploadData(ApiResource, "POST", json);
}
}
catch (Exception ex)
Expand All @@ -68,24 +64,22 @@ public void UpdateTest(string testName,
string stdOut,
string stdErr)
{
var body = new
{
TestName = testName,
TestFramework = testFramework,
FileName = fileName,
Outcome = outcome,
DurationMilliseconds = durationMilliseconds,
ErrorMessage = errorMessage,
ErrorStackTrace = errorStackTrace,
StdOut = TrimStdOut(stdOut),
StdErr = TrimStdOut(stdErr)
};

try
{
var json = Json(
testName,
testFramework,
fileName,
outcome,
durationMilliseconds,
errorMessage,
errorStackTrace,
TrimStdOut(stdOut),
TrimStdOut(stdErr));

using (WebClient wc = GetClient())
{
wc.UploadData(ApiResource, "PUT", Json(body));
wc.UploadData(ApiResource, "PUT", json);
}
}
catch (Exception ex)
Expand All @@ -106,11 +100,57 @@ static string TrimStdOut(string str)
return (str.Length > MaxLength) ? str.Substring(0, MaxLength) : str;
}

static byte[] Json(object data)
static byte[] Json(
string testName,
string testFramework,
string fileName,
string outcome,
long? durationMilliseconds,
string errorMessage,
string errorStackTrace,
string stdOut,
string stdErr)
{
var value = new StringBuilder()
.Append("{")
.Append($"{GetJsonValue("TestName")}:{GetJsonValue(testName)},")
.Append($"{GetJsonValue("TestFramework")}:{GetJsonValue(testFramework)},")
.Append($"{GetJsonValue("FileName")}:{GetJsonValue(fileName)},")
.Append($"{GetJsonValue("Outcome")}:{GetJsonValue(outcome)},")
.Append($"{GetJsonValue("DurationMilliseconds")}:{GetJsonValue(durationMilliseconds)},")
.Append($"{GetJsonValue("ErrorMessage")}:{GetJsonValue(errorMessage)},")
.Append($"{GetJsonValue("ErrorStackTrace")}:{GetJsonValue(errorStackTrace)},")
.Append($"{GetJsonValue("StdOut")}:{GetJsonValue(stdOut)},")
.Append($"{GetJsonValue("StdErr")}:{GetJsonValue(stdErr)}")
.Append("}")
.ToString();

return Encoding.UTF8.GetBytes(value);
}

private static string GetJsonValue(long? value)
{
if (value == null)
return "null";

return value.ToString();
}

private static string GetJsonValue(string value)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
return Encoding.UTF8.GetBytes(json);
if (value == null)
return "null";

var jsonValue = value
.Replace("\\", "\\\\")
.Replace("\b", "\\b")
.Replace("\f", "\\f")
.Replace("\n", "\\n")
.Replace("\r", "\\r")
.Replace("\t", "\\t")
.Replace("\"", "\\\"");

return $"\"{jsonValue}\"";
}

WebClient GetClient()
Expand All @@ -121,4 +161,4 @@ WebClient GetClient()
return wc;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@

<PropertyGroup>
<Version>0.1.0</Version>
<TargetFrameworks>net452</TargetFrameworks>
<TargetFrameworks>net35</TargetFrameworks>
<Description>Machine.Specifications.Reporting is HTML reporting library for the Context/Specification framework Machine.Specifications</Description>
<Authors>Machine Specifications</Authors>
<PackageTags>test;unit;testing;context;specification;bdd;tdd;mspec;reporting</PackageTags>
<PackageReleaseNotes>See http://github.com/machine/machine.specifications.reporting/releases</PackageReleaseNotes>
<PackageIconUrl>http://github.com/machine/machine.specifications/raw/master/Misc/Machine.Specifications-128x128.png</PackageIconUrl>
<PackageIconUrl>http://github.com/machine/media/raw/master/Machine.Specifications-128x128.png</PackageIconUrl>
<PackageProjectUrl>http://github.com/machine/machine.specifications.reporting</PackageProjectUrl>
<PackageLicenseUrl>http://github.com/machine/machine.specifications.reporting/raw/master/License.txt</PackageLicenseUrl>
<FrameworkPathOverride Condition=" '$(TargetFramework)' == 'net35' ">C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client</FrameworkPathOverride>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="Generation\Spark\Templates\*.spark" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Machine.Specifications.Runner.Utility" Version="0.9.0" />
<PackageReference Include="Spark" Version="1.8.1" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<PackageReference Include="Machine.Specifications.Runner.Utility" Version="[0.1, 1.0)" />
<PackageReference Include="Spark" Version="1.7.5.3" />
</ItemGroup>

</Project>

0 comments on commit 291bde7

Please sign in to comment.