use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpCookieTest method test_CookieWithoutValue.
@Test
public void test_CookieWithoutValue() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
response.addHeader("Set-Cookie", "");
}
});
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(client.getCookieStore().getCookies().isEmpty());
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpCookieTest method test_CookieIsSent.
@Test
public void test_CookieIsSent() throws Exception {
final String name = "foo";
final String value = "bar";
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Cookie[] cookies = request.getCookies();
Assert.assertNotNull(cookies);
Assert.assertEquals(1, cookies.length);
Cookie cookie = cookies[0];
Assert.assertEquals(name, cookie.getName());
Assert.assertEquals(value, cookie.getValue());
}
});
String host = "localhost";
int port = connector.getLocalPort();
String path = "/path";
String uri = scheme + "://" + host + ":" + port;
HttpCookie cookie = new HttpCookie(name, value);
client.getCookieStore().add(URI.create(uri), cookie);
Response response = client.GET(scheme + "://" + host + ":" + port + path);
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpResponseAbortTest method testAbortOnContentBeforeRequestTermination.
@Test
public void testAbortOnContentBeforeRequestTermination() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
baseRequest.setHandled(true);
OutputStream output = response.getOutputStream();
output.write(1);
output.flush();
output.write(2);
output.flush();
} catch (IOException ignored) {
// The client may have already closed, and we'll get an exception here, but it's expected
}
}
});
final CountDownLatch abortLatch = new CountDownLatch(1);
final AtomicInteger completes = new AtomicInteger();
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestSuccess(new org.eclipse.jetty.client.api.Request.SuccessListener() {
@Override
public void onSuccess(org.eclipse.jetty.client.api.Request request) {
try {
abortLatch.await(5, TimeUnit.SECONDS);
} catch (InterruptedException x) {
x.printStackTrace();
}
}
}).onResponseContent(new Response.ContentListener() {
@Override
public void onContent(Response response, ByteBuffer content) {
try {
response.abort(new Exception());
abortLatch.countDown();
// Delay to let the request side to finish its processing.
Thread.sleep(1000);
} catch (InterruptedException x) {
x.printStackTrace();
}
}
}).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
completes.incrementAndGet();
Assert.assertTrue(result.isFailed());
completeLatch.countDown();
}
});
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
// Wait to be sure that the complete event is only notified once.
Thread.sleep(1000);
Assert.assertEquals(1, completes.get());
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpClientURITest method testPathWithQueryAndParam.
@Test
public void testPathWithQueryAndParam() throws Exception {
String name1 = "a";
String value1 = "1";
String name2 = "b";
String value2 = "2";
final String query = name1 + "=" + value1 + "&" + name2 + "=" + value2;
final String path = "/path";
String pathQuery = path + "?" + query;
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Assert.assertEquals(path, request.getRequestURI());
Assert.assertEquals(query, request.getQueryString());
}
});
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).path(path + "?" + name1 + "=" + value1).param(name2, value2);
Assert.assertEquals(path, request.getPath());
Assert.assertEquals(query, request.getQuery());
Assert.assertTrue(request.getURI().toString().endsWith(pathQuery));
Fields params = request.getParams();
Assert.assertEquals(2, params.getSize());
Assert.assertEquals(value1, params.get(name1).getValue());
Assert.assertEquals(value2, params.get(name2).getValue());
ContentResponse response = request.send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.
the class HttpClientURITest method testPath.
@Test
public void testPath() throws Exception {
final String path = "/path";
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Assert.assertEquals(path, request.getRequestURI());
}
});
Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).path(path);
Assert.assertEquals(path, request.getPath());
Assert.assertNull(request.getQuery());
Fields params = request.getParams();
Assert.assertEquals(0, params.getSize());
Assert.assertTrue(request.getURI().toString().endsWith(path));
ContentResponse response = request.send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Aggregations