2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "controller/fake_models"
|
|
|
|
require "active_support/logger"
|
|
|
|
require "pathname"
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
class RenderJsonTest < ActionController::TestCase
|
2010-04-22 06:12:38 -04:00
|
|
|
class JsonRenderable
|
2016-10-28 23:05:58 -04:00
|
|
|
def as_json(options = {})
|
2016-08-06 13:35:13 -04:00
|
|
|
hash = { a: :b, c: :d, e: :f }
|
2010-04-22 06:12:38 -04:00
|
|
|
hash.except!(*options[:except]) if options[:except]
|
|
|
|
hash
|
|
|
|
end
|
2010-09-18 10:00:23 -04:00
|
|
|
|
|
|
|
def to_json(options = {})
|
2016-08-06 13:35:13 -04:00
|
|
|
super except: [:c, :e]
|
2010-09-18 10:00:23 -04:00
|
|
|
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
|
2016-08-06 12:54:50 -04:00
|
|
|
"test"
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
def render_json_nil
|
2016-08-06 13:35:13 -04:00
|
|
|
render json: nil
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2011-01-21 19:13:58 -05:00
|
|
|
def render_json_render_to_string
|
2016-08-06 12:54:50 -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
|
2016-08-06 13:35:13 -04:00
|
|
|
render json: ActiveSupport::JSON.encode(hello: "world")
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2010-02-24 13:17:24 -05:00
|
|
|
def render_json_hello_world_with_status
|
2016-08-06 13:35:13 -04:00
|
|
|
render json: ActiveSupport::JSON.encode(hello: "world"), status: 401
|
2010-02-24 13:17:24 -05:00
|
|
|
end
|
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
def render_json_hello_world_with_callback
|
2016-08-06 13:35:13 -04:00
|
|
|
render json: ActiveSupport::JSON.encode(hello: "world"), callback: "alert"
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
2009-05-13 20:00:36 -04:00
|
|
|
|
2009-09-19 14:04:12 -04:00
|
|
|
def render_json_with_custom_content_type
|
2016-08-06 13:35:13 -04:00
|
|
|
render json: ActiveSupport::JSON.encode(hello: "world"), content_type: "text/javascript"
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_symbol_json
|
2016-08-06 13:35:13 -04:00
|
|
|
render json: ActiveSupport::JSON.encode(hello: "world")
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_json_with_render_to_string
|
2016-08-16 03:30:11 -04:00
|
|
|
render json: { hello: render_to_string(partial: "partial") }
|
2009-09-19 14:04:12 -04:00
|
|
|
end
|
2010-04-22 06:12:38 -04:00
|
|
|
|
|
|
|
def render_json_with_extra_options
|
2016-08-06 13:35:13 -04:00
|
|
|
render json: JsonRenderable.new, except: [:c, :e]
|
2010-04-22 06:12:38 -04:00
|
|
|
end
|
2010-09-18 10:00:23 -04:00
|
|
|
|
|
|
|
def render_json_without_options
|
2016-08-06 13:35:13 -04:00
|
|
|
render json: JsonRenderable.new
|
2010-09-18 10:00:23 -04:00
|
|
|
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
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "null", @response.body
|
|
|
|
assert_equal "application/json", @response.content_type
|
2009-05-13 20:00:36 -04:00
|
|
|
end
|
|
|
|
|
2011-01-21 19:13:58 -05:00
|
|
|
def test_render_json_render_to_string
|
|
|
|
get :render_json_render_to_string
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "[]", @response.body
|
2011-01-21 19:13:58 -05:00
|
|
|
end
|
|
|
|
|
2009-05-13 20:00:36 -04:00
|
|
|
def test_render_json
|
|
|
|
get :render_json_hello_world
|
|
|
|
assert_equal '{"hello":"world"}', @response.body
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "application/json", @response.content_type
|
2009-05-13 20:00:36 -04:00
|
|
|
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
|
2016-08-06 12:54:50 -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
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "text/javascript", @response.content_type
|
2009-05-13 20:00:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_symbol_json
|
|
|
|
get :render_symbol_json
|
|
|
|
assert_equal '{"hello":"world"}', @response.body
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "application/json", @response.content_type
|
2009-05-13 20:00:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_json_with_render_to_string
|
|
|
|
get :render_json_with_render_to_string
|
|
|
|
assert_equal '{"hello":"partial html"}', @response.body
|
2016-08-06 12:54:50 -04:00
|
|
|
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
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "application/json", @response.content_type
|
2010-04-22 06:12:38 -04:00
|
|
|
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
|