diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/StatisticsServlet.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/StatisticsServlet.java index a453e1326386..affba5955603 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/StatisticsServlet.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/StatisticsServlet.java @@ -235,7 +235,16 @@ private List getOrderedAcceptableMimeTypes(HttpServletRequest request) return values.getValues(); } - // No accept specified, return that we allow ALL mime types + // No accept header specified, try 'accept' parameter (for those clients that are + // so ancient that they cannot set the standard HTTP `Accept` header) + String acceptParameter = request.getParameter("accept"); + if (acceptParameter != null) + { + // return that we support the one type specified in the 'accept' parameter + return Collections.singletonList(acceptParameter); + } + + // return that we allow ALL mime types return Collections.singletonList("*/*"); } diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/StatisticsServletTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/StatisticsServletTest.java index 576b961f631a..8b5d406094b0 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/StatisticsServletTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/StatisticsServletTest.java @@ -24,6 +24,8 @@ import java.io.StringReader; import java.nio.ByteBuffer; import java.util.Map; +import java.util.function.Consumer; +import java.util.stream.Stream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -44,6 +46,9 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.w3c.dom.Document; import org.xml.sax.InputSource; @@ -141,8 +146,53 @@ public void testGetStats() assertThat("4XX Response Count" + response, stats.responses4xx, is(1)); } - @Test - public void testGetXmlResponse() + public static Stream typeVariations(String mimeType) + { + return Stream.of( + Arguments.of( + new Consumer() + { + @Override + public void accept(HttpTester.Request request) + { + request.setURI("/stats"); + request.setHeader("Accept", mimeType); + } + + @Override + public String toString() + { + return "Header[Accept: " + mimeType + "]"; + } + } + ), + Arguments.of( + new Consumer() + { + @Override + public void accept(HttpTester.Request request) + { + request.setURI("/stats?accept=" + mimeType); + } + + @Override + public String toString() + { + return "query[accept=" + mimeType + "]"; + } + } + ) + ); + } + + public static Stream xmlVariations() + { + return typeVariations("text/xml"); + } + + @ParameterizedTest(name = "[{index}] {0}") + @MethodSource("xmlVariations") + public void testGetXmlResponse(Consumer requestCustomizer) throws Exception { addStatisticsHandler(); @@ -152,10 +202,9 @@ public void testGetXmlResponse() HttpTester.Request request = new HttpTester.Request(); request.setMethod("GET"); - request.setURI("/stats"); - request.setHeader("Accept", "text/xml"); request.setVersion(HttpVersion.HTTP_1_1); request.setHeader("Host", "test"); + requestCustomizer.accept(request); ByteBuffer responseBuffer = _connector.getResponse(request.generate()); response = HttpTester.parseResponse(responseBuffer); @@ -176,8 +225,14 @@ public void testGetXmlResponse() } } - @Test - public void testGetJsonResponse() + public static Stream jsonVariations() + { + return typeVariations("application/json"); + } + + @ParameterizedTest(name = "[{index}] {0}") + @MethodSource("jsonVariations") + public void testGetJsonResponse(Consumer requestCustomizer) throws Exception { addStatisticsHandler(); @@ -187,8 +242,7 @@ public void testGetJsonResponse() HttpTester.Request request = new HttpTester.Request(); request.setMethod("GET"); - request.setURI("/stats"); - request.setHeader("Accept", "application/json"); + requestCustomizer.accept(request); request.setVersion(HttpVersion.HTTP_1_1); request.setHeader("Host", "test"); @@ -213,8 +267,14 @@ public void testGetJsonResponse() assertNotNull(docMap.get("memory")); } - @Test - public void testGetTextResponse() + public static Stream plaintextVariations() + { + return typeVariations("text/plain"); + } + + @ParameterizedTest(name = "[{index}] {0}") + @MethodSource("plaintextVariations") + public void testGetTextResponse(Consumer requestCustomizer) throws Exception { addStatisticsHandler(); @@ -224,8 +284,7 @@ public void testGetTextResponse() HttpTester.Request request = new HttpTester.Request(); request.setMethod("GET"); - request.setURI("/stats"); - request.setHeader("Accept", "text/plain"); + requestCustomizer.accept(request); request.setVersion(HttpVersion.HTTP_1_1); request.setHeader("Host", "test"); @@ -243,8 +302,14 @@ public void testGetTextResponse() assertThat(response.getContent(), containsString("memory: ")); } - @Test - public void testGetHtmlResponse() + public static Stream htmlVariations() + { + return typeVariations("text/html"); + } + + @ParameterizedTest(name = "[{index}] {0}") + @MethodSource("htmlVariations") + public void testGetHtmlResponse(Consumer requestCustomizer) throws Exception { addStatisticsHandler(); @@ -254,8 +319,7 @@ public void testGetHtmlResponse() HttpTester.Request request = new HttpTester.Request(); request.setMethod("GET"); - request.setURI("/stats"); - request.setHeader("Accept", "text/html"); + requestCustomizer.accept(request); request.setVersion(HttpVersion.HTTP_1_1); request.setHeader("Host", "test");