Skip to content

Commit

Permalink
Issue #5539 - Adding query parameter accept variation
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Nov 2, 2020
1 parent 0962eac commit 07a30a7
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,16 @@ private List<String> 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("*/*");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -141,8 +146,53 @@ public void testGetStats()
assertThat("4XX Response Count" + response, stats.responses4xx, is(1));
}

@Test
public void testGetXmlResponse()
public static Stream<Arguments> typeVariations(String mimeType)
{
return Stream.of(
Arguments.of(
new Consumer<HttpTester.Request>()
{
@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<HttpTester.Request>()
{
@Override
public void accept(HttpTester.Request request)
{
request.setURI("/stats?accept=" + mimeType);
}

@Override
public String toString()
{
return "query[accept=" + mimeType + "]";
}
}
)
);
}

public static Stream<Arguments> xmlVariations()
{
return typeVariations("text/xml");
}

@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("xmlVariations")
public void testGetXmlResponse(Consumer<HttpTester.Request> requestCustomizer)
throws Exception
{
addStatisticsHandler();
Expand All @@ -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);
Expand All @@ -176,8 +225,14 @@ public void testGetXmlResponse()
}
}

@Test
public void testGetJsonResponse()
public static Stream<Arguments> jsonVariations()
{
return typeVariations("application/json");
}

@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("jsonVariations")
public void testGetJsonResponse(Consumer<HttpTester.Request> requestCustomizer)
throws Exception
{
addStatisticsHandler();
Expand All @@ -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");

Expand All @@ -213,8 +267,14 @@ public void testGetJsonResponse()
assertNotNull(docMap.get("memory"));
}

@Test
public void testGetTextResponse()
public static Stream<Arguments> plaintextVariations()
{
return typeVariations("text/plain");
}

@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("plaintextVariations")
public void testGetTextResponse(Consumer<HttpTester.Request> requestCustomizer)
throws Exception
{
addStatisticsHandler();
Expand All @@ -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");

Expand All @@ -243,8 +302,14 @@ public void testGetTextResponse()
assertThat(response.getContent(), containsString("memory: "));
}

@Test
public void testGetHtmlResponse()
public static Stream<Arguments> htmlVariations()
{
return typeVariations("text/html");
}

@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("htmlVariations")
public void testGetHtmlResponse(Consumer<HttpTester.Request> requestCustomizer)
throws Exception
{
addStatisticsHandler();
Expand All @@ -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");

Expand Down

0 comments on commit 07a30a7

Please sign in to comment.