2009-05-13 20:00:36 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'controller/fake_models'
|
2011-12-23 13:47:21 -05:00
|
|
|
require 'active_support/logger'
|
2009-05-13 20:00:36 -04:00
|
|
|
require 'pathname'
|
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
class RenderJsonTest < ActionController::TestCase
|
2010-04-22 06:12:38 -04:00
|
|
|
class JsonRenderable
|
|
|
|
def as_json(options={})
|
|
|
|
hash = { :a => :b, :c => :d, :e => :f }
|
|
|
|
hash.except!(*options[:except]) if options[:except]
|
|
|
|
hash
|
|
|
|
end
|
2010-09-18 10:00:23 -04:00
|
|
|
|
|
|
|
def to_json(options = {})
|
|
|
|
super :except => [:c, :e]
|
|
|
|
end
|
2010-04-22 06:12:38 -04:00
|
|
|
end
|
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
protect_from_forgery
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
def self.controller_path
|
|
|
|
'test'
|
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
def render_json_nil
|
|
|
|
render :json => nil
|
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2011-01-21 19:13:58 -05:00
|
|
|
def render_json_render_to_string
|
2015-07-17 21:48:00 -04:00
|
|
|
render plain: render_to_string(json: '[]')
|
2011-01-21 19:13:58 -05:00
|
|
|
end
|
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
def render_json_hello_world
|
|
|
|
render :json => ActiveSupport::JSON.encode(:hello => 'world')
|
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2010-02-24 13:17:24 -05:00
|
|
|
def render_json_hello_world_with_status
|
|
|
|
render :json => ActiveSupport::JSON.encode(:hello => 'world'), :status => 401
|
|
|
|
end
|
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
def render_json_hello_world_with_callback
|
|
|
|
render :json => ActiveSupport::JSON.encode(:hello => 'world'), :callback => 'alert'
|
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
def render_json_with_custom_content_type
|
|
|
|
render :json => ActiveSupport::JSON.encode(:hello => 'world'), :content_type => 'text/javascript'
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_symbol_json
|
|
|
|
render :json => ActiveSupport::JSON.encode(:hello => 'world')
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_json_with_render_to_string
|
|
|
|
render :json => {:hello => render_to_string(:partial => 'partial')}
|
|
|
|
end
|
2010-04-22 06:12:38 -04:00
|
|
|
|
|
|
|
def render_json_with_extra_options
|
|
|
|
render :json => JsonRenderable.new, :except => [:c, :e]
|
|
|
|
end
|
2010-09-18 10:00:23 -04:00
|
|
|
|
|
|
|
def render_json_without_options
|
|
|
|
render :json => JsonRenderable.new
|
|
|
|
end
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
|
|
|
|
tests TestController
|
|
|
|
|
|
|
|
def setup
|
|
|
|
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
|
|
|
|
# a more accurate simulation of what happens in "real life".
|
|
|
|
super
|
2011-12-23 13:47:21 -05:00
|
|
|
@controller.logger = ActiveSupport::Logger.new(nil)
|
2009-05-13 20:00:36 -04:00
|
|
|
|
|
|
|
@request.host = "www.nextangle.com"
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
|
|
|
|
2009-05-13 20:00:36 -04:00
|
|
|
def test_render_json_nil
|
|
|
|
get :render_json_nil
|
|
|
|
assert_equal 'null', @response.body
|
|
|
|
assert_equal 'application/json', @response.content_type
|
|
|
|
end
|
|
|
|
|
2011-01-21 19:13:58 -05:00
|
|
|
def test_render_json_render_to_string
|
|
|
|
get :render_json_render_to_string
|
|
|
|
assert_equal '[]', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-05-13 20:00:36 -04:00
|
|
|
def test_render_json
|
|
|
|
get :render_json_hello_world
|
|
|
|
assert_equal '{"hello":"world"}', @response.body
|
|
|
|
assert_equal 'application/json', @response.content_type
|
|
|
|
end
|
|
|
|
|
2010-02-24 13:17:24 -05:00
|
|
|
def test_render_json_with_status
|
|
|
|
get :render_json_hello_world_with_status
|
|
|
|
assert_equal '{"hello":"world"}', @response.body
|
|
|
|
assert_equal 401, @response.status
|
|
|
|
end
|
|
|
|
|
2009-05-13 20:00:36 -04:00
|
|
|
def test_render_json_with_callback
|
2015-02-01 08:07:42 -05:00
|
|
|
get :render_json_hello_world_with_callback, xhr: true
|
2014-07-09 14:33:09 -04:00
|
|
|
assert_equal '/**/alert({"hello":"world"})', @response.body
|
2012-04-29 10:55:35 -04:00
|
|
|
assert_equal 'text/javascript', @response.content_type
|
2009-05-13 20:00:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_json_with_custom_content_type
|
2015-02-01 08:07:42 -05:00
|
|
|
get :render_json_with_custom_content_type, xhr: true
|
2009-05-13 20:00:36 -04:00
|
|
|
assert_equal '{"hello":"world"}', @response.body
|
|
|
|
assert_equal 'text/javascript', @response.content_type
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_symbol_json
|
|
|
|
get :render_symbol_json
|
|
|
|
assert_equal '{"hello":"world"}', @response.body
|
|
|
|
assert_equal 'application/json', @response.content_type
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_json_with_render_to_string
|
|
|
|
get :render_json_with_render_to_string
|
|
|
|
assert_equal '{"hello":"partial html"}', @response.body
|
|
|
|
assert_equal 'application/json', @response.content_type
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
2010-04-22 06:12:38 -04:00
|
|
|
|
|
|
|
def test_render_json_forwards_extra_options
|
|
|
|
get :render_json_with_extra_options
|
|
|
|
assert_equal '{"a":"b"}', @response.body
|
|
|
|
assert_equal 'application/json', @response.content_type
|
|
|
|
end
|
2010-09-18 10:00:23 -04:00
|
|
|
|
|
|
|
def test_render_json_calls_to_json_from_object
|
|
|
|
get :render_json_without_options
|
|
|
|
assert_equal '{"a":"b"}', @response.body
|
|
|
|
end
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|