Skip to content

Commit 868670a

Browse files
committed
feat: Add Response.withLocation convenience method
Add a withLocation(String) mutator that sets the Location header without the caller naming the key, mirroring the existing withHeader/withStatus/withContentType pattern. Update the created(...) javadoc to point at the new method.
1 parent d1dfdb8 commit 868670a

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/main/java/com/retailsvc/http/Response.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static Response ok(Object body) {
5757

5858
/**
5959
* {@code 201 Created} with {@code body} serialised as JSON. Add a {@code Location} header for the
60-
* new resource via {@link #withHeader(String, String) withHeader("Location", uri)}.
60+
* new resource via {@link #withLocation(String)}.
6161
*/
6262
public static Response created(Object body) {
6363
return new Response(HTTP_CREATED, body, null, Map.of());
@@ -140,6 +140,11 @@ public Response withHeader(String name, String value) {
140140
return new Response(status, body, contentType, merged);
141141
}
142142

143+
/** Sets the {@code Location} header, typically the URI of a newly {@link #created} resource. */
144+
public Response withLocation(String location) {
145+
return withHeader("Location", location);
146+
}
147+
143148
public Response withHeaders(Map<String, String> additional) {
144149
LinkedHashMap<String, String> merged = new LinkedHashMap<>(headers);
145150
merged.putAll(additional);

src/test/java/com/retailsvc/http/ResponseTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ void createdWithLocationViaWithHeader() {
4747
assertThat(r.headers()).containsEntry("Location", "/things/x-1");
4848
}
4949

50+
@Test
51+
void createdWithLocationViaWithLocation() {
52+
Response r = Response.created(Map.of("id", "x-1")).withLocation("/things/x-1");
53+
54+
assertThat(r.status()).isEqualTo(HTTP_CREATED);
55+
assertThat(r.headers()).containsEntry("Location", "/things/x-1");
56+
}
57+
5058
@Test
5159
void notFoundNoBody() {
5260
Response r = Response.notFound();

0 commit comments

Comments
 (0)