use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testUpstreamTransformationThrowsBeforeCommittingProxyRequest.
@Test
public void testUpstreamTransformationThrowsBeforeCommittingProxyRequest() throws Exception {
startServer(new EchoHttpServlet());
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
return new ContentTransformer() {
@Override
public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
throw new NullPointerException("explicitly_thrown_by_test");
}
};
}
});
startClient();
byte[] bytes = new byte[1024];
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).content(new BytesContentProvider(bytes)).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(500, response.getStatus());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testDownstreamTransformationKnownContentLengthDroppingLastChunk.
@Test
public void testDownstreamTransformationKnownContentLengthDroppingLastChunk() throws Exception {
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
byte[] chunk = new byte[1024];
int contentLength = 2 * chunk.length;
response.setContentLength(contentLength);
ServletOutputStream output = response.getOutputStream();
output.write(chunk);
output.flush();
sleep(1000);
output.write(chunk);
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new ContentTransformer() {
@Override
public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
if (!finished)
output.add(input);
}
};
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ForwardProxyTLSServerTest method testProxyAuthentication.
private void testProxyAuthentication(String realm, ConnectHandler connectHandler, boolean includeAddress) throws Exception {
startTLSServer(new ServerHandler());
startProxy(connectHandler);
HttpClient httpClient = new HttpClient(newSslContextFactory());
HttpProxy httpProxy = newHttpProxy();
if (includeAddress)
httpProxy.getIncludedAddresses().add("localhost:" + serverConnector.getLocalPort());
httpClient.getProxyConfiguration().getProxies().add(httpProxy);
URI uri = URI.create((proxySslContextFactory == null ? "http" : "https") + "://localhost:" + proxyConnector.getLocalPort());
httpClient.getAuthenticationStore().addAuthentication(new BasicAuthentication(uri, realm, "proxyUser", "proxyPassword"));
httpClient.start();
try {
String host = "localhost";
String body = "BODY";
ContentResponse response = httpClient.newRequest(host, serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.GET).path("/echo?body=" + URLEncoder.encode(body, "UTF-8")).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
String content = response.getContentAsString();
Assert.assertEquals(body, content);
} finally {
httpClient.stop();
}
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class ProxyServletFailureTest method testServerDown.
@Test
public void testServerDown() throws Exception {
prepareProxy();
prepareServer(new EmptyHttpServlet());
// Shutdown the server
int serverPort = serverConnector.getLocalPort();
server.stop();
ContentResponse response = client.newRequest("localhost", serverPort).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(502, response.getStatus());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AbstractModifyMaxInactiveIntervalTest method testSessionExpiryAfterModifiedMaxInactiveInterval.
@Test
public void testSessionExpiryAfterModifiedMaxInactiveInterval() throws Exception {
int oldMaxInactive = 4;
int newMaxInactive = 20;
int sleep = oldMaxInactive + (int) (oldMaxInactive * 0.8);
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
TestServer server = new TestServer(0, oldMaxInactive, __scavenge, cacheFactory, storeFactory);
ServletContextHandler ctxA = server.addContext("/mod");
ctxA.addServlet(TestModServlet.class, "/test");
server.start();
int port = server.getPort();
try {
HttpClient client = new HttpClient();
client.start();
try {
// Perform a request to create a session
ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?action=create");
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
//do another request to change the maxinactive interval
Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=change&val=" + newMaxInactive);
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
//wait for longer than the old inactive interval
Thread.currentThread().sleep(sleep * 1000L);
//do another request using the cookie to ensure the session is still there
request = client.newRequest("http://localhost:" + port + "/mod/test?action=test&val=" + newMaxInactive);
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
} finally {
client.stop();
}
} finally {
server.stop();
}
}
Aggregations