Skip to content

Commit

Permalink
test: Code cleanup in test project (#82)
Browse files Browse the repository at this point in the history
* Minor code-style cleanups

* Migrate test to System.Text.Json

* Add supported dotnet versions to test project

* Make tests async and simplify TestMimeDbMimeTypesAsync

* Use https for svn.apache.org

* Remove redundant usings

* Update Test/Test.csproj

Drop net7.0 as it will be out-of-support soon

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>

* PR comments: revert changes and ValueTask fix (net48)

* fix spacing

* Update Test/Test.csproj

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>

---------

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
gintsk and viceice authored Feb 14, 2024
1 parent 060f6f6 commit f9b8908
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
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