Skip to content

Commit

Permalink
Added helpers for easier setup and fixed built-in checks for retry po…
Browse files Browse the repository at this point in the history
…licy
  • Loading branch information
oskardudycz committed Aug 15, 2023
1 parent 8ee0c28 commit 9f960a0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
67 changes: 53 additions & 14 deletions src/Ogooreck/API/ApiSpecificationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ public static class ApiSpecification
//// GIVEN ////
///////////////////

public static GivenApiSpecificationBuilder Given<TProgram>(this ApiSpecification<TProgram> api,
params RequestTransform[] when) where TProgram : class =>
api.Given(new RequestDefinition(when));


public static GivenApiSpecificationBuilder Given<TProgram>(this ApiSpecification<TProgram> api,
string description,
params RequestTransform[] when) where TProgram : class =>
api.Given(description, new RequestDefinition(when, description));

///////////////////
//// WHEN ////
///////////////////
Expand Down Expand Up @@ -103,11 +113,29 @@ public static Task And(this Task<Result> result, Task and) =>
result.ContinueWith(_ => and);

public static Task<GivenApiSpecificationBuilder> And(this Task<Result> result) =>
result.ContinueWith(_ => new GivenApiSpecificationBuilder(result.Result.TestContext, result.Result.CreateClient));
result.ContinueWith(
_ => new GivenApiSpecificationBuilder(result.Result.TestContext, result.Result.CreateClient));

public static Task<WhenApiSpecificationBuilder> AndWhen(
this Task<Result> result,
string description,
params RequestTransform[] when
) =>
result.And().When(description, when);

public static Task<WhenApiSpecificationBuilder> AndWhen(this Task<Result> result, params RequestTransform[] when) =>
result.And().When(when);

public static Task<WhenApiSpecificationBuilder> AndWhen(
this Task<Result> result,
string description,
Func<HttpResponseMessage, RequestTransform[]> when
) =>
result.ContinueWith(r =>
new GivenApiSpecificationBuilder(r.Result.TestContext, r.Result.CreateClient)
.When(description, when(r.Result.Response))
);

public static Task<WhenApiSpecificationBuilder> AndWhen(
this Task<Result> result,
Func<HttpResponseMessage, RequestTransform[]> when
Expand All @@ -116,6 +144,13 @@ Func<HttpResponseMessage, RequestTransform[]> when
new GivenApiSpecificationBuilder(r.Result.TestContext, r.Result.CreateClient).When(when(r.Result.Response))
);

public static Task<WhenApiSpecificationBuilder> When(
this Task<GivenApiSpecificationBuilder> result,
string description,
params RequestTransform[] when
) =>
result.ContinueWith(_ => result.Result.When(description, when));

public static Task<WhenApiSpecificationBuilder> When(
this Task<GivenApiSpecificationBuilder> result,
params RequestTransform[] when
Expand All @@ -124,7 +159,7 @@ params RequestTransform[] when

public static Task<WhenApiSpecificationBuilder> Until(
this Task<WhenApiSpecificationBuilder> when,
Func<HttpResponseMessage, TestContext, ValueTask<bool>> check,
RetryCheck check,
int maxNumberOfRetries = 5,
int retryIntervalInMs = 1000
) =>
Expand Down Expand Up @@ -216,14 +251,6 @@ public static Func<HttpResponseMessage, Task<T>> RESPONSE_BODY<T>() =>
public static Func<HttpResponseMessage, Task<T>> CREATED_ID<T>() =>
response => Task.FromResult(response.GetCreatedId<T>());

public static Func<HttpResponseMessage, TestContext, ValueTask<bool>> RESPONSE_ETAG_IS(object eTag,
bool isWeak = true) =>
async (response, ctx) =>
{
await RESPONSE_ETAG_HEADER(eTag, isWeak)(response, ctx);
return true;
};

public static ResponseAssert RESPONSE_ETAG_HEADER(object eTag, bool isWeak = true) =>
RESPONSE_HEADERS(headers =>
{
Expand Down Expand Up @@ -260,15 +287,27 @@ public static ResponseAssert RESPONSE_HEADERS(params Action<HttpResponseHeaders>
return ValueTask.CompletedTask;
};

public static Func<HttpResponseMessage, ValueTask<bool>> RESPONSE_SUCCEEDED() =>
response =>
/////////////////
// UNTIL
////////////////

public static RetryCheck RESPONSE_ETAG_IS(object eTag,
bool isWeak = true) =>
async (response, ctx) =>
{
await RESPONSE_ETAG_HEADER(eTag, isWeak)(response, ctx);
return true;
};

public static RetryCheck RESPONSE_SUCCEEDED() =>
(response, ctx) =>
{
response.EnsureSuccessStatusCode();
return new ValueTask<bool>(true);
};

public static Func<HttpResponseMessage, ValueTask<bool>> RESPONSE_BODY_MATCHES<TBody>(Func<TBody, bool> assert) =>
async response =>
public static RetryCheck RESPONSE_BODY_MATCHES<TBody>(Func<TBody, bool> assert) =>
async (response, ctx) =>
{
response.EnsureSuccessStatusCode();
Expand Down
5 changes: 4 additions & 1 deletion src/Ogooreck/API/RetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Ogooreck.API;

#pragma warning disable CS1591
public record RetryPolicy(
Func<HttpResponseMessage, TestContext, ValueTask<bool>> Check,
RetryCheck Check,
int MaxNumberOfRetries = 5,
int RetryIntervalInMs = 1000
)
Expand Down Expand Up @@ -44,3 +44,6 @@ public async Task<HttpResponseMessage> Perform(Func<CancellationToken, Task<Http
0
);
}

public delegate ValueTask<bool> RetryCheck(HttpResponseMessage responseMessage, TestContext testContext);

2 changes: 1 addition & 1 deletion src/Ogooreck/API/TestBuilders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ApiTestStep when
}

public WhenApiSpecificationBuilder Until(
Func<HttpResponseMessage, TestContext, ValueTask<bool>> check,
RetryCheck check,
int maxNumberOfRetries = 5,
int retryIntervalInMs = 1000
)
Expand Down

0 comments on commit 9f960a0

Please sign in to comment.