Skip to content

Commit

Permalink
Reproduce #6870 Rewritten Balancer
Browse files Browse the repository at this point in the history
Reproduce #6870 Rewritten Balancer

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed Sep 20, 2021
1 parent 4de5fdf commit 1749a90
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions jetty-proxy/pom.xml
Expand Up @@ -47,6 +47,12 @@
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-rewrite</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
Expand Down
Expand Up @@ -31,6 +31,8 @@

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.rewrite.handler.RewriteHandler;
import org.eclipse.jetty.rewrite.handler.VirtualHostRuleContainer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.session.DefaultSessionIdManager;
Expand All @@ -40,6 +42,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class BalancerServletTest
Expand Down Expand Up @@ -111,6 +116,15 @@ private int getServerPort(Server server)
return server.getURI().getPort();
}

protected ContentResponse getBalancedResponse(String path) throws Exception
{
ContentResponse response = client.newRequest("localhost", getServerPort(balancer))
.path(CONTEXT_PATH + SERVLET_PATH + path)
.timeout(5, TimeUnit.SECONDS)
.send();
return response;
}

protected byte[] sendRequestToBalancer(String path) throws Exception
{
ContentResponse response = client.newRequest("localhost", getServerPort(balancer))
Expand Down Expand Up @@ -160,12 +174,44 @@ public void testProxyPassReverse() throws Exception
assertEquals("success", msg);
}

@Test
public void testRewrittenBalancerWithEncodedURI() throws Exception
{
startBalancer(DumpServlet.class);
balancer.stop();
RewriteHandler rewrite = new RewriteHandler();
rewrite.setHandler(balancer.getHandler());
balancer.setHandler(rewrite);
rewrite.setRewriteRequestURI(true);
rewrite.addRule(new VirtualHostRuleContainer());
balancer.start();

ContentResponse response = getBalancedResponse("/test/%0A");
assertThat(response.getStatus(), is(200));
assertThat(response.getContentAsString(), containsString("requestURI='/context/mapping/test/%0A'"));
assertThat(response.getContentAsString(), containsString("servletPath='/mapping'"));
assertThat(response.getContentAsString(), containsString("pathInfo='/test/\n'"));
}

private String readFirstLine(byte[] responseBytes) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(responseBytes)));
return reader.readLine();
}

public static final class DumpServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/plain");
resp.getWriter().printf("requestURI='%s'%n", req.getRequestURI());
resp.getWriter().printf("servletPath='%s'%n", req.getServletPath());
resp.getWriter().printf("pathInfo='%s'%n", req.getPathInfo());
resp.getWriter().flush();
}
}

public static final class CounterServlet extends HttpServlet
{
private final AtomicInteger counter = new AtomicInteger();
Expand Down

0 comments on commit 1749a90

Please sign in to comment.