Skip to content

Commit

Permalink
bugfix: NullPointerException on HttpServletResponseMDC.isAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
jhannes committed Apr 15, 2024
1 parent fcfeed5 commit ae1f05b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private static Marker getMarkerFromContentType(HttpServletResponse response) {

public static boolean isAsset(HttpServletResponse response) {
String contentType = response.getContentType();
return (contentType.startsWith("image/") || contentType.startsWith("font/") || contentType.startsWith("text/html") || contentType.startsWith("text/css") || contentType.startsWith("text/javascript"));
return contentType != null && (contentType.startsWith("image/") || contentType.startsWith("font/") || contentType.startsWith("text/html") || contentType.startsWith("text/css") || contentType.startsWith("text/javascript"));
}

protected static boolean isRedirect(int status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static Marker getMarkerFromContentType(HttpServletResponse response) {

public static boolean isAsset(HttpServletResponse response) {
String contentType = response.getContentType();
return (contentType.startsWith("image/") || contentType.startsWith("font/") || contentType.startsWith("text/html") || contentType.startsWith("text/css") || contentType.startsWith("text/javascript"));
return contentType != null && (contentType.startsWith("image/") || contentType.startsWith("font/") || contentType.startsWith("text/html") || contentType.startsWith("text/css") || contentType.startsWith("text/javascript"));
}

protected static boolean isRedirect(int status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import java.util.HashSet;
import java.util.Map;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class HttpServletResponseMDCTest {
Expand Down Expand Up @@ -73,6 +75,17 @@ public void shouldClassifyMarkerBasedOnContentType() {
assertEquals(HttpServletResponseMDC.JSON, HttpServletResponseMDC.getMarker(mockResponse));
}

@Test
public void shouldIdentifyAssetContent() {
javax.servlet.http.HttpServletResponse mockResponse = Mockito.mock(javax.servlet.http.HttpServletResponse.class);
Mockito.when(mockResponse.getContentType()).thenReturn("image/png");
assertTrue(org.logevents.optional.servlets.HttpServletResponseMDC.isAsset(mockResponse));
Mockito.when(mockResponse.getContentType()).thenReturn("application/json");
assertFalse(org.logevents.optional.servlets.HttpServletResponseMDC.isAsset(mockResponse));
Mockito.when(mockResponse.getContentType()).thenReturn(null);
assertFalse(org.logevents.optional.servlets.HttpServletResponseMDC.isAsset(mockResponse));
}

static HttpServletResponse createMockResponse() {
HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
Mockito.when(mockResponse.getStatus()).thenReturn(401);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.HashSet;
import java.util.Map;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -74,6 +75,17 @@ public void shouldClassifyMarkerBasedOnContentType() {
assertEquals(HttpServletResponseMDC.JSON, HttpServletResponseMDC.getMarker(mockResponse));
}

@Test
public void shouldIdentifyAssetContent() {
HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
Mockito.when(mockResponse.getContentType()).thenReturn("image/png");
assertTrue(HttpServletResponseMDC.isAsset(mockResponse));
Mockito.when(mockResponse.getContentType()).thenReturn("application/json");
assertFalse(HttpServletResponseMDC.isAsset(mockResponse));
Mockito.when(mockResponse.getContentType()).thenReturn(null);
assertFalse(HttpServletResponseMDC.isAsset(mockResponse));
}


static HttpServletResponse createMockResponse() {
HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
Expand Down

0 comments on commit ae1f05b

Please sign in to comment.