From a544d7aa372a080b77940571a3a169496045670e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 7 Dec 2004 11:10:46 +0000 Subject: Added all the HTTP methods as alternatives to the generic "process" for functional testing #276 [Tobias Luetke] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@58 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 13 ++++++++++ actionpack/lib/action_controller/test_process.rb | 15 +++++++++++- .../test/controller/action_pack_assertions_test.rb | 28 +++++++++++++++++++++- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 4d39e25d53..793c93adb8 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -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. diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index 41386ee7d0..41fc2f209b 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -190,10 +190,23 @@ 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 \ No newline at end of file diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index 6d727be5a2..489373d5c3 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -54,6 +54,18 @@ class ActionPackAssertionsController < ActionController::Base 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 @@ ActionPackAssertionsController.template_root = File.dirname(__FILE__) + "/../fix # 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 @@ class ActionPackAssertionsControllerTest < Test::Unit::TestCase 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 -- cgit v1.2.3