Search in sources :

Example 1 with RawHttpClient

use of org.carapaceproxy.utils.RawHttpClient in project carapaceproxy by diennea.

the class XForwardedForFilterTest method testXForwardedForFilter.

@Test
public void testXForwardedForFilter() throws Exception {
    stubFor(get(urlEqualTo("/index.html")).withHeader("X-Forwarded-For", equalTo("127.0.0.1")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/html").withBody("it <b>works</b> !!")));
    TestEndpointMapper mapper = new TestEndpointMapper("localhost", wireMockRule.port());
    EndpointKey key = new EndpointKey("localhost", wireMockRule.port());
    try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
        server.addRequestFilter(new RequestFilterConfiguration(XForwardedForRequestFilter.TYPE, Collections.emptyMap()));
        server.start();
        int port = server.getLocalPort();
        try (RawHttpClient client = new RawHttpClient("localhost", port)) {
            String s = client.executeRequest("GET /index.html HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n").toString();
            System.out.println("s:" + s);
            assertTrue(s.contains("it <b>works</b> !!"));
        }
        try (RawHttpClient client = new RawHttpClient("localhost", port)) {
            String s = client.executeRequest("GET /index.html HTTP/1.1\r\nHost: localhost\r\nX-Forwarded-For: 1.2.3.4\r\nConnection: close\r\n\r\n").toString();
            System.out.println("s:" + s);
            assertTrue(s.contains("it <b>works</b> !!"));
        }
    }
}
Also used : RawHttpClient(org.carapaceproxy.utils.RawHttpClient) TestEndpointMapper(org.carapaceproxy.utils.TestEndpointMapper) HttpProxyServer(org.carapaceproxy.core.HttpProxyServer) RequestFilterConfiguration(org.carapaceproxy.server.config.RequestFilterConfiguration) EndpointKey(org.carapaceproxy.client.EndpointKey) Test(org.junit.Test)

Example 2 with RawHttpClient

use of org.carapaceproxy.utils.RawHttpClient in project carapaceproxy by diennea.

the class XForwardedForFilterTest method testNoXForwardedForFilter.

@Test
public void testNoXForwardedForFilter() throws Exception {
    stubFor(get(urlEqualTo("/index.html")).withHeader("X-Forwarded-For", equalTo("1.2.3.4")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/html").withBody("it <b>works</b> !!")));
    stubFor(get(urlEqualTo("/index.html")).withHeader("X-Forwarded-For", absent()).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/html").withBody("No X-Forwarded-For")));
    TestEndpointMapper mapper = new TestEndpointMapper("localhost", wireMockRule.port());
    EndpointKey key = new EndpointKey("localhost", wireMockRule.port());
    try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
        server.start();
        int port = server.getLocalPort();
        try (RawHttpClient client = new RawHttpClient("localhost", port)) {
            String s = client.executeRequest("GET /index.html HTTP/1.1\r\nHost: localhost\r\nX-Forwarded-For: 1.2.3.4\r\nConnection: close\r\n\r\n").toString();
            System.out.println("s:" + s);
            assertTrue(s.contains("it <b>works</b> !!"));
        }
        try (RawHttpClient client = new RawHttpClient("localhost", port)) {
            String s = client.executeRequest("GET /index.html HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n").toString();
            System.out.println("s:" + s);
            assertTrue(s.contains("No X-Forwarded-For"));
        }
    }
}
Also used : RawHttpClient(org.carapaceproxy.utils.RawHttpClient) TestEndpointMapper(org.carapaceproxy.utils.TestEndpointMapper) HttpProxyServer(org.carapaceproxy.core.HttpProxyServer) EndpointKey(org.carapaceproxy.client.EndpointKey) Test(org.junit.Test)

Example 3 with RawHttpClient

use of org.carapaceproxy.utils.RawHttpClient in project carapaceproxy by diennea.

the class SSLSNITest method testTLSVersion.

@Test
public void testTLSVersion() throws Exception {
    String nonLocalhost = InetAddress.getLocalHost().getCanonicalHostName();
    String certificate = TestUtils.deployResource("localhost.p12", tmpDir.getRoot());
    stubFor(get(urlEqualTo("/index.html")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/html").withHeader("Content-Length", "it <b>works</b> !!".length() + "").withBody("it <b>works</b> !!")));
    TestEndpointMapper mapper = new TestEndpointMapper("localhost", wireMockRule.port(), true);
    // TLS 1.3 support checking
    try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.getRoot())) {
        server.addCertificate(new SSLCertificateConfiguration(nonLocalhost, certificate, "testproxy", STATIC));
        server.addListener(new NetworkListenerConfiguration(nonLocalhost, 0, true, false, null, nonLocalhost, null, null, "TLSv1.3"));
        server.start();
        int port = server.getLocalPort();
        try (RawHttpClient client = new RawHttpClient(nonLocalhost, port, true, nonLocalhost)) {
            RawHttpClient.HttpResponse resp = client.executeRequest("GET /index.html HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n");
            assertTrue(resp.toString().contains("it <b>works</b> !!"));
            SSLSession session = client.getSSLSocket().getSession();
            assertTrue("TLSv1.3".equals(session.getProtocol()));
        }
    }
    // default ssl protocol version support checking
    for (String proto : DEFAULT_SSL_PROTOCOLS) {
        try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.getRoot())) {
            server.addCertificate(new SSLCertificateConfiguration(nonLocalhost, certificate, "testproxy", STATIC));
            server.addListener(new NetworkListenerConfiguration(nonLocalhost, 0, true, false, null, nonLocalhost, null, null, proto));
            server.start();
            int port = server.getLocalPort();
            try (RawHttpClient client = new RawHttpClient(nonLocalhost, port, true, nonLocalhost)) {
                RawHttpClient.HttpResponse resp = client.executeRequest("GET /index.html HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n");
                assertTrue(resp.toString().contains("it <b>works</b> !!"));
                SSLSession session = client.getSSLSocket().getSession();
                assertEquals(proto, session.getProtocol());
            }
        }
    }
    try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.getRoot())) {
        server.addCertificate(new SSLCertificateConfiguration(nonLocalhost, certificate, "testproxy", STATIC));
        server.addListener(new NetworkListenerConfiguration(nonLocalhost, 0, true, false, null, nonLocalhost, null, null));
        server.start();
        int port = server.getLocalPort();
        try (RawHttpClient client = new RawHttpClient(nonLocalhost, port, true, nonLocalhost)) {
            RawHttpClient.HttpResponse resp = client.executeRequest("GET /index.html HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n");
            assertTrue(resp.toString().contains("it <b>works</b> !!"));
            SSLSession session = client.getSSLSocket().getSession();
            assertTrue(DEFAULT_SSL_PROTOCOLS.contains(session.getProtocol()));
        }
    }
    // wrong ssl protocol version checking
    TestUtils.assertThrows(ConfigurationNotValidException.class, () -> {
        try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.getRoot())) {
            server.addCertificate(new SSLCertificateConfiguration(nonLocalhost, certificate, "testproxy", STATIC));
            server.addListener(new NetworkListenerConfiguration(nonLocalhost, 0, true, false, null, nonLocalhost, null, null, "TLSvWRONG"));
        }
    });
}
Also used : RawHttpClient(org.carapaceproxy.utils.RawHttpClient) SSLCertificateConfiguration(org.carapaceproxy.server.config.SSLCertificateConfiguration) TestEndpointMapper(org.carapaceproxy.utils.TestEndpointMapper) HttpProxyServer(org.carapaceproxy.core.HttpProxyServer) SSLSession(javax.net.ssl.SSLSession) NetworkListenerConfiguration(org.carapaceproxy.server.config.NetworkListenerConfiguration) Test(org.junit.Test)

Example 4 with RawHttpClient

use of org.carapaceproxy.utils.RawHttpClient in project carapaceproxy by diennea.

the class UnreachableBackendTest method testNonHttpResponseThenClose.

@Test
public void testNonHttpResponseThenClose() throws Exception {
    stubFor(get(urlEqualTo("/index.html")).willReturn(aResponse().withFault(Fault.RANDOM_DATA_THEN_CLOSE)));
    int dummyport = wireMockRule.port();
    TestEndpointMapper mapper = new TestEndpointMapper("localhost", dummyport, useCache);
    EndpointKey key = new EndpointKey("localhost", dummyport);
    try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder())) {
        Properties properties = new Properties();
        server.configureAtBoot(new PropertiesConfigurationStore(properties));
        server.addListener(new NetworkListenerConfiguration("localhost", 0));
        server.setMapper(mapper);
        server.start();
        int port = server.getLocalPort();
        try (RawHttpClient client = new RawHttpClient("localhost", port)) {
            RawHttpClient.HttpResponse resp = client.executeRequest("GET /index.html HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n");
            String s = resp.toString();
            System.out.println("s:" + s);
            assertEquals("HTTP/1.1 500 Internal Server Error\r\n", resp.getStatusLine());
            assertEquals("<html>\n" + "    <body>\n" + "        An internal error occurred\n" + "    </body>        \n" + "</html>\n", resp.getBodyString());
        }
        assertTrue(server.getBackendHealthManager().isAvailable(key.getHostPort()));
        assertThat((int) ProxyRequestsManager.PENDING_REQUESTS_GAUGE.get(), is(0));
    }
}
Also used : RawHttpClient(org.carapaceproxy.utils.RawHttpClient) TestEndpointMapper(org.carapaceproxy.utils.TestEndpointMapper) HttpProxyServer(org.carapaceproxy.core.HttpProxyServer) PropertiesConfigurationStore(org.carapaceproxy.configstore.PropertiesConfigurationStore) EndpointKey(org.carapaceproxy.client.EndpointKey) Properties(java.util.Properties) NetworkListenerConfiguration(org.carapaceproxy.server.config.NetworkListenerConfiguration) Test(org.junit.Test)

Example 5 with RawHttpClient

use of org.carapaceproxy.utils.RawHttpClient in project carapaceproxy by diennea.

the class UnreachableBackendTest method testConnectionResetByPeer.

@Test
public void testConnectionResetByPeer() throws Exception {
    stubFor(get(urlEqualTo("/index.html")).willReturn(aResponse().withFault(Fault.CONNECTION_RESET_BY_PEER)));
    int dummyport = wireMockRule.port();
    TestEndpointMapper mapper = new TestEndpointMapper("localhost", dummyport, useCache);
    EndpointKey key = new EndpointKey("localhost", dummyport);
    try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
        server.start();
        int port = server.getLocalPort();
        try (RawHttpClient client = new RawHttpClient("localhost", port)) {
            RawHttpClient.HttpResponse resp = client.executeRequest("GET /index.html HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n");
            String s = resp.toString();
            System.out.println("s:" + s);
            assertEquals("HTTP/1.1 500 Internal Server Error\r\n", resp.getStatusLine());
            assertEquals("<html>\n" + "    <body>\n" + "        An internal error occurred\n" + "    </body>        \n" + "</html>\n", resp.getBodyString());
        }
        // no troubles for new connections
        assertTrue(server.getBackendHealthManager().isAvailable(key.getHostPort()));
        assertThat((int) ProxyRequestsManager.PENDING_REQUESTS_GAUGE.get(), is(0));
    }
}
Also used : RawHttpClient(org.carapaceproxy.utils.RawHttpClient) TestEndpointMapper(org.carapaceproxy.utils.TestEndpointMapper) HttpProxyServer(org.carapaceproxy.core.HttpProxyServer) EndpointKey(org.carapaceproxy.client.EndpointKey) Test(org.junit.Test)

Aggregations

RawHttpClient (org.carapaceproxy.utils.RawHttpClient)73 Test (org.junit.Test)73 HttpProxyServer (org.carapaceproxy.core.HttpProxyServer)45 TestEndpointMapper (org.carapaceproxy.utils.TestEndpointMapper)45 EndpointKey (org.carapaceproxy.client.EndpointKey)35 Properties (java.util.Properties)19 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)17 WireMock.get (com.github.tomakehurst.wiremock.client.WireMock.get)17 WireMock.stubFor (com.github.tomakehurst.wiremock.client.WireMock.stubFor)17 WireMock.urlEqualTo (com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo)17 WireMockRule (com.github.tomakehurst.wiremock.junit.WireMockRule)17 Rule (org.junit.Rule)17 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)16 Assert.assertTrue (org.junit.Assert.assertTrue)16 Assert.assertEquals (org.junit.Assert.assertEquals)15 Assert.assertFalse (org.junit.Assert.assertFalse)15 TemporaryFolder (org.junit.rules.TemporaryFolder)15 TestUtils (org.carapaceproxy.utils.TestUtils)14 NetworkListenerConfiguration (org.carapaceproxy.server.config.NetworkListenerConfiguration)13 Parameters (junitparams.Parameters)11