Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 519 lines (446 sloc) 18.015 kb
eab7120 Jeremy Kemper Performance: integration test benchmarking and profiling. [Jeremy Kempe...
jeremy authored
1 require 'stringio'
2 require 'uri'
89978f1 Xavier Noria moves Object#singleton_class to Kernel#singleton_class to match Ruby als...
fxn authored
3 require 'active_support/core_ext/kernel/singleton_class'
4f7565c Xavier Noria adds missing requires for Object#try
fxn authored
4 require 'active_support/core_ext/object/try'
ee395fe Joshua Peek TestProcess belongs in AD
josh authored
5 require 'rack/test'
a27b517 Jeremy Kemper Add missing require for minitest assertions. Breaks calling 'app' in con...
jeremy authored
6 require 'minitest/unit'
4a55d1d Joshua Peek Move integration test runner into ActionDispatch
josh authored
7
8 module ActionDispatch
dfd953e David Heinemeier Hansson Fixed docs
dhh authored
9 module Integration #:nodoc:
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
10 module RequestHelpers
11 # Performs a GET request with the given parameters.
12 #
13 # - +path+: The URI (as a String) on which you want to perform a GET
14 # request.
15 # - +parameters+: The HTTP parameters that you want to pass. This may
16 # be +nil+,
17 # a Hash, or a String that is appropriately encoded
18 # (<tt>application/x-www-form-urlencoded</tt> or
19 # <tt>multipart/form-data</tt>).
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
20 # - +headers_or_env+: Additional headers to pass, as a Hash. The headers will be
17eedd8 Mikhail Vaysman references to the old behavior removed
Vaysman authored
21 # merged into the Rack env hash.
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
22 #
dc69220 Robin Dupret Fix a typo
robin850 authored
23 # This method returns a Response object, which one can use to
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
24 # inspect the details of the response. Furthermore, if this method was
4a55d1d Joshua Peek Move integration test runner into ActionDispatch
josh authored
25 # called from an ActionDispatch::IntegrationTest object, then that
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
26 # object's <tt>@response</tt> instance variable will point to the same
27 # response object.
28 #
002713c David Lee Add config.default_method_for_update to support PATCH
dlee authored
29 # You can also perform POST, PATCH, PUT, DELETE, and HEAD requests with
30 # +#post+, +#patch+, +#put+, +#delete+, and +#head+.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
31 def get(path, parameters = nil, headers_or_env = nil)
32 process :get, path, parameters, headers_or_env
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
33 end
34
07054fe John Firebaugh Fix grammar, formatting, and cross references
jfirebaugh authored
35 # Performs a POST request with the given parameters. See +#get+ for more
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
36 # details.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
37 def post(path, parameters = nil, headers_or_env = nil)
38 process :post, path, parameters, headers_or_env
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
39 end
40
002713c David Lee Add config.default_method_for_update to support PATCH
dlee authored
41 # Performs a PATCH request with the given parameters. See +#get+ for more
42 # details.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
43 def patch(path, parameters = nil, headers_or_env = nil)
44 process :patch, path, parameters, headers_or_env
002713c David Lee Add config.default_method_for_update to support PATCH
dlee authored
45 end
46
07054fe John Firebaugh Fix grammar, formatting, and cross references
jfirebaugh authored
47 # Performs a PUT request with the given parameters. See +#get+ for more
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
48 # details.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
49 def put(path, parameters = nil, headers_or_env = nil)
50 process :put, path, parameters, headers_or_env
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
51 end
52
07054fe John Firebaugh Fix grammar, formatting, and cross references
jfirebaugh authored
53 # Performs a DELETE request with the given parameters. See +#get+ for
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
54 # more details.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
55 def delete(path, parameters = nil, headers_or_env = nil)
56 process :delete, path, parameters, headers_or_env
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
57 end
58
07054fe John Firebaugh Fix grammar, formatting, and cross references
jfirebaugh authored
59 # Performs a HEAD request with the given parameters. See +#get+ for more
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
60 # details.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
61 def head(path, parameters = nil, headers_or_env = nil)
62 process :head, path, parameters, headers_or_env
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
63 end
64
ad46884 Jeremy Kemper Integration tests support the OPTIONS http method
jeremy authored
65 # Performs a OPTIONS request with the given parameters. See +#get+ for
66 # more details.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
67 def options(path, parameters = nil, headers_or_env = nil)
68 process :options, path, parameters, headers_or_env
ad46884 Jeremy Kemper Integration tests support the OPTIONS http method
jeremy authored
69 end
70
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
71 # Performs an XMLHttpRequest request with the given parameters, mirroring
72 # a request from the Prototype library.
73 #
002713c David Lee Add config.default_method_for_update to support PATCH
dlee authored
74 # The request_method is +:get+, +:post+, +:patch+, +:put+, +:delete+ or
75 # +:head+; the parameters are +nil+, a hash, or a url-encoded or multipart
17eedd8 Mikhail Vaysman references to the old behavior removed
Vaysman authored
76 # string; the headers are a hash.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
77 def xml_http_request(request_method, path, parameters = nil, headers_or_env = nil)
78 headers_or_env ||= {}
79 headers_or_env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
80 headers_or_env['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
81 process(request_method, path, parameters, headers_or_env)
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
82 end
83 alias xhr :xml_http_request
84
85 # Follow a single redirect response. If the last response was not a
86 # redirect, an exception will be raised. Otherwise, the redirect is
87 # performed on the location header.
88 def follow_redirect!
89 raise "not a redirect! #{status} #{status_message}" unless redirect?
90 get(response.location)
91 status
92 end
93
94 # Performs a request using the specified method, following any subsequent
95 # redirect. Note that the redirects are followed until the response is
96 # not a redirect--this means you may run into an infinite loop if your
97 # redirect loops back to itself.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
98 def request_via_redirect(http_method, path, parameters = nil, headers_or_env = nil)
99 process(http_method, path, parameters, headers_or_env)
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
100 follow_redirect! while redirect?
101 status
102 end
103
104 # Performs a GET request, following any subsequent redirect.
105 # See +request_via_redirect+ for more information.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
106 def get_via_redirect(path, parameters = nil, headers_or_env = nil)
107 request_via_redirect(:get, path, parameters, headers_or_env)
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
108 end
109
110 # Performs a POST request, following any subsequent redirect.
111 # See +request_via_redirect+ for more information.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
112 def post_via_redirect(path, parameters = nil, headers_or_env = nil)
113 request_via_redirect(:post, path, parameters, headers_or_env)
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
114 end
115
002713c David Lee Add config.default_method_for_update to support PATCH
dlee authored
116 # Performs a PATCH request, following any subsequent redirect.
117 # See +request_via_redirect+ for more information.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
118 def patch_via_redirect(path, parameters = nil, headers_or_env = nil)
119 request_via_redirect(:patch, path, parameters, headers_or_env)
002713c David Lee Add config.default_method_for_update to support PATCH
dlee authored
120 end
121
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
122 # Performs a PUT request, following any subsequent redirect.
123 # See +request_via_redirect+ for more information.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
124 def put_via_redirect(path, parameters = nil, headers_or_env = nil)
125 request_via_redirect(:put, path, parameters, headers_or_env)
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
126 end
127
128 # Performs a DELETE request, following any subsequent redirect.
129 # See +request_via_redirect+ for more information.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
130 def delete_via_redirect(path, parameters = nil, headers_or_env = nil)
131 request_via_redirect(:delete, path, parameters, headers_or_env)
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
132 end
133 end
134
591e87f Joost Baaij Expanded routing documentation with current best practices
tilsammans authored
135 # An instance of this class represents a set of requests and responses
136 # performed sequentially by a test process. Because you can instantiate
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
137 # multiple sessions and run them side-by-side, you can also mimic (to some
138 # limited extent) multiple simultaneous users interacting with your system.
139 #
ebec9d4 Joshua Peek Make integration test runner more Rack friendly and clean out old CGI cr...
josh authored
140 # Typically, you will instantiate a new session using
141 # IntegrationTest#open_session, rather than instantiating
142 # Integration::Session directly.
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
143 class Session
0029d5e Bryan Helmkamp Integrating Rack::MockSession (from Rack::Test)
brynary authored
144 DEFAULT_HOST = "www.example.com"
145
94248ff Rafael Mendonça França Use MiniTest::Assertions instead of Test::Unit::Assertions
rafaelfranca authored
146 include MiniTest::Assertions
ee395fe Joshua Peek TestProcess belongs in AD
josh authored
147 include TestProcess, RequestHelpers, Assertions
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
148
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
149 %w( status status_message headers body redirect? ).each do |method|
150 delegate method, :to => :response, :allow_nil => true
151 end
5b5d0e3 Joshua Peek Use Rack::Head middleware to ensure the body is discarded for HEAD reque...
josh authored
152
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
153 %w( path ).each do |method|
154 delegate method, :to => :request, :allow_nil => true
155 end
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
156
157 # The hostname used in the last request.
570c54c Joshua Peek Fix cookie access in integration tests with other host names
josh authored
158 def host
159 @host || DEFAULT_HOST
160 end
161 attr_writer :host
fe94ba1 David Heinemeier Hansson Added access to remote_addr in integration tests (closes #4266) [Chad Fo...
dhh authored
162
163 # The remote_addr used in the last request.
164 attr_accessor :remote_addr
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
165
9d7de52 Jamis Buck Add Integration::Session.accept for setting the HTTP Accept header to us...
jamis authored
166 # The Accept header to send.
167 attr_accessor :accept
168
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
169 # A map of the cookies returned by the last response, and which will be
170 # sent with the next request.
0029d5e Bryan Helmkamp Integrating Rack::MockSession (from Rack::Test)
brynary authored
171 def cookies
570c54c Joshua Peek Fix cookie access in integration tests with other host names
josh authored
172 _mock_session.cookie_jar
0029d5e Bryan Helmkamp Integrating Rack::MockSession (from Rack::Test)
brynary authored
173 end
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
174
175 # A reference to the controller instance used by the last request.
176 attr_reader :controller
177
178 # A reference to the request instance used by the last request.
179 attr_reader :request
180
181 # A reference to the response instance used by the last request.
182 attr_reader :response
183
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
184 # A running counter of the number of requests processed.
185 attr_accessor :request_count
186
0508318 If IntegrationSession is initialized with an objects that responds to #r...
Carlhuda authored
187 include ActionDispatch::Routing::UrlFor
188
98dc582 Pratik Merge docrails.
lifo authored
189 # Create and initialize a new Session instance.
acfeec5 Joshua Peek Allow integration test rack app to be set with "@app" ivar instead of us...
josh authored
190 def initialize(app)
b1ae796 José Valim Fix an error on 1.8.7.
josevalim authored
191 super()
acfeec5 Joshua Peek Allow integration test rack app to be set with "@app" ivar instead of us...
josh authored
192 @app = app
0508318 If IntegrationSession is initialized with an objects that responds to #r...
Carlhuda authored
193
194 # If the app is a Rails app, make url_helpers available on the session
195 # This makes app.url_for and app.foo_path available in the console
6525002 Piotr Sarnacki Include routes.mounted_helpers into integration tests
drogus authored
196 if app.respond_to?(:routes)
197 singleton_class.class_eval do
198 include app.routes.url_helpers if app.routes.respond_to?(:url_helpers)
199 include app.routes.mounted_helpers if app.routes.respond_to?(:mounted_helpers)
200 end
0508318 If IntegrationSession is initialized with an objects that responds to #r...
Carlhuda authored
201 end
202
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
203 reset!
204 end
205
e306ca3 Andrew White Refactor the handling of default_url_options in integration tests
pixeltrix authored
206 def url_options
207 @url_options ||= default_url_options.dup.tap do |url_options|
208 url_options.reverse_merge!(controller.url_options) if controller
209
210 if @app.respond_to?(:routes) && @app.routes.respond_to?(:default_url_options)
211 url_options.reverse_merge!(@app.routes.default_url_options)
212 end
213
214 url_options.reverse_merge!(:host => host, :protocol => https? ? "https" : "http")
215 end
0508318 If IntegrationSession is initialized with an objects that responds to #r...
Carlhuda authored
216 end
217
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
218 # Resets the instance. This can be used to reset the state information
219 # in an existing session instance, so it can be used from a clean-slate
220 # condition.
221 #
222 # session.reset!
223 def reset!
224 @https = false
225 @controller = @request = @response = nil
570c54c Joshua Peek Fix cookie access in integration tests with other host names
josh authored
226 @_mock_session = nil
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
227 @request_count = 0
e306ca3 Andrew White Refactor the handling of default_url_options in integration tests
pixeltrix authored
228 @url_options = nil
d1c957d Jeremy Kemper Integration tests: alias xhr to xml_http_request and add a request_metho...
jeremy authored
229
0029d5e Bryan Helmkamp Integrating Rack::MockSession (from Rack::Test)
brynary authored
230 self.host = DEFAULT_HOST
fe94ba1 David Heinemeier Hansson Added access to remote_addr in integration tests (closes #4266) [Chad Fo...
dhh authored
231 self.remote_addr = "127.0.0.1"
ebec9d4 Joshua Peek Make integration test runner more Rack friendly and clean out old CGI cr...
josh authored
232 self.accept = "text/xml,application/xml,application/xhtml+xml," +
233 "text/html;q=0.9,text/plain;q=0.8,image/png," +
234 "*/*;q=0.5"
0fbe683 Jamis Buck more integration tweaks
jamis authored
235
0eacdcf Jeremy Kemper Use a consistent load path to avoid double requires. Fix some scattered ...
jeremy authored
236 unless defined? @named_routes_configured
0fbe683 Jamis Buck more integration tweaks
jamis authored
237 # the helpers are made protected by default--we make them public for
238 # easier access during testing and troubleshooting.
239 @named_routes_configured = true
240 end
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
241 end
242
243 # Specify whether or not the session should mimic a secure HTTPS request.
244 #
245 # session.https!
246 # session.https!(false)
ebec9d4 Joshua Peek Make integration test runner more Rack friendly and clean out old CGI cr...
josh authored
247 def https!(flag = true)
d1c957d Jeremy Kemper Integration tests: alias xhr to xml_http_request and add a request_metho...
jeremy authored
248 @https = flag
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
249 end
250
0432d15 Pratik Merge with docrails.
lifo authored
251 # Return +true+ if the session is mimicking a secure HTTPS request.
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
252 #
253 # if session.https?
254 # ...
255 # end
256 def https?
257 @https
258 end
259
260 # Set the host name to use in the next request.
261 #
9326222 David Heinemeier Hansson Use example.com as standard (closes #4413) [anna]
dhh authored
262 # session.host! "www.example.com"
831a234 Aaron Patterson just use alias
tenderlove authored
263 alias :host! :host=
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
264
265 private
570c54c Joshua Peek Fix cookie access in integration tests with other host names
josh authored
266 def _mock_session
267 @_mock_session ||= Rack::MockSession.new(@app, host)
268 end
0029d5e Bryan Helmkamp Integrating Rack::MockSession (from Rack::Test)
brynary authored
269
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
270 # Performs the actual request.
9af59b2 Yves Senn allow headers and env to be passed in `IntegrationTest`.
senny authored
271 def process(method, path, parameters = nil, headers_or_env = nil)
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
272 if path =~ %r{://}
273 location = URI.parse(path)
274 https! URI::HTTPS === location if location.scheme
b28fc68 Andrew White Ensure port is set when passed via the process method
pixeltrix authored
275 host! "#{location.host}:#{location.port}" if location.host
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
276 path = location.query ? "#{location.path}?#{location.query}" : location.path
277 end
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
278
909443e Joshua Peek Expose last controller in rack env["action_controller.instance"]
josh authored
279 unless ActionController::Base < ActionController::Testing
280 ActionController::Base.class_eval do
281 include ActionController::Testing
dd2ed32 Joshua Peek Start to integrate some of the features in Rack::Test.
josh authored
282 end
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
283 end
284
29f13be Santiago Pastorino port is appended twice to HTTP_HOST when host already has the port
spastorino authored
285 hostname, port = host.split(':')
de9b338 Aaron Patterson fixing bug with rails use of rack-test
tenderlove authored
286
3de95fd Santiago Pastorino Revert "Make process reuse the env var passed as argument"
spastorino authored
287 env = {
00d1a57 Joshua Peek Start moving TestRequest and TestResponse into ActionDispatch
josh authored
288 :method => method,
dd2ed32 Joshua Peek Start to integrate some of the features in Rack::Test.
josh authored
289 :params => parameters,
ebec9d4 Joshua Peek Make integration test runner more Rack friendly and clean out old CGI cr...
josh authored
290
29f13be Santiago Pastorino port is appended twice to HTTP_HOST when host already has the port
spastorino authored
291 "SERVER_NAME" => hostname,
56de4e9 Santiago Pastorino Fix the precedence issue here
spastorino authored
292 "SERVER_PORT" => port || (https? ? "443" : "80"),
ebec9d4 Joshua Peek Make integration test runner more Rack friendly and clean out old CGI cr...
josh authored
293 "HTTPS" => https? ? "on" : "off",
294 "rack.url_scheme" => https? ? "https" : "http",
295
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
296 "REQUEST_URI" => path,
29f13be Santiago Pastorino port is appended twice to HTTP_HOST when host already has the port
spastorino authored
297 "HTTP_HOST" => host,
fe94ba1 David Heinemeier Hansson Added access to remote_addr in integration tests (closes #4266) [Chad Fo...
dhh authored
298 "REMOTE_ADDR" => remote_addr,
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
299 "CONTENT_TYPE" => "application/x-www-form-urlencoded",
997e22c José Valim Add a test which ensures action_dispatch.show_exceptions is properly dis...
josevalim authored
300 "HTTP_ACCEPT" => accept
dd2ed32 Joshua Peek Start to integrate some of the features in Rack::Test.
josh authored
301 }
41a14dc Yves Senn `Http::Headers` directly modifies the passed environment.
senny authored
302 # this modifies the passed env directly
303 Http::Headers.new(env).merge!(headers_or_env || {})
5e5e343 Joshua Peek Back off rack 1.1-pre and bundle in the new testing goodies
josh authored
304
570c54c Joshua Peek Fix cookie access in integration tests with other host names
josh authored
305 session = Rack::Test::Session.new(_mock_session)
ab8bf9e Yehuda Katz * Change the object used in routing constraints to be an instance of
wycats authored
306
41a14dc Yves Senn `Http::Headers` directly modifies the passed environment.
senny authored
307 env.merge!(env)
d63b42d Joshua Peek Move header injection back into integration tests
josh authored
308
570c54c Joshua Peek Fix cookie access in integration tests with other host names
josh authored
309 # NOTE: rack-test v0.5 doesn't build a default uri correctly
310 # Make sure requested path is always a full uri
311 uri = URI.parse('/')
312 uri.scheme ||= env['rack.url_scheme']
313 uri.host ||= env['SERVER_NAME']
314 uri.port ||= env['SERVER_PORT'].try(:to_i)
315 uri += path
316
317 session.request(uri.to_s, env)
6940c0d Joshua Peek Unify functional and integration tests cookie helpers
josh authored
318
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
319 @request_count += 1
a79790e Joshua Peek rack-test 0.4.2 has rack 1.1.pre goodies, we'll use it instead
josh authored
320 @request = ActionDispatch::Request.new(session.last_request.env)
570c54c Joshua Peek Fix cookie access in integration tests with other host names
josh authored
321 response = _mock_session.last_response
a3c6ad7 Fix a bunch of pending tests by providing an introspection mode for the ...
Carlhuda authored
322 @response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body)
6940c0d Joshua Peek Unify functional and integration tests cookie helpers
josh authored
323 @html_document = nil
e306ca3 Andrew White Refactor the handling of default_url_options in integration tests
pixeltrix authored
324 @url_options = nil
97a178b Jesse Newland Decorate responses from Rack Middleware and Rails Metal for the purposes...
jnewland authored
325
909443e Joshua Peek Expose last controller in rack env["action_controller.instance"]
josh authored
326 @controller = session.last_request.env['action_controller.instance']
327
9bac470 Joshua Peek Group integration test helpers and delegate other helpers to request and...
josh authored
328 return response.status
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
329 end
330 end
331
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
332 module Runner
0e15f07 Get modules back into integration tests
Carlhuda authored
333 include ActionDispatch::Assertions
334
14866fa Joshua Peek Allow setting a default application for all integration tests
josh authored
335 def app
b8c565f Emilio Tagua Initialize @app if it doesn't exists.
miloops authored
336 @app ||= nil
14866fa Joshua Peek Allow setting a default application for all integration tests
josh authored
337 end
338
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
339 # Reset the current session. This is useful for testing multiple sessions
340 # in a single test case.
341 def reset!
15c31c7 Yehuda Katz open_session can just return the a dup of the current context.
wycats authored
342 @integration_session = Integration::Session.new(app)
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
343 end
344
3008188 Xavier Noria consistently mention first patch, then put
fxn authored
345 %w(get post patch put head delete options cookies assigns
71c4ff0 Joshua Peek Delegate xhr helper method to integration session
josh authored
346 xml_http_request xhr get_via_redirect post_via_redirect).each do |method|
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
347 define_method(method) do |*args|
53b91b1 Emilio Tagua Avoid uninitialized variable warning, reuse @integration_session.
miloops authored
348 reset! unless integration_session
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
349 # reset the html_document variable, but only for new get/post calls
447b6a4 Xavier Noria removes usage of Object#in? from the code base (the method remains defin...
fxn authored
350 @html_document = nil unless method == 'cookies' || method == 'assigns'
53b91b1 Emilio Tagua Avoid uninitialized variable warning, reuse @integration_session.
miloops authored
351 integration_session.__send__(method, *args).tap do
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
352 copy_session_variables!
353 end
354 end
355 end
356
357 # Open a new session instance. If a block is given, the new session is
358 # yielded to the block before being returned.
359 #
360 # session = open_session do |sess|
361 # sess.extend(CustomAssertions)
362 # end
363 #
364 # By default, a single session is automatically created for you, but you
365 # can use this method to open multiple sessions that ought to be tested
366 # simultaneously.
dd2ed32 Joshua Peek Start to integrate some of the features in Rack::Test.
josh authored
367 def open_session(app = nil)
15c31c7 Yehuda Katz open_session can just return the a dup of the current context.
wycats authored
368 dup.tap do |session|
369 yield session if block_given?
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
370 end
371 end
372
373 # Copy the instance variables from the current session instance into the
374 # test instance.
375 def copy_session_variables! #:nodoc:
53b91b1 Emilio Tagua Avoid uninitialized variable warning, reuse @integration_session.
miloops authored
376 return unless integration_session
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
377 %w(controller response request).each do |var|
a1eb4e1 Jeremy Kemper Get rid of 'Object#send!'. It was originally added because it's in Ruby ...
jeremy authored
378 instance_variable_set("@#{var}", @integration_session.__send__(var))
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
379 end
380 end
381
e306ca3 Andrew White Refactor the handling of default_url_options in integration tests
pixeltrix authored
382 def default_url_options
383 reset! unless integration_session
384 integration_session.default_url_options
385 end
8760add Get URL helpers working again in integration tests.
Carlhuda authored
386
e306ca3 Andrew White Refactor the handling of default_url_options in integration tests
pixeltrix authored
387 def default_url_options=(options)
53b91b1 Emilio Tagua Avoid uninitialized variable warning, reuse @integration_session.
miloops authored
388 reset! unless integration_session
e306ca3 Andrew White Refactor the handling of default_url_options in integration tests
pixeltrix authored
389 integration_session.default_url_options = options
8760add Get URL helpers working again in integration tests.
Carlhuda authored
390 end
391
5c86286 Sven Fuchs add respond_to? to ActionDispatch::Integration::Runner
svenfuchs authored
392 def respond_to?(method, include_private = false)
da583df José Valim Remove bazillion warnings from AP suite.
josevalim authored
393 integration_session.respond_to?(method, include_private) || super
5c86286 Sven Fuchs add respond_to? to ActionDispatch::Integration::Runner
svenfuchs authored
394 end
395
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
396 # Delegate unhandled messages to the current session instance.
397 def method_missing(sym, *args, &block)
53b91b1 Emilio Tagua Avoid uninitialized variable warning, reuse @integration_session.
miloops authored
398 reset! unless integration_session
399 if integration_session.respond_to?(sym)
400 integration_session.__send__(sym, *args, &block).tap do
3de8b44 George Ogata Make IntegrationTest::Runner propagate method_missing to ancestors.
oggy authored
401 copy_session_variables!
402 end
403 else
404 super
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
405 end
406 end
53b91b1 Emilio Tagua Avoid uninitialized variable warning, reuse @integration_session.
miloops authored
407
408 private
409 def integration_session
410 @integration_session ||= nil
411 end
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
412 end
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
413 end
414
07054fe John Firebaugh Fix grammar, formatting, and cross references
jfirebaugh authored
415 # An integration test spans multiple controllers and actions,
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
416 # tying them all together to ensure they work together as expected. It tests
417 # more completely than either unit or functional tests do, exercising the
418 # entire stack, from the dispatcher to the database.
419 #
591e87f Joost Baaij Expanded routing documentation with current best practices
tilsammans authored
420 # At its simplest, you simply extend <tt>IntegrationTest</tt> and write your tests
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
421 # using the get/post methods:
422 #
fe5f660 David Heinemeier Hansson Dont encourage __FILE__ bullshit
dhh authored
423 # require "test_helper"
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
424 #
7fc1edd Carlos Antonio da Silva Remove deprecated stuff in ActionController
carlosantoniodasilva authored
425 # class ExampleTest < ActionDispatch::IntegrationTest
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
426 # fixtures :people
427 #
428 # def test_login
429 # # get the login page
430 # get "/login"
431 # assert_equal 200, status
432 #
433 # # post the login and follow through to the home page
d20a529 Avner Cohen 1.9 hash syntax changes to docs
AvnerCohen authored
434 # post "/login", username: people(:jamis).username,
435 # password: people(:jamis).password
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
436 # follow_redirect!
437 # assert_equal 200, status
438 # assert_equal "/home", path
439 # end
440 # end
441 #
442 # However, you can also have multiple session instances open per test, and
443 # even extend those instances with assertions and methods to create a very
444 # powerful testing DSL that is specific for your application. You can even
591e87f Joost Baaij Expanded routing documentation with current best practices
tilsammans authored
445 # reference any named routes you happen to have defined.
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
446 #
fe5f660 David Heinemeier Hansson Dont encourage __FILE__ bullshit
dhh authored
447 # require "test_helper"
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
448 #
7fc1edd Carlos Antonio da Silva Remove deprecated stuff in ActionController
carlosantoniodasilva authored
449 # class AdvancedTest < ActionDispatch::IntegrationTest
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
450 # fixtures :people, :rooms
451 #
452 # def test_login_and_speak
453 # jamis, david = login(:jamis), login(:david)
454 # room = rooms(:office)
455 #
456 # jamis.enter(room)
457 # jamis.speak(room, "anybody home?")
458 #
459 # david.enter(room)
460 # david.speak(room, "hello!")
461 # end
462 #
463 # private
464 #
465 # module CustomAssertions
466 # def enter(room)
467 # # reference a named route, for maximum internal consistency!
d20a529 Avner Cohen 1.9 hash syntax changes to docs
AvnerCohen authored
468 # get(room_url(id: room.id))
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
469 # assert(...)
470 # ...
471 # end
472 #
473 # def speak(room, message)
d20a529 Avner Cohen 1.9 hash syntax changes to docs
AvnerCohen authored
474 # xml_http_request "/say/#{room.id}", message: message
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
475 # assert(...)
476 # ...
477 # end
478 # end
479 #
480 # def login(who)
481 # open_session do |sess|
482 # sess.extend(CustomAssertions)
483 # who = people(who)
d20a529 Avner Cohen 1.9 hash syntax changes to docs
AvnerCohen authored
484 # sess.post "/login", username: who.username,
485 # password: who.password
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
486 # assert(...)
487 # end
488 # end
489 # end
eab7120 Jeremy Kemper Performance: integration test benchmarking and profiling. [Jeremy Kempe...
jeremy authored
490 class IntegrationTest < ActiveSupport::TestCase
8231460 Jeremy Kemper Factor Integration::Runner behavior out of IntegrationTest. Introduce Se...
jeremy authored
491 include Integration::Runner
0e15f07 Get modules back into integration tests
Carlhuda authored
492 include ActionController::TemplateAssertions
e306ca3 Andrew White Refactor the handling of default_url_options in integration tests
pixeltrix authored
493 include ActionDispatch::Routing::UrlFor
14866fa Joshua Peek Allow setting a default application for all integration tests
josh authored
494
495 @@app = nil
496
497 def self.app
5f3b9de Sergey Nartimov remove Rails application fallback from AD::IntegrationTest
lest authored
498 if !@@app && !ActionDispatch.test_app
b955939 Alexey Gaziev Make caller attribute in deprecation methods optional
gazay authored
499 ActiveSupport::Deprecation.warn "Rails application fallback is deprecated and no longer works, please set ActionDispatch.test_app"
5f3b9de Sergey Nartimov remove Rails application fallback from AD::IntegrationTest
lest authored
500 end
501
502 @@app || ActionDispatch.test_app
14866fa Joshua Peek Allow setting a default application for all integration tests
josh authored
503 end
504
505 def self.app=(app)
506 @@app = app
507 end
508
509 def app
510 super || self.class.app
511 end
e306ca3 Andrew White Refactor the handling of default_url_options in integration tests
pixeltrix authored
512
513 def url_options
514 reset! unless integration_session
515 integration_session.url_options
516 end
9507f5d Jamis Buck Add ActionController::IntegrationTest to allow high-level testing of the...
jamis authored
517 end
0ee1cb2 Jeremy Kemper Ruby 1.9 compat, consistent load paths
jeremy authored
518 end
Something went wrong with that request. Please try again.