Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion src/main/java/com/uid2/operator/service/ResponseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ public static void LogInfoAndSendResponse(String status, int statusCode, Routing

public static void LogWarningAndSendResponse(String status, int statusCode, RoutingContext rc, String message) {
String msg = ComposeMessage(status, statusCode, message, new RoutingContextReader(rc), rc.request().remoteAddress().hostAddress());
LOGGER.warn(msg);
String contentType = rc.request().getHeader(HttpHeaders.CONTENT_TYPE);
String contentTypeStr = " Content-Type: " + (contentType != null ? contentType : "null");
LOGGER.warn(msg + contentTypeStr);
final JsonObject json = Response(status, message);
rc.response().setStatusCode(statusCode).putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.end(json.encode());
Expand Down
98 changes: 98 additions & 0 deletions src/test/java/com/uid2/operator/service/ResponseUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,102 @@ void logsWarningWithRefererNull() {
ILoggingEvent loggingEvent = testAppender.list.get(0);
assertThat(loggingEvent.getMessage()).isEqualTo(expected);
}

@Test
void logsWarningWithContentType() {
when(request.getHeader(io.vertx.core.http.HttpHeaders.CONTENT_TYPE)).thenReturn("application/json");
when(rc.request()).thenReturn(request);

ResponseUtil.LogWarningAndSendResponse("Some error status", 400, rc, "Some error message");

String expectedBase = "Response to http request. {" +
"\"errorStatus\":\"Some error status\"," +
"\"contact\":null," +
"\"siteId\":null," +
"\"path\":null," +
"\"statusCode\":400," +
"\"clientAddress\":null," +
"\"message\":\"Some error message\"" +
"}";
String expectedWithContentType = expectedBase + " Content-Type: application/json";

assertThat(testAppender.list).hasSize(1);
ILoggingEvent loggingEvent = testAppender.list.get(0);
assertThat(loggingEvent.getMessage()).isEqualTo(expectedWithContentType);
assertThat(loggingEvent.getLevel()).isEqualTo(Level.WARN);
}

@Test
void logsWarningWithNullContentType() {
when(request.getHeader(io.vertx.core.http.HttpHeaders.CONTENT_TYPE)).thenReturn(null);
when(rc.request()).thenReturn(request);

ResponseUtil.LogWarningAndSendResponse("Some error status", 400, rc, "Some error message");

String expectedBase = "Response to http request. {" +
"\"errorStatus\":\"Some error status\"," +
"\"contact\":null," +
"\"siteId\":null," +
"\"path\":null," +
"\"statusCode\":400," +
"\"clientAddress\":null," +
"\"message\":\"Some error message\"" +
"}";
String expectedWithContentType = expectedBase + " Content-Type: null";

assertThat(testAppender.list).hasSize(1);
ILoggingEvent loggingEvent = testAppender.list.get(0);
assertThat(loggingEvent.getMessage()).isEqualTo(expectedWithContentType);
assertThat(loggingEvent.getLevel()).isEqualTo(Level.WARN);
}

@Test
void logsErrorDoesNotIncludeContentType() {
when(request.getHeader(io.vertx.core.http.HttpHeaders.CONTENT_TYPE)).thenReturn("application/json");
when(rc.request()).thenReturn(request);

ResponseUtil.LogErrorAndSendResponse("Some error status", 500, rc, "Some error message");

String expectedMessage = "Response to http request. {" +
"\"errorStatus\":\"Some error status\"," +
"\"contact\":null," +
"\"siteId\":null," +
"\"path\":null," +
"\"statusCode\":500," +
"\"clientAddress\":null," +
"\"message\":\"Some error message\"" +
"}";

assertThat(testAppender.list).hasSize(1);
ILoggingEvent loggingEvent = testAppender.list.get(0);
assertThat(loggingEvent.getMessage()).isEqualTo(expectedMessage);
assertThat(loggingEvent.getLevel()).isEqualTo(Level.ERROR);
// Verify content type is NOT included
assertThat(loggingEvent.getMessage()).doesNotContain("Content-Type:");
}

@Test
void logsInfoDoesNotIncludeContentType() {
when(request.getHeader(io.vertx.core.http.HttpHeaders.CONTENT_TYPE)).thenReturn("application/json");
when(rc.request()).thenReturn(request);

ResponseUtil.LogInfoAndSendResponse("Some info status", 200, rc, "Some info message");

String expectedMessage = "Response to http request. {" +
"\"errorStatus\":\"Some info status\"," +
"\"contact\":null," +
"\"siteId\":null," +
"\"path\":null," +
"\"statusCode\":200," +
"\"clientAddress\":null," +
"\"message\":\"Some info message\"" +
"}";

assertThat(testAppender.list).hasSize(1);
ILoggingEvent loggingEvent = testAppender.list.get(0);
assertThat(loggingEvent.getMessage()).isEqualTo(expectedMessage);
assertThat(loggingEvent.getLevel()).isEqualTo(Level.INFO);
// Verify content type is NOT included
assertThat(loggingEvent.getMessage()).doesNotContain("Content-Type:");
}
}
Loading