Class: ActionController::Integration::Session

Inherits:
Object
  • Object
show all
Includes:
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: MockCGI

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 Test::Unit::Assertions

#assert_cookie_equal, #assert_dom_equal, #assert_dom_not_equal, #assert_flash_empty, #assert_flash_equal, #assert_flash_exists, #assert_flash_has, #assert_flash_has_no, #assert_flash_not_empty, #assert_flash_not_exists, #assert_generates, #assert_invalid_column_on_record, #assert_invalid_record, #assert_no_cookie, #assert_no_tag, #assert_recognizes, #assert_redirect, #assert_redirect_url, #assert_redirect_url_match, #assert_redirected_to, #assert_rendered_file, #assert_response, #assert_routing, #assert_session_equal, #assert_session_has, #assert_session_has_no, #assert_success, #assert_tag, #assert_template, #assert_template_equal, #assert_template_has, #assert_template_has_no, #assert_template_xpath_match, #assert_valid, #assert_valid_column_on_record, #assert_valid_record, #clean_backtrace

Constructor Details

#initializeSession

Create an initialize a new Session instance.



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

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.



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

def accept
  @accept
end

#controllerObject (readonly)

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



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

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.



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

def cookies
  @cookies
end

#headersObject (readonly)

A map of the headers returned by the last response.



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

def headers
  @headers
end

#hostObject

The hostname used in the last request.



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

def host
  @host
end

#pathObject (readonly)

The URI of the last request.



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

def path
  @path
end

#remote_addrObject

The remote_addr used in the last request.



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

def remote_addr
  @remote_addr
end

#requestObject (readonly)

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



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

def request
  @request
end

#responseObject (readonly)

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



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

def response
  @response
end

#statusObject (readonly)

The integer HTTP status code of the last request.



19
20
21
# File 'lib/action_controller/integration.rb', line 19

def status
  @status
end

#status_messageObject (readonly)

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



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

def status_message
  @status_message
end

Instance Method Details

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



112
113
114
115
116
# File 'lib/action_controller/integration.rb', line 112

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



144
145
146
# File 'lib/action_controller/integration.rb', line 144

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

#get_via_redirect(path, args = {}) ⇒ Object

Performs a GET request, 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.



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

def get_via_redirect(path, args={})
  get path, args
  follow_redirect! while redirect?
  status
end

#host!(name) ⇒ Object

Set the host name to use in the next request.

session.host! "www.example.com"


105
106
107
# File 'lib/action_controller/integration.rb', line 105

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)


89
90
91
# File 'lib/action_controller/integration.rb', line 89

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)


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

def https?
  @https
end

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

Performs a POST 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).



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

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

#post_via_redirect(path, args = {}) ⇒ Object

Performs a POST request, following any subsequent redirect. This is vulnerable to infinite loops, the same as #get_via_redirect.



130
131
132
133
134
# File 'lib/action_controller/integration.rb', line 130

def post_via_redirect(path, args={})
  post path, args
  follow_redirect! while redirect?
  status
end

#redirect?Boolean

Returns true if the last response was a redirect.

Returns:

  • (Boolean)


137
138
139
# File 'lib/action_controller/integration.rb', line 137

def redirect?
  status/100 == 3
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!


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/action_controller/integration.rb', line 62

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

  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 @named_routes_configured
    # install the named routes in this session instance.
    klass = class<<self; self; end
    Routing::NamedRoutes.install(klass)

    # the helpers are made protected by default--we make them public for
    # easier access during testing and troubleshooting.
    klass.send(:public, *Routing::NamedRoutes::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.



166
167
168
# File 'lib/action_controller/integration.rb', line 166

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

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

Performs an XMLHttpRequest request with the given parameters, mimicing the request environment created by the Prototype library. The parameters may be nil, a Hash, or a string that is appropriately encoded (application/x-www-form-urlencoded or multipart/form-data).



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

def xml_http_request(path, parameters=nil, headers=nil)
  headers = (headers || {}).merge("X-Requested-With" => "XMLHttpRequest")
  post(path, parameters, headers)
end