Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 50 additions & 50 deletions tests/Pulse.Tests.Unit/ExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public void Exporter_ClearFiles() {
}

// Assert
dirInfo.GetFiles().Length.Should().Be(10, "because 10 files were created");
Assert.Equal(10, dirInfo.GetFiles().Length);

// Act
Exporter.ClearFiles(dirInfo.FullName);

// Assert
dirInfo.GetFiles().Length.Should().Be(0, "because all files were deleted");
Assert.Empty(dirInfo.GetFiles());
} finally {
dirInfo.Delete(true);
}
Expand Down Expand Up @@ -61,9 +61,9 @@ public void Exporter_ToHtmlTable_ContainsAllHeaders() {

// Assert
foreach (var header in headers) {
fileContent.Should().Contain(header.Key);
Assert.Contains(header.Key, fileContent);
foreach (var value in header.Value) {
fileContent.Should().Contain(value);
Assert.Contains(value, fileContent);
}
}
}
Expand Down Expand Up @@ -91,16 +91,16 @@ public async Task Exporter_Raw_NotSuccess_ContainsAllHeadersInJson() {
var expectedFileName = $"response-1337-status-code-502.json";
await Exporter.ExportRawAsync(response, string.Empty, false);

File.Exists(expectedFileName).Should().BeTrue("because the file was created");
Assert.True(File.Exists(expectedFileName));

var fileContent = await File.ReadAllTextAsync(expectedFileName);

// Assert
fileContent.Should().Contain("502", "because the status code is present");
Assert.Contains("502", fileContent);
foreach (var header in headers) {
fileContent.Should().Contain(header.Key);
Assert.Contains(header.Key, fileContent);
foreach (var value in header.Value) {
fileContent.Should().Contain(value);
Assert.Contains(value, fileContent);
}
}

Expand All @@ -110,13 +110,13 @@ public async Task Exporter_Raw_NotSuccess_ContainsAllHeadersInJson() {
[Fact]
public async Task Exporter_Raw_Success_ContainsOnlyContent() {
// Arrange
const string content = "Hello World";
const string expectedContent = "Hello World";

var response = new Response {
Id = 1337,
StatusCode = HttpStatusCode.OK,
Content = content,
ContentLength = content.Length,
Content = expectedContent,
ContentLength = expectedContent.Length,
Headers = [],
Exception = StrippedException.Default,
Latency = TimeSpan.FromSeconds(1),
Expand All @@ -127,26 +127,26 @@ public async Task Exporter_Raw_Success_ContainsOnlyContent() {
var expectedFileName = $"response-1337-status-code-200.html";
await Exporter.ExportRawAsync(response, string.Empty, false);

File.Exists(expectedFileName).Should().BeTrue("because the file was created");
Assert.True(File.Exists(expectedFileName));

var fileContent = await File.ReadAllTextAsync(expectedFileName);

// Assert
fileContent.Should().Be(content, "because the status code is present");
Assert.Equal(expectedContent, fileContent);

File.Delete(expectedFileName);
}

[Fact]
public async Task Exporter_Raw_NotSuccess_ButHasContent_ContainsOnlyContent() {
// Arrange
const string content = "Hello World";
const string expectedContent = "Hello World";

var response = new Response {
Id = 1337,
StatusCode = HttpStatusCode.BadGateway,
Content = content,
ContentLength = content.Length,
Content = expectedContent,
ContentLength = expectedContent.Length,
Headers = [],
Exception = StrippedException.Default,
Latency = TimeSpan.FromSeconds(1),
Expand All @@ -157,12 +157,12 @@ public async Task Exporter_Raw_NotSuccess_ButHasContent_ContainsOnlyContent() {
var expectedFileName = $"response-1337-status-code-502.html";
await Exporter.ExportRawAsync(response, string.Empty, false);

File.Exists(expectedFileName).Should().BeTrue("because the file was created");
Assert.True(File.Exists(expectedFileName));

var fileContent = await File.ReadAllTextAsync(expectedFileName);

// Assert
fileContent.Should().Be(content, "because the status code is present");
Assert.Equal(expectedContent, fileContent);

File.Delete(expectedFileName);
}
Expand Down Expand Up @@ -190,8 +190,8 @@ public async Task Exporter_ExportHtmlAsync_CorrectFileName() {

// Assert
var file = dirInfo.GetFiles();
file.Length.Should().Be(1, "because 1 file was created");
file[0].Name.Should().Be("response-1337-status-code-200.html", "because the file name is correct");
Assert.Single(file);
Assert.Equal("response-1337-status-code-200.html", file[0].Name);
} finally {
dirInfo.Delete(true);
}
Expand All @@ -207,13 +207,13 @@ public async Task Exporter_ExportHtmlAsync_ContainsAllHeaders() {
new("X-Custom-Header", ["value1", "value2"])
];

const string content = "Hello World";
const string expectedContent = "Hello World";

var response = new Response {
Id = 1337,
StatusCode = HttpStatusCode.OK,
Content = content,
ContentLength = Encoding.Default.GetByteCount(content),
Content = expectedContent,
ContentLength = Encoding.Default.GetByteCount(expectedContent),
Headers = headers,
Exception = StrippedException.Default,
Latency = TimeSpan.FromSeconds(1),
Expand All @@ -225,13 +225,13 @@ public async Task Exporter_ExportHtmlAsync_ContainsAllHeaders() {

// Assert
var file = dirInfo.GetFiles();
file.Length.Should().Be(1, "because 1 file was created");
Assert.Single(file);
var fileContent = await File.ReadAllTextAsync(file[0].FullName);

foreach (var header in headers) {
fileContent.Should().Contain(header.Key);
Assert.Contains(header.Key, fileContent);
foreach (var value in header.Value) {
fileContent.Should().Contain(value);
Assert.Contains(value, fileContent);
}
}
} finally {
Expand All @@ -244,13 +244,13 @@ public async Task Exporter_ExportHtmlAsync_WithoutException_HasContent() {
// Arrange
var dirInfo = Directory.CreateTempSubdirectory();
try {
const string content = "Hello World";
const string expectedContent = "Hello World";

var response = new Response {
Id = 1337,
StatusCode = HttpStatusCode.OK,
Content = content,
ContentLength = Encoding.Default.GetByteCount(content),
Content = expectedContent,
ContentLength = Encoding.Default.GetByteCount(expectedContent),
Headers = [],
Exception = StrippedException.Default,
Latency = TimeSpan.FromSeconds(1),
Expand All @@ -262,9 +262,9 @@ public async Task Exporter_ExportHtmlAsync_WithoutException_HasContent() {

// Assert
var file = dirInfo.GetFiles();
file.Length.Should().Be(1, "because 1 file was created");
Assert.Single(file);
var fileContent = await File.ReadAllTextAsync(file[0].FullName);
fileContent.Should().Contain("Hello World", "because the content is present");
Assert.Contains(expectedContent, fileContent);
} finally {
dirInfo.Delete(true);
}
Expand All @@ -275,13 +275,13 @@ public async Task Exporter_ExportHtmlAsync_RawHtml() {
// Arrange
var dirInfo = Directory.CreateTempSubdirectory();
try {
const string content = "Hello World";
const string expectedContent = "Hello World";

var response = new Response {
Id = 1337,
StatusCode = HttpStatusCode.OK,
Content = content,
ContentLength = Encoding.Default.GetByteCount(content),
Content = expectedContent,
ContentLength = Encoding.Default.GetByteCount(expectedContent),
Headers = [],
Exception = StrippedException.Default,
Latency = TimeSpan.FromSeconds(1),
Expand All @@ -293,9 +293,9 @@ public async Task Exporter_ExportHtmlAsync_RawHtml() {

// Assert
var file = dirInfo.GetFiles();
file.Length.Should().Be(1, "because 1 file was created");
Assert.Single(file);
var fileContent = await File.ReadAllTextAsync(file[0].FullName);
fileContent.Should().Be("Hello World", "because the content and only the content is present");
Assert.Equal(expectedContent, fileContent);
} finally {
dirInfo.Delete(true);
}
Expand All @@ -310,13 +310,13 @@ public async Task Exporter_ExportHtmlAsync_RawJson() {
WriteIndented = false
};

var content = JsonSerializer.Serialize(new ParametersBase(), options);
var expectedContent = JsonSerializer.Serialize(new ParametersBase(), options);

var response = new Response {
Id = 1337,
StatusCode = HttpStatusCode.OK,
Content = content,
ContentLength = Encoding.Default.GetByteCount(content),
Content = expectedContent,
ContentLength = Encoding.Default.GetByteCount(expectedContent),
Headers = [],
Exception = StrippedException.Default,
Latency = TimeSpan.FromSeconds(1),
Expand All @@ -328,10 +328,10 @@ public async Task Exporter_ExportHtmlAsync_RawJson() {

// Assert
var file = dirInfo.GetFiles();
file.Length.Should().Be(1, "because 1 file was created");
Assert.Single(file);
var fileContent = await File.ReadAllTextAsync(file[0].FullName);
fileContent.Should().Be(content, "because the content and only the content is present");
fileContent.Should().NotContain(Environment.NewLine, "because the content is not formatted");
Assert.Equal(expectedContent, fileContent);
Assert.DoesNotContain(Environment.NewLine, fileContent);
} finally {
dirInfo.Delete(true);
}
Expand Down Expand Up @@ -364,9 +364,9 @@ public async Task Exporter_ExportHtmlAsync_RawJson_Formatted() {

// Assert
var file = dirInfo.GetFiles();
file.Length.Should().Be(1, "because 1 file was created");
Assert.Single(file);
var fileContent = await File.ReadAllTextAsync(file[0].FullName);
fileContent.Should().Contain(Environment.NewLine, "because the content is formatted");
Assert.Contains(Environment.NewLine, fileContent);
} finally {
dirInfo.Delete(true);
}
Expand Down Expand Up @@ -395,10 +395,10 @@ public async Task Exporter_ExportHtmlAsync_RawJson_Exception() {

// Assert
var file = dirInfo.GetFiles();
file.Length.Should().Be(1, "because 1 file was created");
Assert.Single(file);
var fileContent = await File.ReadAllTextAsync(file[0].FullName);
fileContent.Should().Contain("test", "because the exception is present");
fileContent.Should().Contain(Environment.NewLine, "because the content is formatted");
Assert.Contains("test", fileContent);
Assert.Contains(Environment.NewLine, fileContent);
} finally {
dirInfo.Delete(true);
}
Expand Down Expand Up @@ -428,10 +428,10 @@ public async Task Exporter_ExportHtmlAsync_WithException_HasExceptionAndNoConten

// Assert
var file = dirInfo.GetFiles();
file.Length.Should().Be(1, "because 1 file was created");
Assert.Single(file);
var fileContent = await File.ReadAllTextAsync(file[0].FullName);
fileContent.Should().NotContain("Hello World", "because the content is not present");
fileContent.Should().Contain(exception.Message, "because the exception is present");
Assert.DoesNotContain("Hello World", fileContent);
Assert.Contains(exception.Message, fileContent);
} finally {
dirInfo.Delete(true);
}
Expand Down
1 change: 0 additions & 1 deletion tests/Pulse.Tests.Unit/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
global using FluentAssertions;
global using Bogus;
4 changes: 2 additions & 2 deletions tests/Pulse.Tests.Unit/HelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void Extensions_GetPercentageBasedColor(double percentage, ConsoleColor e
var color = Helper.GetPercentageBasedColor(percentage);

// Assert
color.ConsoleColor.Should().Be(expected, "because the percentage is correct");
Assert.Equal(expected, color.ConsoleColor);
}

[Theory]
Expand All @@ -33,6 +33,6 @@ public void Extensions_GetStatusCodeBasedColor(HttpStatusCode statusCode, Consol
var color = Helper.GetStatusCodeBasedColor((int)statusCode);

// Assert
color.ConsoleColor.Should().Be(expected, "because the percentage is correct");
Assert.Equal(expected, color.ConsoleColor);
}
}
31 changes: 16 additions & 15 deletions tests/Pulse.Tests.Unit/HttpClientFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void HttpClientFactory_DefaultTimeout_IsInfinite() {
using var httpClient = PulseHttpClientFactory.Create(proxy, ParametersBase.DefaultTimeoutInMs);

// Assert
httpClient.Timeout.Should().Be(Timeout.InfiniteTimeSpan, "because the default timeout is infinite");
Assert.Equal(Timeout.InfiniteTimeSpan, httpClient.Timeout);
}

[Fact]
Expand All @@ -28,7 +28,7 @@ public void HttpClientFactory_WithoutProxy_ReturnsHttpClient() {
using var httpClient = PulseHttpClientFactory.Create(proxy, ParametersBase.DefaultTimeoutInMs);

// Assert
httpClient.Should().NotBeNull("because a HttpClient is returned");
Assert.NotNull(httpClient);
}

[Fact]
Expand All @@ -40,7 +40,7 @@ public void CreateHandler_WithoutProxy_ReturnsSocketsHttpHandler() {
using var handler = PulseHttpClientFactory.CreateHandler(proxy);

// Assert
handler.Should().NotBeNull("because a handler is returned");
Assert.NotNull(handler);
}

[Fact]
Expand All @@ -52,7 +52,7 @@ public void CreateHandler_WithoutProxy() {
using var handler = PulseHttpClientFactory.CreateHandler(proxy);

// Assert
handler.Proxy.Should().BeNull("because no proxy is configured");
Assert.Null(handler.Proxy);
}

[Theory]
Expand All @@ -68,8 +68,8 @@ public void CreateHandler_WithProxy_HostOnly(string host, string expected) {
using var handler = PulseHttpClientFactory.CreateHandler(proxy);

// Assert
handler.UseProxy.Should().BeTrue("because a proxy is configured to be used");
handler.Proxy.Should().NotBeNull("because a valid proxy should be set when UseProxy is true");
Assert.True(handler.UseProxy);
Assert.NotNull(handler.Proxy);

// Create a valid destination Uri
var destination = new Uri("http://example.com");
Expand All @@ -78,7 +78,7 @@ public void CreateHandler_WithProxy_HostOnly(string host, string expected) {
var proxyUri = handler.Proxy!.GetProxy(destination);

// Assert that the Authority (host:port) matches the expected value
proxyUri!.Authority.Should().Be(expected, "because the proxy should point to the expected host and port");
Assert.Equal(expected, proxyUri!.Authority);
}

[Fact]
Expand All @@ -94,9 +94,9 @@ public void CreateHandler_WithProxy_WithoutPassword_NoCredentials() {
using var handler = PulseHttpClientFactory.CreateHandler(proxy);

// Assert
handler.UseProxy.Should().BeTrue("because a proxy is configured to be used");
handler.Proxy.Should().NotBeNull("because a valid proxy should be set when UseProxy is true");
handler.Proxy!.Credentials.Should().BeNull("because no credentials are configured");
Assert.True(handler.UseProxy);
Assert.NotNull(handler.Proxy);
Assert.Null(handler.Proxy!.Credentials);
}

[Fact]
Expand All @@ -113,11 +113,12 @@ public void CreateHandler_WithProxy_WithCredentials() {
using var handler = PulseHttpClientFactory.CreateHandler(proxy);

// Assert
handler.UseProxy.Should().BeTrue("because a proxy is configured to be used");
handler.Proxy.Should().NotBeNull("because a valid proxy should be set when UseProxy is true");
Assert.True(handler.UseProxy);
Assert.NotNull(handler.Proxy);
Assert.NotNull(handler.Proxy!.Credentials);
var credentials = handler.Proxy!.Credentials! as NetworkCredential;
credentials.Should().NotBeNull("because credentials are configured");
credentials!.UserName.Should().Be(proxy.Username, "because the username matches the configured username");
credentials!.Password.Should().Be(proxy.Password, "because the password matches the configured password");
Assert.NotNull(credentials);
Assert.Equal(proxy.Username, credentials.UserName);
Assert.Equal(proxy.Password, credentials.Password);
}
}
Loading
Loading