Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FOUNDATIONS: POST Async with CancellationToken IHttpExchangeService #57

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
be0d85d
ShouldSendHttpRequestWhenPostAsyncIsCalledAsync -> WIP
aosunlana Aug 10, 2024
341b1cb
Merge branch 'main' into users/aosunlana/foundations-httpclient-posta…
LBoullosa Aug 14, 2024
e0ae0ea
ShouldSendHttpRequestWhenPostAsyncIsCalledAsync -> WIP
aosunlana Aug 16, 2024
ae132ff
Merge branch 'users/aosunlana/foundations-httpclient-postasync' of ht…
aosunlana Aug 16, 2024
3973bbc
Merge branch 'main' into users/aosunlana/foundations-httpclient-posta…
LBoullosa Aug 25, 2024
715e1b8
CODE RUB: Refactor CreateRandomHttpProperties Method
aosunlana Aug 25, 2024
b72e713
CODE RUB: Fix variable naming
LBoullosa Aug 26, 2024
b93b64e
ShouldSendHttpRequestWhenPostAsyncIsCalledAsync -> WIP
LBoullosa Aug 26, 2024
41b4938
ShouldSendHttpRequestWhenPostAsyncIsCalledAsync -> FAIL
aosunlana Aug 28, 2024
5303d44
CODE RUB: Re-arrange parameters in ShouldPostAsync Test
aosunlana Aug 28, 2024
fc0ab76
ShouldSendHttpRequestWhenPostAsyncIsCalledAsync -> PASS
aosunlana Aug 28, 2024
553f724
ShouldThrowHttpExchangeValidationExceptionOnPostIfHttpExchangeRequest…
aosunlana Oct 16, 2024
1ade7e7
ShouldThrowHttpExchangeValidationExceptionOnPostIfHttpExchangeRequest…
aosunlana Oct 16, 2024
ec43317
CODE RUB: Code Cleanup
aosunlana Oct 16, 2024
2b8119a
CODE RUB: Code Cleanup
aosunlana Oct 27, 2024
15c9b31
ShouldThrowHttpExchangeValidationExceptionOnPostIfHttpExchangeRequest…
aosunlana Oct 27, 2024
91ebef5
ShouldThrowHttpExchangeValidationExceptionOnPostIfHttpExchangeRequest…
aosunlana Oct 27, 2024
a497c28
CODE RUB: Remove extra parenthesis from new instance
LBoullosa Oct 27, 2024
7b36022
CODE RUB: Change return formatting
LBoullosa Oct 27, 2024
8b395b9
CODE RUB: Fix formating TryCatch
LBoullosa Oct 27, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,18 @@ private static Filler<HttpExchangeResponseHeaders> CreateHttpExchangeResponseHea
}

private static HttpExchangeContentHeaders CreateHttpExchangeContentHeaders(
dynamic randomHeaderProperties) =>
CreateHttpExchangeContentHeadersFiller(randomHeaderProperties).Create();
dynamic randomHeaderProperties)
{
HttpExchangeContentHeaders httpExchangeContentHeaders = null;

if (randomHeaderProperties is not null)
{
httpExchangeContentHeaders =
CreateHttpExchangeContentHeadersFiller(randomHeaderProperties).Create();
}

return httpExchangeContentHeaders;
}

private static Filler<HttpExchangeContentHeaders> CreateHttpExchangeContentHeadersFiller(
dynamic randomHeaderProperties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ private async Task ShouldSendHttpRequestWhenGetAsyncIsCalledAsync(
CreateRandomHttpProperties(
sendUrlParameters,
sendRequestHeaders,
sendRequestContent: false);
sendRequestContent: false,
sendContentHeaders: false,
httpMethod: HttpMethod.Get);

HttpExchange inputHttpExchange =
CreateRandomHttpExchangeRequest(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using STX.REST.RESTFulSense.Clients.Models.Services.HttpExchanges;
using Xunit;

namespace STX.REST.RESTFulSense.Clients.Tests.Unit.Services.Foundations.HttpExchanges
{
public partial class HttpExchangeServiceTests
{
[Theory]
[InlineData(false, false, false, false)]
[InlineData(false, false, true, false)]
[InlineData(false, false, true, true)]
[InlineData(false, true, false, false)]
[InlineData(false, true, true, false)]
[InlineData(false, true, true, true)]
[InlineData(true, false, false, false)]
[InlineData(true, false, true, false)]
[InlineData(true, false, true, true)]
[InlineData(true, true, false, false)]
[InlineData(true, true, true, false)]
[InlineData(true, true, true, true)]
private async Task ShouldSendHttpRequestWhenPostAsyncIsCalledAsync(
bool sendUrlParameters,
bool sendRequestHeaders,
bool sendRequestContent,
bool sendRequestContentHeaders)
{
// given
dynamic randomProperties =
CreateRandomHttpProperties(
sendUrlParameters,
sendRequestHeaders,
sendRequestContent,
sendRequestContentHeaders,
httpMethod: HttpMethod.Post);

HttpExchange inputHttpExchange =
CreateRandomHttpExchangeRequest(
randomProperties);

HttpRequestMessage inputHttpRequestMessage =
CreateHttpRequestMessage(randomProperties);

HttpRequestMessage expectedHttpRequestMessage =
inputHttpRequestMessage;

HttpExchange expectedHttpExchange =
CreateHttpExchangeResponse(
httpExchange: inputHttpExchange,
randomProperties: randomProperties);

HttpResponseMessage randomHttpResponseMessage =
CreateHttpResponseMessage(randomProperties);

HttpResponseMessage returnedHttpResponseMessage =
randomHttpResponseMessage;

this.httpBroker
.Setup(broker =>
broker.SendRequestAsync(
It.IsAny<HttpRequestMessage>(),
default))
.ReturnsAsync(returnedHttpResponseMessage);

// when
HttpExchange actualHttpExchange =
await this.httpExchangeService.PostAsync(
inputHttpExchange);

// then
actualHttpExchange.Should().BeEquivalentTo(
expectedHttpExchange,
options =>
options.Excluding(httpExchange =>
httpExchange.Response.Content.StreamContent));

byte[] actualStreamContent =
await ConvertStreamToByteArray(
actualHttpExchange.Response.Content.StreamContent);

byte[] expectedStreamContent =
await ConvertStreamToByteArray(
expectedHttpExchange.Response.Content.StreamContent);

actualStreamContent.Should().BeEquivalentTo(
expectedStreamContent);

this.httpBroker.Verify(
broker =>
broker.SendRequestAsync(
It.Is<HttpRequestMessage>(actualHttpRequestMessage =>
SameHttpRequestMessageAs(
actualHttpRequestMessage,
expectedHttpRequestMessage)
.Compile()
.Invoke(actualHttpRequestMessage)),
default),
Times.Once);

this.httpBroker.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private async Task ShouldThrowHttpExchangeValidationExceptionOnGetIfHttpExchange
int invalidHttpVersionPolicy)
{
// given
var httpExchange = new HttpExchange()
var httpExchange = new HttpExchange
{
Request = new HttpExchangeRequest
{
Expand All @@ -101,7 +101,7 @@ private async Task ShouldThrowHttpExchangeValidationExceptionOnGetIfHttpExchange
}
};

var invalidHttpExchangeRequestException =
var invalidHttpExchangeRequestException =
new InvalidHttpExchangeRequestException(
message: "Invalid request, fix errors and try again.");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System.Threading.Tasks;
using FluentAssertions;
using STX.REST.RESTFulSense.Clients.Models.Services.HttpExchanges;
using STX.REST.RESTFulSense.Clients.Models.Services.HttpExchanges.Exceptions;
using Xunit;

namespace STX.REST.RESTFulSense.Clients.Tests.Unit.Services.Foundations.HttpExchanges
{
public partial class HttpExchangeServiceTests
{
[Fact]
private async Task ShouldThrowHttpExchangeValidationExceptionOnPostIfHttpExchangeIsInvalidAsync()
{
// given
HttpExchange nullHttpExchange = null;

var invalidHttpExchangeException =
new InvalidHttpExchangeException(
message: "Invalid HttpExchange error occurred, fix errors and try again.");

invalidHttpExchangeException.UpsertDataList(
key: nameof(HttpExchange),
value: "Value is required");

var expectedHttpExchangeValidationException =
new HttpExchangeValidationException(
message: "HttpExchange validation errors occurred, fix errors and try again.",
innerException: invalidHttpExchangeException);

// when
ValueTask<HttpExchange> getAsyncTask = httpExchangeService.GetAsync(nullHttpExchange);

HttpExchangeValidationException actualHttpExchangeValidationException =
await Assert.ThrowsAsync<HttpExchangeValidationException>(
getAsyncTask.AsTask);

// then
actualHttpExchangeValidationException.Should().BeEquivalentTo(
expectedHttpExchangeValidationException);

this.httpBroker.VerifyNoOtherCalls();
}

[Fact]
public async Task ShouldThrowHttpExchangeValidationExceptionOnPostIfHttpExchangeRequestIsNullAsync()
{
// given
var httpExchange = new HttpExchange();

var nullHttpExchangeRequestException =
new NullHttpExchangeRequestException(
message: "Null HttpExchangeRequest error occurred, fix errors and try again.");

var expectedHttpExchangeValidationException =
new HttpExchangeValidationException(
message: "HttpExchange validation errors occurred, fix errors and try again.",
innerException: nullHttpExchangeRequestException);

// when
ValueTask<HttpExchange> postAsyncTask = this.httpExchangeService.PostAsync(httpExchange);

HttpExchangeValidationException actualHttpExchangeValidationException =
await Assert.ThrowsAsync<HttpExchangeValidationException>(postAsyncTask.AsTask);

// then
actualHttpExchangeValidationException.Should().BeEquivalentTo(
expectedHttpExchangeValidationException);

this.httpBroker.VerifyNoOtherCalls();
}

[Theory]
[InlineData(null, null, "GET", "0.1", InvalidVersionPolicy)]
[InlineData("", "", "GET", "0.1", InvalidVersionPolicy)]
[InlineData(" ", " ", "GET", "0.1", InvalidVersionPolicy)]
private async Task ShouldThrowHttpExchangeValidationExceptionOnPostIfHttpExchangeRequestIsInvalidAsync(
string invalidBaseAddress,
string invalidRelativeUrl,
string invalidHttpMethod,
string invalidHttpVersion,
int invalidHttpVersionPolicy)
{
// given
var httpExchange = new HttpExchange
{
Request = new HttpExchangeRequest
{
BaseAddress = invalidBaseAddress,
RelativeUrl = invalidRelativeUrl,
HttpMethod = invalidHttpMethod,
Version = invalidHttpVersion,
VersionPolicy = invalidHttpVersionPolicy
}
};

var invalidHttpExchangeRequestException =
new InvalidHttpExchangeRequestException(
message: "Invalid request, fix errors and try again.");

invalidHttpExchangeRequestException.UpsertDataList(
key: nameof(HttpExchangeRequest.BaseAddress),
value: "Value is required");

invalidHttpExchangeRequestException.UpsertDataList(
key: nameof(HttpExchangeRequest.RelativeUrl),
value: "Value is required");

invalidHttpExchangeRequestException.UpsertDataList(
key: nameof(HttpExchangeRequest.HttpMethod),
value: "HttpMethod is invalid");

invalidHttpExchangeRequestException.UpsertDataList(
key: nameof(HttpExchangeRequest.Version),
value: "HttpVersion is invalid");

invalidHttpExchangeRequestException.UpsertDataList(
key: nameof(HttpExchangeRequest.VersionPolicy),
value: "HttpVersionPolicy is invalid");

var expectedHttpExchangeValidationException =
new HttpExchangeValidationException(
message: "HttpExchange validation errors occurred, fix errors and try again.",
innerException: invalidHttpExchangeRequestException);

// when
ValueTask<HttpExchange> postAsyncTask = httpExchangeService.PostAsync(httpExchange);

HttpExchangeValidationException actualHttpExchangeValidationException =
await Assert.ThrowsAsync<HttpExchangeValidationException>(postAsyncTask.AsTask);

// then
actualHttpExchangeValidationException.Should().BeEquivalentTo(
expectedHttpExchangeValidationException);

this.httpBroker.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private static IDictionary<string, object> CreateRandomUrlParameters()
Key = GetRandomString(),
Value = value
})
.DistinctBy(x=>x.Key)
.DistinctBy(x => x.Key)
glhays marked this conversation as resolved.
Show resolved Hide resolved
.ToDictionary(
key => key.Key,
value => value.Value);
Expand Down Expand Up @@ -243,19 +243,26 @@ private static HttpStatusCode GetRandomHttpStatusCode()
private static long CreateRandomLongToValue(long from) =>
new LongRange(min: from, max: 10).GetValue();

private static dynamic CreateRandomHttpContent()
private static dynamic CreateRandomHttpContent(bool createContentHeaders)
{
dynamic contentHeaders = null;

if (createContentHeaders)
contentHeaders = CreateRandomHttpContentHeader();

return new
{
Headers = CreateRandomHttpContentHeader(),
Headers = contentHeaders,
StreamContent = CreateRandomStream()
};
}

private static dynamic CreateRandomHttpProperties(
bool sendUrlParameters,
bool sendRequestHeaders,
bool sendRequestContent)
bool sendRequestContent,
bool sendContentHeaders,
HttpMethod httpMethod)
{
Uri randomUri = CreateRandomUri();

Expand All @@ -272,7 +279,7 @@ private static dynamic CreateRandomHttpProperties(

dynamic requestContent = default;
if (sendRequestContent)
requestContent = CreateRandomHttpContent();
requestContent = CreateRandomHttpContent(sendContentHeaders);

return new
{
Expand All @@ -282,7 +289,7 @@ private static dynamic CreateRandomHttpProperties(
RelativeUrl = randomUri.PathAndQuery,
Url = randomUri,
UrlParameters = randomUrlParameters,
HttpMethod = HttpMethod.Get,
HttpMethod = httpMethod,
Version = GetRandomHttpVersion(),
VersionPolicy = GetRandomHttpVersionPolicy(),
StatusCode = randomStatusCode,
Expand All @@ -295,7 +302,7 @@ private static dynamic CreateRandomHttpProperties(
RequestHeaders = requestHeaders,
ResponseHeaders = CreateRandomHttpResponseHeader(),
RequestContent = requestContent,
ResponseContent = CreateRandomHttpContent()
ResponseContent = CreateRandomHttpContent(true)
};
}

Expand All @@ -308,7 +315,7 @@ private static HttpExchange CreateRandomHttpExchangeRequest(
{
BaseAddress = randomProperties.BaseAddress,
RelativeUrl = randomProperties.RelativeUrl,
HttpMethod = HttpMethod.Get.Method,
HttpMethod = randomProperties.HttpMethod.Method,
Version = randomProperties.Version.ToString(),
UrlParameters = randomProperties.UrlParameters,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using Xeptions;

namespace STX.REST.RESTFulSense.Clients.Models.Services.HttpExchanges.Exceptions
Expand Down
Loading
Loading