[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit HttpException.java,1.2,1.3 HttpInternalEr
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2001-11-08 22:07:55
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit In directory usw-pr-cvs1:/tmp/cvs-serv5733/src/com/meterware/httpunit Modified Files: HttpException.java HttpInternalErrorException.java HttpNotFoundException.java HttpUnitUtils.java HttpWebResponse.java WebClient.java WebForm.java WebRequest.java WebResponse.java Log Message: Added support for web.xml, response messages, and readonly controls Index: HttpException.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HttpException.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- HttpException.java 2000/11/21 20:44:58 1.2 +++ HttpException.java 2001/11/08 22:07:52 1.3 @@ -2,7 +2,7 @@ /******************************************************************************************************************** * $Id$ * -* Copyright (c) 2000, Russell Gold +* Copyright (c) 2000-2001, 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 @@ -21,6 +21,7 @@ *******************************************************************************************************************/ import java.util.Properties; import java.io.*; +import java.net.URL; /** @@ -36,19 +37,25 @@ } - HttpException( int responseCode, String reason ) { - _reason = reason; + HttpException( int responseCode, String responseMessage, URL baseURL ) { + _responseMessage = responseMessage; _responseCode = responseCode; + _url = baseURL; } public String getMessage() { StringBuffer sb = new StringBuffer( "Error on HTTP request: " ); sb.append( _responseCode ); - if (_reason != null) { + if (_responseMessage != null) { + sb.append( " " ); + sb.append( _responseMessage ); + sb.append( "" ); + } + if (_url != null) { sb.append( " [" ); - sb.append( _reason ); - sb.append( " ]" ); + sb.append( _url.toExternalForm() ); + sb.append( "]" ); } return sb.toString(); } @@ -59,9 +66,15 @@ } + public String getResponseMessage() { + return _responseMessage; + } + + private int _responseCode; + private URL _url; - private String _reason; + private String _responseMessage; } Index: HttpInternalErrorException.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HttpInternalErrorException.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- HttpInternalErrorException.java 2000/11/21 20:44:58 1.3 +++ HttpInternalErrorException.java 2001/11/08 22:07:52 1.4 @@ -2,7 +2,7 @@ /******************************************************************************************************************** * $Id$ * -* Copyright (c) 2000, Russell Gold +* Copyright (c) 2000-2001, 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 @@ -20,18 +20,24 @@ * *******************************************************************************************************************/ import java.net.HttpURLConnection; +import java.net.URL; /** * This exception is thrown when an internal error is found on the server. * @author Seth Ladd + * @author Russell Gold **/ public class HttpInternalErrorException extends HttpException { - public HttpInternalErrorException( String url ) { - super( HttpURLConnection.HTTP_INTERNAL_ERROR, url ); + public HttpInternalErrorException( URL url ) { + super( HttpURLConnection.HTTP_INTERNAL_ERROR, "Internal Error", url ); } + + public HttpInternalErrorException( URL url, Throwable t ) { + super( HttpURLConnection.HTTP_INTERNAL_ERROR, t.toString(), url ); + } } Index: HttpNotFoundException.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HttpNotFoundException.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- HttpNotFoundException.java 2000/11/21 20:44:58 1.3 +++ HttpNotFoundException.java 2001/11/08 22:07:52 1.4 @@ -2,7 +2,7 @@ /******************************************************************************************************************** * $Id$ * -* Copyright (c) 2000, Russell Gold +* Copyright (c) 2000-2001, 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 @@ -20,6 +20,7 @@ * *******************************************************************************************************************/ import java.net.HttpURLConnection; +import java.net.URL; /** @@ -30,8 +31,12 @@ public class HttpNotFoundException extends HttpException { - public HttpNotFoundException( String url ) { - super( HttpURLConnection.HTTP_NOT_FOUND, url ); + public HttpNotFoundException( URL url ) { + super( HttpURLConnection.HTTP_NOT_FOUND, "Not found", url ); + } + + public HttpNotFoundException( URL url, Throwable t ) { + super( HttpURLConnection.HTTP_NOT_FOUND, t.toString(), url ); } Index: HttpUnitUtils.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HttpUnitUtils.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- HttpUnitUtils.java 2001/11/05 15:50:38 1.2 +++ HttpUnitUtils.java 2001/11/08 22:07:52 1.3 @@ -48,7 +48,7 @@ * Returns an interpretation of the specified URL-encoded string. * FIXME: currently assumes iso-8859-1 character set. **/ - static String decode( String byteString ) { + public static String decode( String byteString ) { StringBuffer sb = new StringBuffer(); char[] chars = byteString.toCharArray(); char[] hexNum = { '0', '0', '0' }; Index: HttpWebResponse.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HttpWebResponse.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- HttpWebResponse.java 2001/07/19 17:43:50 1.14 +++ HttpWebResponse.java 2001/11/08 22:07:52 1.15 @@ -67,6 +67,14 @@ } + /** + * Returns the response message associated with this response. + **/ + public String getResponseMessage() { + return _responseMessage; + } + + public String[] getHeaderFieldNames() { Vector names = new Vector(); for (Enumeration e = _headers.keys(); e.hasMoreElements();) { @@ -98,22 +106,26 @@ final private static String FILE_ENCODING = System.getProperty( "file.encoding" ); - private int _responseCode = HttpURLConnection.HTTP_OK; + private int _responseCode = HttpURLConnection.HTTP_OK; + private String _responseMessage = "OK"; private Hashtable _headers = new Hashtable(); - private int getResponseCode( URLConnection connection, String statusHeader ) { - if (statusHeader == null) throw new HttpNotFoundException( connection.getURL().toExternalForm() ); + private void readResponseHeader( URLConnection connection ) { + if (connection.getHeaderField(0) == null) throw new HttpNotFoundException( connection.getURL() ); - StringTokenizer st = new StringTokenizer( statusHeader ); + StringTokenizer st = new StringTokenizer( connection.getHeaderField(0) ); st.nextToken(); if (!st.hasMoreTokens()) { - return HttpURLConnection.HTTP_OK; + _responseCode = HttpURLConnection.HTTP_OK; + _responseMessage = "OK"; } else try { - return Integer.parseInt( st.nextToken() ); + _responseCode = Integer.parseInt( st.nextToken() ); + _responseMessage = st.hasMoreTokens() ? st.nextToken() : ""; } catch (NumberFormatException e) { - return HttpURLConnection.HTTP_INTERNAL_ERROR; + _responseCode = HttpURLConnection.HTTP_INTERNAL_ERROR; + _responseMessage = "Cannot parse response header"; } } @@ -121,9 +133,10 @@ private void readHeaders( URLConnection connection ) { loadHeaders( connection ); if (connection instanceof HttpURLConnection) { - _responseCode = getResponseCode( connection, connection.getHeaderField(0) ); + readResponseHeader( connection ); } else { _responseCode = HttpURLConnection.HTTP_OK; + _responseMessage = "OK"; if (connection.getContentType().startsWith( "text" )) { setContentTypeHeader( connection.getContentType() + "; charset=" + FILE_ENCODING ); } Index: WebClient.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebClient.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- WebClient.java 2001/08/06 20:19:41 1.9 +++ WebClient.java 2001/11/08 22:07:52 1.10 @@ -256,11 +256,11 @@ throw new AuthorizationRequiredException( response.getHeaderField( "WWW-Authenticate" ) ); } else if (!HttpUnitOptions.getExceptionsThrownOnErrorStatus()) { } else if (response.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) { - throw new HttpInternalErrorException( response.getURL().toExternalForm() ); + throw new HttpInternalErrorException( response.getURL() ); } else if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { - throw new HttpNotFoundException( response.getURL().toExternalForm() ); + throw new HttpNotFoundException( response.getURL() ); } else if (response.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) { - throw new HttpException( response.getResponseCode(), response.getURL().toExternalForm() ); + throw new HttpException( response.getResponseCode(), response.getResponseMessage(), response.getURL() ); } } Index: WebForm.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebForm.java,v retrieving revision 1.29 retrieving revision 1.30 diff -u -r1.29 -r1.30 --- WebForm.java 2001/11/05 18:54:51 1.29 +++ WebForm.java 2001/11/08 22:07:52 1.30 @@ -136,16 +136,16 @@ NodeList nl = ((Element) getNode()).getElementsByTagName( "input" ); for (int i = 0; i < nl.getLength(); i++) { - if (NodeUtils.getNodeAttribute( nl.item(i), "type" ).equalsIgnoreCase( "submit" ) - || NodeUtils.getNodeAttribute( nl.item(i), "type" ).equalsIgnoreCase( "image" )) { + if ((hasMatchingAttribute( nl.item(i), "type", "submit" ) || hasMatchingAttribute( nl.item(i), "type", "image" )) + && nl.item(i).getAttributes().getNamedItem( "disabled" ) == null) { _buttonVector.addElement( new SubmitButton( nl.item(i) ) ); } } nl = ((Element) getNode()).getElementsByTagName( "button" ); for (int i = 0; i < nl.getLength(); i++) { - if (NodeUtils.getNodeAttribute( nl.item(i), "type" ).equalsIgnoreCase( "submit" ) - || NodeUtils.getNodeAttribute( nl.item(i), "type" ).equalsIgnoreCase( "" )) { + if ((hasMatchingAttribute( nl.item(i), "type", "submit" ) || hasMatchingAttribute( nl.item(i), "type", "" )) + && nl.item(i).getAttributes().getNamedItem( "disabled" ) == null) { _buttonVector.addElement( new SubmitButton( nl.item(i) ) ); } } @@ -155,6 +155,11 @@ } + private boolean hasMatchingAttribute( Node node, String attributeName, String attributeValue ) { + return NodeUtils.getNodeAttribute( node, attributeName ).equalsIgnoreCase( attributeValue ); + } + + /** * Returns the submit button defined in this form with the specified name. * If more than one such button exists, will return the first found. @@ -400,6 +405,17 @@ } + /** + * Returns the values which *must* for the specified parameter name. + **/ + String[] getRequiredValues( String name ) { + Object result = getParameterRequiredValues().get( name ); + if (result instanceof String[]) return (String[]) result; + if (result instanceof String) return new String[] { (String) result }; + return new String[0]; + } + + //---------------------------------- private members -------------------------------- /** The type of a parameter which accepts any text. **/ @@ -496,6 +512,46 @@ return _defaults; } + private Hashtable _required; + + private Hashtable getParameterRequiredValues() { + if (_required == null) { + NamedNodeMap[] parameters = getParameters(); + Hashtable required = new Hashtable(); + for (int i = 0; i < parameters.length; i++) { + String name = getValue( parameters[i].getNamedItem( "name" ) ); + String value = getValue( parameters[i].getNamedItem( "value" ) ); + String type = getValue( parameters[i].getNamedItem( "type" ) ).toUpperCase(); + if (type == null || type.length() == 0) type = "TEXT"; + if (parameters[i].getNamedItem( "readonly" ) == null) continue; + + if (type.equals( "TEXT" ) || type.equals( "HIDDEN" ) || type.equals( "PASSWORD" )) { + required.put( name, value ); + } else if (type.equals( "RADIO" ) && parameters[i].getNamedItem( "checked" ) != null) { + required.put( name, value ); + } else if (type.equals( "CHECKBOX" ) && parameters[i].getNamedItem( "checked" ) != null) { + if (value.length() == 0) value = "on"; + String[] currentDefaults = (String[]) required.get( name ); + if (currentDefaults == null) { + required.put( name, new String[] { value } ); + } else { + required.put( name, withNewValue( currentDefaults, value ) ); + } + } + } + HTMLSelectElement[] selections = getSelections(); + for (int i = 0; i < selections.length; i++) { + if (selections[i].isDisabled()) required.put( selections[i].getName(), selections[i].getSelected() ); + } + HTMLTextAreaElement[] textAreas = getTextAreas(); + for (int i = 0; i < textAreas.length; i++) { + if (textAreas[i].isReadOnly()) required.put( textAreas[i].getName(), textAreas[i].getValue() ); + } + _required = required; + } + return _required; + } + private Hashtable getParameterOptions() { if (_options == null) { @@ -520,7 +576,9 @@ String type = getValue( parameters[i].getNamedItem( "type" ) ).toUpperCase(); if (type == null || type.length() == 0) type = "TEXT"; - if (type.equals( "RADIO" ) || type.equals( "CHECKBOX" )) { + if ((type.equals( "RADIO" ) || type.equals( "CHECKBOX" )) + && (parameters[i].getNamedItem( "readonly" ) == null + || parameters[i].getNamedItem( "checked" ) != null)) { if (value.length() == 0 && type.equals( "CHECKBOX" )) value = "on"; String[] radioOptions = (String[]) options.get( name ); if (radioOptions == null) { @@ -739,6 +797,11 @@ } + boolean isDisabled() { + return _node.getAttributes().getNamedItem( "disabled" ) != null; + } + + private String getOptionValue( Node optionNode ) { NamedNodeMap nnm = optionNode.getAttributes(); if (nnm.getNamedItem( "value" ) != null) { @@ -776,6 +839,10 @@ String getValue() { return NodeUtils.asText(_node.getChildNodes() ); + } + + boolean isReadOnly() { + return _node.getAttributes().getNamedItem( "readonly" ) != null; } } Index: WebRequest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebRequest.java,v retrieving revision 1.26 retrieving revision 1.27 diff -u -r1.26 -r1.27 --- WebRequest.java 2001/10/19 19:16:57 1.26 +++ WebRequest.java 2001/11/08 22:07:52 1.27 @@ -487,6 +487,12 @@ private void validateParameterValue( String name, String value ) { if (_sourceForm == null) return; + validateOneParameterValue( name, value ); + validateRequiredValues( name, new String[] { value } ); + } + + + private void validateOneParameterValue( String name, String value ) { if (_sourceForm.isTextParameter( name )) return; if (_sourceForm.isFileParameter( name )) throw new IllegalFileParameterException( name ); if (!inArray( name, _sourceForm.getParameterNames() )) throw new NoSuchParameterException( name ); @@ -503,8 +509,17 @@ throw new TextParameterCountException( name, _sourceForm.getNumTextParameters( name ) ); } } + + for (int i = 0; i < values.length; i++) validateOneParameterValue( name, values[i] ); + validateRequiredValues( name, values ); + } + - for (int i = 0; i < values.length; i++) validateParameterValue( name, values[i] ); + private void validateRequiredValues( String name, String[] values ) { + String[] required = _sourceForm.getRequiredValues( name ); + for (int i = 0; i < required.length; i++) { + if (!inArray( required[i], values )) throw new MissingParameterValueException( name, required[i], values ); + } } @@ -642,6 +657,41 @@ private String _parameterName; private String _badValue; private String[] _allowedValues; +} + + +//============================= exception class MissingParameterValueException ====================================== + + +/** + * This exception is thrown on an attempt to remove a required value from a form parameter. + **/ +class MissingParameterValueException extends IllegalRequestParameterException { + + + MissingParameterValueException( String parameterName, String missingValue, String[] proposed ) { + _parameterName = parameterName; + _missingValue = missingValue; + _proposedValues = proposed; + } + + + public String getMessage() { + StringBuffer sb = new StringBuffer(); + sb.append( "Parameter '" ).append( _parameterName ).append( "' must have the value '" ); + sb.append( _missingValue ).append( "'. Attempted to set it to: { " ); + for (int i = 0; i < _proposedValues.length; i++) { + if (i != 0) sb.append( ", " ); + sb.append( _proposedValues[i] ); + } + sb.append( " }" ); + return sb.toString(); + } + + + private String _parameterName; + private String _missingValue; + private String[] _proposedValues; } Index: WebResponse.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v retrieving revision 1.51 retrieving revision 1.52 diff -u -r1.51 -r1.52 --- WebResponse.java 2001/11/05 18:54:51 1.51 +++ WebResponse.java 2001/11/08 22:07:52 1.52 @@ -160,6 +160,13 @@ /** + * Returns the response message associated with this response. + **/ + abstract + public String getResponseMessage(); + + + /** * Returns the content type of this response. **/ public String getContentType() { @@ -948,6 +955,14 @@ **/ public int getResponseCode() { return HttpURLConnection.HTTP_OK; + } + + + /** + * Returns the response message associated with this response. + **/ + public String getResponseMessage() { + return "OK"; } |