JETTY-1415: Start/Stop Server and Client only once in test, code format

This commit is contained in:
Thomas Becker 2011-09-26 17:48:52 +02:00 committed by Jesse McConnell
parent 5e6a9ea78e
commit e14cc536ee
2 changed files with 27 additions and 24 deletions

View File

@ -18,12 +18,11 @@ import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
public class ProxyRule extends PatternRule implements Rule.ApplyURI
public class ProxyRule extends PatternRule
{
private static final Logger _log = Log.getLogger(ProxyRule.class);
@ -44,6 +43,7 @@ public class ProxyRule extends PatternRule implements Rule.ApplyURI
_DontProxyHeaders.add("upgrade");
}
/* ------------------------------------------------------------ */
public ProxyRule()
{
_handling = true;
@ -51,6 +51,7 @@ public class ProxyRule extends PatternRule implements Rule.ApplyURI
}
/* ------------------------------------------------------------ */
private void initializeClient() throws Exception
{
_client = new HttpClient();
@ -65,11 +66,7 @@ public class ProxyRule extends PatternRule implements Rule.ApplyURI
return new HttpURI(_proxyTo + uri);
}
public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
{
System.out.println("applyURI called");
}
/* ------------------------------------------------------------ */
@Override
protected String apply(String target, HttpServletRequest request, final HttpServletResponse response) throws IOException
{
@ -229,6 +226,7 @@ public class ProxyRule extends PatternRule implements Rule.ApplyURI
return target;
}
/* ------------------------------------------------------------ */
private HttpURI createUrl(HttpServletRequest request, final int debug) throws MalformedURLException
{
String uri = request.getRequestURI();
@ -244,6 +242,7 @@ public class ProxyRule extends PatternRule implements Rule.ApplyURI
return url;
}
/* ------------------------------------------------------------ */
private boolean createHeaders(final HttpServletRequest request, final int debug, HttpExchange exchange)
{
// check connection header
@ -315,6 +314,7 @@ public class ProxyRule extends PatternRule implements Rule.ApplyURI
return hasContent;
}
/* ------------------------------------------------------------ */
public void setProxyTo(String proxyTo)
{
this._proxyTo = proxyTo;

View File

@ -30,22 +30,22 @@ import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.junit.After;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class ProxyRuleTest
{
private ProxyRule _rule;
private RewriteHandler _handler;
private Server _proxyServer = new Server();
private Connector _proxyServerConnector = new SelectChannelConnector();
private Server _targetServer = new Server();
private Connector _targetServerConnector = new SelectChannelConnector();
private HttpClient httpClient = new HttpClient();
private static ProxyRule _rule;
private static RewriteHandler _handler;
private static Server _proxyServer = new Server();
private static Connector _proxyServerConnector = new SelectChannelConnector();
private static Server _targetServer = new Server();
private static Connector _targetServerConnector = new SelectChannelConnector();
private static HttpClient _httpClient = new HttpClient();
@Before
public void init() throws Exception
@BeforeClass
public static void setupOnce() throws Exception
{
_targetServer.addConnector(_targetServerConnector);
_targetServer.setHandler(new AbstractHandler()
@ -73,12 +73,15 @@ public class ProxyRuleTest
_proxyServer.setHandler(_handler);
_proxyServer.start();
httpClient.start();
_httpClient.start();
}
@After
public void destroy()
@AfterClass
public static void destroy() throws Exception
{
_httpClient.stop();
_proxyServer.stop();
_targetServer.stop();
_rule = null;
}
@ -92,7 +95,7 @@ public class ProxyRuleTest
String url = "http://localhost:" + _proxyServerConnector.getLocalPort() + "/foo?body=" + URLEncoder.encode(body,"UTF-8");
exchange.setURL(url);
httpClient.send(exchange);
_httpClient.send(exchange);
assertEquals(HttpExchange.STATUS_COMPLETED,exchange.waitForDone());
assertEquals("uri: / some content",exchange.getResponseContent());
assertEquals(201,exchange.getResponseStatus());
@ -108,7 +111,7 @@ public class ProxyRuleTest
String url = "http://localhost:" + _proxyServerConnector.getLocalPort() + "/foo/bar/foobar?body=" + URLEncoder.encode(body,"UTF-8");
exchange.setURL(url);
httpClient.send(exchange);
_httpClient.send(exchange);
assertEquals(HttpExchange.STATUS_COMPLETED,exchange.waitForDone());
assertEquals("uri: /bar/foobar some content",exchange.getResponseContent());
assertEquals(201,exchange.getResponseStatus());
@ -123,7 +126,7 @@ public class ProxyRuleTest
String url = "http://localhost:" + _proxyServerConnector.getLocalPort() + "/foobar?body=" + URLEncoder.encode(body,"UTF-8");
exchange.setURL(url);
httpClient.send(exchange);
_httpClient.send(exchange);
assertEquals(HttpExchange.STATUS_COMPLETED,exchange.waitForDone());
assertEquals(404,exchange.getResponseStatus());
}