Skip to content
Closed
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
6 changes: 6 additions & 0 deletions src/main/java/com/retailsvc/http/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public static Response text(int status, String body) {
status, body.getBytes(StandardCharsets.UTF_8), "text/plain; charset=UTF-8", Map.of());
}

/** {@code status} with {@code body} written as UTF-8 with {@code Content-Type: text/html}. */
public static Response html(int status, String body) {
return new Response(
status, body.getBytes(StandardCharsets.UTF_8), "text/html; charset=UTF-8", Map.of());
}

/**
* {@code status} with pre-serialised {@code bytes} written verbatim under {@code contentType}.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/retailsvc/http/ResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import static java.net.HttpURLConnection.HTTP_CREATED;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_NOT_IMPLEMENTED;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.assertj.core.api.Assertions.assertThat;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -64,6 +66,15 @@ void notFoundWithBody() {
assertThat(r.body()).isEqualTo(problem);
}

@Test
void htmlBody() {
Response r = Response.html(HTTP_OK, "<h1>hi</h1>");

assertThat(r.status()).isEqualTo(HTTP_OK);
assertThat(r.contentType()).isEqualTo("text/html; charset=UTF-8");
assertThat((byte[]) r.body()).isEqualTo("<h1>hi</h1>".getBytes(StandardCharsets.UTF_8));
}

@Test
void notImplementedNoBody() {
Response r = Response.notImplemented();
Expand Down
Loading