Class: ActionController::Integration::Session

Inherits:
Object
  • Object
show all
Includes:
Assertions, TestProcess, Test::Unit::Assertions
Defined in:
lib/action_controller/integration.rb

Overview

An integration Session instance represents a set of requests and responses performed sequentially by some virtual user. Becase you can instantiate multiple sessions and run them side-by-side, you can also mimic (to some limited extent) multiple simultaneous users interacting with your system.

Typically, you will instantiate a new session using IntegrationTest#open_session, rather than instantiating Integration::Session directly.

Defined Under Namespace

Classes: MultiPartNeededException, StubCGI

Constant Summary

Constants included from Assertions

Assertions::NO_STRIP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TestProcess

#assigns, #build_request_uri, #find_all_tag, #find_tag, #fixture_file_upload, #flash, #follow_redirect, #html_document, included, #method_missing, #redirect_to_url, #session, #with_routing

Methods included from Assertions

#clean_backtrace, included

Constructor Details

#initializeSession

Create and initialize a new Session instance.



62
63
64
# File 'lib/action_controller/integration.rb', line 62

def initialize
  reset!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActionController::TestProcess

Instance Attribute Details

#acceptObject

The Accept header to send.



37
38
39
# File 'lib/action_controller/integration.rb', line 37

def accept
  @accept
end

#controllerObject (readonly)

A reference to the controller instance used by the last request.



47
48
49
# File 'lib/action_controller/integration.rb', line 47

def controller
  @controller
end

#cookiesObject (readonly)

A map of the cookies returned by the last response, and which will be sent with the next request.



41
42
43
# File 'lib/action_controller/integration.rb', line 41

def cookies
  @cookies
end

#headersObject (readonly)

A map of the headers returned by the last response.



44
45
46
# File 'lib/action_controller/integration.rb', line 44

def headers
  @headers
end

#hostObject

The hostname used in the last request.



31
32
33
# File 'lib/action_controller/integration.rb', line 31

def host
  @host
end

#pathObject (readonly)

The URI of the last request.



28
29
30
# File 'lib/action_controller/integration.rb', line 28

def path
  @path
end

#remote_addrObject

The remote_addr used in the last request.



34
35
36
# File 'lib/action_controller/integration.rb', line 34

def remote_addr
  @remote_addr
end

#requestObject (readonly)

A reference to the request instance used by the last request.



50
51
52
# File 'lib/action_controller/integration.rb', line 50

def request
  @request
end

#request_countObject

A running counter of the number of requests processed.



56
57
58
# File 'lib/action_controller/integration.rb', line 56

def request_count
  @request_count
end

#responseObject (readonly)

A reference to the response instance used by the last request.



53
54
55
# File 'lib/action_controller/integration.rb', line 53

def response
  @response
end

#statusObject (readonly)

The integer HTTP status code of the last request.



22
23
24
# File 'lib/action_controller/integration.rb', line 22

def status
  @status
end

#status_messageObject (readonly)

The status message that accompanied the status code of the last request.



25
26
27
# File 'lib/action_controller/integration.rb', line 25

def status_message
  @status_message
end

Instance Method Details

#delete(path, parameters = nil, headers = nil) ⇒ Object

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



190
191
192
# File 'lib/action_controller/integration.rb', line 190

def delete(path, parameters = nil, headers = nil)
  process :delete, path, parameters, headers
end

#delete_via_redirect(path, parameters = nil, headers = nil) ⇒ Object

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



158
159
160
# File 'lib/action_controller/integration.rb', line 158

def delete_via_redirect(path, parameters = nil, headers = nil)
  request_via_redirect(:delete, path, parameters, headers)
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.



122
123
124
125
126
# File 'lib/action_controller/integration.rb', line 122

def follow_redirect!
  raise "not a redirect! #{@status} #{@status_message}" unless redirect?
  get(interpret_uri(headers['location'].first))
  status
end

#get(path, parameters = nil, headers = nil) ⇒ Object

Performs a GET request with the given parameters. The parameters may be nil, a Hash, or a string that is appropriately encoded (application/x-www-form-urlencoded or multipart/form-data). The headers should be a hash. The keys will automatically be upcased, with the prefix ‘HTTP_’ added if needed.

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



175
176
177
# File 'lib/action_controller/integration.rb', line 175

def get(path, parameters = nil, headers = nil)
  process :get, path, parameters, headers
end

#get_via_redirect(path, parameters = nil, headers = nil) ⇒ Object

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



140
141
142
# File 'lib/action_controller/integration.rb', line 140

def get_via_redirect(path, parameters = nil, headers = nil)
  request_via_redirect(:get, path, parameters, headers)
end

#head(path, parameters = nil, headers = nil) ⇒ Object

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



195
196
197
# File 'lib/action_controller/integration.rb', line 195

def head(path, parameters = nil, headers = nil)
  process :head, path, parameters, headers
end

#host!(name) ⇒ Object

Set the host name to use in the next request.

session.host! "www.example.com"


115
116
117
# File 'lib/action_controller/integration.rb', line 115

def host!(name)
  @host = name
end

#https!(flag = true) ⇒ Object

Specify whether or not the session should mimic a secure HTTPS request.

session.https!
session.https!(false)


99
100
101
# File 'lib/action_controller/integration.rb', line 99

def https!(flag=true)
  @https = flag
end

#https?Boolean

Return true if the session is mimicing a secure HTTPS request.

if session.https?
  ...
end

Returns:

  • (Boolean)


108
109
110
# File 'lib/action_controller/integration.rb', line 108

def https?
  @https
end

#post(path, parameters = nil, headers = nil) ⇒ Object

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



180
181
182
# File 'lib/action_controller/integration.rb', line 180

def post(path, parameters = nil, headers = nil)
  process :post, path, parameters, headers
end

#post_via_redirect(path, parameters = nil, headers = nil) ⇒ Object

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



146
147
148
# File 'lib/action_controller/integration.rb', line 146

def post_via_redirect(path, parameters = nil, headers = nil)
  request_via_redirect(:post, path, parameters, headers)
end

#put(path, parameters = nil, headers = nil) ⇒ Object

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



185
186
187
# File 'lib/action_controller/integration.rb', line 185

def put(path, parameters = nil, headers = nil)
  process :put, path, parameters, headers
end

#put_via_redirect(path, parameters = nil, headers = nil) ⇒ Object

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



152
153
154
# File 'lib/action_controller/integration.rb', line 152

def put_via_redirect(path, parameters = nil, headers = nil)
  request_via_redirect(:put, path, parameters, headers)
end

#redirect?Boolean

Returns true if the last response was a redirect.

Returns:

  • (Boolean)


163
164
165
# File 'lib/action_controller/integration.rb', line 163

def redirect?
  status/100 == 3
end

#request_via_redirect(http_method, path, parameters = nil, headers = nil) ⇒ 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.



132
133
134
135
136
# File 'lib/action_controller/integration.rb', line 132

def request_via_redirect(http_method, path, parameters = nil, headers = nil)
  send(http_method, path, parameters, headers)
  follow_redirect! while redirect?
  status
end

#reset!Object

Resets the instance. This can be used to reset the state information in an existing session instance, so it can be used from a clean-slate condition.

session.reset!


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/action_controller/integration.rb', line 71

def reset!
  @status = @path = @headers = nil
  @result = @status_message = nil
  @https = false
  @cookies = {}
  @controller = @request = @response = nil
  @request_count = 0

  self.host        = "www.example.com"
  self.remote_addr = "127.0.0.1"
  self.accept      = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"

  unless defined? @named_routes_configured
    # install the named routes in this session instance.
    klass = class<<self; self; end
    Routing::Routes.install_helpers(klass)

    # the helpers are made protected by default--we make them public for
    # easier access during testing and troubleshooting.
    klass.module_eval { public *Routing::Routes.named_routes.helpers }
    @named_routes_configured = true
  end
end

#url_for(options) ⇒ Object

Returns the URL for the given options, according to the rules specified in the application’s routes.



217
218
219
# File 'lib/action_controller/integration.rb', line 217

def url_for(options)
  controller ? controller.url_for(options) : generic_url_rewriter.rewrite(options)
end

#xml_http_request(request_method, path, parameters = nil, headers = nil) ⇒ Object Also known as: xhr

Performs an XMLHttpRequest request with the given parameters, mirroring a request from the Prototype library.

The request_method is :get, :post, :put, :delete or :head; the parameters are nil, a hash, or a url-encoded or multipart string; the headers are a hash. Keys are automatically upcased and prefixed with ‘HTTP_’ if not already.



206
207
208
209
210
211
212
# File 'lib/action_controller/integration.rb', line 206

def xml_http_request(request_method, path, parameters = nil, headers = nil)
  headers ||= {}
  headers['X-Requested-With'] = 'XMLHttpRequest'
  headers['Accept'] ||= 'text/javascript, text/html, application/xml, text/xml, */*'

  process(request_method, path, parameters, headers)
end