[Httpunit-commit] CVS: httpunit/src/com/meterware/servletunit RequestContext.java,1.2,1.3 ServletUni
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2004-03-02 11:13:53
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/servletunit In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27879/src/com/meterware/servletunit Modified Files: RequestContext.java ServletUnitHttpRequest.java Log Message: support request body encoding in ServletUnit Index: RequestContext.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/RequestContext.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- RequestContext.java 13 Feb 2003 20:25:55 -0000 1.2 +++ RequestContext.java 2 Mar 2004 10:54:14 -0000 1.3 @@ -23,6 +23,7 @@ import java.util.*; import java.net.URL; +import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletRequest; @@ -37,6 +38,8 @@ private Hashtable _visibleParameters; private HttpServletRequest _parentRequest; private URL _url; + private byte[] _messageBody; + private String _messageEncoding; RequestContext( URL url ) { @@ -148,6 +151,10 @@ private Hashtable getParameters() { + if (_messageBody != null) { + loadParameters( getMessageBodyAsString() ); + _messageBody = null; + } if (_visibleParameters == null) { if (_parentRequest == null) { _visibleParameters = _parameters; @@ -168,4 +175,23 @@ } + private String getMessageBodyAsString() { + try { + return _messageEncoding == null ? new String( _messageBody ) : new String( _messageBody, _messageEncoding ); + } catch (UnsupportedEncodingException e) { + return ""; + } + } + + + void setMessageBody( byte[] bytes ) { + _messageBody = bytes; + } + + + public void setMessageEncoding( String messageEncoding ) { + _messageEncoding = messageEncoding; + } + + } Index: ServletUnitHttpRequest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitHttpRequest.java,v retrieving revision 1.30 retrieving revision 1.31 diff -u -r1.30 -r1.31 --- ServletUnitHttpRequest.java 26 Feb 2004 17:36:47 -0000 1.30 +++ ServletUnitHttpRequest.java 2 Mar 2004 10:54:14 -0000 1.31 @@ -22,6 +22,7 @@ import com.meterware.httpunit.WebRequest; import com.meterware.httpunit.WebClient; import com.meterware.httpunit.Base64; +import com.meterware.httpunit.HttpUnitUtils; import java.io.BufferedReader; import java.io.IOException; @@ -47,6 +48,7 @@ private Vector _locales; private boolean _secure; private RequestContext _requestContext; + private String _charset; /** @@ -63,12 +65,19 @@ _headers.addEntries( request.getHeaders() ); _messageBody = messageBody; _secure = request.getURL().getProtocol().equalsIgnoreCase( "https" ); - _contentType = (String) _headers.get( "Content-Type" ); - if (_headers.get( "Content-Length") == null) _headers.put( "Content-Length", Integer.toString( messageBody.length ) ); _requestContext = new RequestContext( request.getURL() ); + String contentTypeHeader = (String) _headers.get( "Content-Type" ); + if (contentTypeHeader != null) { + String[] res = HttpUnitUtils.parseContentTypeHeader( contentTypeHeader ); + _contentType = res[0]; + _charset = res[1]; + _requestContext.setMessageEncoding( _charset ); + } + if (_headers.get( "Content-Length") == null) _headers.put( "Content-Length", Integer.toString( messageBody.length ) ); + if (_messageBody != null && (_contentType == null || _contentType.indexOf( "x-www-form-urlencoded" ) >= 0 )) { - _requestContext.loadParameters( new String( _messageBody ) ); + _requestContext.setMessageBody( _messageBody ); } } @@ -344,7 +353,7 @@ * does not use character encoding. **/ public String getCharacterEncoding() { - return null; + return _charset; } @@ -647,8 +656,9 @@ * * @since 1.3 **/ - public void setCharacterEncoding( String s ) throws UnsupportedEncodingException { - // XXX implement me! + public void setCharacterEncoding( String charset ) throws UnsupportedEncodingException { + _charset = charset; + _requestContext.setMessageEncoding( charset ); } |