use of org.carapaceproxy.utils.TestEndpointMapper 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> !!"));
}
}
}
use of org.carapaceproxy.utils.TestEndpointMapper 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"));
}
}
}
use of org.carapaceproxy.utils.TestEndpointMapper in project carapaceproxy by diennea.
the class MultiListeningEndpointTest method test.
@Test
public void test() throws Exception {
stubFor(get(urlEqualTo("/index.html?redir")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/html").withBody("it <b>works</b> !!")));
int port = 1234;
int port2 = 1235;
TestEndpointMapper mapper = new TestEndpointMapper("localhost", wireMockRule.port());
try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", port, mapper, tmpDir.newFolder())) {
server.addListener(new NetworkListenerConfiguration("localhost", port2));
server.start();
// proxy
{
String s = IOUtils.toString(new URL("http://localhost:" + port + "/index.html?redir").toURI(), "utf-8");
System.out.println("s:" + s);
assertEquals("it <b>works</b> !!", s);
}
{
String s = IOUtils.toString(new URL("http://localhost:" + port2 + "/index.html?redir").toURI(), "utf-8");
System.out.println("s:" + s);
assertEquals("it <b>works</b> !!", s);
}
}
}
use of org.carapaceproxy.utils.TestEndpointMapper 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"));
}
});
}
use of org.carapaceproxy.utils.TestEndpointMapper in project carapaceproxy by diennea.
the class CacheContentLengthLimitTest method testFileSizeCache.
private void testFileSizeCache(String body, boolean chunked) throws Exception {
TestEndpointMapper mapper = new TestEndpointMapper("localhost", wireMockRule.port(), true);
EndpointKey key = new EndpointKey("localhost", wireMockRule.port());
// No size checking
{
try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
server.getCurrentConfiguration().setCacheMaxFileSize(0);
server.getCurrentConfiguration().setRequestCompressionEnabled(false);
server.getCache().reloadConfiguration(server.getCurrentConfiguration());
server.start();
// First request
requestAndTestCached(body, chunked, key, server, false, 1);
// Should be cached
requestAndTestCached(body, chunked, key, server, true, 1);
}
}
// Max size set to current content size
{
try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
server.getCurrentConfiguration().setCacheMaxFileSize(body.length());
server.getCurrentConfiguration().setRequestCompressionEnabled(false);
server.getCache().reloadConfiguration(server.getCurrentConfiguration());
server.start();
// First request
requestAndTestCached(body, chunked, key, server, false, 1);
// Should be cached
requestAndTestCached(body, chunked, key, server, true, 1);
}
}
// Max size set to drop current content
{
try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
server.getCurrentConfiguration().setCacheMaxFileSize(body.length() - 1);
server.getCurrentConfiguration().setRequestCompressionEnabled(false);
server.getCache().reloadConfiguration(server.getCurrentConfiguration());
server.start();
// First request
requestAndTestCached(body, chunked, key, server, false, 0);
// Should not be cached
requestAndTestCached(body, chunked, key, server, false, 0);
}
}
}
Aggregations