diff --git a/src/Ogooreck.Sample.Api.Tests/ApiTests.cs b/src/Ogooreck.Sample.Api.Tests/ApiTests.cs index e0e6c83..d7da25d 100644 --- a/src/Ogooreck.Sample.Api.Tests/ApiTests.cs +++ b/src/Ogooreck.Sample.Api.Tests/ApiTests.cs @@ -29,7 +29,7 @@ public Task RegisterProduct() => BODY(new RegisterProductRequest("abc-123", "Ogooreck")) ) .When(POST) - .Then(CREATED); + .Then(CREATED, RESPONSE_LOCATION_HEADER()); #endregion ApiPostSample } diff --git a/src/Ogooreck/API/ApiSpecification.cs b/src/Ogooreck/API/ApiSpecification.cs index 7a32de6..69aef1d 100644 --- a/src/Ogooreck/API/ApiSpecification.cs +++ b/src/Ogooreck/API/ApiSpecification.cs @@ -129,33 +129,16 @@ public static Func, Task OK = HTTP_STATUS(HttpStatusCode.OK); - - public static Func 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 CREATED = HTTP_STATUS(HttpStatusCode.Created); public static Func NO_CONTENT = HTTP_STATUS(HttpStatusCode.NoContent); - public static Func BAD_REQUEST = HTTP_STATUS(HttpStatusCode.BadRequest); public static Func NOT_FOUND = HTTP_STATUS(HttpStatusCode.NotFound); public static Func CONFLICT = HTTP_STATUS(HttpStatusCode.Conflict); - public static Func PRECONDITION_FAILED = HTTP_STATUS(HttpStatusCode.PreconditionFailed); - public static Func - METHOD_NOT_ALLOWED = HTTP_STATUS(HttpStatusCode.MethodNotAllowed); + public static Func METHOD_NOT_ALLOWED = + HTTP_STATUS(HttpStatusCode.MethodNotAllowed); public static Func HTTP_STATUS(HttpStatusCode status) => response => @@ -177,14 +160,43 @@ public static Func RESPONSE_BODY(Action as }; public static Func> 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(true); + public static Func 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 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 RESPONSE_HEADERS(params Action[] headers) => + response => + { + foreach (var header in headers) + { + header(response.Headers); + } + return ValueTask.CompletedTask; }; public static Func> RESPONSE_SUCCEEDED() => diff --git a/src/Ogooreck/Ogooreck.csproj b/src/Ogooreck/Ogooreck.csproj index b88c350..a57c448 100644 --- a/src/Ogooreck/Ogooreck.csproj +++ b/src/Ogooreck/Ogooreck.csproj @@ -1,7 +1,7 @@ - 0.1.1 + 0.1.2 net6.0 true true