Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/com/retailsvc/http/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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<String, String> additional) {
LinkedHashMap<String, String> merged = new LinkedHashMap<>(headers);
merged.putAll(additional);
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/com/retailsvc/http/ResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading