use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class FastCGIProxyServletTest method testURIRewrite.
@Test
public void testURIRewrite() throws Exception {
String originalPath = "/original/index.php";
String originalQuery = "foo=bar";
String remotePath = "/remote/index.php";
prepare(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Assert.assertThat((String) request.getAttribute(FCGI.Headers.REQUEST_URI), Matchers.startsWith(originalPath));
Assert.assertEquals(originalQuery, request.getAttribute(FCGI.Headers.QUERY_STRING));
Assert.assertThat(request.getRequestURI(), Matchers.endsWith(remotePath));
}
});
context.stop();
String pathAttribute = "_path_attribute";
String queryAttribute = "_query_attribute";
ServletHolder fcgi = context.getServletHandler().getServlet("fcgi");
fcgi.setInitParameter(FastCGIProxyServlet.ORIGINAL_URI_ATTRIBUTE_INIT_PARAM, pathAttribute);
fcgi.setInitParameter(FastCGIProxyServlet.ORIGINAL_QUERY_ATTRIBUTE_INIT_PARAM, queryAttribute);
context.insertHandler(new HandlerWrapper() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (target.startsWith("/remote/")) {
request.setAttribute(pathAttribute, originalPath);
request.setAttribute(queryAttribute, originalQuery);
}
super.handle(target, baseRequest, request, response);
}
});
context.start();
ContentResponse response = client.newRequest("localhost", httpConnector.getLocalPort()).path(remotePath).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class TryFilesFilterTest method testHTTPSRequestIsForwarded.
@Test
public void testHTTPSRequestIsForwarded() throws Exception {
final String path = "/one/";
prepare(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Assert.assertTrue("https".equalsIgnoreCase(req.getScheme()));
Assert.assertTrue(req.isSecure());
Assert.assertEquals(forwardPath, req.getRequestURI());
Assert.assertTrue(req.getQueryString().endsWith(path));
}
});
ContentResponse response = client.newRequest("localhost", sslConnector.getLocalPort()).scheme("https").path(path).send();
Assert.assertEquals(200, response.getStatus());
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class TestOSGiUtil method testHttpServiceGreetings.
protected static void testHttpServiceGreetings(BundleContext bundleContext, String protocol, int port) throws Exception {
assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.boot");
assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.httpservice");
assertActiveBundle(bundleContext, "org.eclipse.equinox.http.servlet");
// in the OSGi world this would be bad code and we should use a bundle
// tracker.
// here we purposely want to make sure that the httpService is actually
// ready.
ServiceReference sr = bundleContext.getServiceReference(HttpService.class.getName());
Assert.assertNotNull("The httpServiceOSGiBundle is started and should " + "have deployed a service reference for HttpService", sr);
HttpService http = (HttpService) bundleContext.getService(sr);
http.registerServlet("/greetings", new HttpServlet() {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("Hello");
}
}, null, null);
// now test the servlet
HttpClient client = protocol.equals("https") ? new HttpClient(newSslContextFactory()) : new HttpClient();
try {
client.start();
ContentResponse response = client.GET(protocol + "://127.0.0.1:" + port + "/greetings");
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
String content = new String(response.getContent());
Assert.assertEquals("Hello", content);
} finally {
client.stop();
}
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class DispatcherForwardTest method testQueryAggregatesWithFormBeforeAndAfterForward.
@Test
public void testQueryAggregatesWithFormBeforeAndAfterForward() throws Exception {
// 1. request /one?a=1 + content b=2
// 1. assert params => a=1&b=2
// 1. forward /two?c=3
// 2. assert query => a=1&c=3 + params => a=1&b=2&c=3
// 1. assert query => a=1 + params => a=1&b=2
CountDownLatch latch = new CountDownLatch(1);
final String query1 = "a=1%20one";
final String query2 = "c=3%20three";
final String query3 = "c=3%20three&a=1%20one";
final String form = "b=2%20two";
servlet1 = new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
checkThat(req.getQueryString(), Matchers.equalTo(query1));
checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
checkThat(req.getParameter("b"), Matchers.equalTo("2 two"));
req.getRequestDispatcher("/two?" + query2).forward(req, resp);
checkThat(req.getQueryString(), Matchers.equalTo(query1));
checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
checkThat(req.getParameter("b"), Matchers.equalTo("2 two"));
checkThat(req.getParameter("c"), Matchers.nullValue());
latch.countDown();
}
};
servlet2 = new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
checkThat(req.getQueryString(), Matchers.equalTo(query3));
checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
checkThat(req.getParameter("b"), Matchers.equalTo("2 two"));
checkThat(req.getParameter("c"), Matchers.equalTo("3 three"));
}
};
prepare();
String request = "" + "POST /one?" + query1 + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: " + form.length() + "\r\n" + "Connection: close\r\n" + "\r\n" + form;
String response = connector.getResponses(request);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(response, response.startsWith("HTTP/1.1 200"));
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class DispatcherForwardTest method testQueryRetainedByForwardWithoutQuery.
@Test
public void testQueryRetainedByForwardWithoutQuery() throws Exception {
// 1. request /one?a=1%20one
// 1. forward /two
// 2. assert query => a=1 one
// 1. assert query => a=1 one
CountDownLatch latch = new CountDownLatch(1);
final String query1 = "a=1%20one";
servlet1 = new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
checkThat(req.getQueryString(), Matchers.equalTo(query1));
req.getRequestDispatcher("/two").forward(req, resp);
checkThat(req.getQueryString(), Matchers.equalTo(query1));
checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
latch.countDown();
}
};
servlet2 = new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
checkThat(req.getQueryString(), Matchers.equalTo(query1));
checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
}
};
prepare();
String request = "" + "GET /one?" + query1 + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n";
String response = connector.getResponses(request);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(response, response.startsWith("HTTP/1.1 200"));
}
Aggregations