Permalink
Browse files

Added all the HTTP methods as alternatives to the generic "process" f…

…or functional testing #276 [Tobias Luetke]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@58 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 9492129 commit a544d7aa372a080b77940571a3a169496045670e @dhh dhh committed Dec 7, 2004
View
@@ -1,5 +1,18 @@
*SVN*
+* Added all the HTTP methods as alternatives to the generic "process" for functional testing #276 [Tobias Luetke]. Examples:
+
+ # Calls Controller#miletone with a GET request
+ process :milestone
+
+ # Calls Controller#miletone with a POST request that has parameters
+ post :milestone, { "name" => "David" }
+
+ # Calls Controller#milestone with a HEAD request that has both parameters and session data
+ head :milestone, { "id" => 1 }, { "user_id" => 23 }
+
+ This is especially useful for testing idiomatic REST web services.
+
* Added proper handling of HEAD requests, so that content isn't returned (Request#head? added as well) #277 [Eric Hodel]
* Added indifference to whether @headers["Content-Type"], @headers["Content-type"], or @headers["content-type"] is used.
@@ -190,10 +190,23 @@ def delete() @attributes = {} end
class Test::Unit::TestCase #:nodoc:
private
# execute the request and set/volley the response
- def process(action, parameters = nil, session = nil)
+ def get(action, parameters = nil, session = nil)
+ @request.env['REQUEST_METHOD'] ||= "GET"
@request.action = action.to_s
@request.parameters.update(parameters) unless parameters.nil?
@request.session = ActionController::TestSession.new(session) unless session.nil?
@controller.process(@request, @response)
end
+
+ # execute the request simulating a specific http method and set/volley the response
+ %w( post put delete head ).each do |method|
+ class_eval <<-EOV
+ def #{method}(action, parameters = nil, session = nil)
+ @request.env['REQUEST_METHOD'] ||= "#{method.upcase}"
+ get(action, parameters, session)
+ end
+EOV
+ end
+
+ alias :process :get
end
@@ -54,6 +54,18 @@ def session_stuffing
session['xmas'] = 'turkey'
render_text "ho ho ho"
end
+
+ # raises exception on get requests
+ def raise_on_get
+ raise "get" if @request.get?
+ render_text "request method: #{@request.env['REQUEST_METHOD']}"
+ end
+
+ # raises exception on post requests
+ def raise_on_post
+ raise "post" if @request.post?
+ render_text "request method: #{@request.env['REQUEST_METHOD']}"
+ end
# 911
def rescue_action(e) raise; end
@@ -70,7 +82,7 @@ def rescue_action(e) raise; end
# a test case to exercise the new capabilities TestRequest & TestResponse
-class ActionPackAssertionsControllerTest < Test::Unit::TestCase
+class ActionPackAssertionsControllerTest < Test::Unit::TestCase
# let's get this party started
def setup
@controller = ActionPackAssertionsController.new
@@ -85,6 +97,20 @@ def test_assert_session_has
assert_session_has 'xmas'
assert_session_has_no 'halloween'
end
+
+ # test the get method, make sure the request really was a get
+ def test_get
+ assert_raise(RuntimeError) { get :raise_on_get }
+ get :raise_on_post
+ assert_equal @response.body, 'request method: GET'
+ end
+
+ # test the get method, make sure the request really was a get
+ def test_post
+ assert_raise(RuntimeError) { post :raise_on_post }
+ post :raise_on_get
+ assert_equal @response.body, 'request method: POST'
+ end
# test the assertion of goodies in the template
def test_assert_template_has

0 comments on commit a544d7a

Please sign in to comment.