Froze rails gems
[depot.git] / vendor / rails / actionpack / lib / action_controller / test_process.rb
1 require 'action_controller/assertions'
2 require 'action_controller/test_case'
3
4 module ActionController #:nodoc:
5 class Base
6 attr_reader :assigns
7
8 # Process a test request called with a TestRequest object.
9 def self.process_test(request)
10 new.process_test(request)
11 end
12
13 def process_test(request) #:nodoc:
14 process(request, TestResponse.new)
15 end
16
17 def process_with_test(*args)
18 returning process_without_test(*args) do
19 @assigns = {}
20 (instance_variable_names - @@protected_instance_variables).each do |var|
21 value = instance_variable_get(var)
22 @assigns[var[1..-1]] = value
23 response.template.assigns[var[1..-1]] = value if response
24 end
25 end
26 end
27
28 alias_method_chain :process, :test
29 end
30
31 class TestRequest < AbstractRequest #:nodoc:
32 attr_accessor :cookies, :session_options
33 attr_accessor :query_parameters, :request_parameters, :path, :session
34 attr_accessor :host, :user_agent
35
36 def initialize(query_parameters = nil, request_parameters = nil, session = nil)
37 @query_parameters = query_parameters || {}
38 @request_parameters = request_parameters || {}
39 @session = session || TestSession.new
40
41 initialize_containers
42 initialize_default_values
43
44 super()
45 end
46
47 def reset_session
48 @session = TestSession.new
49 end
50
51 # Wraps raw_post in a StringIO.
52 def body_stream #:nodoc:
53 StringIO.new(raw_post)
54 end
55
56 # Either the RAW_POST_DATA environment variable or the URL-encoded request
57 # parameters.
58 def raw_post
59 env['RAW_POST_DATA'] ||= returning(url_encoded_request_parameters) { |b| b.force_encoding(Encoding::BINARY) if b.respond_to?(:force_encoding) }
60 end
61
62 def port=(number)
63 @env["SERVER_PORT"] = number.to_i
64 port(true)
65 end
66
67 def action=(action_name)
68 @query_parameters.update({ "action" => action_name })
69 @parameters = nil
70 end
71
72 # Used to check AbstractRequest's request_uri functionality.
73 # Disables the use of @path and @request_uri so superclass can handle those.
74 def set_REQUEST_URI(value)
75 @env["REQUEST_URI"] = value
76 @request_uri = nil
77 @path = nil
78 request_uri(true)
79 path(true)
80 end
81
82 def request_uri=(uri)
83 @request_uri = uri
84 @path = uri.split("?").first
85 end
86
87 def accept=(mime_types)
88 @env["HTTP_ACCEPT"] = Array(mime_types).collect { |mime_types| mime_types.to_s }.join(",")
89 accepts(true)
90 end
91
92 def if_modified_since=(last_modified)
93 @env["HTTP_IF_MODIFIED_SINCE"] = last_modified
94 end
95
96 def if_none_match=(etag)
97 @env["HTTP_IF_NONE_MATCH"] = etag
98 end
99
100 def remote_addr=(addr)
101 @env['REMOTE_ADDR'] = addr
102 end
103
104 def request_uri(*args)
105 @request_uri || super
106 end
107
108 def path(*args)
109 @path || super
110 end
111
112 def assign_parameters(controller_path, action, parameters)
113 parameters = parameters.symbolize_keys.merge(:controller => controller_path, :action => action)
114 extra_keys = ActionController::Routing::Routes.extra_keys(parameters)
115 non_path_parameters = get? ? query_parameters : request_parameters
116 parameters.each do |key, value|
117 if value.is_a? Fixnum
118 value = value.to_s
119 elsif value.is_a? Array
120 value = ActionController::Routing::PathSegment::Result.new(value)
121 end
122
123 if extra_keys.include?(key.to_sym)
124 non_path_parameters[key] = value
125 else
126 path_parameters[key.to_s] = value
127 end
128 end
129 @parameters = nil # reset TestRequest#parameters to use the new path_parameters
130 end
131
132 def recycle!
133 self.request_parameters = {}
134 self.query_parameters = {}
135 self.path_parameters = {}
136 unmemoize_all
137 end
138
139 private
140 def initialize_containers
141 @env, @cookies = {}, {}
142 end
143
144 def initialize_default_values
145 @host = "test.host"
146 @request_uri = "/"
147 @user_agent = "Rails Testing"
148 self.remote_addr = "0.0.0.0"
149 @env["SERVER_PORT"] = 80
150 @env['REQUEST_METHOD'] = "GET"
151 end
152
153 def url_encoded_request_parameters
154 params = self.request_parameters.dup
155
156 %w(controller action only_path).each do |k|
157 params.delete(k)
158 params.delete(k.to_sym)
159 end
160
161 params.to_query
162 end
163 end
164
165 # A refactoring of TestResponse to allow the same behavior to be applied
166 # to the "real" CgiResponse class in integration tests.
167 module TestResponseBehavior #:nodoc:
168 # The response code of the request
169 def response_code
170 status[0,3].to_i rescue 0
171 end
172
173 # Returns a String to ensure compatibility with Net::HTTPResponse
174 def code
175 status.to_s.split(' ')[0]
176 end
177
178 def message
179 status.to_s.split(' ',2)[1]
180 end
181
182 # Was the response successful?
183 def success?
184 (200..299).include?(response_code)
185 end
186
187 # Was the URL not found?
188 def missing?
189 response_code == 404
190 end
191
192 # Were we redirected?
193 def redirect?
194 (300..399).include?(response_code)
195 end
196
197 # Was there a server-side error?
198 def error?
199 (500..599).include?(response_code)
200 end
201
202 alias_method :server_error?, :error?
203
204 # Returns the redirection location or nil
205 def redirect_url
206 headers['Location']
207 end
208
209 # Does the redirect location match this regexp pattern?
210 def redirect_url_match?( pattern )
211 return false if redirect_url.nil?
212 p = Regexp.new(pattern) if pattern.class == String
213 p = pattern if pattern.class == Regexp
214 return false if p.nil?
215 p.match(redirect_url) != nil
216 end
217
218 # Returns the template of the file which was used to
219 # render this response (or nil)
220 def rendered_template
221 template.instance_variable_get(:@_first_render)
222 end
223
224 # A shortcut to the flash. Returns an empty hash if no session flash exists.
225 def flash
226 session['flash'] || {}
227 end
228
229 # Do we have a flash?
230 def has_flash?
231 !session['flash'].empty?
232 end
233
234 # Do we have a flash that has contents?
235 def has_flash_with_contents?
236 !flash.empty?
237 end
238
239 # Does the specified flash object exist?
240 def has_flash_object?(name=nil)
241 !flash[name].nil?
242 end
243
244 # Does the specified object exist in the session?
245 def has_session_object?(name=nil)
246 !session[name].nil?
247 end
248
249 # A shortcut to the template.assigns
250 def template_objects
251 template.assigns || {}
252 end
253
254 # Does the specified template object exist?
255 def has_template_object?(name=nil)
256 !template_objects[name].nil?
257 end
258
259 # Returns the response cookies, converted to a Hash of (name => CGI::Cookie) pairs
260 #
261 # assert_equal ['AuthorOfNewPage'], r.cookies['author'].value
262 def cookies
263 headers['cookie'].inject({}) { |hash, cookie| hash[cookie.name] = cookie; hash }
264 end
265
266 # Returns binary content (downloadable file), converted to a String
267 def binary_content
268 raise "Response body is not a Proc: #{body.inspect}" unless body.kind_of?(Proc)
269 require 'stringio'
270
271 sio = StringIO.new
272 body.call(self, sio)
273
274 sio.rewind
275 sio.read
276 end
277 end
278
279 # Integration test methods such as ActionController::Integration::Session#get
280 # and ActionController::Integration::Session#post return objects of class
281 # TestResponse, which represent the HTTP response results of the requested
282 # controller actions.
283 #
284 # See AbstractResponse for more information on controller response objects.
285 class TestResponse < AbstractResponse
286 include TestResponseBehavior
287
288 def recycle!
289 headers.delete('ETag')
290 headers.delete('Last-Modified')
291 end
292 end
293
294 class TestSession #:nodoc:
295 attr_accessor :session_id
296
297 def initialize(attributes = nil)
298 @session_id = ''
299 @attributes = attributes.nil? ? nil : attributes.stringify_keys
300 @saved_attributes = nil
301 end
302
303 def data
304 @attributes ||= @saved_attributes || {}
305 end
306
307 def [](key)
308 data[key.to_s]
309 end
310
311 def []=(key, value)
312 data[key.to_s] = value
313 end
314
315 def update
316 @saved_attributes = @attributes
317 end
318
319 def delete
320 @attributes = nil
321 end
322
323 def close
324 update
325 delete
326 end
327 end
328
329 # Essentially generates a modified Tempfile object similar to the object
330 # you'd get from the standard library CGI module in a multipart
331 # request. This means you can use an ActionController::TestUploadedFile
332 # object in the params of a test request in order to simulate
333 # a file upload.
334 #
335 # Usage example, within a functional test:
336 # post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png')
337 #
338 # Pass a true third parameter to ensure the uploaded file is opened in binary mode (only required for Windows):
339 # post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png', :binary)
340 require 'tempfile'
341 class TestUploadedFile
342 # The filename, *not* including the path, of the "uploaded" file
343 attr_reader :original_filename
344
345 # The content type of the "uploaded" file
346 attr_accessor :content_type
347
348 def initialize(path, content_type = Mime::TEXT, binary = false)
349 raise "#{path} file does not exist" unless File.exist?(path)
350 @content_type = content_type
351 @original_filename = path.sub(/^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/) { $1 }
352 @tempfile = Tempfile.new(@original_filename)
353 @tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding)
354 @tempfile.binmode if binary
355 FileUtils.copy_file(path, @tempfile.path)
356 end
357
358 def path #:nodoc:
359 @tempfile.path
360 end
361
362 alias local_path path
363
364 def method_missing(method_name, *args, &block) #:nodoc:
365 @tempfile.__send__(method_name, *args, &block)
366 end
367 end
368
369 module TestProcess
370 def self.included(base)
371 # execute the request simulating a specific HTTP method and set/volley the response
372 # TODO: this should be un-DRY'ed for the sake of API documentation.
373 %w( get post put delete head ).each do |method|
374 base.class_eval <<-EOV, __FILE__, __LINE__
375 def #{method}(action, parameters = nil, session = nil, flash = nil)
376 @request.env['REQUEST_METHOD'] = "#{method.upcase}" if defined?(@request)
377 process(action, parameters, session, flash)
378 end
379 EOV
380 end
381 end
382
383 # execute the request and set/volley the response
384 def process(action, parameters = nil, session = nil, flash = nil)
385 # Sanity check for required instance variables so we can give an
386 # understandable error message.
387 %w(@controller @request @response).each do |iv_name|
388 if !(instance_variable_names.include?(iv_name) || instance_variable_names.include?(iv_name.to_sym)) || instance_variable_get(iv_name).nil?
389 raise "#{iv_name} is nil: make sure you set it in your test's setup method."
390 end
391 end
392
393 @request.recycle!
394 @response.recycle!
395
396 @html_document = nil
397 @request.env['REQUEST_METHOD'] ||= "GET"
398
399 @request.action = action.to_s
400
401 parameters ||= {}
402 @request.assign_parameters(@controller.class.controller_path, action.to_s, parameters)
403
404 @request.session = ActionController::TestSession.new(session) unless session.nil?
405 @request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash
406 build_request_uri(action, parameters)
407 @controller.process(@request, @response)
408 end
409
410 def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
411 @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
412 @request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*'
413 returning __send__(request_method, action, parameters, session, flash) do
414 @request.env.delete 'HTTP_X_REQUESTED_WITH'
415 @request.env.delete 'HTTP_ACCEPT'
416 end
417 end
418 alias xhr :xml_http_request
419
420 def assigns(key = nil)
421 if key.nil?
422 @response.template.assigns
423 else
424 @response.template.assigns[key.to_s]
425 end
426 end
427
428 def session
429 @response.session
430 end
431
432 def flash
433 @response.flash
434 end
435
436 def cookies
437 @response.cookies
438 end
439
440 def redirect_to_url
441 @response.redirect_url
442 end
443
444 def build_request_uri(action, parameters)
445 unless @request.env['REQUEST_URI']
446 options = @controller.__send__(:rewrite_options, parameters)
447 options.update(:only_path => true, :action => action)
448
449 url = ActionController::UrlRewriter.new(@request, parameters)
450 @request.set_REQUEST_URI(url.rewrite(options))
451 end
452 end
453
454 def html_document
455 xml = @response.content_type =~ /xml$/
456 @html_document ||= HTML::Document.new(@response.body, false, xml)
457 end
458
459 def find_tag(conditions)
460 html_document.find(conditions)
461 end
462
463 def find_all_tag(conditions)
464 html_document.find_all(conditions)
465 end
466
467 def method_missing(selector, *args)
468 if ActionController::Routing::Routes.named_routes.helpers.include?(selector)
469 @controller.send(selector, *args)
470 else
471 super
472 end
473 end
474
475 # Shortcut for <tt>ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + path, type)</tt>:
476 #
477 # post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png')
478 #
479 # To upload binary files on Windows, pass <tt>:binary</tt> as the last parameter.
480 # This will not affect other platforms:
481 #
482 # post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
483 def fixture_file_upload(path, mime_type = nil, binary = false)
484 ActionController::TestUploadedFile.new(
485 Test::Unit::TestCase.respond_to?(:fixture_path) ? Test::Unit::TestCase.fixture_path + path : path,
486 mime_type,
487 binary
488 )
489 end
490
491 # A helper to make it easier to test different route configurations.
492 # This method temporarily replaces ActionController::Routing::Routes
493 # with a new RouteSet instance.
494 #
495 # The new instance is yielded to the passed block. Typically the block
496 # will create some routes using <tt>map.draw { map.connect ... }</tt>:
497 #
498 # with_routing do |set|
499 # set.draw do |map|
500 # map.connect ':controller/:action/:id'
501 # assert_equal(
502 # ['/content/10/show', {}],
503 # map.generate(:controller => 'content', :id => 10, :action => 'show')
504 # end
505 # end
506 # end
507 #
508 def with_routing
509 real_routes = ActionController::Routing::Routes
510 ActionController::Routing.module_eval { remove_const :Routes }
511
512 temporary_routes = ActionController::Routing::RouteSet.new
513 ActionController::Routing.module_eval { const_set :Routes, temporary_routes }
514
515 yield temporary_routes
516 ensure
517 if ActionController::Routing.const_defined? :Routes
518 ActionController::Routing.module_eval { remove_const :Routes }
519 end
520 ActionController::Routing.const_set(:Routes, real_routes) if real_routes
521 end
522 end
523 end
524
525 module Test
526 module Unit
527 class TestCase #:nodoc:
528 include ActionController::TestProcess
529 end
530 end
531 end