[Httpunit-commit] SF.net SVN: httpunit:[1036] trunk/httpunit
Brought to you by:
russgold
|
From: <wol...@us...> - 2009-08-18 16:08:26
|
Revision: 1036
http://httpunit.svn.sourceforge.net/httpunit/?rev=1036&view=rev
Author: wolfgang_fahl
Date: 2009-08-18 16:08:16 +0000 (Tue, 18 Aug 2009)
Log Message:
-----------
implemented CR [ 914314 ] to add getResponse to HttpException
improved handling of undefined resources in PseudoServer
getIncludedScript now handles 404 errors according to the HttpUnit Options settings for script error and exception handling (thanks to Dan Lipofsky for pointing this out)
Modified Paths:
--------------
trunk/httpunit/src/com/meterware/httpunit/HttpException.java
trunk/httpunit/src/com/meterware/httpunit/HttpUnitOptions.java
trunk/httpunit/src/com/meterware/httpunit/HttpWebResponse.java
trunk/httpunit/src/com/meterware/httpunit/ParsedHTML.java
trunk/httpunit/src/com/meterware/httpunit/WebClient.java
trunk/httpunit/src/com/meterware/httpunit/WebResponse.java
trunk/httpunit/src/com/meterware/httpunit/dom/DomBasedScriptingEngineFactory.java
trunk/httpunit/src/com/meterware/httpunit/javascript/JavaScriptEngineFactory.java
trunk/httpunit/src/com/meterware/httpunit/javascript/ScriptingEngineImpl.java
trunk/httpunit/src/com/meterware/httpunit/scripting/ScriptingEngineFactory.java
trunk/httpunit/src/com/meterware/pseudoserver/PseudoServer.java
trunk/httpunit/test/com/meterware/httpunit/WebClientTest.java
trunk/httpunit/test/com/meterware/httpunit/javascript/ScriptingTest.java
Modified: trunk/httpunit/src/com/meterware/httpunit/HttpException.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/HttpException.java 2009-08-18 11:31:16 UTC (rev 1035)
+++ trunk/httpunit/src/com/meterware/httpunit/HttpException.java 2009-08-18 16:08:16 UTC (rev 1036)
@@ -128,5 +128,24 @@
private Throwable _cause;
+
+ // see feature request [ 914314 ] Add HttpException.getResponse for better reporting
+ private WebResponse response;
+
+ /**
+ * return the WebResponse associated with this Exception (if any)
+ * @return
+ */
+ public WebResponse getResponse() {
+ return response;
+ }
+
+ /**
+ * add the given response to this exception
+ * @param response
+ */
+ public void setResponse(WebResponse response) {
+ this.response=response;
+ }
}
\ No newline at end of file
Modified: trunk/httpunit/src/com/meterware/httpunit/HttpUnitOptions.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/HttpUnitOptions.java 2009-08-18 11:31:16 UTC (rev 1035)
+++ trunk/httpunit/src/com/meterware/httpunit/HttpUnitOptions.java 2009-08-18 16:08:16 UTC (rev 1036)
@@ -505,6 +505,9 @@
public void clearErrorMessages() {}
public ScriptingHandler createHandler( HTMLElement element ) { return ScriptableDelegate.NULL_SCRIPT_ENGINE; }
public ScriptingHandler createHandler( WebResponse response ) { return ScriptableDelegate.NULL_SCRIPT_ENGINE; }
+ public void handleScriptException(Exception e, String badScript) {
+ // happily ignore and exception
+ }
};
Modified: trunk/httpunit/src/com/meterware/httpunit/HttpWebResponse.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/HttpWebResponse.java 2009-08-18 11:31:16 UTC (rev 1035)
+++ trunk/httpunit/src/com/meterware/httpunit/HttpWebResponse.java 2009-08-18 16:08:16 UTC (rev 1036)
@@ -2,7 +2,7 @@
/********************************************************************************************************************
* $Id$
*
-* Copyright (c) 2000-2004, Russell Gold
+* Copyright (c) 2000-2009, Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
@@ -58,8 +58,12 @@
/** make sure that any IO exception for HTML received page happens here, not later. **/
if (_responseCode < HttpURLConnection.HTTP_BAD_REQUEST || !throwExceptionOnError) {
- defineRawInputStream( new BufferedInputStream( getInputStream( connection ) ) );
- if (getContentType().startsWith( "text" )) loadResponseText();
+ InputStream inputStream = getInputStream( connection );
+ defineRawInputStream( new BufferedInputStream( inputStream ) );
+ String contentType = getContentType();
+ if (contentType.startsWith( "text" )) {
+ loadResponseText();
+ }
}
}
@@ -81,7 +85,7 @@
InputStream result=null;
// check whether there is an error stream
if (isResponseOnErrorStream( connection )) {
- result=((HttpURLConnection) connection).getErrorStream();
+ result=((HttpURLConnection) connection).getErrorStream();
} else {
// if there is no error stream it depends on the response code
try {
@@ -190,30 +194,40 @@
private int _responseCode = HttpURLConnection.HTTP_OK;
- private String _responseMessage = "OK";
+ private String _responseMessage = "OK";
+
+ /**
+ * set the responseCode to the given code and message
+ * @param code
+ * @param message
+ */
+ private void setResponseCode(int code, String message) {
+ _responseCode = code;
+ _responseMessage=message;
+ }
private Hashtable _headers = new Hashtable();
-
-
+ /**
+ * read the response Header for the given connection and set the response code and
+ * message accordingly
+ * @param connection
+ * @throws IOException
+ */
private void readResponseHeader( HttpURLConnection connection ) throws IOException {
if (!needStatusWorkaround()) {
- _responseCode = connection.getResponseCode();
- _responseMessage = connection.getResponseMessage();
+ setResponseCode(connection.getResponseCode(),connection.getResponseMessage());
} else {
if (connection.getHeaderField(0) == null) throw new UnknownHostException( connection.getURL().toExternalForm() );
StringTokenizer st = new StringTokenizer( connection.getHeaderField(0) );
st.nextToken();
if (!st.hasMoreTokens()) {
- _responseCode = HttpURLConnection.HTTP_OK;
- _responseMessage = "OK";
+ setResponseCode(HttpURLConnection.HTTP_OK,"OK");
} else try {
- _responseCode = Integer.parseInt( st.nextToken() );
- _responseMessage = getRemainingTokens( st );
+ setResponseCode(Integer.parseInt( st.nextToken()) ,getRemainingTokens( st ));
} catch (NumberFormatException e) {
- _responseCode = HttpURLConnection.HTTP_INTERNAL_ERROR;
- _responseMessage = "Cannot parse response header";
+ setResponseCode (HttpURLConnection.HTTP_INTERNAL_ERROR,"Cannot parse response header");
}
}
}
@@ -238,8 +252,7 @@
if (connection instanceof HttpURLConnection) {
readResponseHeader( (HttpURLConnection) connection );
} else {
- _responseCode = HttpURLConnection.HTTP_OK;
- _responseMessage = "OK";
+ setResponseCode (HttpURLConnection.HTTP_OK, "OK");
if (connection.getContentType().startsWith( "text" )) {
setContentTypeHeader( connection.getContentType() + "; charset=" + FILE_ENCODING );
}
Modified: trunk/httpunit/src/com/meterware/httpunit/ParsedHTML.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/ParsedHTML.java 2009-08-18 11:31:16 UTC (rev 1035)
+++ trunk/httpunit/src/com/meterware/httpunit/ParsedHTML.java 2009-08-18 16:08:16 UTC (rev 1036)
@@ -25,6 +25,7 @@
import org.w3c.dom.Document;
import org.w3c.dom.html.*;
+import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.io.IOException;
@@ -380,7 +381,7 @@
} else {
try {
return getIncludedScript( scriptLocation );
- } catch (IOException e) {
+ } catch (Exception e) {
throw new RuntimeException( "Error loading included script: " + e );
}
}
@@ -391,13 +392,30 @@
* Returns the contents of an included script, given its src attribute.
* @param srcAttribute the location of the script.
* @return the contents of the script.
- * @throws java.io.IOException if there is a problem retrieving the script
+ * @throws IOException if there is a problem retrieving the script
*/
String getIncludedScript( String srcAttribute ) throws IOException {
WebRequest req = new GetMethodWebRequest( getBaseURL(), srcAttribute );
WebWindow window = getResponse().getWindow();
- if (window == null) throw new IllegalStateException( "Unable to retrieve script included by this response, since it was loaded by getResource(). Use getResponse() instead.");
- return window.getResource( req ).getText();
+ if (window == null)
+ throw new IllegalStateException( "Unable to retrieve script included by this response, since it was loaded by getResource(). Use getResponse() instead.");
+ WebResponse response = window.getResource( req );
+ // check whether the Source is available
+ int code = response.getResponseCode();
+ // if everything is o.k.
+ if (code<=HttpURLConnection.HTTP_BAD_REQUEST) {
+ // return the text
+ String result =response.getText();
+ return result;
+ } else {
+ // in this case the text would be an error message
+ // we do not return it but set the
+ ScriptException se=new ScriptException(response.getText());
+ String badScript="?";
+ // let scripting engine decide what to do with this exception (throw it or remember it ...)
+ HttpUnitOptions.getScriptingEngine().handleScriptException(se, badScript);
+ return "";
+ }
}
Modified: trunk/httpunit/src/com/meterware/httpunit/WebClient.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/WebClient.java 2009-08-18 11:31:16 UTC (rev 1035)
+++ trunk/httpunit/src/com/meterware/httpunit/WebClient.java 2009-08-18 16:08:16 UTC (rev 1036)
@@ -623,17 +623,25 @@
* @parm response - the response to validate
**/
private void validateHeaders( WebResponse response ) throws HttpException {
- if (!getExceptionsThrownOnErrorStatus())
- return;
- // see feature request [ 914314 ] Add HttpException.getResponse for better reporting
- // for possible improvements here
+ HttpException exception=null;
if (response.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) {
- throw new HttpInternalErrorException( response.getURL() );
+ exception=new HttpInternalErrorException( response.getURL() );
} else if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
- throw new HttpNotFoundException( response.getResponseMessage(), response.getURL() );
+ exception= new HttpNotFoundException( response.getResponseMessage(), response.getURL() );
} else if (response.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) {
- throw new HttpException( response.getResponseCode(), response.getResponseMessage(), response.getURL() );
+ exception= new HttpException( response.getResponseCode(), response.getResponseMessage(), response.getURL() );
}
+ // is there an exception?
+ if (exception!=null) {
+ // see feature request [ 914314 ] Add HttpException.getResponse for better reporting
+ exception.setResponse(response);
+ // shall we ignore errors?
+ if (!getExceptionsThrownOnErrorStatus()) {
+ return;
+ } else {
+ throw exception;
+ }
+ }
}
Modified: trunk/httpunit/src/com/meterware/httpunit/WebResponse.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/WebResponse.java 2009-08-18 11:31:16 UTC (rev 1035)
+++ trunk/httpunit/src/com/meterware/httpunit/WebResponse.java 2009-08-18 16:08:16 UTC (rev 1036)
@@ -282,16 +282,17 @@
* which may be used to represent internal state of this object.
**/
public String getText() throws IOException {
- if (_responseText == null) loadResponseText();
+ if (_responseText == null)
+ loadResponseText();
return _responseText;
}
-
/**
* Returns a buffered input stream for reading the contents of this reply.
**/
public InputStream getInputStream() throws IOException {
- if (_inputStream == null) _inputStream = new ByteArrayInputStream( getText().getBytes() );
+ if (_inputStream == null)
+ _inputStream = new ByteArrayInputStream( getText().getBytes() );
return _inputStream;
}
@@ -1056,7 +1057,7 @@
private String _responseText;
- private InputStream _inputStream;
+ private InputStream _inputStream;
private final URL _pageURL;
Modified: trunk/httpunit/src/com/meterware/httpunit/dom/DomBasedScriptingEngineFactory.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/dom/DomBasedScriptingEngineFactory.java 2009-08-18 11:31:16 UTC (rev 1035)
+++ trunk/httpunit/src/com/meterware/httpunit/dom/DomBasedScriptingEngineFactory.java 2009-08-18 16:08:16 UTC (rev 1036)
@@ -26,6 +26,7 @@
import com.meterware.httpunit.scripting.ScriptingHandler;
import com.meterware.httpunit.scripting.ScriptingEngine;
import com.meterware.httpunit.HttpUnitUtils;
+import com.meterware.httpunit.ScriptException;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.HTMLElement;
@@ -38,6 +39,7 @@
import org.xml.sax.SAXException;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.EcmaError;
+import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.Scriptable;
@@ -143,4 +145,14 @@
public ScriptingHandler createHandler( WebResponse response ) {
return response.createDomScriptingHandler();
}
+
+
+ /**
+ * handle Exceptions
+ * @param e - the exception to handle
+ * @param badScript - the script that caused the problem
+ */
+ public void handleScriptException( Exception e, String badScript ) {
+ ScriptingEngineImpl.handleScriptException(e, badScript);
+ }
}
Modified: trunk/httpunit/src/com/meterware/httpunit/javascript/JavaScriptEngineFactory.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/javascript/JavaScriptEngineFactory.java 2009-08-18 11:31:16 UTC (rev 1035)
+++ trunk/httpunit/src/com/meterware/httpunit/javascript/JavaScriptEngineFactory.java 2009-08-18 16:08:16 UTC (rev 1036)
@@ -82,6 +82,13 @@
public String[] getErrorMessages() {
return ScriptingEngineImpl.getErrorMessages();
}
+
+ /**
+ * delegate the handling for Script exceptions
+ */
+ public void handleScriptException(Exception e, String badScript) {
+ ScriptingEngineImpl.handleScriptException(e, badScript);
+ }
public void clearErrorMessages() {
@@ -99,4 +106,6 @@
public ScriptingHandler createHandler( WebResponse response ) {
return response.createJavascriptScriptingHandler();
}
+
+
}
Modified: trunk/httpunit/src/com/meterware/httpunit/javascript/ScriptingEngineImpl.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/javascript/ScriptingEngineImpl.java 2009-08-18 11:31:16 UTC (rev 1035)
+++ trunk/httpunit/src/com/meterware/httpunit/javascript/ScriptingEngineImpl.java 2009-08-18 16:08:16 UTC (rev 1036)
@@ -44,6 +44,10 @@
}
+ /**
+ * access to the list of error Messages that were collected
+ * @return the array with error Messages
+ */
static public String[] getErrorMessages() {
return (String[]) _errorMessages.toArray( new String[ _errorMessages.size() ] );
}
@@ -56,11 +60,15 @@
*/
static public void handleScriptException( Exception e, String badScript ) {
final String errorMessage = badScript + " failed: " + e;
- if (!(e instanceof EcmaError) && !(e instanceof EvaluatorException)) {
+
+ if (!(e instanceof EcmaError) && !(e instanceof EvaluatorException) && !(e instanceof ScriptException)) {
HttpUnitUtils.handleException(e);
throw new RuntimeException( errorMessage );
} else if (JavaScript.isThrowExceptionsOnError()) {
HttpUnitUtils.handleException(e);
+ if (e instanceof ScriptException)
+ throw (ScriptException)e;
+ else
throw new ScriptException( errorMessage );
} else {
_errorMessages.add( errorMessage );
Modified: trunk/httpunit/src/com/meterware/httpunit/scripting/ScriptingEngineFactory.java
===================================================================
--- trunk/httpunit/src/com/meterware/httpunit/scripting/ScriptingEngineFactory.java 2009-08-18 11:31:16 UTC (rev 1035)
+++ trunk/httpunit/src/com/meterware/httpunit/scripting/ScriptingEngineFactory.java 2009-08-18 16:08:16 UTC (rev 1036)
@@ -64,6 +64,13 @@
* Clears the accumulated script error messages.
*/
public void clearErrorMessages();
+
+ /**
+ * handle Exceptions
+ * @param e - the exception to handle
+ * @param badScript - the script that caused the problem
+ */
+ public void handleScriptException( Exception e, String badScript );
ScriptingHandler createHandler( HTMLElement elementBase );
Modified: trunk/httpunit/src/com/meterware/pseudoserver/PseudoServer.java
===================================================================
--- trunk/httpunit/src/com/meterware/pseudoserver/PseudoServer.java 2009-08-18 11:31:16 UTC (rev 1035)
+++ trunk/httpunit/src/com/meterware/pseudoserver/PseudoServer.java 2009-08-18 16:08:16 UTC (rev 1036)
@@ -100,6 +100,10 @@
}
+ /**
+ * create a PseudoServer with the given socketTimeout
+ * @param socketTimeout - the time out to use
+ */
public PseudoServer( int socketTimeout ) {
_socketTimeout = socketTimeout;
_serverNum = ++_numServers;
@@ -197,9 +201,16 @@
/**
* Defines a resource which will result in an error message.
- **/
- public void setErrorResource( String name, int errorCode, String errorMessage ) {
- _resources.put( asResourceName( name ), new WebResource( errorMessage, errorCode ) );
+ * return it for further use
+ * @param name
+ * @param errorCode
+ * @param errorMessage
+ * @return the resource
+ */
+ public WebResource setErrorResource( String name, int errorCode, String errorMessage ) {
+ WebResource resource = new WebResource( errorMessage, errorCode );
+ _resources.put( asResourceName( name ), resource );
+ return resource;
}
@@ -314,7 +325,9 @@
boolean keepAlive = respondToRequest( request, outputStream );
if (!keepAlive) break;
while (_active && 0 == inputStream.available()) {
- try { Thread.sleep( INPUT_POLL_INTERVAL ); } catch (InterruptedException e) {}
+ try {
+ Thread.sleep( INPUT_POLL_INTERVAL );
+ } catch (InterruptedException e) {}
}
}
} catch (IOException e) {
@@ -329,6 +342,12 @@
}
+ /**
+ * respond to the given request
+ * @param request - the request
+ * @param response - the response stream
+ * @return
+ */
private boolean respondToRequest( HttpRequest request, HttpResponseStream response ) {
debug( "Server thread handling request: " + request );
boolean keepAlive = isKeepAlive( request );
@@ -338,25 +357,39 @@
response.setProtocol( getResponseProtocol( request ) );
resource = getResource( request );
if (resource == null) {
- response.setResponse( HttpURLConnection.HTTP_NOT_FOUND, "unable to find " + request.getURI() );
+ // what resource could not be find?
+ String uri=request.getURI();
+ // 404 - Not Found error code
+ int errorCode=HttpURLConnection.HTTP_NOT_FOUND;
+ // typical 404 error Message
+ String errorMessage="unable to find " + uri;
+ // make sure there is a resource and
+ // next time we'll take it from the resource Cache
+ resource=setErrorResource(uri, errorCode, errorMessage);
+ // set the errorCode for this response
+ response.setResponse(errorCode , errorMessage );
} else {
- if (resource.closesConnection()) keepAlive = false;
if (resource.getResponseCode() != HttpURLConnection.HTTP_OK) {
response.setResponse( resource.getResponseCode(), "" );
- }
- String[] headers = resource.getHeaders();
- for (int i = 0; i < headers.length; i++) {
- debug( "Server thread sending header: " + headers[i] );
- response.addHeader( headers[i] );
- }
+ }
}
+ if (resource.closesConnection()) keepAlive = false;
+ String[] headers = resource.getHeaders();
+ for (int i = 0; i < headers.length; i++) {
+ debug( "Server thread sending header: " + headers[i] );
+ response.addHeader( headers[i] );
+ }
} catch (UnknownMethodException e) {
response.setResponse( HttpURLConnection.HTTP_BAD_METHOD, "unsupported method: " + e.getMethod() );
} catch (Throwable t) {
t.printStackTrace();
response.setResponse( HttpURLConnection.HTTP_INTERNAL_ERROR, t.toString() );
}
- try { response.write( resource ); } catch (IOException e) { System.out.println( "*** Failed to send reply: " + e ); }
+ try {
+ response.write( resource );
+ } catch (IOException e) {
+ System.out.println( "*** Failed to send reply: " + e );
+ }
return keepAlive;
}
@@ -470,6 +503,11 @@
}
+ /**
+ * set the response to the given response Code
+ * @param responseCode
+ * @param responseText
+ */
void setResponse( int responseCode, String responseText ) {
_responseCode = responseCode;
_responseText = responseText;
Modified: trunk/httpunit/test/com/meterware/httpunit/WebClientTest.java
===================================================================
--- trunk/httpunit/test/com/meterware/httpunit/WebClientTest.java 2009-08-18 11:31:16 UTC (rev 1035)
+++ trunk/httpunit/test/com/meterware/httpunit/WebClientTest.java 2009-08-18 16:08:16 UTC (rev 1036)
@@ -72,6 +72,10 @@
}
+ /**
+ * check access to resources that are not defined
+ * @throws Exception
+ */
public void testNotFound() throws Exception {
WebConversation wc = new WebConversation();
WebRequest request = new GetMethodWebRequest( getHostPath() + "/nothing.htm" );
@@ -81,8 +85,50 @@
} catch (HttpNotFoundException e) {
assertEquals( "Response code", HttpURLConnection.HTTP_NOT_FOUND, e.getResponseCode() );
assertEquals( "Response message", "unable to find /nothing.htm", e.getResponseMessage() );
+ assertEquals( "Response text","",e.getResponse().getText());
}
}
+
+ /**
+ * check access to undefined resources
+ * @throws IOException
+ */
+ public void testUndefinedResource() throws IOException {
+ boolean originalState = HttpUnitOptions
+ .getExceptionsThrownOnErrorStatus();
+ // try two cases for throwException true on i==0, false on i==1
+ for (int i = 0; i <2; i++) {
+ boolean throwException = i == 0;
+ HttpUnitOptions.setExceptionsThrownOnErrorStatus(throwException);
+ WebResponse response = null;
+ try {
+ WebConversation wc = new WebConversation();
+ WebRequest request = new GetMethodWebRequest(getHostPath()
+ + "/undefined");
+ response = wc.getResponse(request);
+ if (throwException) {
+ fail("there should have been an exception here");
+ }
+ } catch (HttpNotFoundException hnfe) {
+ assertTrue(throwException);
+ response=hnfe.getResponse();
+ } catch (Exception e) {
+ fail("there should be no exception here");
+ }
+ assertTrue(response != null);
+ assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response
+ .getResponseCode());
+ if (throwException) {
+ assertEquals("with throwException="+throwException,"", response.getText());
+ assertEquals("with throwException="+throwException,"unable to find /undefined",response.getResponseMessage());
+ } else {
+ // FIXME what do we expect here and how do we get it!
+ assertEquals("with throwException="+throwException,"unable to find /undefined", response.getText());
+ assertNull(response.getResponseMessage());
+ }
+ }
+ HttpUnitOptions.setExceptionsThrownOnErrorStatus(originalState);
+ }
public void testNotModifiedResponse() throws Exception {
@@ -770,39 +816,6 @@
}
/**
- * check access to undefined resources
- */
- public void testUndefinedResource() {
- boolean originalState = HttpUnitOptions
- .getExceptionsThrownOnErrorStatus();
- for (int i = 0; i < 1; i++) {
- boolean throwException = i == 0;
- HttpUnitOptions.setExceptionsThrownOnErrorStatus(throwException);
- WebResponse response = null;
- try {
- WebConversation wc = new WebConversation();
- WebRequest request = new GetMethodWebRequest(getHostPath()
- + "/undefined");
- response = wc.getResponse(request);
- if (throwException) {
- fail("there should have been an exception here");
- }
- assertTrue(response != null);
- assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response
- .getResponseCode());
- assertEquals(0, response.getContentLength());
- } catch (Exception e) {
- if (throwException) {
- assertTrue(e instanceof HttpNotFoundException);
- } else {
- fail("there should be no exception here");
- }
- }
- }
- HttpUnitOptions.setExceptionsThrownOnErrorStatus(originalState);
- }
-
- /**
* test for bug report [ 1283878 ] FileNotFoundException using Sun JDK 1.5 on empty error pages
* by Roger Lindsj\xF6
* @throws Exception
Modified: trunk/httpunit/test/com/meterware/httpunit/javascript/ScriptingTest.java
===================================================================
--- trunk/httpunit/test/com/meterware/httpunit/javascript/ScriptingTest.java 2009-08-18 11:31:16 UTC (rev 1035)
+++ trunk/httpunit/test/com/meterware/httpunit/javascript/ScriptingTest.java 2009-08-18 16:08:16 UTC (rev 1036)
@@ -193,8 +193,59 @@
WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" );
response.getLinkWith( "go" ).click();
assertEquals( "Alert message", "Cheese!", wc.popNextAlert() );
- }
+ }
+ /**
+ * test Detection of Javascript files that can not be found
+ * behaviour pointed out by Dan Lipofsky
+ * @throws Exception
+ */
+ public void testBadJavascriptFile() throws Exception {
+ // define xyz.js to create a 404 error
+ // we don't do this - it should be a default behaviour of the Pseudo Server!
+ // defineResource( "xyz.js", "File does not exist: xyz.js", 404);
+ defineResource("OnCommand.html",
+ "<html><head>" +
+ "<script language='JavaScript' src='xyz.js'></script></head>" +
+ "<body>Hello</body></html>" );
+ boolean originalState =
+ HttpUnitOptions.getExceptionsThrownOnErrorStatus();
+ boolean originalScriptState=
+ HttpUnitOptions.getExceptionsThrownOnScriptError();
+ boolean oldDebug= HttpUnitUtils.setEXCEPTION_DEBUG(false);
+
+ // make sure exceptions are thrown
+ HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
+ for (int i=0;i<2;i++) {
+ boolean throwScriptException=i==0;
+ HttpUnitOptions.setExceptionsThrownOnScriptError(throwScriptException);
+ HttpUnitOptions.clearScriptErrorMessages();
+ WebConversation wc = new WebConversation();
+ try {
+ WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" );
+ // WebResponse response = wc.getResponse( getHostPath() + "/xyz.js" );
+ // assertEquals( 404, response.getResponseCode() );
+ if (throwScriptException) {
+ fail("there should have been an exception");
+ } else {
+ String[] errMsgs = HttpUnitOptions.getScriptErrorMessages();
+ assertTrue("There should be an error Message",errMsgs.length==1);
+ String errMsg=errMsgs[0];
+ assertEquals(errMsg,"? failed: com.meterware.httpunit.ScriptException: unable to find /xyz.js");
+ }
+ } catch (ScriptException se) {
+ assertTrue(throwScriptException);
+ } catch (Exception e) {
+ fail("there should be no exception when throwScriptException is "+throwScriptException);
+ }
+ }
+ // Restore exceptions state
+ HttpUnitOptions.setExceptionsThrownOnErrorStatus(originalState );
+ HttpUnitOptions.setExceptionsThrownOnScriptError(originalScriptState);
+ HttpUnitUtils.setEXCEPTION_DEBUG(oldDebug);
+ }
+
+
public void testJavaScriptURLInNewWindow() throws Exception {
defineWebPage( "OnCommand", "<input type='button' id='nowindow' onClick='alert(\"hi\")'></input>\n" +
"<input type='button' id='withwindow' onClick=\"window.open('javascript:alert(\\'hi\\')','_self')\"></input>" );
@@ -279,17 +330,20 @@
"<body>" +
"<a href=\"javascript:sayCheese()\">go</a>" +
"</body></html>" );
- HttpUnitOptions.setExceptionsThrownOnScriptError( true);
- HttpUnitOptions.clearScriptErrorMessages();
WebConversation wc = new WebConversation();
+ boolean oldDebug= HttpUnitUtils.setEXCEPTION_DEBUG(false);
+ HttpUnitOptions.setExceptionsThrownOnScriptError( false);
WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" );
- boolean oldDebug= HttpUnitUtils.setEXCEPTION_DEBUG(false);
try {
+ HttpUnitOptions.setExceptionsThrownOnScriptError( true);
+ HttpUnitOptions.clearScriptErrorMessages();
response.getLinkWith( "go" ).click();
fail("there should have been an exception");
- } catch (Throwable th) {
+ } catch (ScriptException se) {
+ fail("Runtime exception is appropriate in this test case since we ignored the loading error");
+ } catch (RuntimeException rte) {
// java.lang.RuntimeException: Error clicking link: com.meterware.httpunit.ScriptException: URL 'javascript:sayCheese()' failed: org.mozilla.javascript.EcmaError: ReferenceError: "sayCheese" is not defined.
- assertTrue("is not defined should be found in message",th.getMessage().indexOf("not defined")>0);
+ assertTrue("is not defined should be found in message",rte.getMessage().indexOf("not defined")>0);
} finally {
HttpUnitUtils.setEXCEPTION_DEBUG(oldDebug);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|