Permalink
Browse files

Undry ActionController::TestCase#<HTTP_METHODS> for better documentation

  • Loading branch information...
lifo committed Dec 25, 2008
1 parent e898f82 commit 6e2a771661a47fb682108648244837f8616e350d
Showing with 26 additions and 14 deletions.
  1. +25 −12 actionpack/lib/action_controller/test_process.rb
  2. +1 −2 actionpack/test/controller/caching_test.rb
@@ -388,20 +388,33 @@ def method_missing(method_name, *args, &block) #:nodoc:

module TestProcess
def self.included(base)
# execute the request simulating a specific HTTP method and set/volley the response
# TODO: this should be un-DRY'ed for the sake of API documentation.
%w( get post put delete head ).each do |method|
base.class_eval <<-EOV, __FILE__, __LINE__
def #{method}(action, parameters = nil, session = nil, flash = nil)
@request.env['REQUEST_METHOD'] = "#{method.upcase}" if defined?(@request)
process(action, parameters, session, flash)
end
EOV
# Executes a request simulating GET HTTP method and set/volley the response
def get(action, parameters = nil, session = nil, flash = nil)
process(action, parameters, session, flash, "GET")
end

# Executes a request simulating POST HTTP method and set/volley the response
def post(action, parameters = nil, session = nil, flash = nil)
process(action, parameters, session, flash, "POST")
end

# Executes a request simulating PUT HTTP method and set/volley the response
def put(action, parameters = nil, session = nil, flash = nil)
process(action, parameters, session, flash, "PUT")
end

# Executes a request simulating DELETE HTTP method and set/volley the response
def delete(action, parameters = nil, session = nil, flash = nil)
process(action, parameters, session, flash, "DELETE")
end

# Executes a request simulating HEAD HTTP method and set/volley the response
def head(action, parameters = nil, session = nil, flash = nil)
process(action, parameters, session, flash, "HEAD")
end
end

# execute the request and set/volley the response
def process(action, parameters = nil, session = nil, flash = nil)
def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
# Sanity check for required instance variables so we can give an
# understandable error message.
%w(@controller @request @response).each do |iv_name|
@@ -414,7 +427,7 @@ def process(action, parameters = nil, session = nil, flash = nil)
@response.recycle!

@html_document = nil
@request.env['REQUEST_METHOD'] ||= "GET"
@request.env['REQUEST_METHOD'] = http_method

@request.action = action.to_s

@@ -121,8 +121,7 @@ def test_should_cache_ok_at_custom_path
[:get, :post, :put, :delete].each do |method|
unless method == :get and status == :ok
define_method "test_shouldnt_cache_#{method}_with_#{status}_status" do
@request.env['REQUEST_METHOD'] = method.to_s.upcase
process status
send(method, status)
assert_response status
assert_page_not_cached status, "#{method} with #{status} status shouldn't have been cached"
end

0 comments on commit 6e2a771

Please sign in to comment.