Skip to content

Commit

Permalink
Introduce a mechnism to print additional response information in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Dec 7, 2023
1 parent 4daf31e commit d943999
Show file tree
Hide file tree
Showing 43 changed files with 214 additions and 191 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"dotnet-sonarscanner": {
"version": "5.14.0",
"version": "5.15.0",
"commands": [
"dotnet-sonarscanner"
]
Expand Down
2 changes: 1 addition & 1 deletion Modules/Protobuf/GenHTTP.Modules.Protobuf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="protobuf-net" Version="3.2.26" />
<PackageReference Include="protobuf-net" Version="3.2.30" />
</ItemGroup>

<ItemGroup>
Expand Down
39 changes: 8 additions & 31 deletions Playground/Program.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
using System.Collections.Generic;
using GenHTTP.Engine;

using GenHTTP.Engine;

using GenHTTP.Modules.Functional;
using GenHTTP.Modules.Layouting;
using GenHTTP.Modules.IO;
using GenHTTP.Modules.Practices;
using GenHTTP.Modules.Security;

var books = new List<Book>()
{
new Book(1, "Lord of the Rings")
};

var bookApi = Inline.Create()
.Get(() => books) // GET http://localhost:8080/books/
.Put((Book book) => books.Add(book)); // PUT http://localhost:8080/books/

var content = Layout.Create()
.Add("books", bookApi)
.Add(CorsPolicy.Permissive());

return Host.Create()
.Handler(content)
.Defaults()
.Console()
//-:cnd:noEmit
#if DEBUG
.Development()
#endif
//+:cnd:noEmit
.Run();

record class Book(int ID, string Title);
Host.Create()
.Handler(Content.From(Resource.FromString("Hello World")))
.Defaults()
.Development()
.Console()
.Run();
47 changes: 46 additions & 1 deletion Testing/AssertX.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -30,6 +35,46 @@ public static class AssertX

public static void IsNullOrEmpty(string? value) => Assert.IsTrue(string.IsNullOrEmpty(value));

/// <summary>
/// Raises an assertion expection if the response does not have the expected status code
/// and additionally prints information about the response to be able to further debug
/// issues in workflow runs.
/// </summary>
/// <param name="response">The response to be evaluated</param>
/// <param name="expectedStatus">The expected status code to check for</param>
public static async Task AssertStatusAsync(this HttpResponseMessage response, HttpStatusCode expectedStatus)
{
if (response.StatusCode != expectedStatus)
{
var builder = new StringBuilder();

builder.AppendLine($"Response returned with status '{response.StatusCode}', expected '{expectedStatus}'.");
builder.AppendLine();

builder.AppendLine("Headers");
builder.AppendLine();

foreach (var header in response.Headers)
{
builder.AppendLine($" {header.Key} = {string.Join(',', header.Value.ToList())}");
}

builder.AppendLine();

var content = await response.Content.ReadAsStringAsync();

if (!string.IsNullOrEmpty(content))
{
builder.AppendLine("Body");
builder.AppendLine();

builder.AppendLine(content);
}

throw new AssertFailedException(builder.ToString());
}
}

}

}
8 changes: 4 additions & 4 deletions Testing/Engine/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task TestBuilder()

using var response = await runner.GetResponse();

Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.NotFound);
}

[TestMethod]
Expand All @@ -37,7 +37,7 @@ public async Task TestLegacyHttp()

using var response = await runner.GetResponse();

Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.NotFound);
}

[TestMethod]
Expand All @@ -50,7 +50,7 @@ public async Task TestConnectionClose()

using var response = await runner.GetResponse(request);

Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.NotFound);
Assert.IsTrue(response.Headers.Connection.Contains("Close"));
}

Expand All @@ -61,7 +61,7 @@ public async Task TestEmptyQuery()

using var response = await runner.GetResponse("/?");

Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.NotFound);
}


Expand Down
2 changes: 1 addition & 1 deletion Testing/Engine/ErrorHandlingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task TestGenericError()

using var response = await runner.GetResponse();

Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.InternalServerError);
}

}
Expand Down
2 changes: 1 addition & 1 deletion Testing/Engine/HeaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task TestReservedHeaderCannotBeSet()

using var response = await runner.GetResponse();

Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.InternalServerError);
}

}
Expand Down
4 changes: 2 additions & 2 deletions Testing/Engine/HostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task TestStart()

using var response = await runner.GetResponse();

Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.NotFound);
}

[TestMethod]
Expand All @@ -31,7 +31,7 @@ public async Task TestRestart()

using var response = await runner.GetResponse();

Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.NotFound);
}

}
Expand Down
2 changes: 1 addition & 1 deletion Testing/Engine/ResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task TestProperties()

using var response = await runner.GetResponse();

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.OK);

Assert.AreEqual("Hello World", await response.GetContent());
Assert.AreEqual("text/x-custom", response.GetContentHeader("Content-Type"));
Expand Down
2 changes: 1 addition & 1 deletion Testing/Engine/RoutingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task NotFoundForUnknownRoute()
using var runner = TestRunner.Run();

using var response = await runner.GetResponse();
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.NotFound);
}

}
Expand Down
15 changes: 8 additions & 7 deletions Testing/Engine/SecurityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Task TestSecure()

using var response = await client.GetAsync($"https://localhost:{sec}");

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.OK);
Assert.AreEqual("Hello Alice!", await response.Content.ReadAsStringAsync());
});
}
Expand All @@ -52,7 +52,7 @@ public Task TestDefaultRedirection()

using var response = await client.GetAsync($"http://localhost:{insec}");

Assert.AreEqual(HttpStatusCode.MovedPermanently, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.MovedPermanently);
Assert.AreEqual($"https://localhost:{sec}/", response.Headers.GetValues("Location").First());
});
}
Expand All @@ -70,7 +70,7 @@ public Task TestNoRedirectionWithAllowed()

using var response = await client.GetAsync($"http://localhost:{insec}");

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.OK);
}, mode: SecureUpgrade.Allow);
}

Expand All @@ -90,7 +90,8 @@ public Task TestRedirectionWhenRequested()

using var response = await client.SendAsync(request);

Assert.AreEqual(HttpStatusCode.TemporaryRedirect, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.TemporaryRedirect);

Assert.AreEqual($"https://localhost:{sec}/", response.Headers.GetValues("Location").First());
Assert.AreEqual($"Upgrade-Insecure-Requests", response.Headers.GetValues("Vary").First());
}, mode: SecureUpgrade.Allow);
Expand All @@ -109,12 +110,12 @@ public Task TestTransportPolicy()

using var insecureResponse = await client.GetAsync($"http://localhost:{insec}");

Assert.AreEqual(HttpStatusCode.OK, insecureResponse.StatusCode);
await insecureResponse.AssertStatusAsync(HttpStatusCode.OK);
Assert.IsFalse(insecureResponse.Headers.Contains("Strict-Transport-Security"));

using var secureResponse = await client.GetAsync($"https://localhost:{sec}");

Assert.AreEqual(HttpStatusCode.OK, secureResponse.StatusCode);
await secureResponse.AssertStatusAsync(HttpStatusCode.OK);
Assert.AreEqual("max-age=31536000; includeSubDomains; preload", secureResponse.Headers.GetValues("Strict-Transport-Security").First());

}, mode: SecureUpgrade.None);
Expand All @@ -139,7 +140,7 @@ await Assert.ThrowsExceptionAsync<HttpRequestException>(async () =>
using var client = TestRunner.GetClient(ignoreSecurityErrors: true);
using var response = await client.GetAsync($"https://localhost:{sec}");

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.OK);
});
}

Expand Down
2 changes: 1 addition & 1 deletion Testing/GenHTTP.Testing.Acceptance.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

<PackageReference Include="protobuf-net" Version="3.2.26" />
<PackageReference Include="protobuf-net" Version="3.2.30" />

</ItemGroup>

Expand Down
10 changes: 5 additions & 5 deletions Testing/Modules/Authentication/ApiKeyAuthenticationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task TestNoKey()

using var response = await runner.GetResponse();

Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.Unauthorized);
}

[TestMethod]
Expand All @@ -52,7 +52,7 @@ public async Task TestValidKey()

using var response = await runner.GetResponse(request);

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.OK);
}

[TestMethod]
Expand Down Expand Up @@ -83,7 +83,7 @@ public async Task TestValidKeyFromHeader()

using var response = await runner.GetResponse(request);

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.OK);
}

[TestMethod]
Expand All @@ -100,7 +100,7 @@ public async Task TestCustomExtractor()

using var response = await runner.GetResponse(request);

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.OK);
}

[TestMethod]
Expand All @@ -118,7 +118,7 @@ public async Task TestCustomAuthenticator()

using var response = await runner.GetResponse(request);

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.OK);
}

private static TestRunner GetRunnerWithKeys(params string[] keys)
Expand Down
10 changes: 5 additions & 5 deletions Testing/Modules/Authentication/BasicAuthenticationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task TestNoUser()

using var response = await runner.GetResponse();

Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.Unauthorized);
}

[TestMethod]
Expand Down Expand Up @@ -65,7 +65,7 @@ public async Task TestInvalidUser()

using var response = await GetResponse(runner, "u", "password");

Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.Unauthorized);
}

[TestMethod]
Expand All @@ -89,7 +89,7 @@ public async Task TestNoCustomUser()

using var response = await GetResponse(runner, "_", "_");

Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.Unauthorized);
}

[TestMethod]
Expand All @@ -104,7 +104,7 @@ public async Task TestOtherAuthenticationIsNotAccepted()

using var response = await runner.GetResponse(request);

Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.Unauthorized);
}

[TestMethod]
Expand All @@ -119,7 +119,7 @@ public async Task TestNoValidBase64()

using var response = await runner.GetResponse(request);

Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.Unauthorized);
}

private static async Task<HttpResponseMessage> GetResponse(TestRunner runner, string user, string password)
Expand Down
4 changes: 2 additions & 2 deletions Testing/Modules/Authentication/UserInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task TestUserInjected()

using var response = await runner.GetResponse(client: client);

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.OK);
Assert.AreEqual("abc", await response.GetContent());
}

Expand All @@ -38,7 +38,7 @@ public async Task TestNoUser()

using var response = await runner.GetResponse();

Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);
await response.AssertStatusAsync(HttpStatusCode.Unauthorized);
}

#endregion
Expand Down
Loading

0 comments on commit d943999

Please sign in to comment.