use of org.eclipse.jetty.client.api.Request 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.Request 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();
}
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class AbstractModifyMaxInactiveIntervalTest method testSetMaxInactiveIntervalWithNonImmortalSessionAndEviction.
@Test
public void testSetMaxInactiveIntervalWithNonImmortalSessionAndEviction() throws Exception {
int oldMaxInactive = 10;
int newMaxInactive = 2;
int evict = 4;
int sleep = evict;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(evict);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
TestServer server = new TestServer(0, oldMaxInactive, 1, 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 reduce the maxinactive interval
Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=change&val=" + newMaxInactive + "&wait=" + sleep);
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
//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();
}
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class AbstractModifyMaxInactiveIntervalTest method testSetMaxInactiveIntervalWithImmortalSessionAndEviction.
@Test
public void testSetMaxInactiveIntervalWithImmortalSessionAndEviction() throws Exception {
int oldMaxInactive = -1;
//2min
int newMaxInactive = 120;
int evict = 2;
int sleep = evict;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(evict);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
TestServer server = new TestServer(0, oldMaxInactive, 1, 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 reduce the maxinactive interval
Request request = client.newRequest("http://localhost:" + port + "/mod/test?action=change&val=" + newMaxInactive + "&wait=" + sleep);
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
//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();
}
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class AbstractProxySerializationTest method testProxySerialization.
@Test
public void testProxySerialization() throws Exception {
String contextPath = "";
String servletMapping = "/server";
int scavengePeriod = 10;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
TestServer server = new TestServer(0, 20, scavengePeriod, cacheFactory, storeFactory);
ServletContextHandler context = server.addContext(contextPath);
InputStream is = this.getClass().getClassLoader().getResourceAsStream("proxy-serialization.jar");
File testDir = MavenTestingUtils.getTargetTestingDir("proxy-serialization");
testDir.mkdirs();
File extractedJar = new File(testDir, "proxy-serialization.jar");
extractedJar.createNewFile();
IO.copy(is, new FileOutputStream(extractedJar));
URLClassLoader loader = new URLClassLoader(new URL[] { extractedJar.toURI().toURL() }, Thread.currentThread().getContextClassLoader());
context.setClassLoader(loader);
context.addServlet("TestServlet", servletMapping);
customizeContext(context);
try {
server.start();
int port = server.getPort();
HttpClient client = new HttpClient();
client.start();
try {
ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?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=");
//stop the context to be sure the sesssion will be passivated
context.stop();
//restart the context
context.start();
// Make another request using the session id from before
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=test");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
} finally {
client.stop();
}
} finally {
server.stop();
}
}
Aggregations