Skip to content

Commit 3ac22d7

Browse files
committed
Logging content type on error
1 parent d6f864d commit 3ac22d7

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

src/main/java/com/uid2/operator/service/ResponseUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ public static void LogInfoAndSendResponse(String status, int statusCode, Routing
131131

132132
public static void LogWarningAndSendResponse(String status, int statusCode, RoutingContext rc, String message) {
133133
String msg = ComposeMessage(status, statusCode, message, new RoutingContextReader(rc), rc.request().remoteAddress().hostAddress());
134-
LOGGER.warn(msg);
134+
String contentType = rc.request().getHeader(HttpHeaders.CONTENT_TYPE);
135+
String contentTypeStr = " Content-Type: " + (contentType != null ? contentType : "null");
136+
LOGGER.warn(msg + contentTypeStr);
135137
final JsonObject json = Response(status, message);
136138
rc.response().setStatusCode(statusCode).putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
137139
.end(json.encode());

src/test/java/com/uid2/operator/service/ResponseUtilTest.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,102 @@ void logsWarningWithRefererNull() {
225225
ILoggingEvent loggingEvent = testAppender.list.get(0);
226226
assertThat(loggingEvent.getMessage()).isEqualTo(expected);
227227
}
228+
229+
@Test
230+
void logsWarningWithContentType() {
231+
when(request.getHeader(io.vertx.core.http.HttpHeaders.CONTENT_TYPE)).thenReturn("application/json");
232+
when(rc.request()).thenReturn(request);
233+
234+
ResponseUtil.LogWarningAndSendResponse("Some error status", 400, rc, "Some error message");
235+
236+
String expectedBase = "Response to http request. {" +
237+
"\"errorStatus\":\"Some error status\"," +
238+
"\"contact\":null," +
239+
"\"siteId\":null," +
240+
"\"path\":null," +
241+
"\"statusCode\":400," +
242+
"\"clientAddress\":null," +
243+
"\"message\":\"Some error message\"" +
244+
"}";
245+
String expectedWithContentType = expectedBase + " Content-Type: application/json";
246+
247+
assertThat(testAppender.list).hasSize(1);
248+
ILoggingEvent loggingEvent = testAppender.list.get(0);
249+
assertThat(loggingEvent.getMessage()).isEqualTo(expectedWithContentType);
250+
assertThat(loggingEvent.getLevel()).isEqualTo(Level.WARN);
251+
}
252+
253+
@Test
254+
void logsWarningWithNullContentType() {
255+
when(request.getHeader(io.vertx.core.http.HttpHeaders.CONTENT_TYPE)).thenReturn(null);
256+
when(rc.request()).thenReturn(request);
257+
258+
ResponseUtil.LogWarningAndSendResponse("Some error status", 400, rc, "Some error message");
259+
260+
String expectedBase = "Response to http request. {" +
261+
"\"errorStatus\":\"Some error status\"," +
262+
"\"contact\":null," +
263+
"\"siteId\":null," +
264+
"\"path\":null," +
265+
"\"statusCode\":400," +
266+
"\"clientAddress\":null," +
267+
"\"message\":\"Some error message\"" +
268+
"}";
269+
String expectedWithContentType = expectedBase + " Content-Type: null";
270+
271+
assertThat(testAppender.list).hasSize(1);
272+
ILoggingEvent loggingEvent = testAppender.list.get(0);
273+
assertThat(loggingEvent.getMessage()).isEqualTo(expectedWithContentType);
274+
assertThat(loggingEvent.getLevel()).isEqualTo(Level.WARN);
275+
}
276+
277+
@Test
278+
void logsErrorDoesNotIncludeContentType() {
279+
when(request.getHeader(io.vertx.core.http.HttpHeaders.CONTENT_TYPE)).thenReturn("application/json");
280+
when(rc.request()).thenReturn(request);
281+
282+
ResponseUtil.LogErrorAndSendResponse("Some error status", 500, rc, "Some error message");
283+
284+
String expectedMessage = "Response to http request. {" +
285+
"\"errorStatus\":\"Some error status\"," +
286+
"\"contact\":null," +
287+
"\"siteId\":null," +
288+
"\"path\":null," +
289+
"\"statusCode\":500," +
290+
"\"clientAddress\":null," +
291+
"\"message\":\"Some error message\"" +
292+
"}";
293+
294+
assertThat(testAppender.list).hasSize(1);
295+
ILoggingEvent loggingEvent = testAppender.list.get(0);
296+
assertThat(loggingEvent.getMessage()).isEqualTo(expectedMessage);
297+
assertThat(loggingEvent.getLevel()).isEqualTo(Level.ERROR);
298+
// Verify content type is NOT included
299+
assertThat(loggingEvent.getMessage()).doesNotContain("Content-Type:");
300+
}
301+
302+
@Test
303+
void logsInfoDoesNotIncludeContentType() {
304+
when(request.getHeader(io.vertx.core.http.HttpHeaders.CONTENT_TYPE)).thenReturn("application/json");
305+
when(rc.request()).thenReturn(request);
306+
307+
ResponseUtil.LogInfoAndSendResponse("Some info status", 200, rc, "Some info message");
308+
309+
String expectedMessage = "Response to http request. {" +
310+
"\"errorStatus\":\"Some info status\"," +
311+
"\"contact\":null," +
312+
"\"siteId\":null," +
313+
"\"path\":null," +
314+
"\"statusCode\":200," +
315+
"\"clientAddress\":null," +
316+
"\"message\":\"Some info message\"" +
317+
"}";
318+
319+
assertThat(testAppender.list).hasSize(1);
320+
ILoggingEvent loggingEvent = testAppender.list.get(0);
321+
assertThat(loggingEvent.getMessage()).isEqualTo(expectedMessage);
322+
assertThat(loggingEvent.getLevel()).isEqualTo(Level.INFO);
323+
// Verify content type is NOT included
324+
assertThat(loggingEvent.getMessage()).doesNotContain("Content-Type:");
325+
}
228326
}

0 commit comments

Comments
 (0)