Module: ActionDispatch::Integration::RequestHelpers

Included in:
Session
Defined in:
actionpack/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 #process for more details.


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

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.


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

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.


71
72
73
74
75
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 71

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

#get(path, *args) ⇒ Object


14
15
16
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 14

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.


97
98
99
100
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 97

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 #process for more details.


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

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

#patch(path, *args) ⇒ Object

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


26
27
28
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 26

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.


111
112
113
114
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 111

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 #process for more details.


20
21
22
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 20

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.


104
105
106
107
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 104

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 #process for more details.


32
33
34
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 32

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.


118
119
120
121
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 118

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" }

87
88
89
90
91
92
93
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 87

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, parameters = nil, headers_or_env = nil) ⇒ 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', since: 201501011400

58
59
60
61
62
63
64
65
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 58

def xml_http_request(request_method, path, parameters = nil, headers_or_env = nil)
  ActiveSupport::Deprecation.warn(<<-MSG.strip_heredoc)
    `xhr` and `xml_http_request` are deprecated and will be removed in Rails 5.1.
    Switch to e.g. `post comments_path, params: { comment: { body: 'Honey bunny' } }, xhr: true`.
  MSG

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