diff --git a/src/main/java/com/retailsvc/http/Response.java b/src/main/java/com/retailsvc/http/Response.java index f3e35b3..09ea622 100644 --- a/src/main/java/com/retailsvc/http/Response.java +++ b/src/main/java/com/retailsvc/http/Response.java @@ -57,7 +57,7 @@ public static Response ok(Object body) { /** * {@code 201 Created} with {@code body} serialised as JSON. Add a {@code Location} header for the - * new resource via {@link #withHeader(String, String) withHeader("Location", uri)}. + * new resource via {@link #withLocation(String)}. */ public static Response created(Object body) { return new Response(HTTP_CREATED, body, null, Map.of()); @@ -140,6 +140,11 @@ public Response withHeader(String name, String value) { return new Response(status, body, contentType, merged); } + /** Sets the {@code Location} header, typically the URI of a newly {@link #created} resource. */ + public Response withLocation(String location) { + return withHeader("Location", location); + } + public Response withHeaders(Map additional) { LinkedHashMap merged = new LinkedHashMap<>(headers); merged.putAll(additional); diff --git a/src/test/java/com/retailsvc/http/ResponseTest.java b/src/test/java/com/retailsvc/http/ResponseTest.java index 162c00f..d8fd5bd 100644 --- a/src/test/java/com/retailsvc/http/ResponseTest.java +++ b/src/test/java/com/retailsvc/http/ResponseTest.java @@ -47,6 +47,14 @@ void createdWithLocationViaWithHeader() { assertThat(r.headers()).containsEntry("Location", "/things/x-1"); } + @Test + void createdWithLocationViaWithLocation() { + Response r = Response.created(Map.of("id", "x-1")).withLocation("/things/x-1"); + + assertThat(r.status()).isEqualTo(HTTP_CREATED); + assertThat(r.headers()).containsEntry("Location", "/things/x-1"); + } + @Test void notFoundNoBody() { Response r = Response.notFound();