commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From morg...@apache.org
Subject cvs commit: jakarta-commons/latka/src/java/org/apache/commons/latka/xml RequestBodyHandler.java RequestHandler.java
Date Mon, 15 Jul 2002 16:10:27 GMT
morgand     2002/07/15 09:10:27

  Modified:    latka/src/java/org/apache/commons/latka/http Request.java
                        RequestImpl.java
               latka/src/java/org/apache/commons/latka/validators
                        GoldenFileValidator.java
               latka/src/java/org/apache/commons/latka/xml
                        RequestHandler.java
  Added:       latka/src/java/org/apache/commons/latka/xml
                        RequestBodyHandler.java
  Log:
  added "requestBody" suite element for manual POSTs
  
  Revision  Changes    Path
  1.15      +135 -127  jakarta-commons/latka/src/java/org/apache/commons/latka/http/Request.java
  
  Index: Request.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/http/Request.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- Request.java	4 Jul 2002 14:06:32 -0000	1.14
  +++ Request.java	15 Jul 2002 16:10:26 -0000	1.15
  @@ -73,132 +73,140 @@
    */
   public interface Request {
   
  -  /** An integer representing the HTTP GET method */
  -  public static final int HTTP_METHOD_GET = 0;
  -  /** An integer representing the HTTP POST method */
  -  public static final int HTTP_METHOD_POST = 1;
  -  /** An integer representing the HTTP POST method */
  -  public static final int HTTP_METHOD_HEAD = 2;
  -  
  -  /**
  -   * Execute this HTTP request.
  -   *
  -   * @return a Response object represnting the HTTP response to the request
  -   *
  -   * @throws IOException if the remote server could not be reached
  -   */
  -  Response execute() throws IOException;
  -
  -  /**
  -   * Assigns a text label to this request.  This label
  -   * will be made available to a LatkaEventListener
  -   * during a test's execution
  -   * 
  -   * @return the label associated with this request
  -   */
  -  String getLabel();
  -
  -  /**
  -   * Gets the URL that Latka will attempt to contact.  Note:
  -   * since Latka will respect most redirects, this may not
  -   * be the URL that returns the actual response.
  -   * 
  -   * @return the URL associated with this HTTP request
  -   */
  -  URL getURL();
  -
  -  /**
  -   * Associate a parameter with this request.
  -   *
  -   * @param name  the name of the parameter
  -   * @param value the value of the parameter
  -   *
  -   */
  -  void addParameter(String name, String value);
  -
  -  /**
  -   * Set all the parameters for the request.  Overrides
  -   * any parameters that have been already set by addParameter();
  -   * 
  -   * @param parameters
  -   *               all parameters for this request
  -   */
  -  void setParameters(Parameters parameters);
  -
  -  /**
  -   * Get the parameters for the request, so that they can
  -   * be copied to another request if necessary.
  -   * 
  -   * @return parameters for this request
  -   */
  -  Parameters getParameters();
  -  
  -  /**
  -   * Sets a request header.
  -   * 
  -   * @param headerName header name
  -   * @param headerValue header value or null for a null header
  -   */
  -  void addHeader(String headerName, String headerValue);
  -
  -  /**
  -   * Set all the headers for the request.  Overrides
  -   * any headers that have been already set by addHeader();
  -   * 
  -   * @param requestHeaders
  -   *               all headers for this request
  -   */
  -  void setHeaders(RequestHeaders requestHeaders);
  -
  -  /**
  -   * Get the headers for the request, so that they can
  -   * be copied to another request if necessary.
  -   * 
  -   * @return headers for this request
  -   */
  -  RequestHeaders getHeaders();
  -
  -  // getHeader method purposely ommitted.  HttpClient does not retain
  -  // header information after the request is submitted.
  -
  -  /**
  -   * Retrieve the session associated with this request.
  -   *
  -   * @return a <code>Session</code> object
  -   */
  -  Session getSession();
  -
  -  /**
  -   * The amount of time it took to execute the
  -   * request in milliseconds, or -1 if the request has not
  -   * been executed successfully
  -   *
  -   * @return time it took to execute the request in millis
  -   */
  -  int getRequestTiming();
  -
  -  /**
  -   * Sets the basic authentication credentials for this request,
  -   * if any.
  -   * 
  -   * @param credentials user's identification
  -   */
  -  void setCredentials(Credentials credentials);
  -
  -  /**
  -   * Whether or not this request will instruct HttpClient
  -   * to follow local redirects automatically.
  -   * 
  -   * @return true if HttpClient will redirect a 301 or 302 response
  -   */
  -  boolean followRedirects();
  -
  -  /**
  -   * Return the constant representing the HTTP method
  -   * to use in this request
  -   * 
  -   * @return HTTP method for this request
  -   */
  -  int getMethod();
  +    /** An integer representing the HTTP GET method */
  +    public static final int HTTP_METHOD_GET = 0;
  +    /** An integer representing the HTTP POST method */
  +    public static final int HTTP_METHOD_POST = 1;
  +    /** An integer representing the HTTP POST method */
  +    public static final int HTTP_METHOD_HEAD = 2;
  +
  +    /**
  +     * Execute this HTTP request.
  +     *
  +     * @return a Response object represnting the HTTP response to the request
  +     *
  +     * @throws IOException if the remote server could not be reached
  +     */
  +    Response execute() throws IOException;
  +
  +    /**
  +     * Assigns a text label to this request.  This label
  +     * will be made available to a LatkaEventListener
  +     * during a test's execution
  +     * 
  +     * @return the label associated with this request
  +     */
  +    String getLabel();
  +
  +    /**
  +     * Gets the URL that Latka will attempt to contact.  Note:
  +     * since Latka will respect most redirects, this may not
  +     * be the URL that returns the actual response.
  +     * 
  +     * @return the URL associated with this HTTP request
  +     */
  +    URL getURL();
  +
  +    /**
  +     * Associate a parameter with this request.
  +     *
  +     * @param name  the name of the parameter
  +     * @param value the value of the parameter
  +     *
  +     */
  +    void addParameter(String name, String value);
  +
  +    /**
  +     * Set all the parameters for the request.  Overrides
  +     * any parameters that have been already set by addParameter();
  +     * 
  +     * @param parameters
  +     *               all parameters for this request
  +     */
  +    void setParameters(Parameters parameters);
  +
  +    /**
  +     * Get the parameters for the request, so that they can
  +     * be copied to another request if necessary.
  +     * 
  +     * @return parameters for this request
  +     */
  +    Parameters getParameters();
  +
  +    /**
  +     * Sets a request header.
  +     * 
  +     * @param headerName header name
  +     * @param headerValue header value or null for a null header
  +     */
  +    void addHeader(String headerName, String headerValue);
  +
  +    /**
  +     * Set all the headers for the request.  Overrides
  +     * any headers that have been already set by addHeader();
  +     * 
  +     * @param requestHeaders
  +     *               all headers for this request
  +     */
  +    void setHeaders(RequestHeaders requestHeaders);
  +
  +    /**
  +     * Get the headers for the request, so that they can
  +     * be copied to another request if necessary.
  +     * 
  +     * @return headers for this request
  +     */
  +    RequestHeaders getHeaders();
  +
  +    // getHeader method purposely ommitted.  HttpClient does not retain
  +    // header information after the request is submitted.
  +
  +    /**
  +     * Retrieve the session associated with this request.
  +     *
  +     * @return a <code>Session</code> object
  +     */
  +    Session getSession();
  +
  +    /**
  +     * The amount of time it took to execute the
  +     * request in milliseconds, or -1 if the request has not
  +     * been executed successfully
  +     *
  +     * @return time it took to execute the request in millis
  +     */
  +    int getRequestTiming();
  +
  +    /**
  +     * Sets the basic authentication credentials for this request,
  +     * if any.
  +     * 
  +     * @param credentials user's identification
  +     */
  +    void setCredentials(Credentials credentials);
  +
  +    /**
  +     * Whether or not this request will instruct HttpClient
  +     * to follow local redirects automatically.
  +     * 
  +     * @return true if HttpClient will redirect a 301 or 302 response
  +     */
  +    boolean followRedirects();
  +
  +    /**
  +     * Return the constant representing the HTTP method
  +     * to use in this request
  +     * 
  +     * @return HTTP method for this request
  +     */
  +    int getMethod();
  +
  +    /**
  +     * Set the request body for a manual POST.  You may not
  +     * both set the request body and add parameters.
  +     * 
  +     * @param body   Body to POST
  +     */
  +    void setRequestBody(String body);
   
   }
  
  
  
  1.24      +19 -5     jakarta-commons/latka/src/java/org/apache/commons/latka/http/RequestImpl.java
  
  Index: RequestImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/http/RequestImpl.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- RequestImpl.java	14 Jul 2002 16:43:18 -0000	1.23
  +++ RequestImpl.java	15 Jul 2002 16:10:26 -0000	1.24
  @@ -115,6 +115,8 @@
       protected RequestHeaders _requestHeaders = new RequestHeadersImpl();
       /** parameters sent with this request */
       protected Parameters _parameters = new ParametersImpl();
  +    /** manually constructed post body */
  +    protected String _requestBody = null;
       /** whether or not redirect responses should be followed as part of the
           request processing*/
       protected boolean _followRedirects = true;
  @@ -224,6 +226,14 @@
       public Parameters getParameters() {
           return _parameters;
       }
  +
  +    /**
  +     * Defined in the implemented interface
  +     * @see Request#setRequestBody(String)
  +     */
  +    public void setRequestBody(String body) {
  +        _requestBody = body;
  +    }
       
       /**
        * Defined in the implemented interface
  @@ -262,10 +272,14 @@
               _httpMethod.addRequestHeader(header[0], header[1]);
           }
           
  -        List parameters = _parameters.getParameters();
  -        for (int i = 0; i < parameters.size(); ++i) {
  -            String[] parameter = (String[]) parameters.get(i);
  -            addHttpClientParameter(parameter[0], parameter[1]);
  +        if (_requestBody != null) {
  +            ((PostMethod) _httpMethod).setRequestBody(_requestBody);
  +        } else {
  +            List parameters = _parameters.getParameters();
  +            for (int i = 0; i < parameters.size(); ++i) {
  +                String[] parameter = (String[]) parameters.get(i);
  +                addHttpClientParameter(parameter[0], parameter[1]);
  +            }
           }
           
           // for timing
  
  
  
  1.6       +126 -100  jakarta-commons/latka/src/java/org/apache/commons/latka/validators/GoldenFileValidator.java
  
  Index: GoldenFileValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/validators/GoldenFileValidator.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- GoldenFileValidator.java	11 Apr 2002 13:46:25 -0000	1.5
  +++ GoldenFileValidator.java	15 Jul 2002 16:10:27 -0000	1.6
  @@ -1,8 +1,4 @@
   /*
  - *
  - *
  - *
  - *
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
  @@ -61,8 +57,11 @@
   
   package org.apache.commons.latka.validators;
   
  +import java.io.ByteArrayOutputStream;
   import java.io.File;
  +import java.io.FileInputStream;
   import java.io.FileReader;
  +import java.io.InputStream;
   import java.io.IOException;
   
   import java.util.StringTokenizer;
  @@ -80,114 +79,141 @@
   public class GoldenFileValidator extends BaseValidator 
   implements Validator {
   
  -  protected File _goldFile = null;
  -  protected boolean _ignoreWhitespace=false;
  -  protected static String TRUE_MESSAGE = "EXPECTED TO MATCH GOLDEN FILE: ";
  -  protected StringBuffer _matchLog = new StringBuffer();
  -
  -  public GoldenFileValidator(File goldenFile) {
  -    this(null,goldenFile);
  -  }
  -
  -  public GoldenFileValidator(String label, File goldenFile) {
  -    super(label);
  -    _goldFile = goldenFile;
  -  }
  -
  -  public void setIgnoreWhitespace(boolean ignoreWhitespace) {
  -    _ignoreWhitespace = ignoreWhitespace;
  -  }
  -
  -  public void validate(Response response)
  -  throws ValidationException {
  -
  -    String goldFileString = null;
  -
  -    try {
  -      FileReader reader = new FileReader(_goldFile);
  -
  -      StringBuffer buf = new StringBuffer();
  -
  -      while (true) {
  -        int ch = reader.read();
  -        if (ch < 0) {
  -          break;
  +    protected File _goldFile = null;
  +    protected boolean _ignoreWhitespace=false;
  +    protected static String TRUE_MESSAGE = "EXPECTED TO MATCH GOLDEN FILE: ";
  +    protected StringBuffer _matchLog = new StringBuffer();
  +
  +    public GoldenFileValidator(File goldenFile) {
  +        this(null,goldenFile);
  +    }
  +
  +    public GoldenFileValidator(String label, File goldenFile) {
  +        super(label);
  +        _goldFile = goldenFile;
  +    }
  +
  +    public void setIgnoreWhitespace(boolean ignoreWhitespace) {
  +        _ignoreWhitespace = ignoreWhitespace;
  +    }
  +
  +    public void validate(Response response)
  +    throws ValidationException {
  +
  +        // Compare the results and set the status
  +        boolean cmp=true;
  +
  +        if (_ignoreWhitespace) {
  +            String goldFileString = null;
  +
  +            try {
  +                FileReader reader = new FileReader(_goldFile);
  +
  +                StringBuffer buf = new StringBuffer();
  +
  +                while (true) {
  +                    int ch = reader.read();
  +                    if (ch < 0) {
  +                        break;
  +                    }
  +                    buf.append((char) ch);
  +                }
  +
  +                goldFileString = buf.toString();
  +            } catch (IOException e) {
  +                fail(e.toString());
  +            }
  +
  +            cmp=compareWeak(response.getResource(), goldFileString );
  +        } else {
  +            try {
  +                InputStream responseStream = response.getStream();
  +                ByteArrayOutputStream responseBytes = new ByteArrayOutputStream();
  +                while (true) {
  +                    int ch = responseStream.read();
  +                    if (ch < 0) {
  +                        break;
  +                    }
  +                    responseBytes.write(ch);
  +                }
  +
  +                FileInputStream fileStream = new FileInputStream(_goldFile);
  +                ByteArrayOutputStream fileBytes = new ByteArrayOutputStream();
  +                while (true) {
  +                    int ch = fileStream.read();
  +                    if (ch < 0) {
  +                        break;
  +                    }
  +                    fileBytes.write(ch);
  +                }
  +
  +                cmp=compare(new String(responseBytes.toByteArray()).getBytes(), 
  +                            new String(fileBytes.toByteArray()).getBytes() );
  +            } catch (IOException e) {
  +                fail(e.toString());
  +            }
           }
  -        buf.append((char) ch);
  -      }
   
  -      goldFileString = buf.toString();
  -    } catch (IOException e) {
  -      fail(e.toString());
  -    }
  -
  -    // Compare the results and set the status
  -    boolean cmp=true;
  +        if (cmp == false) {
  +            StringBuffer buf = new StringBuffer();
  +            buf.append(TRUE_MESSAGE);
  +            buf.append(_goldFile);
  +            buf.append("\n");
  +            buf.append(_matchLog);
  +            fail(buf.toString());
  +        }
   
  -    if(_ignoreWhitespace) {
  -        cmp=compareWeak(response.getResource(), goldFileString );
  -    } else {
  -        cmp=compare(response.getResource(), goldFileString );
  -    }
   
  -    if (cmp == false) {
  -      StringBuffer buf = new StringBuffer();
  -      buf.append(TRUE_MESSAGE);
  -      buf.append(_goldFile);
  -      buf.append("\n");
  -      buf.append(_matchLog);
  -      fail(buf.toString());
       }
   
  +    // Compare the actual result and the expected result.
  +    protected boolean compare(byte[] array1, byte[] array2) {
  +        if ( array1==null || array2==null) return false;
  +        if ( array1.length != array2.length ) {
  +            log("Wrong size " + array1.length + " " + array2.length );
  +            log("char 1:" + array1[array2.length - 1]);
  +            log("char 2:" + array2[array2.length - 1]);
  +            return false;
  +        }
   
  -  }
  -
  -  // Compare the actual result and the expected result.
  -  protected boolean compare(String str1, String str2) {
  -    if ( str1==null || str2==null) return false;
  -    if ( str1.length() != str2.length() ) {
  -      log("Wrong size " + str1.length() +" " + str2.length() );
  -      return false;
  -    }
  -
  -    for (int i=0; i<str1.length() ; i++ ) {
  -      if (str1.charAt( i ) != str2.charAt( i ) ) {
  -        log("Error at " + i  + " " + str1.charAt(1) +
  -            str2.charAt(i));
  -        return false;
  -      }
  +        for (int i=0; i<array1.length ; i++ ) {
  +            if (array1[i] != array2[i] ) {
  +                log("Error at " + i  + "comparing '" + array1[i] +
  +                    "' to '" + array2[i] + "'");
  +                return false;
  +            }
  +        }
  +        return true;
       }
  -    return true;
  -  }
  -
  -  // Compare the actual result and the expected result.
  -  // Original compare - ignores whitespace ( because most
  -  // golden files are wrong !)
  -  protected boolean compareWeak(String str1, String str2) {
  -    if ( str1==null || str2==null) return false;
   
  -    StringTokenizer st1=new StringTokenizer(str1);
  -    StringTokenizer st2=new StringTokenizer(str2);
  +    // Compare the actual result and the expected result.
  +    // Original compare - ignores whitespace ( because most
  +    // golden files are wrong !)
  +    protected boolean compareWeak(String str1, String str2) {
  +        if ( str1==null || str2==null) return false;
  +
  +        StringTokenizer st1=new StringTokenizer(str1);
  +        StringTokenizer st2=new StringTokenizer(str2);
  +
  +        while (st1.hasMoreTokens() && st2.hasMoreTokens()) {
  +            String tok1 = st1.nextToken();
  +            String tok2 = st2.nextToken();
  +            if (!tok1.equals(tok2)) {
  +                log("\tFAIL*** : Live token = " + tok1 
  +                    + ", did not match golden file token = " + tok2);
  +                return false;
  +            }
  +        }
   
  -    while (st1.hasMoreTokens() && st2.hasMoreTokens()) {
  -      String tok1 = st1.nextToken();
  -      String tok2 = st2.nextToken();
  -      if (!tok1.equals(tok2)) {
  -        log("\tFAIL*** : Live token = " + tok1 
  -            + ", did not match golden file token = " + tok2);
  -        return false;
  -      }
  +        if (st1.hasMoreTokens() || st2.hasMoreTokens()) {
  +            return false;
  +        } else {
  +            return true;
  +        }
       }
   
  -    if (st1.hasMoreTokens() || st2.hasMoreTokens()) {
  -      return false;
  -    } else {
  -      return true;
  +    protected void log(String message) {
  +        _matchLog.append(message);
       }
  -  }
  -
  -  protected void log(String message) {
  -    _matchLog.append(message);
  -  }
   
   }
  
  
  
  1.24      +7 -1      jakarta-commons/latka/src/java/org/apache/commons/latka/xml/RequestHandler.java
  
  Index: RequestHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/xml/RequestHandler.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- RequestHandler.java	29 Jun 2002 10:30:21 -0000	1.23
  +++ RequestHandler.java	15 Jul 2002 16:10:27 -0000	1.24
  @@ -182,6 +182,12 @@
               ParameterHandler paramHandler = new ParameterHandler(_reader,_request);
               paramHandler.delegate(uri, localName, qName, atts);
               return;
  +        } else if (localName.equals("requestBody")) {
  +            _log.info("setting request body");
  +
  +            RequestBodyHandler bodyHandler = new RequestBodyHandler(_reader,_request);
  +            bodyHandler.delegate(uri, localName, qName, atts);
  +            return;
           } else if (localName.equals("validate")) {
               _log.info("encountered validations");
   
  
  
  
  1.1                  jakarta-commons/latka/src/java/org/apache/commons/latka/xml/RequestBodyHandler.java
  
  Index: RequestBodyHandler.java
  ===================================================================
  package org.apache.commons.latka.xml;
  
  import org.apache.commons.latka.http.Request;
  
  import org.xml.sax.SAXException;
  import org.xml.sax.XMLReader;
  
  public class RequestBodyHandler extends LatkaHandler {
  
      protected Request _request = null;
      protected StringBuffer _requestBody = null;
  
      public RequestBodyHandler(XMLReader reader, Request request) {
          super(reader);
          _request = request;
          _requestBody = new StringBuffer();
      }
  
      public void characters(char[] ch, int start, int length) {
          _requestBody.append(ch,start,length);
      }
  
      public void endElement(String uri, String localName, String qName) 
          throws SAXException {
  
          int method = _request.getMethod();
          if (method != Request.HTTP_METHOD_POST) {
              throw new SAXException("Request bodies are only supported in POSTs");
          }
  
          _request.setRequestBody(_requestBody.toString());
          release();
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message