Skip to content

Commit

Permalink
Improved the Location URL handling for PostResults
Browse files Browse the repository at this point in the history
  • Loading branch information
jezzsantos committed Nov 3, 2023
1 parent a2f0bed commit c300e2c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/CarsApi.IntegrationTests/CarsApiSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion src/CarsApi/Apis/Cars/CarsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<ApiPostResult<Car, GetCarResponse>> Register(RegisterCarReques
request.Year, request.Jurisdiction, request.NumberPlate, cancellationToken);

return () => car.HandleApplicationResult<GetCarResponse, Car>(c =>
new PostResult<GetCarResponse>(new GetCarResponse { Car = c }, $"/cars/{c.Id}"));
new PostResult<GetCarResponse>(new GetCarResponse { Car = c }, new GetCarRequest { Id = c.Id }.ToUrl()));
}

public async Task<ApiPutPatchResult<Car, GetCarResponse>> ScheduleMaintenance(ScheduleMaintenanceCarRequest request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestResponse>
{
}
Expand Down
8 changes: 8 additions & 0 deletions src/Infrastructure.Web.Api.Common/RequestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public static void RewindBody(this HttpRequest httpRequest)
}
}

/// <summary>
/// Returns the <see cref="RequestInfo.Route" /> for the <see cref="request" />
/// </summary>
public static string ToUrl(this IWebRequest request)
{
return request.GetRequestInfo().Route;
}

private static RouteAttribute GetRouteFromAttribute(IWebRequest request)
{
var attribute = request.GetType().GetCustomAttribute<RouteAttribute>();
Expand Down
4 changes: 2 additions & 2 deletions src/Infrastructure.Web.Api.Interfaces/ApiResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public delegate Result<TResponse, Error> ApiSearchResult<TResource, TResponse>()
public class PostResult<TResponse>
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; }
Expand Down

0 comments on commit c300e2c

Please sign in to comment.