Search in sources :

Example 1 with ProxyServlet

use of org.mockserver.proxyservlet.ProxyServlet in project mockserver by mock-server.

the class ServletProxyIntegrationTest method startServer.

@BeforeClass
@SuppressWarnings("deprecation")
public static void startServer() throws Exception {
    servletContext = "";
    // start server
    echoServer = new EchoServer(false);
    // wait for server to start up
    TimeUnit.MILLISECONDS.sleep(500);
    // start proxy (in tomcat)
    tomcat = new Tomcat();
    tomcat.setBaseDir(new File(".").getCanonicalPath() + File.separatorChar + "tomcat" + (servletContext.length() > 0 ? "_" + servletContext : ""));
    // add http port
    tomcat.setPort(PROXY_PORT);
    Connector defaultConnector = tomcat.getConnector();
    defaultConnector.setRedirectPort(PROXY_HTTPS_PORT);
    // add https connector
    KeyStoreFactory keyStoreFactory = new KeyStoreFactory(configuration(), new MockServerLogger());
    keyStoreFactory.loadOrCreateKeyStore();
    Connector httpsConnector = new Connector();
    httpsConnector.setPort(PROXY_HTTPS_PORT);
    httpsConnector.setSecure(true);
    httpsConnector.setAttribute("keyAlias", KeyStoreFactory.KEY_STORE_CERT_ALIAS);
    httpsConnector.setAttribute("keystorePass", KeyStoreFactory.KEY_STORE_PASSWORD);
    httpsConnector.setAttribute("keystoreFile", new File(keyStoreFactory.keyStoreFileName).getAbsoluteFile());
    httpsConnector.setAttribute("sslProtocol", "TLS");
    httpsConnector.setAttribute("clientAuth", false);
    httpsConnector.setAttribute("SSLEnabled", true);
    Service service = tomcat.getService();
    service.addConnector(httpsConnector);
    // add servlet
    Context ctx = tomcat.addContext("/" + servletContext, new File(".").getAbsolutePath());
    tomcat.addServlet("/" + servletContext, "mockServerServlet", new ProxyServlet());
    ctx.addServletMappingDecoded("/*", "mockServerServlet");
    ctx.addApplicationListener(ProxyServlet.class.getName());
    // start server
    tomcat.start();
    // start client
    mockServerClient = new MockServerClient("localhost", PROXY_PORT, servletContext);
}
Also used : Context(org.apache.catalina.Context) Connector(org.apache.catalina.connector.Connector) Tomcat(org.apache.catalina.startup.Tomcat) KeyStoreFactory(org.mockserver.socket.tls.KeyStoreFactory) MockServerLogger(org.mockserver.logging.MockServerLogger) ProxyServlet(org.mockserver.proxyservlet.ProxyServlet) Service(org.apache.catalina.Service) MockServerClient(org.mockserver.client.MockServerClient) File(java.io.File) EchoServer(org.mockserver.echo.http.EchoServer) BeforeClass(org.junit.BeforeClass)

Example 2 with ProxyServlet

use of org.mockserver.proxyservlet.ProxyServlet in project mockserver by mock-server.

the class ServletProxyWithContextPathIntegrationTest method startServer.

@BeforeClass
@SuppressWarnings("deprecation")
public static void startServer() throws Exception {
    servletContext = "proxy";
    // start server
    echoServer = new EchoServer(false);
    // wait for server to start up
    TimeUnit.MILLISECONDS.sleep(500);
    // start proxy (in tomcat)
    tomcat = new Tomcat();
    tomcat.setBaseDir(new File(".").getCanonicalPath() + File.separatorChar + "tomcat" + (servletContext.length() > 0 ? "_" + servletContext : ""));
    // add http port
    tomcat.setPort(PROXY_PORT);
    Connector defaultConnector = tomcat.getConnector();
    defaultConnector.setRedirectPort(PROXY_HTTPS_PORT);
    // add https connector
    KeyStoreFactory keyStoreFactory = new KeyStoreFactory(configuration(), new MockServerLogger());
    keyStoreFactory.loadOrCreateKeyStore();
    Connector httpsConnector = new Connector();
    httpsConnector.setPort(PROXY_HTTPS_PORT);
    httpsConnector.setSecure(true);
    httpsConnector.setAttribute("keyAlias", KeyStoreFactory.KEY_STORE_CERT_ALIAS);
    httpsConnector.setAttribute("keystorePass", KeyStoreFactory.KEY_STORE_PASSWORD);
    httpsConnector.setAttribute("keystoreFile", new File(keyStoreFactory.keyStoreFileName).getAbsoluteFile());
    httpsConnector.setAttribute("sslProtocol", "TLS");
    httpsConnector.setAttribute("clientAuth", false);
    httpsConnector.setAttribute("SSLEnabled", true);
    Service service = tomcat.getService();
    service.addConnector(httpsConnector);
    // add servlet
    Context ctx = tomcat.addContext("/" + servletContext, new File(".").getAbsolutePath());
    tomcat.addServlet("/" + servletContext, "mockServerServlet", new ProxyServlet());
    ctx.addServletMappingDecoded("/*", "mockServerServlet");
    ctx.addApplicationListener(ProxyServlet.class.getName());
    // start server
    tomcat.start();
    // start client
    mockServerClient = new MockServerClient("localhost", PROXY_PORT, servletContext);
}
Also used : Context(org.apache.catalina.Context) Connector(org.apache.catalina.connector.Connector) Tomcat(org.apache.catalina.startup.Tomcat) KeyStoreFactory(org.mockserver.socket.tls.KeyStoreFactory) MockServerLogger(org.mockserver.logging.MockServerLogger) ProxyServlet(org.mockserver.proxyservlet.ProxyServlet) Service(org.apache.catalina.Service) MockServerClient(org.mockserver.client.MockServerClient) File(java.io.File) EchoServer(org.mockserver.echo.http.EchoServer) BeforeClass(org.junit.BeforeClass)

Aggregations

File (java.io.File)2 Context (org.apache.catalina.Context)2 Service (org.apache.catalina.Service)2 Connector (org.apache.catalina.connector.Connector)2 Tomcat (org.apache.catalina.startup.Tomcat)2 BeforeClass (org.junit.BeforeClass)2 MockServerClient (org.mockserver.client.MockServerClient)2 EchoServer (org.mockserver.echo.http.EchoServer)2 MockServerLogger (org.mockserver.logging.MockServerLogger)2 ProxyServlet (org.mockserver.proxyservlet.ProxyServlet)2 KeyStoreFactory (org.mockserver.socket.tls.KeyStoreFactory)2