actionpack 3.2.14.rc2 → 3.2.14
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of actionpack might be problematic. Click here for more details.
- data/CHANGELOG.md +1 -6
- data/lib/action_pack/version.rb +1 -1
- metadata +12 -22
- data/lib/action_controller/test_case.rb.orig +0 -707
- data/lib/action_dispatch/testing/integration.rb.orig +0 -525
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,4 @@
|
|
1
|
-
## Rails 3.2.14
|
2
|
-
|
3
|
-
* No changes.
|
4
|
-
|
5
|
-
|
6
|
-
## Rails 3.2.14.rc1 (Jul 12, 2013) ##
|
1
|
+
## Rails 3.2.14 (Jul 22, 2013) ##
|
7
2
|
|
8
3
|
* Merge `:action` from routing scope and assign endpoint if both `:controller`
|
9
4
|
and `:action` are present. The endpoint assignment only occurs if there is
|
data/lib/action_pack/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: actionpack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 3
|
8
8
|
- 2
|
9
9
|
- 14
|
10
|
-
|
11
|
-
- 2
|
12
|
-
version: 3.2.14.rc2
|
10
|
+
version: 3.2.14
|
13
11
|
platform: ruby
|
14
12
|
authors:
|
15
13
|
- David Heinemeier Hansson
|
@@ -17,7 +15,7 @@ autorequire:
|
|
17
15
|
bindir: bin
|
18
16
|
cert_chain: []
|
19
17
|
|
20
|
-
date: 2013-07-
|
18
|
+
date: 2013-07-22 00:00:00 -03:00
|
21
19
|
default_executable:
|
22
20
|
dependencies:
|
23
21
|
- !ruby/object:Gem::Dependency
|
@@ -26,14 +24,12 @@ dependencies:
|
|
26
24
|
requirements:
|
27
25
|
- - "="
|
28
26
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
27
|
+
hash: 19
|
30
28
|
segments:
|
31
29
|
- 3
|
32
30
|
- 2
|
33
31
|
- 14
|
34
|
-
|
35
|
-
- 2
|
36
|
-
version: 3.2.14.rc2
|
32
|
+
version: 3.2.14
|
37
33
|
type: :runtime
|
38
34
|
name: activesupport
|
39
35
|
version_requirements: *id001
|
@@ -44,14 +40,12 @@ dependencies:
|
|
44
40
|
requirements:
|
45
41
|
- - "="
|
46
42
|
- !ruby/object:Gem::Version
|
47
|
-
hash:
|
43
|
+
hash: 19
|
48
44
|
segments:
|
49
45
|
- 3
|
50
46
|
- 2
|
51
47
|
- 14
|
52
|
-
|
53
|
-
- 2
|
54
|
-
version: 3.2.14.rc2
|
48
|
+
version: 3.2.14
|
55
49
|
type: :runtime
|
56
50
|
name: activemodel
|
57
51
|
version_requirements: *id002
|
@@ -250,7 +244,6 @@ files:
|
|
250
244
|
- lib/action_controller/railties/paths.rb
|
251
245
|
- lib/action_controller/record_identifier.rb
|
252
246
|
- lib/action_controller/test_case.rb
|
253
|
-
- lib/action_controller/test_case.rb.orig
|
254
247
|
- lib/action_controller/vendor/html-scanner/html/document.rb
|
255
248
|
- lib/action_controller/vendor/html-scanner/html/node.rb
|
256
249
|
- lib/action_controller/vendor/html-scanner/html/sanitizer.rb
|
@@ -316,7 +309,6 @@ files:
|
|
316
309
|
- lib/action_dispatch/testing/assertions/tag.rb
|
317
310
|
- lib/action_dispatch/testing/assertions.rb
|
318
311
|
- lib/action_dispatch/testing/integration.rb
|
319
|
-
- lib/action_dispatch/testing/integration.rb.orig
|
320
312
|
- lib/action_dispatch/testing/performance_test.rb
|
321
313
|
- lib/action_dispatch/testing/test_process.rb
|
322
314
|
- lib/action_dispatch/testing/test_request.rb
|
@@ -408,14 +400,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
408
400
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
409
401
|
none: false
|
410
402
|
requirements:
|
411
|
-
- - "
|
403
|
+
- - ">="
|
412
404
|
- !ruby/object:Gem::Version
|
413
|
-
hash:
|
405
|
+
hash: 3
|
414
406
|
segments:
|
415
|
-
-
|
416
|
-
|
417
|
-
- 1
|
418
|
-
version: 1.3.1
|
407
|
+
- 0
|
408
|
+
version: "0"
|
419
409
|
requirements:
|
420
410
|
- none
|
421
411
|
rubyforge_project:
|
@@ -1,707 +0,0 @@
|
|
1
|
-
require 'rack/session/abstract/id'
|
2
|
-
require 'active_support/core_ext/object/to_query'
|
3
|
-
require 'active_support/core_ext/module/anonymous'
|
4
|
-
require 'active_support/core_ext/hash/keys'
|
5
|
-
|
6
|
-
module ActionController
|
7
|
-
module TemplateAssertions
|
8
|
-
extend ActiveSupport::Concern
|
9
|
-
|
10
|
-
included do
|
11
|
-
setup :setup_subscriptions
|
12
|
-
teardown :teardown_subscriptions
|
13
|
-
end
|
14
|
-
|
15
|
-
def setup_subscriptions
|
16
|
-
@_partials = Hash.new(0)
|
17
|
-
@_templates = Hash.new(0)
|
18
|
-
@_layouts = Hash.new(0)
|
19
|
-
@_files = Hash.new(0)
|
20
|
-
|
21
|
-
ActiveSupport::Notifications.subscribe("render_template.action_view") do |_name, _start, _finish, _id, payload|
|
22
|
-
path = payload[:layout]
|
23
|
-
if path
|
24
|
-
@_layouts[path] += 1
|
25
|
-
if path =~ /^layouts\/(.*)/
|
26
|
-
@_layouts[$1] += 1
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
ActiveSupport::Notifications.subscribe("!render_template.action_view") do |_name, _start, _finish, _id, payload|
|
32
|
-
path = payload[:virtual_path]
|
33
|
-
next unless path
|
34
|
-
partial = path =~ /^.*\/_[^\/]*$/
|
35
|
-
|
36
|
-
if partial
|
37
|
-
@_partials[path] += 1
|
38
|
-
@_partials[path.split("/").last] += 1
|
39
|
-
end
|
40
|
-
|
41
|
-
@_templates[path] += 1
|
42
|
-
end
|
43
|
-
|
44
|
-
ActiveSupport::Notifications.subscribe("!render_template.action_view") do |_name, _start, _finish, _id, payload|
|
45
|
-
next if payload[:virtual_path] # files don't have virtual path
|
46
|
-
|
47
|
-
path = payload[:identifier]
|
48
|
-
if path
|
49
|
-
@_files[path] += 1
|
50
|
-
@_files[path.split("/").last] += 1
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def teardown_subscriptions
|
56
|
-
ActiveSupport::Notifications.unsubscribe("render_template.action_view")
|
57
|
-
ActiveSupport::Notifications.unsubscribe("!render_template.action_view")
|
58
|
-
end
|
59
|
-
|
60
|
-
def process(*args)
|
61
|
-
@_partials = Hash.new(0)
|
62
|
-
@_templates = Hash.new(0)
|
63
|
-
@_layouts = Hash.new(0)
|
64
|
-
super
|
65
|
-
end
|
66
|
-
|
67
|
-
# Asserts that the request was rendered with the appropriate template file or partials.
|
68
|
-
#
|
69
|
-
# # assert that the "new" view template was rendered
|
70
|
-
# assert_template "new"
|
71
|
-
#
|
72
|
-
# # assert that the exact template "admin/posts/new" was rendered
|
73
|
-
# assert_template %r{\Aadmin/posts/new\Z}
|
74
|
-
#
|
75
|
-
# # assert that the layout 'admin' was rendered
|
76
|
-
# assert_template layout: 'admin'
|
77
|
-
# assert_template layout: 'layouts/admin'
|
78
|
-
# assert_template layout: :admin
|
79
|
-
#
|
80
|
-
# # assert that no layout was rendered
|
81
|
-
# assert_template layout: nil
|
82
|
-
# assert_template layout: false
|
83
|
-
#
|
84
|
-
# # assert that the "_customer" partial was rendered twice
|
85
|
-
# assert_template partial: '_customer', count: 2
|
86
|
-
#
|
87
|
-
# # assert that no partials were rendered
|
88
|
-
# assert_template partial: false
|
89
|
-
#
|
90
|
-
# In a view test case, you can also assert that specific locals are passed
|
91
|
-
# to partials:
|
92
|
-
#
|
93
|
-
# # assert that the "_customer" partial was rendered with a specific object
|
94
|
-
# assert_template partial: '_customer', locals: { customer: @customer }
|
95
|
-
def assert_template(options = {}, message = nil)
|
96
|
-
# Force body to be read in case the template is being streamed.
|
97
|
-
response.body
|
98
|
-
|
99
|
-
case options
|
100
|
-
when NilClass, Regexp, String, Symbol
|
101
|
-
options = options.to_s if Symbol === options
|
102
|
-
rendered = @_templates
|
103
|
-
msg = message || sprintf("expecting <%s> but rendering with <%s>",
|
104
|
-
options.inspect, rendered.keys)
|
105
|
-
matches_template =
|
106
|
-
case options
|
107
|
-
when String
|
108
|
-
!options.empty? && rendered.any? do |t, num|
|
109
|
-
options_splited = options.split(File::SEPARATOR)
|
110
|
-
t_splited = t.split(File::SEPARATOR)
|
111
|
-
t_splited.last(options_splited.size) == options_splited
|
112
|
-
end
|
113
|
-
when Regexp
|
114
|
-
rendered.any? { |t,num| t.match(options) }
|
115
|
-
when NilClass
|
116
|
-
rendered.blank?
|
117
|
-
end
|
118
|
-
assert matches_template, msg
|
119
|
-
when Hash
|
120
|
-
options.assert_valid_keys(:layout, :partial, :locals, :count, :file)
|
121
|
-
|
122
|
-
if options.key?(:layout)
|
123
|
-
expected_layout = options[:layout]
|
124
|
-
msg = message || sprintf("expecting layout <%s> but action rendered <%s>",
|
125
|
-
expected_layout, @_layouts.keys)
|
126
|
-
|
127
|
-
case expected_layout
|
128
|
-
when String, Symbol
|
129
|
-
assert_includes @_layouts.keys, expected_layout.to_s, msg
|
130
|
-
when Regexp
|
131
|
-
assert(@_layouts.keys.any? {|l| l =~ expected_layout }, msg)
|
132
|
-
when nil, false
|
133
|
-
assert(@_layouts.empty?, msg)
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
if options[:file]
|
138
|
-
assert_includes @_files.keys, options[:file]
|
139
|
-
end
|
140
|
-
|
141
|
-
if expected_partial = options[:partial]
|
142
|
-
if expected_locals = options[:locals]
|
143
|
-
if defined?(@_rendered_views)
|
144
|
-
view = expected_partial.to_s.sub(/^_/, '').sub(/\/_(?=[^\/]+\z)/, '/')
|
145
|
-
|
146
|
-
partial_was_not_rendered_msg = "expected %s to be rendered but it was not." % view
|
147
|
-
assert_includes @_rendered_views.rendered_views, view, partial_was_not_rendered_msg
|
148
|
-
|
149
|
-
msg = 'expecting %s to be rendered with %s but was with %s' % [expected_partial,
|
150
|
-
expected_locals,
|
151
|
-
@_rendered_views.locals_for(view)]
|
152
|
-
assert(@_rendered_views.view_rendered?(view, options[:locals]), msg)
|
153
|
-
else
|
154
|
-
warn "the :locals option to #assert_template is only supported in a ActionView::TestCase"
|
155
|
-
end
|
156
|
-
elsif expected_count = options[:count]
|
157
|
-
actual_count = @_partials[expected_partial]
|
158
|
-
msg = message || sprintf("expecting %s to be rendered %s time(s) but rendered %s time(s)",
|
159
|
-
expected_partial, expected_count, actual_count)
|
160
|
-
assert(actual_count == expected_count.to_i, msg)
|
161
|
-
else
|
162
|
-
msg = message || sprintf("expecting partial <%s> but action rendered <%s>",
|
163
|
-
options[:partial], @_partials.keys)
|
164
|
-
assert_includes @_partials, expected_partial, msg
|
165
|
-
end
|
166
|
-
elsif options.key?(:partial)
|
167
|
-
assert @_partials.empty?,
|
168
|
-
"Expected no partials to be rendered"
|
169
|
-
end
|
170
|
-
else
|
171
|
-
raise ArgumentError, "assert_template only accepts a String, Symbol, Hash, Regexp, or nil"
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
class TestRequest < ActionDispatch::TestRequest #:nodoc:
|
177
|
-
DEFAULT_ENV = ActionDispatch::TestRequest::DEFAULT_ENV.dup
|
178
|
-
DEFAULT_ENV.delete 'PATH_INFO'
|
179
|
-
|
180
|
-
def initialize(env = {})
|
181
|
-
super
|
182
|
-
|
183
|
-
self.session = TestSession.new
|
184
|
-
self.session_options = TestSession::DEFAULT_OPTIONS.merge(:id => SecureRandom.hex(16))
|
185
|
-
end
|
186
|
-
|
187
|
-
def assign_parameters(routes, controller_path, action, parameters = {})
|
188
|
-
parameters = parameters.symbolize_keys.merge(:controller => controller_path, :action => action)
|
189
|
-
extra_keys = routes.extra_keys(parameters)
|
190
|
-
non_path_parameters = get? ? query_parameters : request_parameters
|
191
|
-
parameters.each do |key, value|
|
192
|
-
if value.is_a?(Array) && (value.frozen? || value.any?(&:frozen?))
|
193
|
-
value = value.map{ |v| v.duplicable? ? v.dup : v }
|
194
|
-
elsif value.is_a?(Hash) && (value.frozen? || value.any?{ |k,v| v.frozen? })
|
195
|
-
value = Hash[value.map{ |k,v| [k, v.duplicable? ? v.dup : v] }]
|
196
|
-
elsif value.frozen? && value.duplicable?
|
197
|
-
value = value.dup
|
198
|
-
end
|
199
|
-
|
200
|
-
if extra_keys.include?(key.to_sym)
|
201
|
-
non_path_parameters[key] = value
|
202
|
-
else
|
203
|
-
if value.is_a?(Array)
|
204
|
-
value = value.map(&:to_param)
|
205
|
-
else
|
206
|
-
value = value.to_param
|
207
|
-
end
|
208
|
-
|
209
|
-
path_parameters[key.to_s] = value
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
# Clear the combined params hash in case it was already referenced.
|
214
|
-
@env.delete("action_dispatch.request.parameters")
|
215
|
-
|
216
|
-
params = self.request_parameters.dup
|
217
|
-
%w(controller action only_path).each do |k|
|
218
|
-
params.delete(k)
|
219
|
-
params.delete(k.to_sym)
|
220
|
-
end
|
221
|
-
data = params.to_query
|
222
|
-
|
223
|
-
@env['CONTENT_LENGTH'] = data.length.to_s
|
224
|
-
@env['rack.input'] = StringIO.new(data)
|
225
|
-
end
|
226
|
-
|
227
|
-
def recycle!
|
228
|
-
@formats = nil
|
229
|
-
@env.delete_if { |k, v| k =~ /^(action_dispatch|rack)\.request/ }
|
230
|
-
@env.delete_if { |k, v| k =~ /^action_dispatch\.rescue/ }
|
231
|
-
@symbolized_path_params = nil
|
232
|
-
@method = @request_method = nil
|
233
|
-
@fullpath = @ip = @remote_ip = @protocol = nil
|
234
|
-
@env['action_dispatch.request.query_parameters'] = {}
|
235
|
-
@set_cookies ||= {}
|
236
|
-
@set_cookies.update(Hash[cookie_jar.instance_variable_get("@set_cookies").map{ |k,o| [k,o[:value]] }])
|
237
|
-
deleted_cookies = cookie_jar.instance_variable_get("@delete_cookies")
|
238
|
-
@set_cookies.reject!{ |k,v| deleted_cookies.include?(k) }
|
239
|
-
cookie_jar.update(rack_cookies)
|
240
|
-
cookie_jar.update(cookies)
|
241
|
-
cookie_jar.update(@set_cookies)
|
242
|
-
cookie_jar.recycle!
|
243
|
-
end
|
244
|
-
|
245
|
-
private
|
246
|
-
|
247
|
-
def default_env
|
248
|
-
DEFAULT_ENV
|
249
|
-
end
|
250
|
-
end
|
251
|
-
|
252
|
-
class TestResponse < ActionDispatch::TestResponse
|
253
|
-
def recycle!
|
254
|
-
initialize
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
# Methods #destroy and #load! are overridden to avoid calling methods on the
|
259
|
-
# @store object, which does not exist for the TestSession class.
|
260
|
-
class TestSession < Rack::Session::Abstract::SessionHash #:nodoc:
|
261
|
-
DEFAULT_OPTIONS = Rack::Session::Abstract::ID::DEFAULT_OPTIONS
|
262
|
-
|
263
|
-
def initialize(session = {})
|
264
|
-
super(nil, nil)
|
265
|
-
@id = SecureRandom.hex(16)
|
266
|
-
@data = stringify_keys(session)
|
267
|
-
@loaded = true
|
268
|
-
end
|
269
|
-
|
270
|
-
def exists?
|
271
|
-
true
|
272
|
-
end
|
273
|
-
|
274
|
-
def keys
|
275
|
-
@data.keys
|
276
|
-
end
|
277
|
-
|
278
|
-
def values
|
279
|
-
@data.values
|
280
|
-
end
|
281
|
-
|
282
|
-
def destroy
|
283
|
-
clear
|
284
|
-
end
|
285
|
-
|
286
|
-
private
|
287
|
-
|
288
|
-
def load!
|
289
|
-
@id
|
290
|
-
end
|
291
|
-
end
|
292
|
-
|
293
|
-
# Superclass for ActionController functional tests. Functional tests allow you to
|
294
|
-
# test a single controller action per test method. This should not be confused with
|
295
|
-
# integration tests (see ActionDispatch::IntegrationTest), which are more like
|
296
|
-
# "stories" that can involve multiple controllers and multiple actions (i.e. multiple
|
297
|
-
# different HTTP requests).
|
298
|
-
#
|
299
|
-
# == Basic example
|
300
|
-
#
|
301
|
-
# Functional tests are written as follows:
|
302
|
-
# 1. First, one uses the +get+, +post+, +patch+, +put+, +delete+ or +head+ method to simulate
|
303
|
-
# an HTTP request.
|
304
|
-
# 2. Then, one asserts whether the current state is as expected. "State" can be anything:
|
305
|
-
# the controller's HTTP response, the database contents, etc.
|
306
|
-
#
|
307
|
-
# For example:
|
308
|
-
#
|
309
|
-
# class BooksControllerTest < ActionController::TestCase
|
310
|
-
# def test_create
|
311
|
-
# # Simulate a POST response with the given HTTP parameters.
|
312
|
-
# post(:create, book: { title: "Love Hina" })
|
313
|
-
#
|
314
|
-
# # Assert that the controller tried to redirect us to
|
315
|
-
# # the created book's URI.
|
316
|
-
# assert_response :found
|
317
|
-
#
|
318
|
-
# # Assert that the controller really put the book in the database.
|
319
|
-
# assert_not_nil Book.find_by(title: "Love Hina")
|
320
|
-
# end
|
321
|
-
# end
|
322
|
-
#
|
323
|
-
# You can also send a real document in the simulated HTTP request.
|
324
|
-
#
|
325
|
-
# def test_create
|
326
|
-
# json = {book: { title: "Love Hina" }}.to_json
|
327
|
-
# post :create, json
|
328
|
-
# end
|
329
|
-
#
|
330
|
-
# == Special instance variables
|
331
|
-
#
|
332
|
-
# ActionController::TestCase will also automatically provide the following instance
|
333
|
-
# variables for use in the tests:
|
334
|
-
#
|
335
|
-
# <b>@controller</b>::
|
336
|
-
# The controller instance that will be tested.
|
337
|
-
# <b>@request</b>::
|
338
|
-
# An ActionController::TestRequest, representing the current HTTP
|
339
|
-
# request. You can modify this object before sending the HTTP request. For example,
|
340
|
-
# you might want to set some session properties before sending a GET request.
|
341
|
-
# <b>@response</b>::
|
342
|
-
# An ActionController::TestResponse object, representing the response
|
343
|
-
# of the last HTTP response. In the above example, <tt>@response</tt> becomes valid
|
344
|
-
# after calling +post+. If the various assert methods are not sufficient, then you
|
345
|
-
# may use this object to inspect the HTTP response in detail.
|
346
|
-
#
|
347
|
-
# (Earlier versions of \Rails required each functional test to subclass
|
348
|
-
# Test::Unit::TestCase and define @controller, @request, @response in +setup+.)
|
349
|
-
#
|
350
|
-
# == Controller is automatically inferred
|
351
|
-
#
|
352
|
-
# ActionController::TestCase will automatically infer the controller under test
|
353
|
-
# from the test class name. If the controller cannot be inferred from the test
|
354
|
-
# class name, you can explicitly set it with +tests+.
|
355
|
-
#
|
356
|
-
# class SpecialEdgeCaseWidgetsControllerTest < ActionController::TestCase
|
357
|
-
# tests WidgetController
|
358
|
-
# end
|
359
|
-
#
|
360
|
-
# == \Testing controller internals
|
361
|
-
#
|
362
|
-
# In addition to these specific assertions, you also have easy access to various collections that the regular test/unit assertions
|
363
|
-
# can be used against. These collections are:
|
364
|
-
#
|
365
|
-
# * assigns: Instance variables assigned in the action that are available for the view.
|
366
|
-
# * session: Objects being saved in the session.
|
367
|
-
# * flash: The flash objects currently in the session.
|
368
|
-
# * cookies: \Cookies being sent to the user on this request.
|
369
|
-
#
|
370
|
-
# These collections can be used just like any other hash:
|
371
|
-
#
|
372
|
-
# assert_not_nil assigns(:person) # makes sure that a @person instance variable was set
|
373
|
-
# assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave"
|
374
|
-
# assert flash.empty? # makes sure that there's nothing in the flash
|
375
|
-
#
|
376
|
-
# For historic reasons, the assigns hash uses string-based keys. So <tt>assigns[:person]</tt> won't work, but <tt>assigns["person"]</tt> will. To
|
377
|
-
# appease our yearning for symbols, though, an alternative accessor has been devised using a method call instead of index referencing.
|
378
|
-
# So <tt>assigns(:person)</tt> will work just like <tt>assigns["person"]</tt>, but again, <tt>assigns[:person]</tt> will not work.
|
379
|
-
#
|
380
|
-
# On top of the collections, you have the complete url that a given action redirected to available in <tt>redirect_to_url</tt>.
|
381
|
-
#
|
382
|
-
# For redirects within the same controller, you can even call follow_redirect and the redirect will be followed, triggering another
|
383
|
-
# action call which can then be asserted against.
|
384
|
-
#
|
385
|
-
# == Manipulating session and cookie variables
|
386
|
-
#
|
387
|
-
# Sometimes you need to set up the session and cookie variables for a test.
|
388
|
-
# To do this just assign a value to the session or cookie collection:
|
389
|
-
#
|
390
|
-
# session[:key] = "value"
|
391
|
-
# cookies[:key] = "value"
|
392
|
-
#
|
393
|
-
# To clear the cookies for a test just clear the cookie collection:
|
394
|
-
#
|
395
|
-
# cookies.clear
|
396
|
-
#
|
397
|
-
# == \Testing named routes
|
398
|
-
#
|
399
|
-
# If you're using named routes, they can be easily tested using the original named routes' methods straight in the test case.
|
400
|
-
#
|
401
|
-
# assert_redirected_to page_url(title: 'foo')
|
402
|
-
class TestCase < ActiveSupport::TestCase
|
403
|
-
module Behavior
|
404
|
-
extend ActiveSupport::Concern
|
405
|
-
include ActionDispatch::TestProcess
|
406
|
-
include ActiveSupport::Testing::ConstantLookup
|
407
|
-
|
408
|
-
attr_reader :response, :request
|
409
|
-
|
410
|
-
module ClassMethods
|
411
|
-
|
412
|
-
# Sets the controller class name. Useful if the name can't be inferred from test class.
|
413
|
-
# Normalizes +controller_class+ before using.
|
414
|
-
#
|
415
|
-
# tests WidgetController
|
416
|
-
# tests :widget
|
417
|
-
# tests 'widget'
|
418
|
-
def tests(controller_class)
|
419
|
-
case controller_class
|
420
|
-
when String, Symbol
|
421
|
-
self.controller_class = "#{controller_class.to_s.camelize}Controller".constantize
|
422
|
-
when Class
|
423
|
-
self.controller_class = controller_class
|
424
|
-
else
|
425
|
-
raise ArgumentError, "controller class must be a String, Symbol, or Class"
|
426
|
-
end
|
427
|
-
end
|
428
|
-
|
429
|
-
def controller_class=(new_class)
|
430
|
-
prepare_controller_class(new_class) if new_class
|
431
|
-
self._controller_class = new_class
|
432
|
-
end
|
433
|
-
|
434
|
-
def controller_class
|
435
|
-
if current_controller_class = self._controller_class
|
436
|
-
current_controller_class
|
437
|
-
else
|
438
|
-
self.controller_class = determine_default_controller_class(name)
|
439
|
-
end
|
440
|
-
end
|
441
|
-
|
442
|
-
def determine_default_controller_class(name)
|
443
|
-
determine_constant_from_test_name(name) do |constant|
|
444
|
-
Class === constant && constant < ActionController::Metal
|
445
|
-
end
|
446
|
-
end
|
447
|
-
|
448
|
-
def prepare_controller_class(new_class)
|
449
|
-
new_class.send :include, ActionController::TestCase::RaiseActionExceptions
|
450
|
-
end
|
451
|
-
|
452
|
-
end
|
453
|
-
|
454
|
-
# Simulate a GET request with the given parameters.
|
455
|
-
#
|
456
|
-
# - +action+: The controller action to call.
|
457
|
-
# - +parameters+: The HTTP parameters that you want to pass. This may
|
458
|
-
# be +nil+, a hash, or a string that is appropriately encoded
|
459
|
-
# (<tt>application/x-www-form-urlencoded</tt> or <tt>multipart/form-data</tt>).
|
460
|
-
# - +session+: A hash of parameters to store in the session. This may be +nil+.
|
461
|
-
# - +flash+: A hash of parameters to store in the flash. This may be +nil+.
|
462
|
-
#
|
463
|
-
# You can also simulate POST, PATCH, PUT, DELETE, HEAD, and OPTIONS requests with
|
464
|
-
# +post+, +patch+, +put+, +delete+, +head+, and +options+.
|
465
|
-
#
|
466
|
-
# Note that the request method is not verified. The different methods are
|
467
|
-
# available to make the tests more expressive.
|
468
|
-
def get(action, *args)
|
469
|
-
process(action, "GET", *args)
|
470
|
-
end
|
471
|
-
|
472
|
-
# Simulate a POST request with the given parameters and set/volley the response.
|
473
|
-
# See +get+ for more details.
|
474
|
-
def post(action, *args)
|
475
|
-
process(action, "POST", *args)
|
476
|
-
end
|
477
|
-
|
478
|
-
# Simulate a PATCH request with the given parameters and set/volley the response.
|
479
|
-
# See +get+ for more details.
|
480
|
-
def patch(action, *args)
|
481
|
-
process(action, "PATCH", *args)
|
482
|
-
end
|
483
|
-
|
484
|
-
# Simulate a PUT request with the given parameters and set/volley the response.
|
485
|
-
# See +get+ for more details.
|
486
|
-
def put(action, *args)
|
487
|
-
process(action, "PUT", *args)
|
488
|
-
end
|
489
|
-
|
490
|
-
# Simulate a DELETE request with the given parameters and set/volley the response.
|
491
|
-
# See +get+ for more details.
|
492
|
-
def delete(action, *args)
|
493
|
-
process(action, "DELETE", *args)
|
494
|
-
end
|
495
|
-
|
496
|
-
<<<<<<< HEAD
|
497
|
-
# Simulate a HEAD request with the given parameters and set/volley the response.
|
498
|
-
# See +get+ for more details.
|
499
|
-
def head(action, *args)
|
500
|
-
process(action, "HEAD", *args)
|
501
|
-
end
|
502
|
-
|
503
|
-
# Simulate a OPTIONS request with the given parameters and set/volley the response.
|
504
|
-
# See +get+ for more details.
|
505
|
-
def options(action, *args)
|
506
|
-
process(action, "OPTIONS", *args)
|
507
|
-
=======
|
508
|
-
# Executes a request simulating HEAD HTTP method and set/volley the response
|
509
|
-
def head(action, parameters = nil, session = nil, flash = nil)
|
510
|
-
process(action, "HEAD", parameters, session, flash)
|
511
|
-
>>>>>>> parent of 0303c23... Add the options method to action_controller testcase.
|
512
|
-
end
|
513
|
-
|
514
|
-
def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
|
515
|
-
@request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
|
516
|
-
@request.env['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
|
517
|
-
__send__(request_method, action, parameters, session, flash).tap do
|
518
|
-
@request.env.delete 'HTTP_X_REQUESTED_WITH'
|
519
|
-
@request.env.delete 'HTTP_ACCEPT'
|
520
|
-
end
|
521
|
-
end
|
522
|
-
alias xhr :xml_http_request
|
523
|
-
|
524
|
-
def paramify_values(hash_or_array_or_value)
|
525
|
-
case hash_or_array_or_value
|
526
|
-
when Hash
|
527
|
-
Hash[hash_or_array_or_value.map{|key, value| [key, paramify_values(value)] }]
|
528
|
-
when Array
|
529
|
-
hash_or_array_or_value.map {|i| paramify_values(i)}
|
530
|
-
when Rack::Test::UploadedFile, ActionDispatch::Http::UploadedFile
|
531
|
-
hash_or_array_or_value
|
532
|
-
else
|
533
|
-
hash_or_array_or_value.to_param
|
534
|
-
end
|
535
|
-
end
|
536
|
-
|
537
|
-
def process(action, http_method = 'GET', *args)
|
538
|
-
check_required_ivars
|
539
|
-
http_method, args = handle_old_process_api(http_method, args, caller)
|
540
|
-
|
541
|
-
if args.first.is_a?(String) && http_method != 'HEAD'
|
542
|
-
@request.env['RAW_POST_DATA'] = args.shift
|
543
|
-
end
|
544
|
-
|
545
|
-
parameters, session, flash = args
|
546
|
-
|
547
|
-
# Ensure that numbers and symbols passed as params are converted to
|
548
|
-
# proper params, as is the case when engaging rack.
|
549
|
-
parameters = paramify_values(parameters) if html_format?(parameters)
|
550
|
-
|
551
|
-
@html_document = nil
|
552
|
-
|
553
|
-
unless @controller.respond_to?(:recycle!)
|
554
|
-
@controller.extend(Testing::Functional)
|
555
|
-
@controller.class.class_eval { include Testing }
|
556
|
-
end
|
557
|
-
|
558
|
-
@request.recycle!
|
559
|
-
@response.recycle!
|
560
|
-
@controller.recycle!
|
561
|
-
|
562
|
-
@request.env['REQUEST_METHOD'] = http_method
|
563
|
-
|
564
|
-
parameters ||= {}
|
565
|
-
controller_class_name = @controller.class.anonymous? ?
|
566
|
-
"anonymous" :
|
567
|
-
@controller.class.controller_path
|
568
|
-
|
569
|
-
@request.assign_parameters(@routes, controller_class_name, action.to_s, parameters)
|
570
|
-
|
571
|
-
@request.session.update(session) if session
|
572
|
-
@request.flash.update(flash || {})
|
573
|
-
|
574
|
-
@controller.request = @request
|
575
|
-
@controller.response = @response
|
576
|
-
|
577
|
-
build_request_uri(action, parameters)
|
578
|
-
|
579
|
-
name = @request.parameters[:action]
|
580
|
-
|
581
|
-
@controller.process(name)
|
582
|
-
|
583
|
-
if cookies = @request.env['action_dispatch.cookies']
|
584
|
-
cookies.write(@response)
|
585
|
-
end
|
586
|
-
@response.prepare!
|
587
|
-
|
588
|
-
@assigns = @controller.respond_to?(:view_assigns) ? @controller.view_assigns : {}
|
589
|
-
@request.session['flash'] = @request.flash.to_session_value
|
590
|
-
@request.session.delete('flash') if @request.session['flash'].blank?
|
591
|
-
@response
|
592
|
-
end
|
593
|
-
|
594
|
-
def setup_controller_request_and_response
|
595
|
-
@request = build_request
|
596
|
-
@response = build_response
|
597
|
-
@response.request = @request
|
598
|
-
|
599
|
-
@controller = nil unless defined? @controller
|
600
|
-
|
601
|
-
if klass = self.class.controller_class
|
602
|
-
unless @controller
|
603
|
-
begin
|
604
|
-
@controller = klass.new
|
605
|
-
rescue
|
606
|
-
warn "could not construct controller #{klass}" if $VERBOSE
|
607
|
-
end
|
608
|
-
end
|
609
|
-
end
|
610
|
-
|
611
|
-
if @controller
|
612
|
-
@controller.request = @request
|
613
|
-
@controller.params = {}
|
614
|
-
end
|
615
|
-
end
|
616
|
-
|
617
|
-
def build_request
|
618
|
-
TestRequest.new
|
619
|
-
end
|
620
|
-
|
621
|
-
def build_response
|
622
|
-
TestResponse.new
|
623
|
-
end
|
624
|
-
|
625
|
-
included do
|
626
|
-
include ActionController::TemplateAssertions
|
627
|
-
include ActionDispatch::Assertions
|
628
|
-
class_attribute :_controller_class
|
629
|
-
setup :setup_controller_request_and_response
|
630
|
-
end
|
631
|
-
|
632
|
-
private
|
633
|
-
def check_required_ivars
|
634
|
-
# Sanity check for required instance variables so we can give an
|
635
|
-
# understandable error message.
|
636
|
-
[:@routes, :@controller, :@request, :@response].each do |iv_name|
|
637
|
-
if !instance_variable_defined?(iv_name) || instance_variable_get(iv_name).nil?
|
638
|
-
raise "#{iv_name} is nil: make sure you set it in your test's setup method."
|
639
|
-
end
|
640
|
-
end
|
641
|
-
end
|
642
|
-
|
643
|
-
def handle_old_process_api(http_method, args, callstack)
|
644
|
-
# 4.0: Remove this method.
|
645
|
-
if http_method.is_a?(Hash)
|
646
|
-
ActiveSupport::Deprecation.warn("TestCase#process now expects the HTTP method as second argument: process(action, http_method, params, session, flash)", callstack)
|
647
|
-
args.unshift(http_method)
|
648
|
-
http_method = args.last.is_a?(String) ? args.last : "GET"
|
649
|
-
end
|
650
|
-
|
651
|
-
[http_method, args]
|
652
|
-
end
|
653
|
-
|
654
|
-
def build_request_uri(action, parameters)
|
655
|
-
unless @request.env["PATH_INFO"]
|
656
|
-
options = @controller.respond_to?(:url_options) ? @controller.__send__(:url_options).merge(parameters) : parameters
|
657
|
-
options.update(
|
658
|
-
:only_path => true,
|
659
|
-
:action => action,
|
660
|
-
:relative_url_root => nil,
|
661
|
-
:_recall => @request.symbolized_path_parameters)
|
662
|
-
|
663
|
-
url, query_string = @routes.url_for(options).split("?", 2)
|
664
|
-
|
665
|
-
@request.env["SCRIPT_NAME"] = @controller.config.relative_url_root
|
666
|
-
@request.env["PATH_INFO"] = url
|
667
|
-
@request.env["QUERY_STRING"] = query_string || ""
|
668
|
-
end
|
669
|
-
end
|
670
|
-
|
671
|
-
def html_format?(parameters)
|
672
|
-
return true unless parameters.is_a?(Hash)
|
673
|
-
Mime.fetch(parameters[:format]) { Mime['html'] }.html?
|
674
|
-
end
|
675
|
-
end
|
676
|
-
|
677
|
-
# When the request.remote_addr remains the default for testing, which is 0.0.0.0, the exception is simply raised inline
|
678
|
-
# (skipping the regular exception handling from rescue_action). If the request.remote_addr is anything else, the regular
|
679
|
-
# rescue_action process takes place. This means you can test your rescue_action code by setting remote_addr to something else
|
680
|
-
# than 0.0.0.0.
|
681
|
-
#
|
682
|
-
# The exception is stored in the exception accessor for further inspection.
|
683
|
-
module RaiseActionExceptions
|
684
|
-
def self.included(base) #:nodoc:
|
685
|
-
unless base.method_defined?(:exception) && base.method_defined?(:exception=)
|
686
|
-
base.class_eval do
|
687
|
-
attr_accessor :exception
|
688
|
-
protected :exception, :exception=
|
689
|
-
end
|
690
|
-
end
|
691
|
-
end
|
692
|
-
|
693
|
-
protected
|
694
|
-
def rescue_action_without_handler(e)
|
695
|
-
self.exception = e
|
696
|
-
|
697
|
-
if request.remote_addr == "0.0.0.0"
|
698
|
-
raise(e)
|
699
|
-
else
|
700
|
-
super(e)
|
701
|
-
end
|
702
|
-
end
|
703
|
-
end
|
704
|
-
|
705
|
-
include Behavior
|
706
|
-
end
|
707
|
-
end
|
@@ -1,525 +0,0 @@
|
|
1
|
-
require 'stringio'
|
2
|
-
require 'uri'
|
3
|
-
require 'active_support/core_ext/kernel/singleton_class'
|
4
|
-
require 'active_support/core_ext/object/try'
|
5
|
-
require 'rack/test'
|
6
|
-
require 'minitest/unit'
|
7
|
-
|
8
|
-
module ActionDispatch
|
9
|
-
module Integration #:nodoc:
|
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>).
|
20
|
-
# - +headers_or_env+: Additional headers to pass, as a Hash. The headers will be
|
21
|
-
# merged into the Rack env hash.
|
22
|
-
#
|
23
|
-
# This method returns a Response object, which one can use to
|
24
|
-
# inspect the details of the response. Furthermore, if this method was
|
25
|
-
# called from an ActionDispatch::IntegrationTest object, then that
|
26
|
-
# object's <tt>@response</tt> instance variable will point to the same
|
27
|
-
# response object.
|
28
|
-
#
|
29
|
-
# You can also perform POST, PATCH, PUT, DELETE, and HEAD requests with
|
30
|
-
# +#post+, +#patch+, +#put+, +#delete+, and +#head+.
|
31
|
-
def get(path, parameters = nil, headers_or_env = nil)
|
32
|
-
process :get, path, parameters, headers_or_env
|
33
|
-
end
|
34
|
-
|
35
|
-
# Performs a POST request with the given parameters. See +#get+ for more
|
36
|
-
# details.
|
37
|
-
def post(path, parameters = nil, headers_or_env = nil)
|
38
|
-
process :post, path, parameters, headers_or_env
|
39
|
-
end
|
40
|
-
|
41
|
-
# Performs a PATCH request with the given parameters. See +#get+ for more
|
42
|
-
# details.
|
43
|
-
def patch(path, parameters = nil, headers_or_env = nil)
|
44
|
-
process :patch, path, parameters, headers_or_env
|
45
|
-
end
|
46
|
-
|
47
|
-
# Performs a PUT request with the given parameters. See +#get+ for more
|
48
|
-
# details.
|
49
|
-
def put(path, parameters = nil, headers_or_env = nil)
|
50
|
-
process :put, path, parameters, headers_or_env
|
51
|
-
end
|
52
|
-
|
53
|
-
# Performs a DELETE request with the given parameters. See +#get+ for
|
54
|
-
# more details.
|
55
|
-
def delete(path, parameters = nil, headers_or_env = nil)
|
56
|
-
process :delete, path, parameters, headers_or_env
|
57
|
-
end
|
58
|
-
|
59
|
-
# Performs a HEAD request with the given parameters. See +#get+ for more
|
60
|
-
# details.
|
61
|
-
def head(path, parameters = nil, headers_or_env = nil)
|
62
|
-
process :head, path, parameters, headers_or_env
|
63
|
-
end
|
64
|
-
|
65
|
-
<<<<<<< HEAD
|
66
|
-
# Performs a OPTIONS request with the given parameters. See +#get+ for
|
67
|
-
# more details.
|
68
|
-
def options(path, parameters = nil, headers_or_env = nil)
|
69
|
-
process :options, path, parameters, headers_or_env
|
70
|
-
end
|
71
|
-
|
72
|
-
=======
|
73
|
-
>>>>>>> parent of ad46884... Integration tests support the OPTIONS http method
|
74
|
-
# Performs an XMLHttpRequest request with the given parameters, mirroring
|
75
|
-
# a request from the Prototype library.
|
76
|
-
#
|
77
|
-
# The request_method is +:get+, +:post+, +:patch+, +:put+, +:delete+ or
|
78
|
-
# +:head+; the parameters are +nil+, a hash, or a url-encoded or multipart
|
79
|
-
# string; the headers are a hash.
|
80
|
-
def xml_http_request(request_method, path, parameters = nil, headers_or_env = nil)
|
81
|
-
headers_or_env ||= {}
|
82
|
-
headers_or_env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
|
83
|
-
headers_or_env['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
|
84
|
-
process(request_method, path, parameters, headers_or_env)
|
85
|
-
end
|
86
|
-
alias xhr :xml_http_request
|
87
|
-
|
88
|
-
# Follow a single redirect response. If the last response was not a
|
89
|
-
# redirect, an exception will be raised. Otherwise, the redirect is
|
90
|
-
# performed on the location header.
|
91
|
-
def follow_redirect!
|
92
|
-
raise "not a redirect! #{status} #{status_message}" unless redirect?
|
93
|
-
get(response.location)
|
94
|
-
status
|
95
|
-
end
|
96
|
-
|
97
|
-
# Performs a request using the specified method, following any subsequent
|
98
|
-
# redirect. Note that the redirects are followed until the response is
|
99
|
-
# not a redirect--this means you may run into an infinite loop if your
|
100
|
-
# redirect loops back to itself.
|
101
|
-
def request_via_redirect(http_method, path, parameters = nil, headers_or_env = nil)
|
102
|
-
process(http_method, path, parameters, headers_or_env)
|
103
|
-
follow_redirect! while redirect?
|
104
|
-
status
|
105
|
-
end
|
106
|
-
|
107
|
-
# Performs a GET request, following any subsequent redirect.
|
108
|
-
# See +request_via_redirect+ for more information.
|
109
|
-
def get_via_redirect(path, parameters = nil, headers_or_env = nil)
|
110
|
-
request_via_redirect(:get, path, parameters, headers_or_env)
|
111
|
-
end
|
112
|
-
|
113
|
-
# Performs a POST request, following any subsequent redirect.
|
114
|
-
# See +request_via_redirect+ for more information.
|
115
|
-
def post_via_redirect(path, parameters = nil, headers_or_env = nil)
|
116
|
-
request_via_redirect(:post, path, parameters, headers_or_env)
|
117
|
-
end
|
118
|
-
|
119
|
-
# Performs a PATCH request, following any subsequent redirect.
|
120
|
-
# See +request_via_redirect+ for more information.
|
121
|
-
def patch_via_redirect(path, parameters = nil, headers_or_env = nil)
|
122
|
-
request_via_redirect(:patch, path, parameters, headers_or_env)
|
123
|
-
end
|
124
|
-
|
125
|
-
# Performs a PUT request, following any subsequent redirect.
|
126
|
-
# See +request_via_redirect+ for more information.
|
127
|
-
def put_via_redirect(path, parameters = nil, headers_or_env = nil)
|
128
|
-
request_via_redirect(:put, path, parameters, headers_or_env)
|
129
|
-
end
|
130
|
-
|
131
|
-
# Performs a DELETE request, following any subsequent redirect.
|
132
|
-
# See +request_via_redirect+ for more information.
|
133
|
-
def delete_via_redirect(path, parameters = nil, headers_or_env = nil)
|
134
|
-
request_via_redirect(:delete, path, parameters, headers_or_env)
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
# An instance of this class represents a set of requests and responses
|
139
|
-
# performed sequentially by a test process. Because you can instantiate
|
140
|
-
# multiple sessions and run them side-by-side, you can also mimic (to some
|
141
|
-
# limited extent) multiple simultaneous users interacting with your system.
|
142
|
-
#
|
143
|
-
# Typically, you will instantiate a new session using
|
144
|
-
# IntegrationTest#open_session, rather than instantiating
|
145
|
-
# Integration::Session directly.
|
146
|
-
class Session
|
147
|
-
DEFAULT_HOST = "www.example.com"
|
148
|
-
|
149
|
-
include MiniTest::Assertions
|
150
|
-
include TestProcess, RequestHelpers, Assertions
|
151
|
-
|
152
|
-
%w( status status_message headers body redirect? ).each do |method|
|
153
|
-
delegate method, :to => :response, :allow_nil => true
|
154
|
-
end
|
155
|
-
|
156
|
-
%w( path ).each do |method|
|
157
|
-
delegate method, :to => :request, :allow_nil => true
|
158
|
-
end
|
159
|
-
|
160
|
-
# The hostname used in the last request.
|
161
|
-
def host
|
162
|
-
@host || DEFAULT_HOST
|
163
|
-
end
|
164
|
-
attr_writer :host
|
165
|
-
|
166
|
-
# The remote_addr used in the last request.
|
167
|
-
attr_accessor :remote_addr
|
168
|
-
|
169
|
-
# The Accept header to send.
|
170
|
-
attr_accessor :accept
|
171
|
-
|
172
|
-
# A map of the cookies returned by the last response, and which will be
|
173
|
-
# sent with the next request.
|
174
|
-
def cookies
|
175
|
-
_mock_session.cookie_jar
|
176
|
-
end
|
177
|
-
|
178
|
-
# A reference to the controller instance used by the last request.
|
179
|
-
attr_reader :controller
|
180
|
-
|
181
|
-
# A reference to the request instance used by the last request.
|
182
|
-
attr_reader :request
|
183
|
-
|
184
|
-
# A reference to the response instance used by the last request.
|
185
|
-
attr_reader :response
|
186
|
-
|
187
|
-
# A running counter of the number of requests processed.
|
188
|
-
attr_accessor :request_count
|
189
|
-
|
190
|
-
include ActionDispatch::Routing::UrlFor
|
191
|
-
|
192
|
-
# Create and initialize a new Session instance.
|
193
|
-
def initialize(app)
|
194
|
-
super()
|
195
|
-
@app = app
|
196
|
-
|
197
|
-
# If the app is a Rails app, make url_helpers available on the session
|
198
|
-
# This makes app.url_for and app.foo_path available in the console
|
199
|
-
if app.respond_to?(:routes)
|
200
|
-
singleton_class.class_eval do
|
201
|
-
include app.routes.url_helpers if app.routes.respond_to?(:url_helpers)
|
202
|
-
include app.routes.mounted_helpers if app.routes.respond_to?(:mounted_helpers)
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
reset!
|
207
|
-
end
|
208
|
-
|
209
|
-
def url_options
|
210
|
-
@url_options ||= default_url_options.dup.tap do |url_options|
|
211
|
-
url_options.reverse_merge!(controller.url_options) if controller
|
212
|
-
|
213
|
-
if @app.respond_to?(:routes) && @app.routes.respond_to?(:default_url_options)
|
214
|
-
url_options.reverse_merge!(@app.routes.default_url_options)
|
215
|
-
end
|
216
|
-
|
217
|
-
url_options.reverse_merge!(:host => host, :protocol => https? ? "https" : "http")
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
# Resets the instance. This can be used to reset the state information
|
222
|
-
# in an existing session instance, so it can be used from a clean-slate
|
223
|
-
# condition.
|
224
|
-
#
|
225
|
-
# session.reset!
|
226
|
-
def reset!
|
227
|
-
@https = false
|
228
|
-
@controller = @request = @response = nil
|
229
|
-
@_mock_session = nil
|
230
|
-
@request_count = 0
|
231
|
-
@url_options = nil
|
232
|
-
|
233
|
-
self.host = DEFAULT_HOST
|
234
|
-
self.remote_addr = "127.0.0.1"
|
235
|
-
self.accept = "text/xml,application/xml,application/xhtml+xml," +
|
236
|
-
"text/html;q=0.9,text/plain;q=0.8,image/png," +
|
237
|
-
"*/*;q=0.5"
|
238
|
-
|
239
|
-
unless defined? @named_routes_configured
|
240
|
-
# the helpers are made protected by default--we make them public for
|
241
|
-
# easier access during testing and troubleshooting.
|
242
|
-
@named_routes_configured = true
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
# Specify whether or not the session should mimic a secure HTTPS request.
|
247
|
-
#
|
248
|
-
# session.https!
|
249
|
-
# session.https!(false)
|
250
|
-
def https!(flag = true)
|
251
|
-
@https = flag
|
252
|
-
end
|
253
|
-
|
254
|
-
# Return +true+ if the session is mimicking a secure HTTPS request.
|
255
|
-
#
|
256
|
-
# if session.https?
|
257
|
-
# ...
|
258
|
-
# end
|
259
|
-
def https?
|
260
|
-
@https
|
261
|
-
end
|
262
|
-
|
263
|
-
# Set the host name to use in the next request.
|
264
|
-
#
|
265
|
-
# session.host! "www.example.com"
|
266
|
-
alias :host! :host=
|
267
|
-
|
268
|
-
private
|
269
|
-
def _mock_session
|
270
|
-
@_mock_session ||= Rack::MockSession.new(@app, host)
|
271
|
-
end
|
272
|
-
|
273
|
-
# Performs the actual request.
|
274
|
-
def process(method, path, parameters = nil, headers_or_env = nil)
|
275
|
-
if path =~ %r{://}
|
276
|
-
location = URI.parse(path)
|
277
|
-
https! URI::HTTPS === location if location.scheme
|
278
|
-
host! "#{location.host}:#{location.port}" if location.host
|
279
|
-
path = location.query ? "#{location.path}?#{location.query}" : location.path
|
280
|
-
end
|
281
|
-
|
282
|
-
unless ActionController::Base < ActionController::Testing
|
283
|
-
ActionController::Base.class_eval do
|
284
|
-
include ActionController::Testing
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
hostname, port = host.split(':')
|
289
|
-
|
290
|
-
env = {
|
291
|
-
:method => method,
|
292
|
-
:params => parameters,
|
293
|
-
|
294
|
-
"SERVER_NAME" => hostname,
|
295
|
-
"SERVER_PORT" => port || (https? ? "443" : "80"),
|
296
|
-
"HTTPS" => https? ? "on" : "off",
|
297
|
-
"rack.url_scheme" => https? ? "https" : "http",
|
298
|
-
|
299
|
-
"REQUEST_URI" => path,
|
300
|
-
"HTTP_HOST" => host,
|
301
|
-
"REMOTE_ADDR" => remote_addr,
|
302
|
-
"CONTENT_TYPE" => "application/x-www-form-urlencoded",
|
303
|
-
"HTTP_ACCEPT" => accept
|
304
|
-
}
|
305
|
-
# this modifies the passed env directly
|
306
|
-
Http::Headers.new(env).merge!(headers_or_env || {})
|
307
|
-
|
308
|
-
session = Rack::Test::Session.new(_mock_session)
|
309
|
-
|
310
|
-
env.merge!(env)
|
311
|
-
|
312
|
-
# NOTE: rack-test v0.5 doesn't build a default uri correctly
|
313
|
-
# Make sure requested path is always a full uri
|
314
|
-
uri = URI.parse('/')
|
315
|
-
uri.scheme ||= env['rack.url_scheme']
|
316
|
-
uri.host ||= env['SERVER_NAME']
|
317
|
-
uri.port ||= env['SERVER_PORT'].try(:to_i)
|
318
|
-
uri += path
|
319
|
-
|
320
|
-
session.request(uri.to_s, env)
|
321
|
-
|
322
|
-
@request_count += 1
|
323
|
-
@request = ActionDispatch::Request.new(session.last_request.env)
|
324
|
-
response = _mock_session.last_response
|
325
|
-
@response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body)
|
326
|
-
@html_document = nil
|
327
|
-
@url_options = nil
|
328
|
-
|
329
|
-
@controller = session.last_request.env['action_controller.instance']
|
330
|
-
|
331
|
-
return response.status
|
332
|
-
end
|
333
|
-
end
|
334
|
-
|
335
|
-
module Runner
|
336
|
-
include ActionDispatch::Assertions
|
337
|
-
|
338
|
-
def app
|
339
|
-
@app ||= nil
|
340
|
-
end
|
341
|
-
|
342
|
-
# Reset the current session. This is useful for testing multiple sessions
|
343
|
-
# in a single test case.
|
344
|
-
def reset!
|
345
|
-
@integration_session = Integration::Session.new(app)
|
346
|
-
end
|
347
|
-
|
348
|
-
<<<<<<< HEAD
|
349
|
-
%w(get post patch put head delete options cookies assigns
|
350
|
-
=======
|
351
|
-
%w(get post put head delete cookies assigns
|
352
|
-
>>>>>>> parent of ad46884... Integration tests support the OPTIONS http method
|
353
|
-
xml_http_request xhr get_via_redirect post_via_redirect).each do |method|
|
354
|
-
define_method(method) do |*args|
|
355
|
-
reset! unless integration_session
|
356
|
-
# reset the html_document variable, but only for new get/post calls
|
357
|
-
@html_document = nil unless method == 'cookies' || method == 'assigns'
|
358
|
-
integration_session.__send__(method, *args).tap do
|
359
|
-
copy_session_variables!
|
360
|
-
end
|
361
|
-
end
|
362
|
-
end
|
363
|
-
|
364
|
-
# Open a new session instance. If a block is given, the new session is
|
365
|
-
# yielded to the block before being returned.
|
366
|
-
#
|
367
|
-
# session = open_session do |sess|
|
368
|
-
# sess.extend(CustomAssertions)
|
369
|
-
# end
|
370
|
-
#
|
371
|
-
# By default, a single session is automatically created for you, but you
|
372
|
-
# can use this method to open multiple sessions that ought to be tested
|
373
|
-
# simultaneously.
|
374
|
-
def open_session(app = nil)
|
375
|
-
dup.tap do |session|
|
376
|
-
yield session if block_given?
|
377
|
-
end
|
378
|
-
end
|
379
|
-
|
380
|
-
# Copy the instance variables from the current session instance into the
|
381
|
-
# test instance.
|
382
|
-
def copy_session_variables! #:nodoc:
|
383
|
-
return unless integration_session
|
384
|
-
%w(controller response request).each do |var|
|
385
|
-
instance_variable_set("@#{var}", @integration_session.__send__(var))
|
386
|
-
end
|
387
|
-
end
|
388
|
-
|
389
|
-
def default_url_options
|
390
|
-
reset! unless integration_session
|
391
|
-
integration_session.default_url_options
|
392
|
-
end
|
393
|
-
|
394
|
-
def default_url_options=(options)
|
395
|
-
reset! unless integration_session
|
396
|
-
integration_session.default_url_options = options
|
397
|
-
end
|
398
|
-
|
399
|
-
def respond_to?(method, include_private = false)
|
400
|
-
integration_session.respond_to?(method, include_private) || super
|
401
|
-
end
|
402
|
-
|
403
|
-
# Delegate unhandled messages to the current session instance.
|
404
|
-
def method_missing(sym, *args, &block)
|
405
|
-
reset! unless integration_session
|
406
|
-
if integration_session.respond_to?(sym)
|
407
|
-
integration_session.__send__(sym, *args, &block).tap do
|
408
|
-
copy_session_variables!
|
409
|
-
end
|
410
|
-
else
|
411
|
-
super
|
412
|
-
end
|
413
|
-
end
|
414
|
-
|
415
|
-
private
|
416
|
-
def integration_session
|
417
|
-
@integration_session ||= nil
|
418
|
-
end
|
419
|
-
end
|
420
|
-
end
|
421
|
-
|
422
|
-
# An integration test spans multiple controllers and actions,
|
423
|
-
# tying them all together to ensure they work together as expected. It tests
|
424
|
-
# more completely than either unit or functional tests do, exercising the
|
425
|
-
# entire stack, from the dispatcher to the database.
|
426
|
-
#
|
427
|
-
# At its simplest, you simply extend <tt>IntegrationTest</tt> and write your tests
|
428
|
-
# using the get/post methods:
|
429
|
-
#
|
430
|
-
# require "test_helper"
|
431
|
-
#
|
432
|
-
# class ExampleTest < ActionDispatch::IntegrationTest
|
433
|
-
# fixtures :people
|
434
|
-
#
|
435
|
-
# def test_login
|
436
|
-
# # get the login page
|
437
|
-
# get "/login"
|
438
|
-
# assert_equal 200, status
|
439
|
-
#
|
440
|
-
# # post the login and follow through to the home page
|
441
|
-
# post "/login", username: people(:jamis).username,
|
442
|
-
# password: people(:jamis).password
|
443
|
-
# follow_redirect!
|
444
|
-
# assert_equal 200, status
|
445
|
-
# assert_equal "/home", path
|
446
|
-
# end
|
447
|
-
# end
|
448
|
-
#
|
449
|
-
# However, you can also have multiple session instances open per test, and
|
450
|
-
# even extend those instances with assertions and methods to create a very
|
451
|
-
# powerful testing DSL that is specific for your application. You can even
|
452
|
-
# reference any named routes you happen to have defined.
|
453
|
-
#
|
454
|
-
# require "test_helper"
|
455
|
-
#
|
456
|
-
# class AdvancedTest < ActionDispatch::IntegrationTest
|
457
|
-
# fixtures :people, :rooms
|
458
|
-
#
|
459
|
-
# def test_login_and_speak
|
460
|
-
# jamis, david = login(:jamis), login(:david)
|
461
|
-
# room = rooms(:office)
|
462
|
-
#
|
463
|
-
# jamis.enter(room)
|
464
|
-
# jamis.speak(room, "anybody home?")
|
465
|
-
#
|
466
|
-
# david.enter(room)
|
467
|
-
# david.speak(room, "hello!")
|
468
|
-
# end
|
469
|
-
#
|
470
|
-
# private
|
471
|
-
#
|
472
|
-
# module CustomAssertions
|
473
|
-
# def enter(room)
|
474
|
-
# # reference a named route, for maximum internal consistency!
|
475
|
-
# get(room_url(id: room.id))
|
476
|
-
# assert(...)
|
477
|
-
# ...
|
478
|
-
# end
|
479
|
-
#
|
480
|
-
# def speak(room, message)
|
481
|
-
# xml_http_request "/say/#{room.id}", message: message
|
482
|
-
# assert(...)
|
483
|
-
# ...
|
484
|
-
# end
|
485
|
-
# end
|
486
|
-
#
|
487
|
-
# def login(who)
|
488
|
-
# open_session do |sess|
|
489
|
-
# sess.extend(CustomAssertions)
|
490
|
-
# who = people(who)
|
491
|
-
# sess.post "/login", username: who.username,
|
492
|
-
# password: who.password
|
493
|
-
# assert(...)
|
494
|
-
# end
|
495
|
-
# end
|
496
|
-
# end
|
497
|
-
class IntegrationTest < ActiveSupport::TestCase
|
498
|
-
include Integration::Runner
|
499
|
-
include ActionController::TemplateAssertions
|
500
|
-
include ActionDispatch::Routing::UrlFor
|
501
|
-
|
502
|
-
@@app = nil
|
503
|
-
|
504
|
-
def self.app
|
505
|
-
if !@@app && !ActionDispatch.test_app
|
506
|
-
ActiveSupport::Deprecation.warn "Rails application fallback is deprecated and no longer works, please set ActionDispatch.test_app"
|
507
|
-
end
|
508
|
-
|
509
|
-
@@app || ActionDispatch.test_app
|
510
|
-
end
|
511
|
-
|
512
|
-
def self.app=(app)
|
513
|
-
@@app = app
|
514
|
-
end
|
515
|
-
|
516
|
-
def app
|
517
|
-
super || self.class.app
|
518
|
-
end
|
519
|
-
|
520
|
-
def url_options
|
521
|
-
reset! unless integration_session
|
522
|
-
integration_session.url_options
|
523
|
-
end
|
524
|
-
end
|
525
|
-
end
|