File: content_type_test.rb

package info (click to toggle)
rails 2.3.5-1.2+squeeze8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 18,248 kB
  • ctags: 20,944
  • sloc: ruby: 122,413; makefile: 72; sql: 43; sh: 1
file content (178 lines) | stat: -rw-r--r-- 4,985 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require 'abstract_unit'

class ContentTypeController < ActionController::Base
  def render_content_type_from_body
    response.content_type = Mime::RSS
    render :text => "hello world!"
  end

  def render_defaults
    render :text => "hello world!"
  end

  def render_content_type_from_render
    render :text => "hello world!", :content_type => Mime::RSS
  end

  def render_charset_from_body
    response.charset = "utf-16"
    render :text => "hello world!"
  end

  def render_nil_charset_from_body
    response.charset = nil
    render :text => "hello world!"
  end

  def render_default_for_rhtml
  end

  def render_default_for_rxml
  end

  def render_default_for_rjs
  end

  def render_change_for_rxml
    response.content_type = Mime::HTML
    render :action => "render_default_for_rxml"
  end

  def render_default_content_types_for_respond_to
    respond_to do |format|
      format.html { render :text   => "hello world!" }
      format.xml  { render :action => "render_default_content_types_for_respond_to.rhtml" }
      format.js   { render :text   => "hello world!" }
      format.rss  { render :text   => "hello world!", :content_type => Mime::XML }
    end
  end
  
  def render_content_type_from_user_input
    response.content_type= params[:hello]
    render :text=>"hello"
  end

  def rescue_action(e) raise end
end

class ContentTypeTest < ActionController::TestCase
  tests ContentTypeController

  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".
    @controller.logger = Logger.new(nil)
  end

  def test_render_defaults
    get :render_defaults
    assert_equal "utf-8", @response.charset
    assert_equal Mime::HTML, @response.content_type
  end

  def test_render_changed_charset_default
    ContentTypeController.default_charset = "utf-16"
    get :render_defaults
    assert_equal "utf-16", @response.charset
    assert_equal Mime::HTML, @response.content_type
    ContentTypeController.default_charset = "utf-8"
  end

  def test_content_type_from_body
    get :render_content_type_from_body
    assert_equal "application/rss+xml", @response.content_type
    assert_equal "utf-8", @response.charset
  end

  def test_content_type_from_render
    get :render_content_type_from_render
    assert_equal "application/rss+xml", @response.content_type
    assert_equal "utf-8", @response.charset
  end

  def test_charset_from_body
    get :render_charset_from_body
    assert_equal Mime::HTML, @response.content_type
    assert_equal "utf-16", @response.charset
  end

  def test_nil_charset_from_body
    get :render_nil_charset_from_body
    assert_equal Mime::HTML, @response.content_type
    assert_equal "utf-8", @response.charset, @response.headers.inspect
  end

  def test_nil_default_for_rhtml
    ContentTypeController.default_charset = nil
    get :render_default_for_rhtml
    assert_equal Mime::HTML, @response.content_type
    assert_nil @response.charset, @response.headers.inspect
  ensure
    ContentTypeController.default_charset = "utf-8"
  end

  def test_default_for_rhtml
    get :render_default_for_rhtml
    assert_equal Mime::HTML, @response.content_type
    assert_equal "utf-8", @response.charset
  end

  def test_default_for_rxml
    get :render_default_for_rxml
    assert_equal Mime::XML, @response.content_type
    assert_equal "utf-8", @response.charset
  end

  def test_default_for_rjs
    xhr :post, :render_default_for_rjs
    assert_equal Mime::JS, @response.content_type
    assert_equal "utf-8", @response.charset
  end

  def test_change_for_rxml
    get :render_change_for_rxml
    assert_equal Mime::HTML, @response.content_type
    assert_equal "utf-8", @response.charset
  end
  
  def test_user_supplied_value
    get :render_content_type_from_user_input, :hello=>"hello/world\r\nAttack: true"
    assert_equal "hello/world%0D%0AAttack: true", @response.content_type
  end
end

class AcceptBasedContentTypeTest < ActionController::TestCase

  tests ContentTypeController

  def setup
    ActionController::Base.use_accept_header = true
  end

  def teardown
    ActionController::Base.use_accept_header = false
  end


  def test_render_default_content_types_for_respond_to
    @request.accept = Mime::HTML.to_s
    get :render_default_content_types_for_respond_to
    assert_equal Mime::HTML, @response.content_type

    @request.accept = Mime::JS.to_s
    get :render_default_content_types_for_respond_to
    assert_equal Mime::JS, @response.content_type
  end

  def test_render_default_content_types_for_respond_to_with_template
    @request.accept = Mime::XML.to_s
    get :render_default_content_types_for_respond_to
    assert_equal Mime::XML, @response.content_type
  end

  def test_render_default_content_types_for_respond_to_with_overwrite
    @request.accept = Mime::RSS.to_s
    get :render_default_content_types_for_respond_to
    assert_equal Mime::XML, @response.content_type
  end
end