Skip to content

Commit

Permalink
(Tests) Increase coverage on filters (#151)
Browse files Browse the repository at this point in the history
* Add IPAddress to TestPayload in unit tests

Added IPAddress property to the TestPayload class within the MessagePackQueueExtensionsTests. Adjusted the associated Equals and GetHashCode methods to include the new IPAddress property. This ensures proper comparison functionality and hash generation for the updated test payloads.

* Prevent closing provided stream

* Cover more equality test

* Add Zip compress / decompress test

* Include a websocket exchange in test case

* Update test to include ws message

* Add ws exchange in directory archiving tests

* Remove ApplyXorSlow method from WsMessage

The ApplyXorSlow method within WsMessage.cs file is no longer necessary and has therefore been removed. This function wasn't efficient and its functionality is covered by the ApplyXor method, contributing to cleaner and more maintainable code.

* Add self-equality check in EqualityTesterBase
.

* Add test.json to Fluxzy.Tests.csproj.

* Relocated MockedResponseExtensions to its own file.

* Add GetHeaderValueOrDefault method to MockedResponseContent.

* Add unit tests for mock response content.

* Add unit tests for DefaultDnsSolver
.

* Initialize DNS resolution result
.

* Add NavigationTests and clean up Program.cs

A new test file, NavigationTests.cs, has been introduced to cover CLI navigation. Concurrently, code in Program.cs has been streamlined for better clarity and readability. Unused environment variable options have been removed, and redundant spacing has been eliminated.

* Add JsonConstructor to AgentLabelFilter

* Add more filter tests

* Increase default settings coverage

* Use DefaultAgentProvider

* Fix ApplyTagAction default scope

* Add tests for non empty constructors filter type
  • Loading branch information
haga-rak authored Jan 17, 2024
1 parent 6fa89e6 commit d68e90e
Show file tree
Hide file tree
Showing 24 changed files with 676 additions and 168 deletions.
4 changes: 3 additions & 1 deletion src/Fluxzy.Core/Archiving/ZipHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public static async Task Compress(
if (!directoryInfo.Exists)
throw new ArgumentException($"Directory {directoryInfo.FullName} does not exists");

await using var zipStream = new ZipOutputStream(output);
await using var zipStream = new ZipOutputStream(output) {
IsStreamOwner = false
};

zipStream.SetLevel(3);

Expand Down
13 changes: 0 additions & 13 deletions src/Fluxzy.Core/Clients/H11/WsMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,6 @@ public WsMessage(int id)
[Key(10)]
public int FrameCount { get; set; }

internal void ApplyXorSlow(Span<byte> data, int mask, int countIndex)
{
if (mask == 0)
return;

Span<byte> maskData = stackalloc byte[4];
BinaryPrimitives.WriteInt32BigEndian(maskData, mask);

for (var i = 0; i < data.Length; i++) {
data[i] ^= maskData[i % 4];
}
}

internal void ApplyXor(Span<byte> data, uint mask, int countIndex)
{
if (mask == 0)
Expand Down
6 changes: 6 additions & 0 deletions src/Fluxzy.Core/Clients/Mock/MockedResponseContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public override Stream ReadBody(Authority authority)
{
return Body == null ? Stream.Null : Body.GetStream();
}

public string? GetHeaderValueOrDefault(string headerName, string? defaultValue = default)
{
return Headers
.FirstOrDefault(k => k.Name.Equals(headerName, StringComparison.OrdinalIgnoreCase))?.Value ?? defaultValue;
}
}

public class MockedResponseHeader
Expand Down
1 change: 1 addition & 0 deletions src/Fluxzy.Core/Clients/PoolBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public async ValueTask<IHttpConnectionPool>

if (dnsResolutionResult.Item2 != null)
{
dnsResolutionResult.Item2.Init();
return dnsResolutionResult.Item2;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Fluxzy.Core/Rules/Actions/ApplyTagAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Fluxzy.Rules.Actions
[ActionMetadata("Affect a tag to exchange. Tags are meta-information and do not alter the connection.")]
public class ApplyTagAction : Action
{
public override FilterScope ActionScope => FilterScope.OnAuthorityReceived;
public override FilterScope ActionScope => FilterScope.RequestHeaderReceivedFromClient;

/// <summary>
/// Tag value
Expand Down
115 changes: 0 additions & 115 deletions src/Fluxzy.Core/Rules/Actions/HighLevelActions/MockedResponseAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Fluxzy.Clients.Mock;
using Fluxzy.Core;
using Fluxzy.Core.Breakpoints;
using Fluxzy.Rules.Extensions;

namespace Fluxzy.Rules.Actions.HighLevelActions
{
Expand Down Expand Up @@ -85,118 +84,4 @@ public static MockedResponseAction BuildDefaultInstance()
});
}
}


public static class MockedResponseExtensions
{
/// <summary>
/// Generates a mocked response for a file and configures it as a response action for a given action builder.
/// </summary>
/// <param name="actionBuilder">The action builder to configure.</param>
/// <param name="fileName">The name of the file to generate a response from.</param>
/// <param name="statusCode">The HTTP status code for the response (default is 200).</param>
/// <param name="contentType">The content type of the response (default is null).</param>
/// <param name="headers">Additional headers to be included in the response (default is null).</param>
/// <returns>An instance of <see cref="IConfigureFilterBuilder"/> for further configuration.</returns>
public static IConfigureFilterBuilder ReplyFile(
this IConfigureActionBuilder actionBuilder, string fileName,
int statusCode = 200, string? contentType = null, params (string, string)[] headers)
{
var content = MockedResponseContent.CreateFromFile(fileName, statusCode, contentType);

foreach (var (name, value) in headers)
content.Headers.Add(new MockedResponseHeader(name, value));

actionBuilder.Do(new MockedResponseAction(content));
return new ConfigureFilterBuilderBuilder(actionBuilder.Setting);
}

/// <summary>
/// Configures the simulated response with text content.
/// </summary>
/// <param name="actionBuilder">The action builder.</param>
/// <param name="text">The text content to be returned.</param>
/// <param name="statusCode">The HTTP status code of the response. Default is 200.</param>
/// <param name="contentType">The content type of the response. Default is "text/plain".</param>
/// <param name="headers">The headers to be added to the response.</param>
/// <returns>A configured filter builder.</returns>
public static IConfigureFilterBuilder ReplyText(
this IConfigureActionBuilder actionBuilder, string text,
int statusCode = 200, string? contentType = "text/plain", params (string, string)[] headers)
{
var content = MockedResponseContent.CreateFromString(text, statusCode, contentType ?? "");

foreach (var (name, value) in headers)
content.Headers.Add(new MockedResponseHeader(name, value));

actionBuilder.Do(new MockedResponseAction(content));
return new ConfigureFilterBuilderBuilder(actionBuilder.Setting);
}

/// <summary>
/// Configures a mocked response that returns a byte array content.
/// </summary>
/// <param name="actionBuilder">The action builder.</param>
/// <param name="bytes">The byte array content to be returned.</param>
/// <param name="statusCode">The HTTP status code to be returned. The default value is 200.</param>
/// <param name="contentType">The content type of the response. The default value is "application/octet-stream".</param>
/// <param name="headers">The additional headers to be included in the response.</param>
/// <returns>The configure filter builder.</returns>
public static IConfigureFilterBuilder ReplyByteArray(
this IConfigureActionBuilder actionBuilder, byte[] bytes,
int statusCode = 200, string? contentType = "application/octet-stream", params (string, string)[] headers)
{
var content = MockedResponseContent.CreateFromByteArray(bytes, statusCode, contentType ?? "");

foreach (var (name, value) in headers)
content.Headers.Add(new MockedResponseHeader(name, value));

actionBuilder.Do(new MockedResponseAction(content));
return new ConfigureFilterBuilderBuilder(actionBuilder.Setting);
}

/// <summary>
/// Sets up a mocked response with JSON content.
/// </summary>
/// <param name="actionBuilder">The action builder.</param>
/// <param name="json">The JSON content to be returned.</param>
/// <param name="statusCode">The HTTP status code of the response. Default is 200.</param>
/// <param name="headers">The custom headers to be included in the response.</param>
/// <returns>A configure filter builder for further configuration.</returns>
public static IConfigureFilterBuilder ReplyJson(
this IConfigureActionBuilder actionBuilder, string json,
int statusCode = 200, params (string, string)[] headers)
{
var content = MockedResponseContent.CreateFromString(json, statusCode, "application/json");

foreach (var (name, value) in headers)
content.Headers.Add(new MockedResponseHeader(name, value));

actionBuilder.Do(new MockedResponseAction(content));

return new ConfigureFilterBuilderBuilder(actionBuilder.Setting);
}

/// <summary>
/// Generates a mocked response with JSON content from a file and configures the action builder to reply with the generated response.
/// </summary>
/// <param name="actionBuilder">The action builder to configure.</param>
/// <param name="fileName">The name of the file that contains the JSON content.</param>
/// <param name="statusCode">The status code to be set in the response. Default is 200.</param>
/// <param name="headers">Additional headers to be included in the response.</param>
/// <returns>A configure filter builder instance.</returns>
public static IConfigureFilterBuilder ReplyJsonFile(
this IConfigureActionBuilder actionBuilder, string fileName,
int statusCode = 200, params (string, string)[] headers)
{
var content = MockedResponseContent.CreateFromFile(fileName, statusCode, "application/json");

foreach (var (name, value) in headers)
content.Headers.Add(new MockedResponseHeader(name, value));

actionBuilder.Do(new MockedResponseAction(content));

return new ConfigureFilterBuilderBuilder(actionBuilder.Setting);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright 2021 - Haga Rakotoharivelo - https://github.com/haga-rak

using Fluxzy.Clients.Mock;
using Fluxzy.Rules.Extensions;

namespace Fluxzy.Rules.Actions.HighLevelActions
{
public static class MockedResponseExtensions
{
/// <summary>
/// Generates a mocked response for a file and configures it as a response action for a given action builder.
/// </summary>
/// <param name="actionBuilder">The action builder to configure.</param>
/// <param name="fileName">The name of the file to generate a response from.</param>
/// <param name="statusCode">The HTTP status code for the response (default is 200).</param>
/// <param name="contentType">The content type of the response (default is null).</param>
/// <param name="headers">Additional headers to be included in the response (default is null).</param>
/// <returns>An instance of <see cref="IConfigureFilterBuilder"/> for further configuration.</returns>
public static IConfigureFilterBuilder ReplyFile(
this IConfigureActionBuilder actionBuilder, string fileName,
int statusCode = 200, string? contentType = null, params (string, string)[] headers)
{
var content = MockedResponseContent.CreateFromFile(fileName, statusCode, contentType);

foreach (var (name, value) in headers)
content.Headers.Add(new MockedResponseHeader(name, value));

actionBuilder.Do(new MockedResponseAction(content));
return new ConfigureFilterBuilderBuilder(actionBuilder.Setting);
}

/// <summary>
/// Configures the simulated response with text content.
/// </summary>
/// <param name="actionBuilder">The action builder.</param>
/// <param name="text">The text content to be returned.</param>
/// <param name="statusCode">The HTTP status code of the response. Default is 200.</param>
/// <param name="contentType">The content type of the response. Default is "text/plain".</param>
/// <param name="headers">The headers to be added to the response.</param>
/// <returns>A configured filter builder.</returns>
public static IConfigureFilterBuilder ReplyText(
this IConfigureActionBuilder actionBuilder, string text,
int statusCode = 200, string? contentType = "text/plain", params (string, string)[] headers)
{
var content = MockedResponseContent.CreateFromString(text, statusCode, contentType ?? "");

foreach (var (name, value) in headers)
content.Headers.Add(new MockedResponseHeader(name, value));

actionBuilder.Do(new MockedResponseAction(content));
return new ConfigureFilterBuilderBuilder(actionBuilder.Setting);
}

/// <summary>
/// Configures a mocked response that returns a byte array content.
/// </summary>
/// <param name="actionBuilder">The action builder.</param>
/// <param name="bytes">The byte array content to be returned.</param>
/// <param name="statusCode">The HTTP status code to be returned. The default value is 200.</param>
/// <param name="contentType">The content type of the response. The default value is "application/octet-stream".</param>
/// <param name="headers">The additional headers to be included in the response.</param>
/// <returns>The configure filter builder.</returns>
public static IConfigureFilterBuilder ReplyByteArray(
this IConfigureActionBuilder actionBuilder, byte[] bytes,
int statusCode = 200, string? contentType = "application/octet-stream", params (string, string)[] headers)
{
var content = MockedResponseContent.CreateFromByteArray(bytes, statusCode, contentType ?? "");

foreach (var (name, value) in headers)
content.Headers.Add(new MockedResponseHeader(name, value));

actionBuilder.Do(new MockedResponseAction(content));
return new ConfigureFilterBuilderBuilder(actionBuilder.Setting);
}

/// <summary>
/// Sets up a mocked response with JSON content.
/// </summary>
/// <param name="actionBuilder">The action builder.</param>
/// <param name="json">The JSON content to be returned.</param>
/// <param name="statusCode">The HTTP status code of the response. Default is 200.</param>
/// <param name="headers">The custom headers to be included in the response.</param>
/// <returns>A configure filter builder for further configuration.</returns>
public static IConfigureFilterBuilder ReplyJson(
this IConfigureActionBuilder actionBuilder, string json,
int statusCode = 200, params (string, string)[] headers)
{
var content = MockedResponseContent.CreateFromString(json, statusCode, "application/json");

foreach (var (name, value) in headers)
content.Headers.Add(new MockedResponseHeader(name, value));

actionBuilder.Do(new MockedResponseAction(content));

return new ConfigureFilterBuilderBuilder(actionBuilder.Setting);
}

/// <summary>
/// Generates a mocked response with JSON content from a file and configures the action builder to reply with the generated response.
/// </summary>
/// <param name="actionBuilder">The action builder to configure.</param>
/// <param name="fileName">The name of the file that contains the JSON content.</param>
/// <param name="statusCode">The status code to be set in the response. Default is 200.</param>
/// <param name="headers">Additional headers to be included in the response.</param>
/// <returns>A configure filter builder instance.</returns>
public static IConfigureFilterBuilder ReplyJsonFile(
this IConfigureActionBuilder actionBuilder, string fileName,
int statusCode = 200, params (string, string)[] headers)
{
var content = MockedResponseContent.CreateFromFile(fileName, statusCode, "application/json");

foreach (var (name, value) in headers)
content.Headers.Add(new MockedResponseHeader(name, value));

actionBuilder.Do(new MockedResponseAction(content));

return new ConfigureFilterBuilderBuilder(actionBuilder.Setting);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2021 - Haga Rakotoharivelo - https://github.com/haga-rak

using System.Collections.Generic;
using System.Text.Json.Serialization;
using Fluxzy.Core;

namespace Fluxzy.Rules.Filters.RequestFilters
Expand All @@ -11,6 +12,12 @@ namespace Fluxzy.Rules.Filters.RequestFilters
)]
public class AgentLabelFilter : StringFilter
{
[JsonConstructor]
public AgentLabelFilter()
: this(string.Empty)
{
}

public AgentLabelFilter(string pattern)
: base(pattern, StringSelectorOperation.Contains)
{
Expand Down
18 changes: 2 additions & 16 deletions src/Fluxzy/Program.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
// Copyright 2021 - Haga Rakotoharivelo - https://github.com/haga-rak

using System;
using System.IO;
using System.IO.Compression;
using System.Threading;
using System.Threading.Tasks;
using Fluxzy.Cli.Commands;

namespace Fluxzy.Cli
{
internal class Program
{
private static async Task<int> Main(string[] args)
internal static async Task<int> Main(string[] args)
{
if (Environment.GetEnvironmentVariable("appdata") == null) {

// For Linux and OSX environment this EV is missing, so we need to set it manually
// to XDG_DATA_HOME

Environment.SetEnvironmentVariable("appdata",
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
Environment.SetEnvironmentVariable("appdata", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
}

// Environment.SetEnvironmentVariable("EnableDumpStackTraceOn502", "true");
// Environment.SetEnvironmentVariable("InsertFluxzyMetricsOnResponseHeader", "true");
// Environment.SetEnvironmentVariable("EnableH2Tracing", "true");
// Environment.SetEnvironmentVariable("EnableH2TracingFilterHosts", "casalemedia.com");
// Environment.SetEnvironmentVariable("EnableH1Tracing", "true");

var exitCode = await FluxzyStartup.Run(args, null, CancellationToken.None);

return exitCode;
Expand Down
4 changes: 2 additions & 2 deletions test/Fluxzy.Tests/Cli/Dissects/DissectCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public async Task Read_Check_Count(string input)
StringSplitOptions.RemoveEmptyEntries);

Assert.Equal(0, runResult.ExitCode);
Assert.Equal(16, stdOutLines.Length);
Assert.Equal("99 - https://en.wikipedia.org/static/favicon/wikipedia.ico - 200", stdOutLines.Last());
Assert.Equal(18, stdOutLines.Length);
Assert.Equal("186 - https://demo.piesocket.com/v3/channel_123?api_key=VCXCEuvhGcBDP7XhiJJUDvR1e1D3eiVjgZ9VRiaV&notify_self - 101", stdOutLines.Last());
}

[Fact]
Expand Down
Loading

0 comments on commit d68e90e

Please sign in to comment.