001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cluster.web.jk.test;
023:
024: import java.io.IOException;
025: import java.net.HttpURLConnection;
026:
027: import junit.framework.Test;
028:
029: import org.apache.commons.httpclient.Header;
030: import org.apache.commons.httpclient.HttpClient;
031: import org.apache.commons.httpclient.methods.GetMethod;
032: import org.jboss.test.JBossClusteredTestCase;
033: import org.jboss.test.cluster.test.BaseTest;
034:
035: public class JvmRouteURLRewritingTestCase extends BaseTest {
036:
037: public JvmRouteURLRewritingTestCase(String name) {
038: super (name);
039: }
040:
041: public static Test suite() throws Exception {
042: Test t1 = JBossClusteredTestCase.getDeploySetup(
043: JvmRouteURLRewritingTestCase.class, "http-jk.war");
044: return t1;
045: }
046:
047: public void testJkFailoverWithURLRewriting() throws Exception {
048: getLog().debug("Enter testJkFailoverWithURLRewriting()");
049:
050: // Create an instance of HttpClient.
051: HttpClient client = new HttpClient();
052:
053: String sessId0 = makeGetWithRequestCount(client, baseURL0_,
054: null, "1");
055:
056: // Fail over to node1
057: String sessId1 = makeGetWithRequestCount(client, baseURL1_,
058: sessId0, "2");
059:
060: // Fail back over to node0
061: makeGetWithRequestCount(client, baseURL0_, sessId1, "3");
062: }
063:
064: private String makeGetWithRequestCount(HttpClient client,
065: String urlBase, String sessionId, String expectedCount) {
066: String url = urlBase + "/http-jk/accessSession.jsp";
067: if (sessionId != null) {
068: url = url + ";jsessionid=" + sessionId;
069: }
070:
071: GetMethod method = new GetMethod(url);
072: int responseCode = 0;
073: try {
074: responseCode = client.executeMethod(method);
075: } catch (IOException e) {
076: e.printStackTrace();
077: fail("HttpClient executeMethod fails." + e.toString());
078: }
079: assertTrue("Get OK with url: " + url + " responseCode: "
080: + responseCode,
081: responseCode == HttpURLConnection.HTTP_OK);
082:
083: // Validate that the request count is as expected,
084: // proving that state replication happened
085: Header hdr = method.getResponseHeader("X-TestRequestCount");
086: assertNotNull("Got the X-TestRequestCount header", hdr);
087: assertEquals("X-TestRequestCount header is correct",
088: expectedCount, hdr.getValue());
089:
090: // Find out the session id
091: hdr = method.getResponseHeader("X-TestJSessionID");
092: assertNotNull("Got the X-TestJSessionID header", hdr);
093: String id = hdr.getValue();
094:
095: if (sessionId != null) {
096: // Check the real session id is correct
097: assertEquals("Real session id is correct",
098: stripJvmRoute(sessionId), stripJvmRoute(id));
099: // Check the session id has changed
100: assertFalse("Session id has changed", sessionId.equals(id));
101: }
102:
103: // Read the response body and confirm the URL was properly encoded.
104: byte[] responseBody = null;
105: try {
106: // Read the response body.
107: responseBody = method.getResponseBody();
108: } catch (IOException e) {
109: e.printStackTrace();
110: fail("HttpClient getResponseBody fails." + e.toString());
111: }
112: String body = new String(responseBody);
113: String expectedURL = "accessSession.jsp;jsessionid=" + id;
114: assertTrue("URL encoded properly: " + body, body
115: .indexOf(expectedURL) > -1);
116:
117: return id;
118: }
119:
120: }
|