Module: ActionDispatch::Integration::RequestHelpers

Included in:
Session
Defined in:
lib/action_dispatch/testing/integration.rb

Instance Method Summary collapse

Instance Method Details

#delete(path, *args) ⇒ Object

Performs a DELETE request with the given parameters. See #get for more details.



63
64
65
# File 'lib/action_dispatch/testing/integration.rb', line 63

def delete(path, *args)
  process_with_kwargs(:delete, path, *args)
end

#delete_via_redirect(path, *args) ⇒ Object

Performs a DELETE request, following any subsequent redirect. See request_via_redirect for more information.



162
163
164
165
# File 'lib/action_dispatch/testing/integration.rb', line 162

def delete_via_redirect(path, *args)
  ActiveSupport::Deprecation.warn('`delete_via_redirect` is deprecated and will be removed in Rails 5.1. Please use `follow_redirect!` manually after the request call for the same behavior.')
  request_via_redirect(:delete, path, *args)
end

#follow_redirect!Object

Follow a single redirect response. If the last response was not a redirect, an exception will be raised. Otherwise, the redirect is performed on the location header.



108
109
110
111
112
# File 'lib/action_dispatch/testing/integration.rb', line 108

def follow_redirect!
  raise "not a redirect! #{status} #{status_message}" unless redirect?
  get(response.location)
  status
end

#get(path, *args) ⇒ Object

Performs a GET request with the given parameters.

  • path: The URI (as a String) on which you want to perform a GET 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).

  • 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.

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

You can also perform POST, PATCH, PUT, DELETE, and HEAD requests with #post, #patch, #put, #delete, and #head.

Example:

get '/feed', params: { since: 201501011400 }
post '/profile', headers: { "X-Test-Header" => "testvalue" }


39
40
41
# File 'lib/action_dispatch/testing/integration.rb', line 39

def get(path, *args)
  process_with_kwargs(:get, path, *args)
end

#get_via_redirect(path, *args) ⇒ Object

Performs a GET request, following any subsequent redirect. See request_via_redirect for more information.



134
135
136
137
# File 'lib/action_dispatch/testing/integration.rb', line 134

def get_via_redirect(path, *args)
  ActiveSupport::Deprecation.warn('`get_via_redirect` is deprecated and will be removed in Rails 5.1. Please use `follow_redirect!` manually after the request call for the same behavior.')
  request_via_redirect(:get, path, *args)
end

#head(path, *args) ⇒ Object

Performs a HEAD request with the given parameters. See #get for more details.



69
70
71
# File 'lib/action_dispatch/testing/integration.rb', line 69

def head(path, *args)
  process_with_kwargs(:head, path, *args)
end

#patch(path, *args) ⇒ Object

Performs a PATCH request with the given parameters. See #get for more details.



51
52
53
# File 'lib/action_dispatch/testing/integration.rb', line 51

def patch(path, *args)
  process_with_kwargs(:patch, path, *args)
end

#patch_via_redirect(path, *args) ⇒ Object

Performs a PATCH request, following any subsequent redirect. See request_via_redirect for more information.



148
149
150
151
# File 'lib/action_dispatch/testing/integration.rb', line 148

def patch_via_redirect(path, *args)
  ActiveSupport::Deprecation.warn('`patch_via_redirect` is deprecated and will be removed in Rails 5.1. Please use `follow_redirect!` manually after the request call for the same behavior.')
  request_via_redirect(:patch, path, *args)
end

#post(path, *args) ⇒ Object

Performs a POST request with the given parameters. See #get for more details.



45
46
47
# File 'lib/action_dispatch/testing/integration.rb', line 45

def post(path, *args)
  process_with_kwargs(:post, path, *args)
end

#post_via_redirect(path, *args) ⇒ Object

Performs a POST request, following any subsequent redirect. See request_via_redirect for more information.



141
142
143
144
# File 'lib/action_dispatch/testing/integration.rb', line 141

def post_via_redirect(path, *args)
  ActiveSupport::Deprecation.warn('`post_via_redirect` is deprecated and will be removed in Rails 5.1. Please use `follow_redirect!` manually after the request call for the same behavior.')
  request_via_redirect(:post, path, *args)
end

#put(path, *args) ⇒ Object

Performs a PUT request with the given parameters. See #get for more details.



57
58
59
# File 'lib/action_dispatch/testing/integration.rb', line 57

def put(path, *args)
  process_with_kwargs(:put, path, *args)
end

#put_via_redirect(path, *args) ⇒ Object

Performs a PUT request, following any subsequent redirect. See request_via_redirect for more information.



155
156
157
158
# File 'lib/action_dispatch/testing/integration.rb', line 155

def put_via_redirect(path, *args)
  ActiveSupport::Deprecation.warn('`put_via_redirect` is deprecated and will be removed in Rails 5.1. Please use `follow_redirect!` manually after the request call for the same behavior.')
  request_via_redirect(:put, path, *args)
end

#request_via_redirect(http_method, path, *args) ⇒ Object

Performs a request using the specified method, following any subsequent redirect. Note that the redirects are followed until the response is not a redirect–this means you may run into an infinite loop if your redirect loops back to itself.

Example:

request_via_redirect :post, '/welcome',
  params: { ref_id: 14 },
  headers: { "X-Test-Header" => "testvalue" }


124
125
126
127
128
129
130
# File 'lib/action_dispatch/testing/integration.rb', line 124

def request_via_redirect(http_method, path, *args)
  ActiveSupport::Deprecation.warn('`request_via_redirect` is deprecated and will be removed in Rails 5.1. Please use `follow_redirect!` manually after the request call for the same behavior.')
  process_with_kwargs(http_method, path, *args)

  follow_redirect! while redirect?
  status
end

#xml_http_request(request_method, path, *args) ⇒ Object Also known as: xhr

Performs an XMLHttpRequest request with the given parameters, mirroring an AJAX request made from JavaScript.

The request_method is :get, :post, :patch, :put, :delete or :head; the parameters are nil, a hash, or a url-encoded or multipart string; the headers are a hash.

Example:

xhr :get, '/feed', params: { since: 201501011400 }


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/action_dispatch/testing/integration.rb', line 83

def xml_http_request(request_method, path, *args)
  if kwarg_request?(args)
    params, headers, env = args.first.values_at(:params, :headers, :env)
  else
    params = args[0]
    headers = args[1]
    env = {}

    if params.present? || headers.present?
      non_kwarg_request_warning
    end
  end

  ActiveSupport::Deprecation.warn(<<-MSG.strip_heredoc)
    xhr and xml_http_request methods are deprecated in favor of
    `get "/posts", xhr: true` and `post "/posts/1", xhr: true`.
  MSG

  process(request_method, path, params: params, headers: headers, xhr: true)
end