Skip to content

Commit db24306

Browse files
committed
feat: Add Request.contentType() convenience accessor
Returns the Content-Type header as Optional<String> (blank treated as absent, matching header(name)). The most frequently inspected header by handler code; saves the magic-string typo risk.
1 parent bb9bdad commit db24306

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ public <T> T asPojo(Class<T> type) {
102102
+ " does not support typed conversion; the mapper must implement TypedTypeMapper");
103103
}
104104

105+
/**
106+
* Value of the {@code Content-Type} request header, or {@link Optional#empty()} if absent or
107+
* blank. Convenience for {@code header("Content-Type")} — the most frequently inspected header.
108+
*/
109+
public Optional<String> contentType() {
110+
return header(CONTENT_TYPE);
111+
}
112+
105113
public String operationId() {
106114
return operationId;
107115
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,28 @@ void queryParamBlankIsTreatedAsAbsent() {
177177
assertThat(req.queryParam("offset")).isEmpty();
178178
}
179179

180+
@Test
181+
void contentTypeShortcutsContentTypeHeader() {
182+
Request req =
183+
new Request(
184+
new byte[0],
185+
null,
186+
null,
187+
"op",
188+
Map.of(),
189+
null,
190+
headers("Content-Type", "application/json"));
191+
192+
assertThat(req.contentType()).contains("application/json");
193+
}
194+
195+
@Test
196+
void contentTypeEmptyWhenHeaderAbsent() {
197+
Request req = new Request(new byte[0], null, null, "op", Map.of(), null, NO_HEADERS);
198+
199+
assertThat(req.contentType()).isEmpty();
200+
}
201+
180202
@Test
181203
void headerReturnsOptionalAndBlankIsAbsent() {
182204
Request req =

0 commit comments

Comments
 (0)