Skip to content

Commit abeb77b

Browse files
committed
Make TestProcess methods public for access via Integration::Session. Make return values from some of the Integration::Session methods sane.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3724 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
1 parent 9ded584 commit abeb77b

File tree

2 files changed

+112
-115
lines changed

2 files changed

+112
-115
lines changed

actionpack/lib/action_controller/integration_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -72,6 +72,7 @@ def reset!
72
def https!(flag=true)
72
def https!(flag=true)
73
@https = flag
73
@https = flag
74
initialize_url_writer
74
initialize_url_writer
75+
@https
75
end
76
end
76

77

77
# Return +true+ if the session is mimicing a secure HTTPS request.
78
# Return +true+ if the session is mimicing a secure HTTPS request.
@@ -89,6 +90,7 @@ def https?
89
def host!(name)
90
def host!(name)
90
@host = name
91
@host = name
91
initialize_url_writer
92
initialize_url_writer
93+
@host
92
end
94
end
93

95

94
# Follow a single redirect response. If the last response was not a
96
# Follow a single redirect response. If the last response was not a
@@ -97,6 +99,7 @@ def host!(name)
97
def follow_redirect!
99
def follow_redirect!
98
raise "not a redirect! #{@status} #{@status_message}" unless redirect?
100
raise "not a redirect! #{@status} #{@status_message}" unless redirect?
99
get(interpret_uri(headers["location"].first))
101
get(interpret_uri(headers["location"].first))
102+
status
100
end
103
end
101

104

102
# Performs a GET request, following any subsequent redirect. Note that
105
# Performs a GET request, following any subsequent redirect. Note that
@@ -106,13 +109,15 @@ def follow_redirect!
106
def get_via_redirect(path, args={})
109
def get_via_redirect(path, args={})
107
get path, args
110
get path, args
108
follow_redirect! while redirect?
111
follow_redirect! while redirect?
112+
status
109
end
113
end
110

114

111
# Performs a POST request, following any subsequent redirect. This is
115
# Performs a POST request, following any subsequent redirect. This is
112
# vulnerable to infinite loops, the same as #get_via_redirect.
116
# vulnerable to infinite loops, the same as #get_via_redirect.
113
def post_via_redirect(path, args={})
117
def post_via_redirect(path, args={})
114
post path, args
118
post path, args
115
follow_redirect! while redirect?
119
follow_redirect! while redirect?
120+
status
116
end
121
end
117

122

118
# Returns +true+ if the last response was a redirect.
123
# Returns +true+ if the last response was a redirect.
@@ -217,6 +222,7 @@ def process(method, path, parameters=nil, headers=nil)
217
@response = @controller.response
222
@response = @controller.response
218

223

219
parse_result
224
parse_result
225+
return status
220
end
226
end
221

227

222
# Parses the result of the response and extracts the various values,
228
# Parses the result of the response and extracts the various values,
@@ -254,7 +260,7 @@ def initialize_url_writer
254
'QUERY_STRING' => "",
260
'QUERY_STRING' => "",
255
"REQUEST_URI" => "/",
261
"REQUEST_URI" => "/",
256
"HTTP_HOST" => host,
262
"HTTP_HOST" => host,
257-
"SERVER_PORT" => https? ? "80" : "443",
263+
"SERVER_PORT" => https? ? "443" : "80",
258
"HTTPS" => https? ? "on" : "off")
264
"HTTPS" => https? ? "on" : "off")
259
@rewriter = ActionController::UrlRewriter.new(ActionController::CgiRequest.new(cgi), {})
265
@rewriter = ActionController::UrlRewriter.new(ActionController::CgiRequest.new(cgi), {})
260
end
266
end

actionpack/lib/action_controller/test_process.rb

Lines changed: 105 additions & 114 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -274,145 +274,136 @@ def delete() @attributes = {} end
274
end
274
end
275

275

276
module TestProcess
276
module TestProcess
277-
private
277+
def self.included(base)
278-
def self.included(base)
278+
# execute the request simulating a specific http method and set/volley the response
279-
# execute the request simulating a specific http method and set/volley the response
279+
%w( get post put delete head ).each do |method|
280-
%w( get post put delete head ).each do |method|
280+
base.class_eval <<-EOV, __FILE__, __LINE__
281-
base.class_eval <<-EOV, __FILE__, __LINE__
281+
def #{method}(action, parameters = nil, session = nil, flash = nil)
282-
def #{method}(action, parameters = nil, session = nil, flash = nil)
282+
@request.env['REQUEST_METHOD'] = "#{method.upcase}" if @request
283-
@request.env['REQUEST_METHOD'] = "#{method.upcase}" if @request
283+
process(action, parameters, session, flash)
284-
process(action, parameters, session, flash)
284+
end
285-
end
285+
EOV
286-
EOV
287-
end
288
end
286
end
287+
end
289

288

290-
# execute the request and set/volley the response
289+
# execute the request and set/volley the response
291-
def process(action, parameters = nil, session = nil, flash = nil)
290+
def process(action, parameters = nil, session = nil, flash = nil)
292-
# Sanity check for required instance variables so we can give an
291+
# Sanity check for required instance variables so we can give an
293-
# understandable error message.
292+
# understandable error message.
294-
%w(controller request response).each do |iv_name|
293+
%w(controller request response).each do |iv_name|
295-
raise "@#{iv_name} is nil: make sure you set it in your test's setup method." if instance_variable_get("@#{iv_name}").nil?
294+
raise "@#{iv_name} is nil: make sure you set it in your test's setup method." if instance_variable_get("@#{iv_name}").nil?
296-
end
295+
end
297

296

298-
@request.recycle!
297+
@request.recycle!
299

298

300-
@html_document = nil
299+
@html_document = nil
301-
@request.env['REQUEST_METHOD'] ||= "GET"
300+
@request.env['REQUEST_METHOD'] ||= "GET"
302-
@request.action = action.to_s
301+
@request.action = action.to_s
303

302

304-
parameters ||= {}
303+
parameters ||= {}
305-
@request.assign_parameters(@controller.class.controller_path, action.to_s, parameters)
304+
@request.assign_parameters(@controller.class.controller_path, action.to_s, parameters)
306

305

307-
@request.session = ActionController::TestSession.new(session) unless session.nil?
306+
@request.session = ActionController::TestSession.new(session) unless session.nil?
308-
@request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash
307+
@request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash
309-
build_request_uri(action, parameters)
308+
build_request_uri(action, parameters)
310-
@controller.process(@request, @response)
309+
@controller.process(@request, @response)
311-
end
310+
end
312

311

313-
def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
312+
def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
314-
@request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
313+
@request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
315-
returning self.send(request_method, action, parameters, session, flash) do
314+
returning self.send(request_method, action, parameters, session, flash) do
316-
@request.env.delete 'HTTP_X_REQUESTED_WITH'
315+
@request.env.delete 'HTTP_X_REQUESTED_WITH'
317-
end
318
end
316
end
319-
alias xhr :xml_http_request
317+
end
320-
318+
alias xhr :xml_http_request
321-
def follow_redirect
322-
if @response.redirected_to[:controller]
323-
raise "Can't follow redirects outside of current controller (#{@response.redirected_to[:controller]})"
324-
end
325

319

326-
get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys)
320+
def follow_redirect
321+
if @response.redirected_to[:controller]
322+
raise "Can't follow redirects outside of current controller (#{@response.redirected_to[:controller]})"
327
end
323
end
328

324

329-
def assigns(key = nil)
325+
get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys)
330-
if key.nil?
326+
end
331-
@response.template.assigns
332-
else
333-
@response.template.assigns[key.to_s]
334-
end
335-
end
336

327

337-
def build_request_uri(action, parameters)
328+
def build_request_uri(action, parameters)
338-
options = @controller.send(:rewrite_options, parameters)
329+
options = @controller.send(:rewrite_options, parameters)
339-
options.update(:only_path => true, :action => action)
330+
options.update(:only_path => true, :action => action)
340-
url = ActionController::UrlRewriter.new(@request, parameters)
331+
url = ActionController::UrlRewriter.new(@request, parameters)
341-
@request.set_REQUEST_URI(url.rewrite(options))
332+
@request.set_REQUEST_URI(url.rewrite(options))
342-
end
333+
end
343

334

344-
def session
335+
def session
345-
@response.session
336+
@response.session
346-
end
337+
end
347

338

348-
def flash
339+
def flash
349-
@response.flash
340+
@response.flash
350-
end
341+
end
351

342

352-
def cookies
343+
def cookies
353-
@response.cookies
344+
@response.cookies
354-
end
345+
end
355

346

356-
def redirect_to_url
347+
def redirect_to_url
357-
@response.redirect_url
348+
@response.redirect_url
358-
end
349+
end
359

350

360-
def build_request_uri(action, parameters)
351+
def build_request_uri(action, parameters)
361-
unless @request.env['REQUEST_URI']
352+
unless @request.env['REQUEST_URI']
362-
options = @controller.send(:rewrite_options, parameters)
353+
options = @controller.send(:rewrite_options, parameters)
363-
options.update(:only_path => true, :action => action)
354+
options.update(:only_path => true, :action => action)
364

355

365-
url = ActionController::UrlRewriter.new(@request, parameters)
356+
url = ActionController::UrlRewriter.new(@request, parameters)
366-
@request.set_REQUEST_URI(url.rewrite(options))
357+
@request.set_REQUEST_URI(url.rewrite(options))
367-
end
368
end
358
end
359+
end
369

360

370-
def html_document
361+
def html_document
371-
@html_document ||= HTML::Document.new(@response.body)
362+
@html_document ||= HTML::Document.new(@response.body)
372-
end
363+
end
373

364

374-
def find_tag(conditions)
365+
def find_tag(conditions)
375-
html_document.find(conditions)
366+
html_document.find(conditions)
376-
end
367+
end
377

368

378-
def find_all_tag(conditions)
369+
def find_all_tag(conditions)
379-
html_document.find_all(conditions)
370+
html_document.find_all(conditions)
380-
end
371+
end
381

372

382-
def method_missing(selector, *args)
373+
def method_missing(selector, *args)
383-
return @controller.send(selector, *args) if ActionController::Routing::NamedRoutes::Helpers.include?(selector)
374+
return @controller.send(selector, *args) if ActionController::Routing::NamedRoutes::Helpers.include?(selector)
384-
return super
375+
return super
385-
end
376+
end
386

377

387-
# A helper to make it easier to test different route configurations.
378+
# A helper to make it easier to test different route configurations.
388-
# This method temporarily replaces ActionController::Routing::Routes
379+
# This method temporarily replaces ActionController::Routing::Routes
389-
# with a new RouteSet instance.
380+
# with a new RouteSet instance.
390-
#
381+
#
391-
# The new instance is yielded to the passed block. Typically the block
382+
# The new instance is yielded to the passed block. Typically the block
392-
# will create some routes using map.draw { map.connect ... }:
383+
# will create some routes using map.draw { map.connect ... }:
393-
#
384+
#
394-
# with_routing do |set|
385+
# with_routing do |set|
395-
# set.draw { set.connect ':controller/:id/:action' }
386+
# set.draw { set.connect ':controller/:id/:action' }
396-
# assert_equal(
387+
# assert_equal(
397-
# ['/content/10/show', {}],
388+
# ['/content/10/show', {}],
398-
# set.generate(:controller => 'content', :id => 10, :action => 'show')
389+
# set.generate(:controller => 'content', :id => 10, :action => 'show')
399-
# )
390+
# )
400-
# end
391+
# end
401-
#
392+
#
402-
def with_routing
393+
def with_routing
403-
real_routes = ActionController::Routing::Routes
394+
real_routes = ActionController::Routing::Routes
404-
ActionController::Routing.send :remove_const, :Routes
395+
ActionController::Routing.send :remove_const, :Routes
405-
396+
406-
temporary_routes = ActionController::Routing::RouteSet.new
397+
temporary_routes = ActionController::Routing::RouteSet.new
407-
ActionController::Routing.send :const_set, :Routes, temporary_routes
398+
ActionController::Routing.send :const_set, :Routes, temporary_routes
408-
399+
409-
yield temporary_routes
400+
yield temporary_routes
410-
ensure
401+
ensure
411-
if ActionController::Routing.const_defined? :Routes
402+
if ActionController::Routing.const_defined? :Routes
412-
ActionController::Routing.send(:remove_const, :Routes)
403+
ActionController::Routing.send(:remove_const, :Routes)
413-
end
414-
ActionController::Routing.const_set(:Routes, real_routes) if real_routes
415
end
404
end
405+
ActionController::Routing.const_set(:Routes, real_routes) if real_routes
406+
end
416
end
407
end
417
end
408
end
418

409

0 commit comments

Comments
 (0)