Skip to content

Commit

Permalink
Broke down the CREATED status with Location Header checks to give pos…
Browse files Browse the repository at this point in the history
…sibility of providing custom header easier.

Added also RESPONSE_HEADERS helper and aligned response headers check with it
  • Loading branch information
oskardudycz committed Jun 28, 2022
1 parent a3bd8be commit c4b4641
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Ogooreck.Sample.Api.Tests/ApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Task RegisterProduct() =>
BODY(new RegisterProductRequest("abc-123", "Ogooreck"))
)
.When(POST)
.Then(CREATED);
.Then(CREATED, RESPONSE_LOCATION_HEADER());

#endregion ApiPostSample
}
64 changes: 38 additions & 26 deletions src/Ogooreck/API/ApiSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,33 +129,16 @@ public static Func<HttpClient, Func<HttpRequestMessage>, Task<HttpResponseMessag
//// THEN ////
///////////////////
public static Func<HttpResponseMessage, ValueTask> OK = HTTP_STATUS(HttpStatusCode.OK);

public static Func<HttpResponseMessage, ValueTask> CREATED =>
async response =>
{
await HTTP_STATUS(HttpStatusCode.Created)(response);

var locationHeader = response.Headers.Location;

locationHeader.Should().NotBeNull();

var location = locationHeader!.ToString();

location.Should().StartWith(response.RequestMessage!.RequestUri!.AbsolutePath);
};


public static Func<HttpResponseMessage, ValueTask> CREATED = HTTP_STATUS(HttpStatusCode.Created);
public static Func<HttpResponseMessage, ValueTask> NO_CONTENT = HTTP_STATUS(HttpStatusCode.NoContent);

public static Func<HttpResponseMessage, ValueTask> BAD_REQUEST = HTTP_STATUS(HttpStatusCode.BadRequest);
public static Func<HttpResponseMessage, ValueTask> NOT_FOUND = HTTP_STATUS(HttpStatusCode.NotFound);
public static Func<HttpResponseMessage, ValueTask> CONFLICT = HTTP_STATUS(HttpStatusCode.Conflict);

public static Func<HttpResponseMessage, ValueTask> PRECONDITION_FAILED =
HTTP_STATUS(HttpStatusCode.PreconditionFailed);

public static Func<HttpResponseMessage, ValueTask>
METHOD_NOT_ALLOWED = HTTP_STATUS(HttpStatusCode.MethodNotAllowed);
public static Func<HttpResponseMessage, ValueTask> METHOD_NOT_ALLOWED =
HTTP_STATUS(HttpStatusCode.MethodNotAllowed);

public static Func<HttpResponseMessage, ValueTask> HTTP_STATUS(HttpStatusCode status) =>
response =>
Expand All @@ -177,14 +160,43 @@ public static Func<HttpResponseMessage, ValueTask> RESPONSE_BODY<T>(Action<T> as
};

public static Func<HttpResponseMessage, ValueTask<bool>> RESPONSE_ETAG_IS(object eTag, bool isWeak = true) =>
response =>
async response =>
{
response.Headers.ETag.Should().NotBeNull().And.NotBe("");
response.Headers.ETag!.Tag.Should().NotBeEmpty();
await RESPONSE_ETAG_HEADER(eTag, isWeak)(response);
return true;
};

response.Headers.ETag.IsWeak.Should().Be(isWeak);
response.Headers.ETag.Tag.Should().Be($"\"{eTag}\"");
return new ValueTask<bool>(true);
public static Func<HttpResponseMessage, ValueTask> RESPONSE_ETAG_HEADER(object eTag, bool isWeak = true) =>
RESPONSE_HEADERS(headers =>
{
headers.ETag.Should().NotBeNull().And.NotBe("");
headers.ETag!.Tag.Should().NotBeEmpty();

headers.ETag.IsWeak.Should().Be(isWeak);
headers.ETag.Tag.Should().Be($"\"{eTag}\"");
});

public static Func<HttpResponseMessage, ValueTask> RESPONSE_LOCATION_HEADER(string? prefix = null) =>
async response =>
{
await HTTP_STATUS(HttpStatusCode.Created)(response);

var locationHeader = response.Headers.Location;

locationHeader.Should().NotBeNull();

var location = locationHeader!.ToString();

location.Should().StartWith(prefix ?? response.RequestMessage!.RequestUri!.AbsolutePath);
};
public static Func<HttpResponseMessage, ValueTask> RESPONSE_HEADERS(params Action<HttpResponseHeaders>[] headers) =>
response =>
{
foreach (var header in headers)
{
header(response.Headers);
}
return ValueTask.CompletedTask;
};

public static Func<HttpResponseMessage, ValueTask<bool>> RESPONSE_SUCCEEDED() =>
Expand Down
2 changes: 1 addition & 1 deletion src/Ogooreck/Ogooreck.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>0.1.1</VersionPrefix>
<VersionPrefix>0.1.2</VersionPrefix>
<TargetFramework>net6.0</TargetFramework>
<GenerateAssemblyTitleAttribute>true</GenerateAssemblyTitleAttribute>
<GenerateAssemblyDescriptionAttribute>true</GenerateAssemblyDescriptionAttribute>
Expand Down

0 comments on commit c4b4641

Please sign in to comment.