/*
* Copyright (c) xlightweb.org, 2008 - 2009. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
* The latest copy of this software may be found on http://www.xlightweb.org/
*/
package org.xlightweb;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.mortbay.jetty.servlet.HashSessionManager;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.jetty.servlet.SessionHandler;
import org.xlightweb.IHttpExchange;
import org.xlightweb.IHttpRequest;
import org.xlightweb.IHttpRequestHandler;
import org.xlightweb.client.HttpClient;
import org.xlightweb.server.HttpServer;
/**
*
* @author grro@xlightweb.org
*/
public final class CompareContextTest {
@Test
public void testContextPath() throws Exception {
// start jetty server
int jettyPort = 9437;
org.mortbay.jetty.Server jettyServer = new org.mortbay.jetty.Server(jettyPort);
org.mortbay.jetty.servlet.Context rootCtx = new org.mortbay.jetty.servlet.Context(jettyServer, "/ctx/subctx");
rootCtx.setSessionHandler(new SessionHandler(new HashSessionManager()));
ServletHolder servletHolder = new ServletHolder(new CompareServlet());
rootCtx.addServlet(servletHolder, "/hdlPath/*");
jettyServer.start();
Context ctx = new Context("/ctx/subctx");
ctx.addHandler("/hdlPath/*", new MyHandler());
HttpServer server = new HttpServer(ctx);
server.start();
HttpClient httpClient = new HttpClient();
IHttpResponse response1 = httpClient.call(new GetRequest("http://localhost:" + jettyPort + "/ctx/subctx/hdlPath/test/?param=value"));
String body1 = response1.getBlockingBody().readString();
IHttpResponse response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx/subctx/hdlPath/test/?param=value"));
String body = response.getBlockingBody().readString();
System.out.println(body);
Assert.assertEquals(body1, body);
jettyServer.stop();
server.close();
}
private static final class MyHandler implements IHttpRequestHandler {
public void onRequest(IHttpExchange exchange) throws IOException,BadMessageException {
IHttpRequest req = exchange.getRequest();
StringBuilder sb = new StringBuilder();
sb.append("path=" + req.getRequestHandlerPath() + "\r\n");
sb.append("ctx=" + req.getContextPath() + "\r\n");
sb.append("uri=" + req.getRequestURI() + "\r\n");
sb.append("pathInfo=" + req.getPathInfo() + "\r\n");
exchange.send(new HttpResponse(200, "text/plain", sb.toString()));
}
}
private static final class CompareServlet extends HttpServlet {
private static final long serialVersionUID = 6949652395346462235L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter pw = resp.getWriter();
pw.write("path=" + req.getServletPath() + "\r\n");
pw.write("ctx=" + req.getContextPath() + "\r\n");
pw.write("uri=" + req.getRequestURI() + "\r\n");
pw.write("pathInfo=" + req.getPathInfo() + "\r\n");
pw.close();
}
}
}