Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Ac process #4270

Closed
wants to merge 18 commits into from

2 participants

@apotonick

Not 18, but the last 8 commits (from 18deb74 to HEAD). Didn't know how to limit that on github.

Started cleaning up ActionController::TestCase and allow sending real documents to #post, #get, etc., like:

post :create, "<order />"
@josevalim
Owner

Your pull request cannot be automatically merged as Github says and it also has a bunch of merge commits. Could you please rebase it and squash it into one commit? Thanks!

@josevalim
Owner

New pull request please?

@apotonick apotonick closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 30, 2010
  1. @apotonick
  2. @apotonick
Commits on Jun 28, 2011
  1. @apotonick
  2. @apotonick
Commits on Jul 13, 2011
  1. @apotonick
Commits on Sep 20, 2011
  1. @apotonick
  2. @apotonick

    merge.

    apotonick authored
Commits on Sep 22, 2011
  1. @apotonick
  2. @apotonick
Commits on Dec 22, 2011
  1. @apotonick
Commits on Dec 28, 2011
  1. @apotonick

    changed the API for AC::TestCase#process: the second argument is now …

    apotonick authored
    …the HTTP method since this is always required.
  2. @apotonick
Commits on Jan 1, 2012
  1. @apotonick

    added tests.

    apotonick authored
  2. @apotonick
  3. @apotonick
  4. @apotonick
  5. @apotonick
  6. @apotonick

    added docs.

    apotonick authored
This page is out of date. Refresh to see the latest.
View
61 actionpack/lib/action_controller/test_case.rb
@@ -252,6 +252,12 @@ def exists?
# end
# end
#
+ # You can also send a real document in the simulated HTTP request.
+ #
+ # def test_create
+ # json = {:book => { :title => "Love Hina" }}.to_json
+ # post :create, json
+ #
# == Special instance variables
#
# ActionController::TestCase will also automatically provide the following instance
@@ -376,23 +382,23 @@ def prepare_controller_class(new_class)
end
# 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")
+ def get(action, *args)
+ process(action, "GET", *args)
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")
+ def post(action, *args)
+ process(action, "POST", *args)
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")
+ def put(action, *args)
+ process(action, "PUT", *args)
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")
+ def delete(action, *args)
+ process(action, "DELETE", *args)
end
# Executes a request simulating HEAD HTTP method and set/volley the response
@@ -423,19 +429,20 @@ def paramify_values(hash_or_array_or_value)
end
end
- def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
+ def process(action, http_method = 'GET', *args)
+ check_required_ivars
+ http_method, args = handle_old_process_api(http_method, args)
+
+ if args.first.is_a?(String)
+ @request.env['RAW_POST_DATA'] = args.shift
+ end
+
+ parameters, session, flash = args
+
# Ensure that numbers and symbols passed as params are converted to
# proper params, as is the case when engaging rack.
parameters = paramify_values(parameters)
- # Sanity check for required instance variables so we can give an
- # understandable error message.
- %w(@routes @controller @request @response).each do |iv_name|
- if !(instance_variable_names.include?(iv_name) || instance_variable_names.include?(iv_name.to_sym)) || instance_variable_get(iv_name).nil?
- raise "#{iv_name} is nil: make sure you set it in your test's setup method."
- end
- end
-
@request.recycle!
@response.recycle!
@controller.response_body = nil
@@ -496,6 +503,26 @@ def rescue_action_in_public!
end
private
+ def check_required_ivars
+ # Sanity check for required instance variables so we can give an
+ # understandable error message.
+ %w(@routes @controller @request @response).each do |iv_name|
+ if !(instance_variable_names.include?(iv_name) || instance_variable_names.include?(iv_name.to_sym)) || instance_variable_get(iv_name).nil?
+ raise "#{iv_name} is nil: make sure you set it in your test's setup method."
+ end
+ end
+ end
+
+ def handle_old_process_api(http_method, args)
+ # 4.0: Remove this method.
+ if http_method.is_a?(Hash)
+ ActiveSupport::Deprecation.warn("TestCase#process now expects the HTTP method as second argument: process(action, http_method, params, session, flash)")
+ args.unshift(http_method)
+ http_method = args.last.is_a?(String) ? args.last : "GET"
+ end
+
+ [http_method, args]
+ end
def build_request_uri(action, parameters)
unless @request.env["PATH_INFO"]
View
8 actionpack/test/abstract/callbacks_test.rb
@@ -245,14 +245,14 @@ class TestHalting < ActiveSupport::TestCase
assert_equal "Success", controller.response_body
end
end
-
+
class CallbacksWithArgs < ControllerWithCallbacks
set_callback :process_action, :before, :first
def first
@text = "Hello world"
end
-
+
def index(text)
self.response_body = @text + text
end
@@ -261,11 +261,9 @@ def index(text)
class TestCallbacksWithArgs < ActiveSupport::TestCase
test "callbacks still work when invoking process with multiple args" do
controller = CallbacksWithArgs.new
- controller.process(:index, " Howdy!")
+ result = controller.process(:index, " Howdy!")
assert_equal "Hello world Howdy!", controller.response_body
end
end
-
-
end
end
View
38 actionpack/test/controller/test_test.rb → actionpack/test/controller/test_case_test.rb
@@ -171,6 +171,21 @@ def test_body_stream
assert_equal params.to_query, @response.body
end
+
+ def test_document_body_and_params_with_post
+ post :test_params, :id => 1
+ assert_equal("{\"id\"=>\"1\", \"controller\"=>\"test_test/test\", \"action\"=>\"test_params\"}", @response.body)
+ end
+
+ def test_document_body_with_post
+ post :render_body, "document body"
+ assert_equal "document body", @response.body
+ end
+
+ def test_document_body_with_put
+ put :render_body, "document body"
+ assert_equal "document body", @response.body
+ end
def test_process_without_flash
process :set_flash
@@ -178,12 +193,12 @@ def test_process_without_flash
end
def test_process_with_flash
- process :set_flash, nil, nil, { "test" => "value" }
+ process :set_flash, "GET", nil, nil, { "test" => "value" }
assert_equal '>value<', flash['test']
end
def test_process_with_flash_now
- process :set_flash_now, nil, nil, { "test_now" => "value_now" }
+ process :set_flash_now, "GET", nil, nil, { "test_now" => "value_now" }
assert_equal '>value_now<', flash['test_now']
end
@@ -196,7 +211,7 @@ def test_process_with_session
end
def test_process_with_session_arg
- process :no_op, nil, { 'string' => 'value1', :symbol => 'value2' }
+ process :no_op, "GET", nil, { 'string' => 'value1', :symbol => 'value2' }
assert_equal 'value1', session['string']
assert_equal 'value1', session[:string]
assert_equal 'value2', session['symbol']
@@ -227,18 +242,25 @@ def test_process_with_request_uri_with_no_params
end
def test_process_with_request_uri_with_params
- process :test_uri, :id => 7
+ process :test_uri, "GET", :id => 7
assert_equal "/test_test/test/test_uri/7", @response.body
end
+
+ def test_process_with_old_api
+ assert_deprecated do
+ process :test_uri, :id => 7
+ assert_equal "/test_test/test/test_uri/7", @response.body
+ end
+ end
def test_process_with_request_uri_with_params_with_explicit_uri
@request.env['PATH_INFO'] = "/explicit/uri"
- process :test_uri, :id => 7
+ process :test_uri, "GET", :id => 7
assert_equal "/explicit/uri", @response.body
end
def test_process_with_query_string
- process :test_query_string, :q => 'test'
+ process :test_query_string, "GET", :q => 'test'
assert_equal "q=test", @response.body
end
@@ -250,9 +272,9 @@ def test_process_with_query_string_with_explicit_uri
end
def test_multiple_calls
- process :test_only_one_param, :left => true
+ process :test_only_one_param, "GET", :left => true
assert_equal "OK", @response.body
- process :test_only_one_param, :right => true
+ process :test_only_one_param, "GET", :right => true
assert_equal "OK", @response.body
end
View
1  actionpack/test/template/lookup_context_test.rb
@@ -250,3 +250,4 @@ def setup
end
end
+
Something went wrong with that request. Please try again.