Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Issue #721 - Enhancing multi-line response header testcase
  • Loading branch information
joakime committed Jul 13, 2016
1 parent e4249fd commit 241ef90
Showing 1 changed file with 16 additions and 4 deletions.
Expand Up @@ -22,6 +22,7 @@
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.net.URLDecoder;
import java.nio.ByteBuffer;

import javax.servlet.ServletException;
Expand All @@ -33,7 +34,6 @@
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.BufferUtil;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -59,7 +59,12 @@ public static class MultilineResponseValueServlet extends HttpServlet
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException
{
// The bad use-case
response.setHeader("X-example", req.getPathInfo());
String pathInfo = req.getPathInfo();
if(pathInfo != null && pathInfo.length() > 1 && pathInfo.startsWith("/"))
{
pathInfo = pathInfo.substring(1);
}
response.setHeader("X-example", pathInfo);

// The correct use
response.setContentType("text/plain");
Expand Down Expand Up @@ -122,19 +127,26 @@ public void testResponseWebSocketHeaderFormat() throws Exception
@Test
public void testMultilineResponseHeaderValue() throws Exception
{
String actualPathInfo = "%0A%20Content-Type%3A%20image/png%0A%20Content-Length%3A%208%0A%20%0A%20yuck<!--";

HttpTester.Request request = new HttpTester.Request();
request.setMethod("GET");
request.setURI("/multiline/%0A%20Content-Type%3A%20image/png%0A%20Content-Length%3A%208%0A%20%0A%20yuck<!--");
request.setURI("/multiline/" + actualPathInfo);
request.setVersion(HttpVersion.HTTP_1_1);
request.setHeader("Connection", "close");
request.setHeader("Host", "test");

ByteBuffer responseBuffer = connector.getResponse(request.generate());
System.err.println(BufferUtil.toUTF8String(responseBuffer));
// System.err.println(BufferUtil.toUTF8String(responseBuffer));
HttpTester.Response response = HttpTester.parseResponse(responseBuffer);

// Now test for properly formatted HTTP Response Headers.
assertThat("Response Code",response.getStatus(),is(200));
assertThat("Response Header Content-Type",response.get("Content-Type"),is("text/plain;charset=UTF-8"));

String expected = actualPathInfo.replaceAll("%0A", " "); // replace OBS fold with space
expected = URLDecoder.decode(expected, "utf-8"); // decode the rest
expected = expected.trim(); // trim whitespace at start/end
assertThat("Response Header X-example", response.get("X-Example"), is(expected));
}
}

0 comments on commit 241ef90

Please sign in to comment.