-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add general testing framework that can be used by projects (#428)
- Loading branch information
1 parent
a508411
commit 542af86
Showing
88 changed files
with
8,339 additions
and
8,164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,80 @@ | ||
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; | ||
|
||
namespace GenHTTP.Testing.Acceptance | ||
{ | ||
|
||
/// <summary> | ||
/// Compatibility assertions for XUnit. | ||
/// </summary> | ||
public static class AssertX | ||
{ | ||
|
||
public static void Contains(string searchFor, string? content) => Assert.IsTrue(content?.Contains(searchFor) ?? false); | ||
|
||
public static void DoesNotContain(string searchFor, string? content) => Assert.IsFalse(content?.Contains(searchFor) ?? false); | ||
|
||
public static void StartsWith(string searchFor, string? content) => Assert.IsTrue(content?.StartsWith(searchFor) ?? false); | ||
|
||
public static void EndsWith(string searchFor, string? content) => Assert.IsTrue(content?.EndsWith(searchFor) ?? false); | ||
|
||
public static void Single<T>(IEnumerable<T> collection) => Assert.IsTrue(collection.Count() == 1); | ||
|
||
public static void Empty<T>(IEnumerable<T> collection) => Assert.IsFalse(collection.Any()); | ||
|
||
public static void Contains<T>(T value, IEnumerable<T> collection) => Assert.IsTrue(collection.Contains(value)); | ||
|
||
public static void DoesNotContain<T>(T value, IEnumerable<T> collection) => Assert.IsFalse(collection.Contains(value)); | ||
|
||
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()); | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
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; | ||
|
||
namespace GenHTTP.Testing.Acceptance | ||
{ | ||
|
||
/// <summary> | ||
/// Compatibility assertions for XUnit. | ||
/// </summary> | ||
public static class AssertX | ||
{ | ||
|
||
public static void Contains(string searchFor, string? content) => Assert.IsTrue(content?.Contains(searchFor) ?? false); | ||
|
||
public static void DoesNotContain(string searchFor, string? content) => Assert.IsFalse(content?.Contains(searchFor) ?? false); | ||
|
||
public static void StartsWith(string searchFor, string? content) => Assert.IsTrue(content?.StartsWith(searchFor) ?? false); | ||
|
||
public static void EndsWith(string searchFor, string? content) => Assert.IsTrue(content?.EndsWith(searchFor) ?? false); | ||
|
||
public static void Single<T>(IEnumerable<T> collection) => Assert.IsTrue(collection.Count() == 1); | ||
|
||
public static void Empty<T>(IEnumerable<T> collection) => Assert.IsFalse(collection.Any()); | ||
|
||
public static void Contains<T>(T value, IEnumerable<T> collection) => Assert.IsTrue(collection.Contains(value)); | ||
|
||
public static void DoesNotContain<T>(T value, IEnumerable<T> collection) => Assert.IsFalse(collection.Contains(value)); | ||
|
||
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()); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
162 changes: 82 additions & 80 deletions
162
Testing/Engine/BasicTests.cs → Testing/Acceptance/Engine/BasicTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,82 @@ | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace GenHTTP.Testing.Acceptance.Engine | ||
{ | ||
|
||
[TestClass] | ||
public sealed class BasicTests | ||
{ | ||
|
||
[TestMethod] | ||
public async Task TestBuilder() | ||
{ | ||
using var runner = new TestRunner(); | ||
|
||
runner.Host.RequestMemoryLimit(128) | ||
.TransferBufferSize(128) | ||
.RequestReadTimeout(TimeSpan.FromSeconds(2)) | ||
.Backlog(1); | ||
|
||
runner.Start(); | ||
|
||
using var response = await runner.GetResponse(); | ||
|
||
await response.AssertStatusAsync(HttpStatusCode.NotFound); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestLegacyHttp() | ||
{ | ||
using var runner = TestRunner.Run(); | ||
|
||
using var client = TestRunner.GetClient(version: new Version(1, 0)); | ||
|
||
using var response = await runner.GetResponse(); | ||
|
||
await response.AssertStatusAsync(HttpStatusCode.NotFound); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestConnectionClose() | ||
{ | ||
using var runner = TestRunner.Run(); | ||
|
||
var request = runner.GetRequest(); | ||
request.Headers.Add("Connection", "close"); | ||
|
||
using var response = await runner.GetResponse(request); | ||
|
||
await response.AssertStatusAsync(HttpStatusCode.NotFound); | ||
Assert.IsTrue(response.Headers.Connection.Contains("Close")); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestEmptyQuery() | ||
{ | ||
using var runner = TestRunner.Run(); | ||
|
||
using var response = await runner.GetResponse("/?"); | ||
|
||
await response.AssertStatusAsync(HttpStatusCode.NotFound); | ||
} | ||
|
||
|
||
[TestMethod] | ||
public async Task TestKeepalive() | ||
{ | ||
using var runner = TestRunner.Run(); | ||
|
||
using var response = await runner.GetResponse(); | ||
|
||
Assert.IsTrue(response.Headers.Connection.Contains("Keep-Alive")); | ||
} | ||
|
||
} | ||
|
||
} | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
|
||
using GenHTTP.Modules.Layouting; | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace GenHTTP.Testing.Acceptance.Engine | ||
{ | ||
|
||
[TestClass] | ||
public sealed class BasicTests | ||
{ | ||
|
||
[TestMethod] | ||
public async Task TestBuilder() | ||
{ | ||
using var runner = new TestHost(Layout.Create()); | ||
|
||
runner.Host.RequestMemoryLimit(128) | ||
.TransferBufferSize(128) | ||
.RequestReadTimeout(TimeSpan.FromSeconds(2)) | ||
.Backlog(1); | ||
|
||
runner.Start(); | ||
|
||
using var response = await runner.GetResponseAsync(); | ||
|
||
await response.AssertStatusAsync(HttpStatusCode.NotFound); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestLegacyHttp() | ||
{ | ||
using var runner = TestHost.Run(Layout.Create()); | ||
|
||
using var client = TestHost.GetClient(protocolVersion: new Version(1, 0)); | ||
|
||
using var response = await runner.GetResponseAsync(); | ||
|
||
await response.AssertStatusAsync(HttpStatusCode.NotFound); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestConnectionClose() | ||
{ | ||
using var runner = TestHost.Run(Layout.Create()); | ||
|
||
var request = runner.GetRequest(); | ||
request.Headers.Add("Connection", "close"); | ||
|
||
using var response = await runner.GetResponseAsync(request); | ||
|
||
await response.AssertStatusAsync(HttpStatusCode.NotFound); | ||
Assert.IsTrue(response.Headers.Connection.Contains("Close")); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestEmptyQuery() | ||
{ | ||
using var runner = TestHost.Run(Layout.Create()); | ||
|
||
using var response = await runner.GetResponseAsync("/?"); | ||
|
||
await response.AssertStatusAsync(HttpStatusCode.NotFound); | ||
} | ||
|
||
|
||
[TestMethod] | ||
public async Task TestKeepalive() | ||
{ | ||
using var runner = TestHost.Run(Layout.Create()); | ||
|
||
using var response = await runner.GetResponseAsync(); | ||
|
||
Assert.IsTrue(response.Headers.Connection.Contains("Keep-Alive")); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.