instance method process

Ruby on Rails edge

Since v3.0.20

Available in: v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.11.3 v5.0.7.2 v5.1.7 v5.2.8.1 v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3.2 v8.0.5.1 v8.1.3.1 edge

Signature

process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil, query: nil, body: nil)

Performs the actual request.

  • method: The HTTP method (GET, POST, PATCH, PUT, DELETE, HEAD, OPTIONS) as a symbol.

  • path: The URI (as a String) on which you want to perform the request.

  • params: The HTTP parameters that you want to pass. This may be nil, a Hash, or a String that is appropriately encoded (application/x-www-form-urlencoded or multipart/form-data). For GET requests, params are sent as query string. For other methods, params are sent as the request body. Use query: or body: for explicit control.

  • headers: Additional headers to pass, as a Hash. The headers will be merged into the Rack env hash.

  • env: Additional env to pass, as a Hash. The headers will be merged into the Rack env hash.

  • xhr: Set to true if you want to make an Ajax request. Adds request headers characteristic of XMLHttpRequest, e.g. HTTP_X_REQUESTED_WITH. The headers will be merged into the Rack env hash.

  • as: Used for encoding the request with different content type. Supports :json by default and will set the appropriate request headers. The headers will be merged into the Rack env hash.

  • query: Parameters to always send as URL query string, regardless of HTTP method. Accepts a Hash or pre-encoded String.

  • body: Parameters to always send as the request body, encoded per as:. Can be combined with query: to send both query string and body params.

This method is rarely used directly. Use RequestHelpers#get, RequestHelpers#post, or other standard HTTP methods in integration tests. #process is only required when using a request method that doesn’t have a method defined in the integration tests.

This method returns the response status, after performing the request. Furthermore, if this method was called from an ActionDispatch::IntegrationTest object, then that object’s @response instance variable will point to a ActionDispatch::TestResponse object which one can use to inspect the details of the response.

Example:

process :get, '/author', params: { since: 201501011400 }
process :get, '/author', query: { since: 201501011400 }, as: :json
process :post, '/search', query: { page: 1 }, body: { q: "rails" }, as: :json

Parameters

method req
path req
params key = nil
headers key = nil
env key = nil
xhr key = false
as key = nil
query key = nil
body key = nil
Source
# File actionpack/lib/action_dispatch/testing/integration.rb, line 245
      def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil, query: nil, body: nil)
        request_encoder = RequestEncoder.encoder(as)
        headers ||= {}

        if query
          query_string = query.is_a?(String) ? query : Rack::Utils.build_nested_query(query)
          path = path.include?("?") ? "#{path}&#{query_string}" : "#{path}?#{query_string}"
        end

        if path.include?("://")
          path = build_expanded_path(path) do |location|
            https! URI::HTTPS === location if location.scheme

            if url_host = location.host
              default = Rack::Request::DEFAULT_PORTS[location.scheme]
              url_host += ":#{location.port}" if default != location.port
              host! url_host
            end
          end
        end

        hostname, port = host.split(":")

        body_params = body || (method != :get ? params : nil)
        query_params = body ? nil : (method == :get ? params : nil)

        request_env = {
          :method => method,
          :params => body_params ? request_encoder.encode_params(body_params) : query_params,

          "SERVER_NAME"     => hostname,
          "SERVER_PORT"     => port || (https? ? "443" : "80"),
          "HTTPS"           => https? ? "on" : "off",
          "rack.url_scheme" => https? ? "https" : "http",

          "REQUEST_URI"    => path,
          "HTTP_HOST"      => host,
          "REMOTE_ADDR"    => remote_addr,
          "HTTP_ACCEPT"    => request_encoder.accept_header || accept
        }

        if body_params && request_encoder.content_type
          request_env["CONTENT_TYPE"] = request_encoder.content_type
        end

        wrapped_headers = Http::Headers.from_hash({})
        wrapped_headers.merge!(headers) if headers

        if xhr
          wrapped_headers["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"
          wrapped_headers["HTTP_ACCEPT"] ||= [Mime[:js], Mime[:html], Mime[:xml], "text/xml", "*/*"].join(", ")
        end

        # This modifies the passed request_env directly.
        if wrapped_headers.present?
          Http::Headers.from_hash(request_env).merge!(wrapped_headers)
        end
        if env.present?
          Http::Headers.from_hash(request_env).merge!(env)
        end

        session = Rack::Test::Session.new(_mock_session)

        # NOTE: rack-test v0.5 doesn't build a default uri correctly Make sure requested
        # path is always a full URI.
        uri = build_full_uri(path, request_env)

        if method == :get && String === request_env[:params]
          # rack-test will needlessly parse and rebuild a :params
          # querystring, using Rack's query parser. At best that's a
          # waste of time; at worst it can change the value.

          uri << "?" << request_env.delete(:params)
        end

        session.request(uri, request_env)

        @request_count += 1
        @request = ActionDispatch::Request.new(session.last_request.env)
        response = _mock_session.last_response
        @response = ActionDispatch::TestResponse.from_response(response)
        @response.request = @request
        @html_document = nil
        @url_options = nil

        @controller = @request.controller_instance

        response.status
      end

Defined in actionpack/lib/action_dispatch/testing/integration.rb line 245 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionDispatch::Integration::Session

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close