Skip to content

Commit

Permalink
(Tests) Add producer actions tests (#139)
Browse files Browse the repository at this point in the history
* Add test on Certificate and FluxzySetting

* Add ImageResultProducer unit test

* Add ResponseBodySummaryProducer test cases

A new set of unit tests has been added to validate the functionality of the ResponseBodySummaryProducer class. This additional test coverage checks if the result produced by the class correctly matches the expected content type or returns null depending on different URL cases.

* Add new unit test for ResponseTextContentProducer

A new unit test has been introduced in the Producers.cs file. This test checks the ResponseTextContentProducer's functionality by asserting whether a returned result is either null or not null based on different URLs.

* Add SetCookieProducer unit test and disable auto-redirect

A new unit test for the SetCookieProducer class has been added, testing its functionality against different URL input values. In addition, the auto-redirect option in the HttpClientHandler within the QuickArchiveBuilder has been disabled to ensure tests get more accurate results.

* Update RequestJsonBodyProducer unit test

The unit test for RequestJsonBodyProducer has been enhanced with two inputs scenarios. Now it can handle cases where the HttpRequestMessage content could be null or not. The Assertions will now apply based on the boolean 'pass' parameter.

* Add ProducersActions tests and update HTTP request handling

New unit tests for `ProducersActions` have been included. Additionally, decompression handling has been refined for `QuickArchiveBuilder` and the HTTP request raw header format has been updated in `Producers` tests.

* Add ContentLength assertion in Producers unit test

Added an assertion in the Producers.cs structure of the Fluxzy.Tests to ensure that the ContentLength attribute of the result is greater than 0. This is to prevent a situation where a result is obtained but there is no content within it.

* Add unit test for ResponseBodyJsonProducer

A new unit test has been added for the ResponseBodyJsonProducer class. This test checks whether the producer can correctly build and format responses based on different URLs. It uses Assert methods to verify the results.

* Add new test case to ResponseBodyJsonProducer

An additional test case has been added to the unit test for the method ResponseBodyJsonProducer. This ensures that the method correctly handles different inputs.

* Add additional assertions in Producers unit tests
.

* Enhance ProducerContext unit test in Fluxzy.Tests.

---------

Co-authored-by: fluxzy-ci <admin@fluxzy.io>
  • Loading branch information
haga-rak and fluxzy-ci authored Jan 9, 2024
1 parent ff21d5f commit 9d83861
Show file tree
Hide file tree
Showing 7 changed files with 375 additions and 11 deletions.
32 changes: 32 additions & 0 deletions src/Fluxzy.Core/ArchivingPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2021 - Haga Rakotoharivelo - https://github.com/haga-rak

using System;
using System.IO;
using System.Text.Json.Serialization;

Expand Down Expand Up @@ -60,6 +61,37 @@ public static ArchivingPolicy CreateFromDirectory(string path)

return CreateFromDirectory(directoryInfo);
}

protected bool Equals(ArchivingPolicy other)
{
return Type == other.Type && Directory == other.Directory;
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

if (obj.GetType() != this.GetType())
{
return false;
}

return Equals((ArchivingPolicy)obj);
}

public override int GetHashCode()
{
return HashCode.Combine((int)Type, Directory);
}

}

/// <summary>
Expand Down
15 changes: 14 additions & 1 deletion test/Fluxzy.Tests/UnitTests/Formatters/ProducerContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@ public async Task ProducerFactoryCreate()
var producerFactory = new ProducerFactory(archiveReaderProvider, FormatSettings.Default);
var firstExchange = archiveReader.ReadAllExchanges().First();

_ = await producerFactory.GetProducerContext(firstExchange.Id);
var context = await producerFactory.GetProducerContext(firstExchange.Id);

Assert.NotNull(context);

_ = producerFactory.GetRequestFormattedResults(firstExchange.Id, context).ToList();
var responses = producerFactory.GetResponseFormattedResults(firstExchange.Id, context).ToList();

Assert.NotEmpty(responses);

foreach (var response in responses) {
Assert.NotNull(response.Title);
Assert.NotNull(response.Type);
Assert.Null(response.ErrorMessage);
}
}
}
}
169 changes: 161 additions & 8 deletions test/Fluxzy.Tests/UnitTests/Formatters/Producers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Xunit;
using System.Threading.Tasks;
using Fluxzy.Formatters.Producers.Requests;
using Fluxzy.Formatters.Producers.Responses;
using Fluxzy.Tests._Fixtures;

namespace Fluxzy.Tests.UnitTests.Formatters
Expand Down Expand Up @@ -37,6 +38,9 @@ public async Task QueryString(string url)
}
else {
Assert.NotNull(result);
Assert.NotNull(result.Title);
Assert.Null(result.ErrorMessage);
Assert.NotNull(result.Type);
Assert.Equal(queryNameValue.Count, result.Items.Count);

foreach (var item in result.Items)
Expand Down Expand Up @@ -173,7 +177,7 @@ public async Task RawRequestHeaderProducer(string url)
var result = producer.Build(firstExchange, producerContext);

Assert.NotNull(result);
Assert.Equal("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n", result.RawHeader);
Assert.Equal("GET / HTTP/1.1\r\nHost: example.com\r\nAccept-Encoding: gzip, deflate, br\r\n\r\n", result.RawHeader);
}

[Theory]
Expand All @@ -199,25 +203,33 @@ public async Task RequestBodyAnalysis(string url)
}

[Theory]
[InlineData("https://example.com")]
public async Task RequestJsonBodyProducer(string url)
[InlineData("https://example.com", true)]
[InlineData("https://example.com", false)]
public async Task RequestJsonBodyProducer(string url, bool pass)
{
var randomFile = GetRegisteredRandomFile();
var uri = new Uri(url);

var producer = new RequestJsonBodyProducer();
var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri);
requestMessage.Content = new StringContent("{ }", Encoding.UTF8, "application/json");

if (pass)
requestMessage.Content = new StringContent("{ }", Encoding.UTF8, "application/json");

await QuickArchiveBuilder.MakeQuickArchive(requestMessage, randomFile);

var (producerContext, firstExchange) = await Init(randomFile);

var result = producer.Build(firstExchange, producerContext);

Assert.NotNull(result);
Assert.Equal("{ }", result.RawBody);
Assert.Equal("{}", result.FormattedBody);

if (pass) {
Assert.NotNull(result);
Assert.Equal("{ }", result.RawBody);
Assert.Equal("{}", result.FormattedBody);
}
else {
Assert.Null(result);
}
}

[Theory]
Expand All @@ -240,5 +252,146 @@ public async Task RequestTextBodyProducer(string url)
Assert.NotNull(result);
Assert.Equal("{ }", result.Text);
}

[Theory]
[InlineData("https://sandbox.smartizy.com/global-health-check", true)]
[InlineData("https://sandbox.smartizy.com/content-produce/0/0", false)]
public async Task ResponseBodySummaryProducer(string url, bool match)
{
var randomFile = GetRegisteredRandomFile();
var uri = new Uri(url);

var producer = new ResponseBodySummaryProducer();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);

await QuickArchiveBuilder.MakeQuickArchive(requestMessage, randomFile);

var (producerContext, firstExchange) = await Init(randomFile);

var result = producer.Build(firstExchange, producerContext);

if (match) {
Assert.NotNull(result);
Assert.NotNull(result.BodyText);
Assert.NotNull(result.PreferredFileName);
Assert.NotNull(result.Compression);
Assert.True(result.ContentLength > 0);
Assert.Equal("application/json; charset=utf-8", result.ContentType);
}
else {
Assert.Null(result);
}
}

[Theory]
[InlineData("https://sandbox.smartizy.com/global-health-check", true)]
[InlineData("https://sandbox.smartizy.com/content-produce/0/0", false)]
[InlineData("https://example.com", false)]
public async Task ResponseBodyJsonProducer(string url, bool match)
{
var randomFile = GetRegisteredRandomFile();
var uri = new Uri(url);

var producer = new ResponseBodyJsonProducer();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);

await QuickArchiveBuilder.MakeQuickArchive(requestMessage, randomFile);

var (producerContext, firstExchange) = await Init(randomFile);

var result = producer.Build(firstExchange, producerContext);

if (match) {
Assert.NotNull(result);
Assert.NotNull(result.FormattedContent);
}
else {
Assert.Null(result);
}
}

[Theory]
[InlineData("https://sandbox.smartizy.com/global-health-check", true)]
[InlineData("https://sandbox.smartizy.com/content-produce/0/0", false)]
public async Task ResponseTextContentProducer(string url, bool match)
{
var randomFile = GetRegisteredRandomFile();
var uri = new Uri(url);

var producer = new ResponseTextContentProducer();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);

await QuickArchiveBuilder.MakeQuickArchive(requestMessage, randomFile);

var (producerContext, firstExchange) = await Init(randomFile);

var result = producer.Build(firstExchange, producerContext);

if (match) {
Assert.NotNull(result);
}
else {
Assert.Null(result);
}
}
[Theory]
[InlineData("https://registry.2befficient.io:40300/cookies/set/abc/def", true)]
[InlineData("https://sandbox.smartizy.com/content-produce/0/0", false)]
public async Task SetCookieProducer(string url, bool match)
{
var randomFile = GetRegisteredRandomFile();
var uri = new Uri(url);

var producer = new SetCookieProducer();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);

await QuickArchiveBuilder.MakeQuickArchive(requestMessage, randomFile);

var (producerContext, firstExchange) = await Init(randomFile);

var result = producer.Build(firstExchange, producerContext);

if (match) {
Assert.NotNull(result);
Assert.Single(result.Cookies);
Assert.Equal("abc", result.Cookies[0].Name);
Assert.Equal("def", result.Cookies[0].Value);
Assert.Null(result.Cookies[0].Domain);
Assert.Equal("/", result.Cookies[0].Path);
Assert.Null(result.Cookies[0].SameSite);
Assert.False(result.Cookies[0].Secure);
Assert.False(result.Cookies[0].HttpOnly);
}
else {
Assert.Null(result);
}
}

[Theory]
[InlineData("https://www.fluxzy.io/assets/images/logo-small.png", true)]
[InlineData("https://www.fluxzy.io/favicon.ico", true)]
[InlineData("https://example.com", false)]
public async Task ImageResultProducer(string url, bool image)
{
var randomFile = GetRegisteredRandomFile();
var uri = new Uri(url);

var producer = new ImageResultProducer();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);

await QuickArchiveBuilder.MakeQuickArchive(requestMessage, randomFile);

var (producerContext, firstExchange) = await Init(randomFile);

var result = producer.Build(firstExchange, producerContext);

if (image) {
Assert.NotNull(result);
Assert.Contains("image", result.ContentType);
}
else {
Assert.Null(result);
}
}
}
}
87 changes: 87 additions & 0 deletions test/Fluxzy.Tests/UnitTests/Formatters/ProducersActions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2021 - Haga Rakotoharivelo - https://github.com/haga-rak

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Fluxzy.Formatters;
using Fluxzy.Formatters.Producers.ProducerActions.Actions;
using Fluxzy.Tests._Fixtures;
using Xunit;

namespace Fluxzy.Tests.UnitTests.Formatters
{
public class ProducersActions : FormatterTestBase
{
[Theory]
[InlineData("https://example.com", true, true)]
[InlineData("https://example.com", false, true)]
[InlineData("https://sandbox.smartizy.com/swagger/index.html", false, true)]
[InlineData("https://sandbox.smartizy.com/swagger/index.html", true, true)]
public async Task SaveResponseBodyAction(string url, bool decode, bool pass)
{
var randomFile = GetRegisteredRandomFile();
var outFile = GetRegisteredRandomFile();
var uri = new Uri(url);

var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);

await QuickArchiveBuilder.MakeQuickArchive(requestMessage, randomFile, setting => {
setting.UseBouncyCastle = true;
} );

var archiveReaderProvider = new FromFileArchiveFileProvider(randomFile);

var producerFactory = new ProducerFactory(archiveReaderProvider, FormatSettings.Default);

var action = new SaveResponseBodyAction(producerFactory);

var result = await action.Do(2, decode, outFile);

if (pass) {
Assert.True(result);
Assert.True(System.IO.File.Exists(outFile));
}
else {
Assert.False(result);
Assert.False(System.IO.File.Exists(outFile));
}
}

[Theory]
[InlineData("https://example.com", true)]
[InlineData("https://example.com", false)]
public async Task SaveRequestBodyAction(string url, bool pass)
{
var randomFile = GetRegisteredRandomFile();
var outFile = GetRegisteredRandomFile();
var uri = new Uri(url);

var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri);

if (pass) {
requestMessage.Content = new StringContent("Hello world");
}

await QuickArchiveBuilder.MakeQuickArchive(requestMessage, randomFile, setting => {
setting.UseBouncyCastle = true;
} );

var archiveReaderProvider = new FromFileArchiveFileProvider(randomFile);

var producerFactory = new ProducerFactory(archiveReaderProvider, FormatSettings.Default);

var action = new SaveRequestBodyProducerAction(producerFactory);

var result = await action.Do(2, outFile);

if (pass) {
Assert.True(result);
Assert.True(System.IO.File.Exists(outFile));
}
else {
Assert.False(result);
Assert.False(System.IO.File.Exists(outFile));
}
}
}
}
Loading

0 comments on commit 9d83861

Please sign in to comment.