001: /*
002: * Copyright (c) xsocket.org, 2006 - 2008. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
019: * The latest copy of this software may be found on http://www.xsocket.org/
020: */
021: package org.xsocket.connection.http;
022:
023: import java.io.IOException;
024: import java.io.PrintWriter;
025: import java.net.URLEncoder;
026: import java.util.ArrayList;
027: import java.util.Collections;
028: import java.util.HashMap;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.Set;
032:
033: import javax.servlet.ServletException;
034: import javax.servlet.http.HttpServlet;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037:
038: import org.apache.commons.httpclient.HttpClient;
039: import org.apache.commons.httpclient.methods.GetMethod;
040: import org.junit.Assert;
041:
042: import org.junit.Test;
043: import org.xsocket.connection.Server;
044: import org.xsocket.connection.ConnectionUtils;
045: import org.xsocket.connection.http.BodyDataSink;
046: import org.xsocket.connection.http.HttpRequest;
047: import org.xsocket.connection.http.HttpResponseHeader;
048: import org.xsocket.connection.http.server.HttpServer;
049: import org.xsocket.connection.http.server.HttpServerConnection;
050: import org.xsocket.connection.http.server.IHttpResponseContext;
051: import org.xsocket.connection.http.server.IHttpRequestHandler;
052:
053: /**
054: *
055: * @author grro@xsocket.org
056: */
057: public final class CompareParametersTest {
058:
059: @Test
060: public void testCompareParameters() throws Exception {
061:
062: // start jetty server
063: int jettyPort = 8982;
064: WebContainer servletEngine = new WebContainer(jettyPort,
065: new CompareParametersServlet());
066: servletEngine.start();
067:
068: // start xSocket
069: Server server = new HttpServer(0,
070: new CompareParametersHandler());
071: ConnectionUtils.start(server);
072:
073: // compare calls
074: callAndCompare(jettyPort, server.getLocalPort(),
075: "/wiki/Comet_%28programming%29");
076: callAndCompare(jettyPort, server.getLocalPort(),
077: "/wiki/Comet_(programming)");
078: callAndCompare(jettyPort, server.getLocalPort(),
079: "/test/test2#fragment");
080: callAndCompare(jettyPort, server.getLocalPort(),
081: "/test/tee?aa=33&bb=44&aa=55");
082: callAndCompare(jettyPort, server.getLocalPort(), "/test?"
083: + URLEncoder.encode("Schl�ssel", "UTF-8") + "="
084: + URLEncoder.encode("Sch�n", "UTF-8"));
085: callAndCompare(jettyPort, server.getLocalPort(), "/test?key="
086: + URLEncoder.encode("important&confidential", "UTF-8"));
087: callAndCompare(jettyPort, server.getLocalPort(),
088: "/test/tee?aa=33&bb=44");
089: callAndCompare(jettyPort, server.getLocalPort(), "/test/");
090:
091: // shutdown server
092: servletEngine.stop();
093: server.close();
094: }
095:
096: @Test
097: public void testCompareParametersWithServletPath() throws Exception {
098:
099: // start jetty server
100: int jettyPort = 8982;
101: WebContainer servletEngine = new WebContainer(jettyPort,
102: new CompareParametersServlet(), "/path");
103: servletEngine.start();
104:
105: // start xSocket
106: Server server = new HttpServer(0,
107: new CompareParametersHandler());
108: ConnectionUtils.start(server);
109:
110: // compare calls
111: callAndCompare(jettyPort, server.getLocalPort(),
112: "/path/test/tee?aa=33&bb=44&aa=55");
113:
114: // shutdown server
115: servletEngine.stop();
116: server.close();
117: }
118:
119: private void callAndCompare(int jettyPort, int xSocketPort,
120: String path) throws IOException {
121:
122: HttpClient httpClient = new HttpClient();
123:
124: GetMethod jettyMethod = new GetMethod("http://localhost:"
125: + jettyPort + path);
126: int jettyStatusCode = httpClient.executeMethod(jettyMethod);
127: String jettyResponse = jettyMethod.getResponseBodyAsString()
128: .trim();
129: jettyMethod.releaseConnection();
130:
131: GetMethod xSocketMethod = new GetMethod("http://localhost:"
132: + xSocketPort + path);
133: int xSocketStatusCode = httpClient.executeMethod(xSocketMethod);
134: String xSocketResponse = xSocketMethod
135: .getResponseBodyAsString().trim();
136: xSocketMethod.releaseConnection();
137:
138: Assert.assertEquals(jettyStatusCode, xSocketStatusCode);
139: Assert.assertEquals(jettyResponse, xSocketResponse);
140: }
141:
142: private static final class CompareParametersHandler implements
143: IHttpRequestHandler {
144:
145: public void onRequest(HttpRequest request,
146: IHttpResponseContext httpServerEndpoint)
147: throws IOException {
148:
149: // send response
150: HttpResponseHeader responseHeader = new HttpResponseHeader(
151: 200, "text/plain");
152:
153: BodyDataSink bodyDataSink = httpServerEndpoint
154: .send(responseHeader);
155: bodyDataSink.write("requestUri=" + request.getRequestURI()
156: + "\r\n");
157: bodyDataSink.write("queryString="
158: + request.getQueryString() + "\r\n");
159:
160: Map<String, String> params = new HashMap<String, String>();
161: params.putAll(request.getParameterMap());
162:
163: for (String key : sort(params.keySet())) {
164: String value = request.getParameter(key);
165: bodyDataSink.write("[param] " + key + "=" + value
166: + "\r\n");
167: }
168:
169: bodyDataSink.close();
170: }
171: }
172:
173: private static List<String> sort(Set<String> s) {
174: List<String> l = new ArrayList<String>();
175: l.addAll(s);
176: Collections.sort(l);
177: return l;
178: }
179:
180: private static final class CompareParametersServlet extends
181: HttpServlet {
182:
183: @Override
184: protected void doGet(HttpServletRequest req,
185: HttpServletResponse resp) throws ServletException,
186: IOException {
187: resp.setContentType("text/plain");
188: PrintWriter writer = resp.getWriter();
189: writer.write("requestUri=" + req.getRequestURI() + "\r\n");
190: writer
191: .write("queryString=" + req.getQueryString()
192: + "\r\n");
193:
194: Map<String, String> params = new HashMap<String, String>();
195: params.putAll(req.getParameterMap());
196:
197: for (String key : sort(params.keySet())) {
198: String value = req.getParameter(key);
199: String txt = "[param] " + key + "=" + value + "\r\n";
200: writer.write(txt);
201: }
202:
203: writer.close();
204: }
205: }
206: }
|