User: starksm
Date: 01/05/05 21:15:26
Modified: src/main/org/jboss/test/web/test TestWebIntegration.java
Added: src/main/org/jboss/test/web/test HttpClient.java
Log:
Switch to simple HttpClient since the jave HttpUrlConnection http: client
does not allow reliable access to error information.
Revision Changes Path
1.2 +36 -36
jbosstest/src/main/org/jboss/test/web/test/TestWebIntegration.java
Index: TestWebIntegration.java
===================================================================
RCS file:
/cvsroot/jboss/jbosstest/src/main/org/jboss/test/web/test/TestWebIntegration.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestWebIntegration.java 2001/05/05 20:59:35 1.1
+++ TestWebIntegration.java 2001/05/06 04:15:26 1.2
@@ -1,8 +1,5 @@
package org.jboss.test.web.test;
-import java.io.BufferedReader;
-import java.io.InputStream;
-import java.io.InputStreamReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
@@ -19,8 +16,11 @@
is best to connect the webserver using a browser to look for additional error
info.
+The secure access tests require a user named 'jduke' with a password of 'theduke'
+with a role of 'AuthorizedUser' in the servlet container.
+
@author [EMAIL PROTECTED]
-@versioin $Revision: 1.1 $
+@version $Revision: 1.2 $
*/
public class TestWebIntegration extends TestCase
{
@@ -44,15 +44,15 @@
// Test for the existence of a web server
Integer port = Integer.getInteger("web.port", 8080);
- baseURL = "http://localhost:" + port;
- URL url = new URL(baseURL);
+ baseURL = "http://jduke:theduke@localhost:" + port + '/';
+ URL url = new URL(baseURL+"index.html");
try
{
- HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
+ HttpClient httpConn = new HttpClient(url);
int responseCode = httpConn.getResponseCode();
if( responseCode != HttpURLConnection.HTTP_OK )
{
- System.out.println(baseURL+" did not result in HTTP OK, skipping
tests");
+ System.out.println(baseURL+" result="+responseCode+" in HTTP OK,
skipping tests");
}
else
{
@@ -74,7 +74,7 @@
if( webServerAvailable == false )
return;
- URL url = new URL(baseURL+"/jbosstest/ENCServlet");
+ URL url = new URL(baseURL+"jbosstest/ENCServlet");
accessURL(url);
}
/** Access the http://localhost/jbosstest/EJBServlet
@@ -84,7 +84,7 @@
if( webServerAvailable == false )
return;
- URL url = new URL(baseURL+"/jbosstest/EJBServlet");
+ URL url = new URL(baseURL+"jbosstest/EJBServlet");
accessURL(url);
}
/** Access the http://localhost/jbosstest/snoop.jsp
@@ -93,8 +93,28 @@
{
if( webServerAvailable == false )
return;
+
+ URL url = new URL(baseURL+"jbosstest/snoop.jsp");
+ accessURL(url);
+ }
+ /** Access the http://localhost/jbosstest/restricted/SecureServlet
+ */
+ public void testSecureServlet() throws Exception
+ {
+ if( webServerAvailable == false )
+ return;
+
+ URL url = new URL(baseURL+"jbosstest/restricted/SecureServlet");
+ accessURL(url);
+ }
+ /** Access the http://localhost/jbosstest/restricted/SecureEJBAccess
+ */
+ public void testSecureEJBAccess() throws Exception
+ {
+ if( webServerAvailable == false )
+ return;
- URL url = new URL(baseURL+"/jbosstest/snoop.jsp");
+ URL url = new URL(baseURL+"jbosstest/restricted/SecureEJBAccess");
accessURL(url);
}
@@ -103,34 +123,14 @@
try
{
System.out.println("Connecting to: "+url);
- HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
- int responseCode = -1;
- String response = null;
- try
- {
- httpConn.connect();
- responseCode = httpConn.getResponseCode();
- response = httpConn.getResponseMessage();
- }
- catch(Exception e)
- {
- System.out.println("Failed to connect to: "+url);
- response = e.toString();
- }
+ HttpClient httpConn = new HttpClient(url);
+ int responseCode = httpConn.getResponseCode();
+ String response = httpConn.getResponseMessage();
System.out.println("responseCode="+responseCode+", response="+response);
if( responseCode != HttpURLConnection.HTTP_OK )
{
- InputStream is = httpConn.getErrorStream();
- if( is != null )
- {
- BufferedReader reader = new BufferedReader(new
InputStreamReader(is));
- String line;
- while( (line = reader.readLine()) != null )
- {
- System.out.println(line);
- }
- reader.close();
- }
+ StringBuffer content = httpConn.getContent();
+ System.out.println(content);
fail("Access to: "+url+" failed with responseCode="+responseCode);
}
}
1.1 jbosstest/src/main/org/jboss/test/web/test/HttpClient.java
Index: HttpClient.java
===================================================================
package org.jboss.test.web.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URL;
import java.util.HashMap;
// Unpublished Base64 encoder class
import sun.misc.BASE64Encoder;
/** A simple socket based client for accesing http content.
@author [EMAIL PROTECTED]
@version $Revision: 1.1 $
*/
public class HttpClient
{
URL reqURL;
int responseCode = -1;
String response;
StringBuffer content;
HashMap headers = new HashMap();
/** Creates new HttpClient */
public HttpClient(String url) throws IOException
{
this.reqURL = new URL(url);
}
public HttpClient(URL url) throws IOException
{
this.reqURL = url;
}
public int getResponseCode() throws IOException
{
if( responseCode < 0 )
transfer();
return responseCode;
}
public String getResponseMessage() throws IOException
{
if( responseCode < 0 )
transfer();
return response;
}
public StringBuffer getContent() throws IOException
{
if( responseCode < 0 )
transfer();
return content;
}
public int transfer() throws IOException
{
String host = reqURL.getHost();
int port = reqURL.getPort();
if( port < 0 )
port = 80;
Socket conn = new Socket(host, port);
OutputStream os = conn.getOutputStream();
writeHeader(os);
InputStream is = conn.getInputStream();
readHeader(is);
os.close();
is.close();
return responseCode;
}
private void writeHeader(OutputStream os)
{
PrintWriter pw = new PrintWriter(os);
pw.write("GET " + reqURL.getPath() + " HTTP/1.1\r\n");
pw.write("Accept: */*\r\n");
pw.write("User-Agent: JBossTest WebClient\r\n");
String userInfo = reqURL.getUserInfo();
if( userInfo != null )
{
BASE64Encoder encoder = new BASE64Encoder();
byte[] userInfoBytes = userInfo.getBytes();
String authInfo = "Basic " + encoder.encode(userInfoBytes);
pw.write("Authorization: " + authInfo+"\r\n");
}
pw.write("\r\n");
pw.flush();
}
private void readHeader(InputStream is) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while( (line = reader.readLine()) != null )
{
// Check for "\r\n\r\n" end of headers marker
if( line.length() == 0 )
break;
if( responseCode < 0 )
{
// Parse the intial line for the "HTTP/1.x 200 OK" response
response = line.substring(13);
line = line.substring(9, 12);
responseCode = Integer.parseInt(line);
}
else
{
int separator = line.indexOf(':');
if( separator > 0 )
{
String key = line.substring(0, separator);
String value = line.substring(separator+1);
headers.put(key, value);
}
else
{
System.out.println("Invalid header '"+line+"'");
}
}
}
content = new StringBuffer();
while( (line = reader.readLine()) != null )
{
if( line.length() == 0 )
break;
content.append(line);
content.append('\n');
}
}
public static void main(String[] args) throws IOException
{
HttpClient hc = new
HttpClient("http://jduke:theduke@localhost:8080/jbosstest/restricted/SecureServlet");
System.out.println(hc.transfer());
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development