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.util.web;
023:
024: import java.net.URL;
025: import java.net.HttpURLConnection;
026: import java.io.IOException;
027: import org.apache.commons.httpclient.HttpClient;
028: import org.apache.commons.httpclient.UsernamePasswordCredentials;
029: import org.apache.commons.httpclient.HttpMethodBase;
030: import org.apache.commons.httpclient.Header;
031: import org.apache.commons.httpclient.methods.GetMethod;
032: import org.apache.commons.httpclient.methods.PostMethod;
033: import org.apache.commons.httpclient.methods.HeadMethod;
034: import org.apache.commons.httpclient.methods.OptionsMethod;
035: import org.apache.commons.httpclient.methods.PutMethod;
036: import org.apache.commons.httpclient.methods.DeleteMethod;
037: import org.apache.commons.httpclient.methods.TraceMethod;
038: import org.jboss.logging.Logger;
039:
040: /** Utilities for client http requests
041: *
042: * @author Scott.Stark@jboss.org
043: * @version $Revision: 57211 $
044: */
045: public class HttpUtils {
046: private static Logger log = Logger.getLogger(HttpUtils.class);
047: private static String baseURL = "http://jduke:theduke@"
048: + System.getProperty("jbosstest.server.host", "localhost")
049: + ":" + Integer.getInteger("web.port", 8080) + "/";
050: private static String baseURLNoAuth = "http://"
051: + System.getProperty("jbosstest.server.host", "localhost")
052: + ":" + Integer.getInteger("web.port", 8080) + "/";
053:
054: public static final int GET = 1;
055: public static final int POST = 2;
056: public static final int HEAD = 3;
057: public static final int OPTIONS = 4;
058: public static final int PUT = 5;
059: public static final int DELETE = 6;
060: public static final int TRACE = 7;
061:
062: public static String getBaseURL() {
063: return baseURL;
064: }
065:
066: public static String getBaseURL(String username, String password) {
067: String url = "http://"
068: + username
069: + ":"
070: + password
071: + "@"
072: + System.getProperty("jbosstest.server.host",
073: "localhost") + ":"
074: + Integer.getInteger("web.port", 8080) + "/";
075: return url;
076: }
077:
078: public static String getBaseURLNoAuth() {
079: return baseURLNoAuth;
080: }
081:
082: /** Perform a get on the indicated URL and assert an HTTP_OK response code
083: *
084: * @param url
085: * @return The commons HttpClient used to perform the get
086: * @throws Exception on any failure
087: */
088: public static HttpMethodBase accessURL(URL url) throws Exception {
089: return accessURL(url, "JBossTest Servlets",
090: HttpURLConnection.HTTP_OK);
091: }
092:
093: /** Perform a get on the indicated URL and assert that the response code
094: * matches the expectedHttpCode argument.
095: *
096: * @param url
097: * @param expectedHttpCode the http response code expected
098: * @return The commons HttpClient used to perform the get
099: * @throws Exception on any failure
100: */
101: public static HttpMethodBase accessURL(URL url, String realm,
102: int expectedHttpCode) throws Exception {
103: return accessURL(url, realm, expectedHttpCode, null);
104: }
105:
106: public static HttpMethodBase accessURL(URL url, String realm,
107: int expectedHttpCode, int type) throws Exception {
108: return accessURL(url, realm, expectedHttpCode, null, type);
109: }
110:
111: public static HttpMethodBase accessURL(URL url, String realm,
112: int expectedHttpCode, Header[] hdrs) throws Exception {
113: return accessURL(url, realm, expectedHttpCode, hdrs, GET);
114: }
115:
116: public static HttpMethodBase accessURL(URL url, String realm,
117: int expectedHttpCode, Header[] hdrs, int type)
118: throws Exception {
119: HttpClient httpConn = new HttpClient();
120: HttpMethodBase request = createMethod(url, type);
121: int hdrCount = hdrs != null ? hdrs.length : 0;
122: for (int n = 0; n < hdrCount; n++)
123: request.addRequestHeader(hdrs[n]);
124: try {
125: log.debug("Connecting to: " + url);
126: String userInfo = url.getUserInfo();
127: if (userInfo != null) {
128: UsernamePasswordCredentials auth = new UsernamePasswordCredentials(
129: userInfo);
130: httpConn.getState().setCredentials(realm,
131: url.getHost(), auth);
132: }
133: log.debug("RequestURI: " + request.getURI());
134: int responseCode = httpConn.executeMethod(request);
135: String response = request.getStatusText();
136: log.debug("responseCode=" + responseCode + ", response="
137: + response);
138: String content = request.getResponseBodyAsString();
139: log.debug(content);
140: // Validate that we are seeing the requested response code
141: if (responseCode != expectedHttpCode) {
142: throw new IOException("Expected reply code:"
143: + expectedHttpCode + ", actual=" + responseCode);
144: }
145: } catch (IOException e) {
146: throw e;
147: }
148: return request;
149: }
150:
151: public static HttpMethodBase createMethod(URL url, int type) {
152: HttpMethodBase request = null;
153: switch (type) {
154: case GET:
155: request = new GetMethod(url.toString());
156: break;
157: case POST:
158: request = new PostMethod(url.toString());
159: break;
160: case HEAD:
161: request = new HeadMethod(url.toString());
162: break;
163: case OPTIONS:
164: request = new OptionsMethod(url.toString());
165: break;
166: case PUT:
167: request = new PutMethod(url.toString());
168: break;
169: case DELETE:
170: request = new DeleteMethod(url.toString());
171: break;
172: case TRACE:
173: request = new TraceMethod(url.toString());
174: break;
175: }
176: return request;
177: }
178: }
|