Class RequestBuilder

java.lang.Object
org.javalite.activeweb.RequestBuilder

public class RequestBuilder extends Object
Class is used in DSL for building a fake request for a controller to be tested. This class is not used directly.
Author:
Igor Polevoy
  • Constructor Details

    • RequestBuilder

      public RequestBuilder(String controllerPath)
  • Method Details

    • formItem

      public RequestBuilder formItem(Object fieldName, Object value)
      Convenience method to add a non-file form item to request. Will call formItem(String, String, boolean, String, byte[]) internally. Content type will be set to "text/plain", and "isFile" to false.
      Parameters:
      fieldName - name of field - toString() will be used to add to form item
      value - - value of parameter, toString().getBytes() will be used to add to form item
      Returns:
    • formItems

      public RequestBuilder formItems(Object... namesAndValues)
      Convenience method for sending pairs of name and values with multi-part request.
      Parameters:
      namesAndValues - names and following corresponding values. The following pattern is expected: name,value,name1,value1...
      Returns:
    • formItem

      public RequestBuilder formItem(String fileName, String fieldName, boolean isFile, String contentType, byte[] content)
      Adds an "uploaded" file to the request. Do not forget to set the content type to: "multipart/form-data", or this method will be ignored.
      Parameters:
      fileName - name of file.
      fieldName - name of field name - this is typically a name of a HTML form field.
      isFile - set true for file, false for regular field.
      contentType - this is content type for this field, not the request. Set to a value reflecting the file content, such as "image/png", "application/pdf", etc.
      content - this is the binary content of the file.
      Returns:
      RequestBuilder for setting additional request parameters.
    • formItem

      public RequestBuilder formItem(FormItem item)
      Adds "uploaded" file to the request. Do not forget to set the content type to: "multipart/form-data", or this method will be ignored.
      Parameters:
      item - this can be an instance of a FormItem or FileItem.
      Returns:
      RequestBuilder for setting additional request parameters.
    • param

      public RequestBuilder param(String name, Object value)
      Sets a single parameter for request.

      For parameters with multiple values, set the value to be List

      Parameters:
      name - name of parameter.
      value - value of parameter.
      Returns:
      instance of RequestBuilder.
    • param

      public RequestBuilder param(String name)
      Convenience method, exists to pass parameters with blank values. Calling this method such as: param("flag") is equivalent to: param("flag", "")
      Parameters:
      name - name of parameter to pass
      Returns:
      instance of RequestBuilder.
    • header

      public RequestBuilder header(String name, String value)
      Sets a single header for the request.
      Parameters:
      name - name of header.
      value - value of header.
      Returns:
      instance of RequestBuilder.
    • headers

      public RequestBuilder headers(String... namesAndValues)
      Convenience method to set names and values for headers. If arguments are indexed from 1, then every odd argument is a name and every even argument that follows it is a value corresponding to the preceding odd argument.
      Parameters:
      namesAndValues - names and following corresponding values
      Returns:
      instance of RequestBuilder
    • params

      public RequestBuilder params(Object... namesAndValues)
      Convenience method for setting parameters of the request. If arguments are indexed from 1, then every odd argument is a name and every even argument that follows it is a value corresponding to the preceding odd argument.

      For parameters with multiple values, set the value to be List

      Parameters:
      namesAndValues - names and following corresponding values
      Returns:
      instance of RequestBuilder.
    • contentType

      public RequestBuilder contentType(String contentType)
      Sets content type on request.
      Parameters:
      contentType - content type.
      Returns:
      instance of RequestBuilder
    • cookie

      public RequestBuilder cookie(Cookie cookie)
      Adds cookie to current request.
    • content

      public RequestBuilder content(String content)
      Use to post content to a tested controller.
      Parameters:
      content - content string
      Returns:
      self
    • content

      public RequestBuilder content(byte[] content)
      Use to post content to a tested controller.
      Parameters:
      content - content bytes
      Returns:
      self
    • get

      public void get(String actionName)
      Simulate HTTP GET call to an action of controller.
      Parameters:
      actionName - name of action as on a URL - not CamelCase.
    • post

      public void post(String actionName)
      Simulate HTTP POST call to an action of controller.
      Parameters:
      actionName - name of action as on a URL - not CamelCase.
    • put

      public void put(String actionName)
      Simulate HTTP PUT call to an action of controller.
      Parameters:
      actionName - name of action as on a URL - not CamelCase.
    • delete

      public void delete(String actionName)
      Simulate HTTP DELETE call to an action of controller.
      Parameters:
      actionName - name of action as on a URL - not CamelCase.
    • options

      public void options(String actionName)
      Simulate HTTP OPTIONS call to an action of controller.
      Parameters:
      actionName - name of action as on a URL - not CamelCase.
    • id

      public RequestBuilder id(Object id)
      Sets ID for this request. This method will convert ID value to string before executing a controller.
      Parameters:
      id - id for this request; this value is accessible inside controller with getId() method.
    • format

      public RequestBuilder format(String format)
      Sets format for this request. Format is the part of URI that is trailing after a last dot, as in: /books.xml, here "xml" is a format.
      Parameters:
      format - format for this request.
    • queryString

      public RequestBuilder queryString(String queryString)
      Sets a query string (as in URL) for the request.
      Parameters:
      queryString - query string value
      Returns:
      instance of RequestBuilder.
    • remoteAddress

      public RequestBuilder remoteAddress(String remoteAddress)
      Use to simulate a remote IP address. Use RequestAccess.remoteAddress() to retrieve it.
      Parameters:
      remoteAddress - simulated remote IP address.