diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-12-06 22:27:08 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-12-06 22:27:08 +0000 |
commit | 42596543dc0bac7aad9763b82ee4cc2b17f3110c (patch) | |
tree | 873c69b136730b34564b45bbd7c220e165272ddf /actionpack/test/controller | |
parent | 4c9000d51d0c39cfff438767f4709b8d8ad1c692 (diff) | |
download | rails-42596543dc0bac7aad9763b82ee4cc2b17f3110c.tar.gz rails-42596543dc0bac7aad9763b82ee4cc2b17f3110c.tar.bz2 rails-42596543dc0bac7aad9763b82ee4cc2b17f3110c.zip |
respond_to recognizes JSON. render :json => @person.to_json automatically sets the content type and takes a :callback option to specify a client-side function to call using the rendered JSON as an argument. References #4185.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5694 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/test/controller')
-rw-r--r-- | actionpack/test/controller/mime_responds_test.rb | 20 | ||||
-rw-r--r-- | actionpack/test/controller/render_test.rb | 18 |
2 files changed, 38 insertions, 0 deletions
diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 1d7fdc0052..8f1c56710f 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -19,6 +19,13 @@ class RespondToController < ActionController::Base type.all { render :text => "Nothing" } end end + + def json_or_yaml + respond_to do |type| + type.json { render :text => "JSON" } + type.yaml { render :yaml => "YAML" } + end + end def html_or_xml respond_to do |type| @@ -163,6 +170,19 @@ class MimeControllerTest < Test::Unit::TestCase get :just_xml assert_response 406 end + + def test_json_or_yaml + get :json_or_yaml + assert_equal 'JSON', @response.body + + @request.env["HTTP_ACCEPT"] = "text/yaml" + get :json_or_yaml + assert_equal 'YAML', @response.body + + @request.env["HTTP_ACCEPT"] = "text/x-json" + get :json_or_yaml + assert_equal 'JSON', @response.body + end def test_js_or_anything @request.env["HTTP_ACCEPT"] = "text/javascript, */*" diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 66429662e6..3cff829f8a 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -38,6 +38,14 @@ class TestController < ActionController::Base def render_text_hello_world render_text "hello world" end + + def render_json_hello_world + render_json({:hello => 'world'}.to_json) + end + + def render_json_hello_world_with_callback + render_json({:hello => 'world'}.to_json, 'alert') + end def render_custom_code render_text "hello world", "404 Moved" @@ -163,6 +171,16 @@ class RenderTest < Test::Unit::TestCase get :render_text_hello_world assert_equal "hello world", @response.body end + + def test_do_with_render_json + get :render_json_hello_world + assert_equal '{hello: "world"}', @response.body + end + + def test_do_with_render_json_with_callback + get :render_json_hello_world_with_callback + assert_equal 'alert({hello: "world"})', @response.body + end def test_do_with_render_custom_code get :render_custom_code |