Skip to content

Commit

Permalink
Fair use policy (#140)
Browse files Browse the repository at this point in the history
Groundwork for fair use policy.

Updated to .NET 8.

Moved to Minimal API rather than MVC.
  • Loading branch information
aaronpowell authored May 23, 2024
1 parent 5933fd9 commit 2f2fb6f
Show file tree
Hide file tree
Showing 35 changed files with 654 additions and 519 deletions.
60 changes: 60 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Set default behavior to automatically normalize line endings.
* text=auto

# Collapse these files in PRs by default
*.xlf linguist-generated=true
*.lcl linguist-generated=true

*.jpg binary
*.png binary
*.gif binary

# Force bash scripts to always use lf line endings so that if a repo is accessed
# in Unix via a file share from Windows, the scripts will work.
*.in text eol=lf
*.sh text eol=lf

# Likewise, force cmd and batch scripts to always use crlf
*.cmd text eol=crlf
*.bat text eol=crlf

*.cs text=auto diff=csharp
*.vb text=auto
*.resx text=auto
*.c text=auto
*.cpp text=auto
*.cxx text=auto
*.h text=auto
*.hxx text=auto
*.py text=auto
*.rb text=auto
*.java text=auto
*.html text=auto
*.htm text=auto
*.css text=auto
*.scss text=auto
*.sass text=auto
*.less text=auto
*.js text=auto
*.lisp text=auto
*.clj text=auto
*.sql text=auto
*.php text=auto
*.lua text=auto
*.m text=auto
*.asm text=auto
*.erl text=auto
*.fs text=auto
*.fsx text=auto
*.hs text=auto

*.csproj text=auto
*.vbproj text=auto
*.fsproj text=auto
*.dbproj text=auto
*.sln text=auto eol=crlf

# Set linguist language for .h files explicitly based on
# https://github.com/github/linguist/issues/1626#issuecomment-401442069
# this only affects the repo's language statistics
*.h linguist-language=C
2 changes: 1 addition & 1 deletion src/Teapot.Web.Tests/HttpMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Teapot.Web.Tests;

public class HttpMethods
{
public static HttpMethod[] All => new[] { Get, Put, Post, Delete, Head, Options, Trace, Patch };
public static HttpMethod[] All => [Get, Put, Post, Delete, Head, Options, Trace, Patch];
}
30 changes: 17 additions & 13 deletions src/Teapot.Web.Tests/IntegrationTests/CustomHeaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Teapot.Web.Controllers;

namespace Teapot.Web.Tests.IntegrationTests;
public class CustomHeaderTests {
private static readonly HttpClient _httpClient = new WebApplicationFactory<Program>().CreateDefaultClient();

public class CustomHeaderTests
{

[Test]
public async Task CanSetCustomHeaders() {
public async Task CanSetCustomHeaders()
{
HttpClient httpClient = new WebApplicationFactory<Program>().CreateDefaultClient();
string uri = "/200";
string headerName = "Foo";
string headerValue = "bar";

using HttpRequestMessage request = new(HttpMethod.Get, uri);
request.Headers.Add($"{StatusController.CUSTOM_RESPONSE_HEADER_PREFIX}{headerName}", headerValue);
request.Headers.Add($"{StatusExtensions.CUSTOM_RESPONSE_HEADER_PREFIX}{headerName}", headerValue);

using var response = await _httpClient.SendAsync(request);
using HttpResponseMessage response = await httpClient.SendAsync(request);

var headers = response.Headers;
Assert.That(headers.Contains(headerName), Is.True);
Assert.That(headers.TryGetValues(headerName, out var values), Is.True);
Assert.That(values, Is.Not.Null);
Assert.That(values.Count(), Is.EqualTo(1));
Assert.That(values.First(), Is.EqualTo(headerValue));
System.Net.Http.Headers.HttpResponseHeaders headers = response.Headers;
Assert.Multiple(() =>
{
Assert.That(headers.Contains(headerName), Is.True);
Assert.That(headers.TryGetValues(headerName, out IEnumerable<string>? values), Is.True);
Assert.That(values, Is.Not.Null);
Assert.That(values!.Count(), Is.EqualTo(1));
Assert.That(values!.First(), Is.EqualTo(headerValue));
});
}

}
31 changes: 18 additions & 13 deletions src/Teapot.Web.Tests/IntegrationTests/RandomTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@
namespace Teapot.Web.Tests.IntegrationTests;

[TestFixtureSource(typeof(HttpMethods), nameof(HttpMethods.All))]
public class RandomTests
public class RandomTests(HttpMethod httpMethod)
{
private readonly HttpMethod _httpMethod;

private static readonly HttpClient _httpClient = new WebApplicationFactory<Program>().CreateDefaultClient();
[OneTimeSetUp]
public void OneTimeSetUp()
{
_httpClient = new WebApplicationFactory<Program>().CreateDefaultClient();
}

public RandomTests(HttpMethod httpMethod)
[OneTimeTearDown]
public void OneTimeTearDown()
{
_httpMethod = httpMethod;
_httpClient.Dispose();
}

private HttpClient _httpClient = null!;

[TestCase("foo")]
[TestCase("200,x")]
[TestCase("200.0")]
[TestCase("-1,0,1")]
[TestCase("-1-1")]
public async Task ParseError(string input)
{
var uri = $"/random/{input}";
using var httpRequest = new HttpRequestMessage(_httpMethod, uri);
using var response = await _httpClient.SendAsync(httpRequest);
string uri = $"/random/{input}";
using HttpRequestMessage httpRequest = new(httpMethod, uri);
using HttpResponseMessage response = await _httpClient.SendAsync(httpRequest);
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}

Expand All @@ -34,11 +39,11 @@ public async Task ParseError(string input)
[TestCase("190-199,570-590")]
public async Task ParsedAsExpected(string input)
{
var uri = $"/random/{input}";
using var httpRequest = new HttpRequestMessage(_httpMethod, uri);
using var response = await _httpClient.SendAsync(httpRequest);
string uri = $"/random/{input}";
using HttpRequestMessage httpRequest = new(httpMethod, uri);
using HttpResponseMessage response = await _httpClient.SendAsync(httpRequest);
Assert.That((int)response.StatusCode, Is.InRange(100, 599));
var body = await response.Content.ReadAsStringAsync();
string body = await response.Content.ReadAsStringAsync();
Assert.Multiple(() =>
{
Assert.That(body.ReplaceLineEndings(), Does.EndWith("Unknown Code"));
Expand Down
52 changes: 28 additions & 24 deletions src/Teapot.Web.Tests/IntegrationTests/StatusCodeTests.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Teapot.Web.Controllers;

namespace Teapot.Web.Tests.IntegrationTests;

[TestFixtureSource(typeof(HttpMethods), nameof(HttpMethods.All))]
public class StatusCodeTests
public class StatusCodeTests(HttpMethod httpMethod)
{
private readonly HttpMethod _httpMethod;

private static readonly HttpClient _httpClient = new WebApplicationFactory<Program>().CreateDefaultClient();
[OneTimeSetUp]
public void OneTimeSetUp()
{
_httpClient = new WebApplicationFactory<Program>().CreateDefaultClient();
}

public StatusCodeTests(HttpMethod httpMethod)
[OneTimeTearDown]
public void OneTimeTearDown()
{
_httpMethod = httpMethod;
_httpClient.Dispose();
}

private HttpClient _httpClient = null!;

[TestCaseSource(typeof(TestCases), nameof(TestCases.StatusCodesWithContent))]
public async Task ResponseWithContent([Values] TestCase testCase)
{
var uri = $"/{testCase.Code}";
using var httpRequest = new HttpRequestMessage(_httpMethod, uri);
using var response = await _httpClient.SendAsync(httpRequest);
string uri = $"/{testCase.Code}";
using HttpRequestMessage httpRequest = new(httpMethod, uri);
using HttpResponseMessage response = await _httpClient.SendAsync(httpRequest);
Assert.That((int)response.StatusCode, Is.EqualTo(testCase.Code));
var body = await response.Content.ReadAsStringAsync();
string body = await response.Content.ReadAsStringAsync();
Assert.Multiple(() =>
{
Assert.That(body.ReplaceLineEndings(), Is.EqualTo(testCase.Body));
Expand All @@ -35,11 +39,11 @@ public async Task ResponseWithContent([Values] TestCase testCase)
[TestCaseSource(typeof(TestCases), nameof(TestCases.StatusCodesWithContent))]
public async Task ResponseWithContentSuppressedViaQs([Values] TestCase testCase)
{
var uri = $"/{testCase.Code}?{nameof(CustomHttpStatusCodeResult.SuppressBody)}=true";
using var httpRequest = new HttpRequestMessage(_httpMethod, uri);
using var response = await _httpClient.SendAsync(httpRequest);
string uri = $"/{testCase.Code}?{nameof(CustomHttpStatusCodeResult.SuppressBody)}=true";
using HttpRequestMessage httpRequest = new(httpMethod, uri);
using HttpResponseMessage response = await _httpClient.SendAsync(httpRequest);
Assert.That((int)response.StatusCode, Is.EqualTo(testCase.Code));
var body = await response.Content.ReadAsStringAsync();
string body = await response.Content.ReadAsStringAsync();
Assert.Multiple(() =>
{
Assert.That(body, Is.Empty);
Expand All @@ -52,12 +56,12 @@ public async Task ResponseWithContentSuppressedViaQs([Values] TestCase testCase)
[TestCaseSource(typeof(TestCases), nameof(TestCases.StatusCodesWithContent))]
public async Task ResponseWithContentSuppressedViaHeader([Values] TestCase testCase)
{
var uri = $"/{testCase.Code}";
using var httpRequest = new HttpRequestMessage(_httpMethod, uri);
httpRequest.Headers.Add(StatusController.SUPPRESS_BODY_HEADER, "true");
using var response = await _httpClient.SendAsync(httpRequest);
string uri = $"/{testCase.Code}";
using HttpRequestMessage httpRequest = new(httpMethod, uri);
httpRequest.Headers.Add(StatusExtensions.SUPPRESS_BODY_HEADER, "true");
using HttpResponseMessage response = await _httpClient.SendAsync(httpRequest);
Assert.That((int)response.StatusCode, Is.EqualTo(testCase.Code));
var body = await response.Content.ReadAsStringAsync();
string body = await response.Content.ReadAsStringAsync();
Assert.Multiple(() =>
{
Assert.That(body, Is.Empty);
Expand All @@ -70,11 +74,11 @@ public async Task ResponseWithContentSuppressedViaHeader([Values] TestCase testC
[TestCaseSource(typeof(TestCases), nameof(TestCases.StatusCodesNoContent))]
public async Task ResponseNoContent([Values] TestCase testCase)
{
var uri = $"/{testCase.Code}";
using var httpRequest = new HttpRequestMessage(_httpMethod, uri);
using var response = await _httpClient.SendAsync(httpRequest);
string uri = $"/{testCase.Code}";
using HttpRequestMessage httpRequest = new(httpMethod, uri);
using HttpResponseMessage response = await _httpClient.SendAsync(httpRequest);
Assert.That((int)response.StatusCode, Is.EqualTo(testCase.Code));
var body = await response.Content.ReadAsStringAsync();
string body = await response.Content.ReadAsStringAsync();
Assert.Multiple(() =>
{
Assert.That(body, Is.Empty);
Expand Down
22 changes: 14 additions & 8 deletions src/Teapot.Web.Tests/Teapot.Web.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Net.Primitives" Version="4.3.1" />
</ItemGroup>

Expand Down
22 changes: 11 additions & 11 deletions src/Teapot.Web.Tests/TestCases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace Teapot.Web.Tests;

public class TestCases
{
private static readonly TeapotStatusCodeResults All = new(
new AmazonStatusCodeResults(),
new CloudflareStatusCodeResults(),
new EsriStatusCodeResults(),
new LaravelStatusCodeResults(),
new MicrosoftStatusCodeResults(),
new NginxStatusCodeResults(),
new TwitterStatusCodeResults()
private static readonly TeapotStatusCodeMetadataCollection All = new(
new AmazonStatusCodeMetadata(),
new CloudflareStatusCodeMetadata(),
new EsriStatusCodeMetadata(),
new LaravelStatusCodeMetadata(),
new MicrosoftStatusCodeMetadata(),
new NginxStatusCodeMetadata(),
new TwitterStatusCodeMetadata()
);

private static readonly HttpStatusCode[] NoContentStatusCodes = new[]
Expand All @@ -34,12 +34,12 @@ public class TestCases

private static TestCase Map(HttpStatusCode code)
{
var key = (int)code;
int key = (int)code;
return new(key, All[key].Description, All[key].Body);
}

private static TestCase Map(KeyValuePair<int, TeapotStatusCodeResult> code)
private static TestCase Map(KeyValuePair<int, TeapotStatusCodeMetadata> code)
{
return new TestCase(code.Key, code.Value.Description, code.Value.Body);
return new(code.Key, code.Value.Description, code.Value.Body);
}
}
Loading

0 comments on commit 2f2fb6f

Please sign in to comment.