Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Code cleanup in test project #82

Merged
merged 11 commits into from
Feb 14, 2024
5 changes: 2 additions & 3 deletions Test/BasicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Test
[TestClass]
public class BasicTest
{
private readonly Dictionary<string, string> _expectedTypes = new Dictionary<string, string>
private readonly Dictionary<string, string> _expectedTypes = new()
{
{"PNG", "image/png"},
{"png", "image/png"},
Expand Down Expand Up @@ -94,7 +94,7 @@ public void TestMimeTypeLookupToGetExtensions()
{
var expected = new[] { KnownMimeTypes.FileExtensions.Json, KnownMimeTypes.FileExtensions.Map };
var actual = MimeUtility.GetExtensions(KnownMimeTypes.Json);
Assert.IsTrue(actual.All(x => expected.Contains(x)));
Assert.IsTrue(Array.TrueForAll(actual, x => expected.Contains(x)));

var @null = MimeUtility.GetExtensions("invalid");
Assert.IsNull(@null);
Expand Down Expand Up @@ -122,6 +122,5 @@ public void TestMimeTypeToExtenstionsLookup()
Assert.IsTrue(kv.Value.Length > 0, $"{kv.Key} cannot have zero extensions.");
}
}

}
}
2 changes: 0 additions & 2 deletions Test/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// Setting ComVisible to false makes the types in this assembly not visible
Expand Down
29 changes: 15 additions & 14 deletions Test/TemplateSourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;

Expand All @@ -10,14 +11,14 @@ namespace Test
[TestClass]
public class TemplateSourceTests
{
private const string APACHE_URL = "http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types";
private const string APACHE_URL = "https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types";
private const string NGINX_URL = "https://raw.githubusercontent.com/h5bp/server-configs-nginx/master/mime.types";
private const string MIMEDB_URL = "https://raw.githubusercontent.com/jshttp/mime-db/master/src/custom-types.json";

[TestMethod]
public void TestApacheMimeTypes()
public async Task TestApacheMimeTypesAsync()
{
var keyPairs = GetMimeTypesFromText(APACHE_URL, line =>
var keyPairs = await GetMimeTypesFromTextAsync(APACHE_URL, line =>
{
if (line[0] == '#') return null;
var parts = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
Expand All @@ -28,9 +29,9 @@ public void TestApacheMimeTypes()
}

[TestMethod]
public void TestNginxMimeTypes()
public async Task TestNginxMimeTypesAsync()
{
var keyPairs = GetMimeTypesFromText(NGINX_URL, line =>
var keyPairs = await GetMimeTypesFromTextAsync(NGINX_URL, line =>
{
line = line.Trim().TrimEnd(';');
if (line[0] == '#' || line[0] == '}' || line.StartsWith("types {")) return null;
Expand All @@ -42,12 +43,12 @@ public void TestNginxMimeTypes()
}

[TestMethod]
public void TestMimeDbMimeTypes()
public async Task TestMimeDbMimeTypesAsync()
{
var keyPairs = GetMimeTypesFromJson(MIMEDB_URL, resource =>
var keyPairs = await GetMimeTypesFromJsonAsync(MIMEDB_URL, resource =>
{
return from mimeTypes in resource.Children()
let mimeType = ((JProperty) mimeTypes).Name
let mimeType = ((JProperty)mimeTypes).Name
from mimeTypeProperties in mimeTypes.Children()
from mimeTypeProperty in mimeTypeProperties.Children()
from mimeTypeValue in mimeTypeProperty.Values()
Expand All @@ -62,23 +63,23 @@ select new[]
Assert.IsTrue(keyPairs.Any());
}

private static string GetPageContent(string url)
private static async Task<string> GetPageContentAsync(string url)
{
using var client = new HttpClient();
return client.GetStringAsync(url).GetAwaiter().GetResult();
return await client.GetStringAsync(url);
}

private static IEnumerable<string[]> GetMimeTypesFromText(string url, Func<string, string[]> processLine)
private static async Task<IEnumerable<string[]>> GetMimeTypesFromTextAsync(string url, Func<string, string[]> processLine)
{
var content = GetPageContent(url);
var content = await GetPageContentAsync(url);
if (string.IsNullOrEmpty(content)) return Enumerable.Empty<string[]>();
var lines = content.Split(new[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
return lines.Select(processLine).Where(newKeyPairs => newKeyPairs != null).ToList();
}

private static IEnumerable<string[]> GetMimeTypesFromJson(string url, Func<JObject, IEnumerable<string[]>> processObject)
private static async Task<IEnumerable<string[]>> GetMimeTypesFromJsonAsync(string url, Func<JObject, IEnumerable<string[]>> processObject)
{
var content = GetPageContent(url);
var content = await GetPageContentAsync(url);
if (string.IsNullOrEmpty(content)) return Enumerable.Empty<string[]>();
var resource = JObject.Parse(content);
return processObject(resource);
Expand Down
6 changes: 3 additions & 3 deletions Test/Test.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net48</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSUnixLike())">net6.0</TargetFrameworks>
<TargetFrameworks>net8.0;net6.0;net48</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSUnixLike())">net8.0;net6.0</TargetFrameworks>
<RunSettingsFilePath>$(MSBuildThisFileDirectory)/test.runsettings</RunSettingsFilePath>
<LangVersion>latest</LangVersion>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down