1
+ require 'test_helper'
2
+ require 'pathname'
3
+
4
+ class RenderJsonTest < ActionController ::TestCase
5
+ class JsonRenderable
6
+ def as_json ( options = { } )
7
+ hash = { :a => :b , :c => :d , :e => :f }
8
+ hash . except! ( *options [ :except ] ) if options [ :except ]
9
+ hash
10
+ end
11
+
12
+ def to_json ( options = { } )
13
+ super :except => [ :c , :e ]
14
+ end
15
+ end
16
+
17
+ class JsonSerializer
18
+ def initialize ( object , scope )
19
+ @object , @scope = object , scope
20
+ end
21
+
22
+ def as_json ( *)
23
+ { :object => @object . as_json , :scope => @scope . as_json }
24
+ end
25
+ end
26
+
27
+ class JsonSerializable
28
+ def initialize ( skip = false )
29
+ @skip = skip
30
+ end
31
+
32
+ def active_model_serializer
33
+ JsonSerializer unless @skip
34
+ end
35
+
36
+ def as_json ( *)
37
+ { :serializable_object => true }
38
+ end
39
+ end
40
+
41
+ class TestController < ActionController ::Base
42
+ protect_from_forgery
43
+
44
+ serialization_scope :current_user
45
+ attr_reader :current_user
46
+
47
+ def self . controller_path
48
+ 'test'
49
+ end
50
+
51
+ def render_json_nil
52
+ render :json => nil
53
+ end
54
+
55
+ def render_json_render_to_string
56
+ render :text => render_to_string ( :json => '[]' )
57
+ end
58
+
59
+ def render_json_hello_world
60
+ render :json => ActiveSupport ::JSON . encode ( :hello => 'world' )
61
+ end
62
+
63
+ def render_json_hello_world_with_status
64
+ render :json => ActiveSupport ::JSON . encode ( :hello => 'world' ) , :status => 401
65
+ end
66
+
67
+ def render_json_hello_world_with_callback
68
+ render :json => ActiveSupport ::JSON . encode ( :hello => 'world' ) , :callback => 'alert'
69
+ end
70
+
71
+ def render_json_with_custom_content_type
72
+ render :json => ActiveSupport ::JSON . encode ( :hello => 'world' ) , :content_type => 'text/javascript'
73
+ end
74
+
75
+ def render_symbol_json
76
+ render :json => ActiveSupport ::JSON . encode ( :hello => 'world' )
77
+ end
78
+
79
+ def render_json_with_extra_options
80
+ render :json => JsonRenderable . new , :except => [ :c , :e ]
81
+ end
82
+
83
+ def render_json_without_options
84
+ render :json => JsonRenderable . new
85
+ end
86
+
87
+ def render_json_with_serializer
88
+ @current_user = Struct . new ( :as_json ) . new ( :current_user => true )
89
+ render :json => JsonSerializable . new
90
+ end
91
+
92
+ def render_json_with_serializer_api_but_without_serializer
93
+ @current_user = Struct . new ( :as_json ) . new ( :current_user => true )
94
+ render :json => JsonSerializable . new ( true )
95
+ end
96
+ end
97
+
98
+ tests TestController
99
+
100
+ def setup
101
+ # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
102
+ # a more accurate simulation of what happens in "real life".
103
+ super
104
+ @controller . logger = Logger . new ( nil )
105
+
106
+ @request . host = "www.nextangle.com"
107
+ end
108
+
109
+ def test_render_json_nil
110
+ get :render_json_nil
111
+ assert_equal 'null' , @response . body
112
+ assert_equal 'application/json' , @response . content_type
113
+ end
114
+
115
+ def test_render_json_render_to_string
116
+ get :render_json_render_to_string
117
+ assert_equal '[]' , @response . body
118
+ end
119
+
120
+
121
+ def test_render_json
122
+ get :render_json_hello_world
123
+ assert_equal '{"hello":"world"}' , @response . body
124
+ assert_equal 'application/json' , @response . content_type
125
+ end
126
+
127
+ def test_render_json_with_status
128
+ get :render_json_hello_world_with_status
129
+ assert_equal '{"hello":"world"}' , @response . body
130
+ assert_equal 401 , @response . status
131
+ end
132
+
133
+ def test_render_json_with_callback
134
+ get :render_json_hello_world_with_callback
135
+ assert_equal 'alert({"hello":"world"})' , @response . body
136
+ assert_equal 'application/json' , @response . content_type
137
+ end
138
+
139
+ def test_render_json_with_custom_content_type
140
+ get :render_json_with_custom_content_type
141
+ assert_equal '{"hello":"world"}' , @response . body
142
+ assert_equal 'text/javascript' , @response . content_type
143
+ end
144
+
145
+ def test_render_symbol_json
146
+ get :render_symbol_json
147
+ assert_equal '{"hello":"world"}' , @response . body
148
+ assert_equal 'application/json' , @response . content_type
149
+ end
150
+
151
+ def test_render_json_forwards_extra_options
152
+ get :render_json_with_extra_options
153
+ assert_equal '{"a":"b"}' , @response . body
154
+ assert_equal 'application/json' , @response . content_type
155
+ end
156
+
157
+ def test_render_json_calls_to_json_from_object
158
+ get :render_json_without_options
159
+ assert_equal '{"a":"b"}' , @response . body
160
+ end
161
+
162
+ def test_render_json_with_serializer
163
+ get :render_json_with_serializer
164
+ assert_match '"scope":{"current_user":true}' , @response . body
165
+ assert_match '"object":{"serializable_object":true}' , @response . body
166
+ end
167
+
168
+ def test_render_json_with_serializer_api_but_without_serializer
169
+ get :render_json_with_serializer_api_but_without_serializer
170
+ assert_match '{"serializable_object":true}' , @response . body
171
+ end
172
+ end
0 commit comments