diff --git a/src/CarsApi.IntegrationTests/CarsApiSpec.cs b/src/CarsApi.IntegrationTests/CarsApiSpec.cs index 744ec3fb..d78a37c8 100644 --- a/src/CarsApi.IntegrationTests/CarsApiSpec.cs +++ b/src/CarsApi.IntegrationTests/CarsApiSpec.cs @@ -5,6 +5,7 @@ using CarsDomain; using Domain.Interfaces; using FluentAssertions; +using Infrastructure.Web.Api.Common; using Infrastructure.Web.Api.Interfaces.Operations.Cars; using IntegrationTesting.WebApi.Common; using Xunit; @@ -60,9 +61,9 @@ public async Task WhenRegisterCar_ThenReturnsCar() NumberPlate = "aplate" }); - var location = result.Headers.Location?.ToString(); - location.Should().StartWith("/cars/car_"); var car = result.Content.Value.Car!; + var location = result.Headers.Location?.ToString(); + location.Should().Be(new GetCarRequest { Id = car.Id }.ToUrl()); car.Id.Should().NotBeEmpty(); car.Manufacturer!.Make.Should().Be(Manufacturer.AllowedMakes[0]); car.Manufacturer!.Model.Should().Be(Manufacturer.AllowedModels[0]); diff --git a/src/CarsApi/Apis/Cars/CarsApi.cs b/src/CarsApi/Apis/Cars/CarsApi.cs index 7e7418d7..180e6863 100644 --- a/src/CarsApi/Apis/Cars/CarsApi.cs +++ b/src/CarsApi/Apis/Cars/CarsApi.cs @@ -39,7 +39,7 @@ public async Task> Register(RegisterCarReques request.Year, request.Jurisdiction, request.NumberPlate, cancellationToken); return () => car.HandleApplicationResult(c => - new PostResult(new GetCarResponse { Car = c }, $"/cars/{c.Id}")); + new PostResult(new GetCarResponse { Car = c }, new GetCarRequest { Id = c.Id }.ToUrl())); } public async Task> ScheduleMaintenance(ScheduleMaintenanceCarRequest request, diff --git a/src/Infrastructure.Web.Api.Common.UnitTests/RequestExtensionsSpec.cs b/src/Infrastructure.Web.Api.Common.UnitTests/RequestExtensionsSpec.cs index 143aa851..32098efc 100644 --- a/src/Infrastructure.Web.Api.Common.UnitTests/RequestExtensionsSpec.cs +++ b/src/Infrastructure.Web.Api.Common.UnitTests/RequestExtensionsSpec.cs @@ -266,6 +266,25 @@ public void WhenGetRequestInfoAndRouteTemplateHasPlaceholdersWithSomeDataValuesF result.IsTestingOnly.Should().BeFalse(); } + [Fact] + public void WhenToUrl_ThenReturnsUrl() + { + var datum = new DateTime(2023, 10, 29, 12, 30, 15, DateTimeKind.Utc).ToNearestSecond(); + var request = new HasPlaceholdersPostRequest + { + Id = "anid", + AStringProperty1 = "avalue1", + AStringProperty2 = null, + AStringProperty3 = null, + ANumberProperty = null, + ADateTimeProperty = datum + }; + + var result = request.ToUrl(); + + result.Should().Be("/aroute/anid/apath1/xxxyyy/apath2/avalue1/apath3"); + } + private class NoRouteRequest : IWebRequest { } diff --git a/src/Infrastructure.Web.Api.Common/RequestExtensions.cs b/src/Infrastructure.Web.Api.Common/RequestExtensions.cs index 29462934..d3c916ac 100644 --- a/src/Infrastructure.Web.Api.Common/RequestExtensions.cs +++ b/src/Infrastructure.Web.Api.Common/RequestExtensions.cs @@ -41,6 +41,14 @@ public static void RewindBody(this HttpRequest httpRequest) } } + /// + /// Returns the for the + /// + public static string ToUrl(this IWebRequest request) + { + return request.GetRequestInfo().Route; + } + private static RouteAttribute GetRouteFromAttribute(IWebRequest request) { var attribute = request.GetType().GetCustomAttribute(); diff --git a/src/Infrastructure.Web.Api.Interfaces/ApiResult.cs b/src/Infrastructure.Web.Api.Interfaces/ApiResult.cs index 63bf0f1b..a64a1216 100644 --- a/src/Infrastructure.Web.Api.Interfaces/ApiResult.cs +++ b/src/Infrastructure.Web.Api.Interfaces/ApiResult.cs @@ -61,10 +61,10 @@ public delegate Result ApiSearchResult() public class PostResult where TResponse : IWebResponse { - public PostResult(TResponse response, string? location = null) + public PostResult(TResponse response, string? resourceLocation = null) { Response = response; - Location = location; + Location = resourceLocation; } public string? Location { get; }