Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / test / controller / rack_test.rb
1 require 'abstract_unit'
2
3 class BaseRackTest < Test::Unit::TestCase
4 def setup
5 @env = {
6 "HTTP_MAX_FORWARDS" => "10",
7 "SERVER_NAME" => "glu.ttono.us",
8 "FCGI_ROLE" => "RESPONDER",
9 "AUTH_TYPE" => "Basic",
10 "HTTP_X_FORWARDED_HOST" => "glu.ttono.us",
11 "HTTP_ACCEPT_CHARSET" => "UTF-8",
12 "HTTP_ACCEPT_ENCODING" => "gzip, deflate",
13 "HTTP_CACHE_CONTROL" => "no-cache, max-age=0",
14 "HTTP_PRAGMA" => "no-cache",
15 "HTTP_USER_AGENT" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en)",
16 "PATH_INFO" => "/homepage/",
17 "HTTP_ACCEPT_LANGUAGE" => "en",
18 "HTTP_NEGOTIATE" => "trans",
19 "HTTP_HOST" => "glu.ttono.us:8007",
20 "HTTP_REFERER" => "http://www.google.com/search?q=glu.ttono.us",
21 "HTTP_FROM" => "googlebot",
22 "SERVER_PROTOCOL" => "HTTP/1.1",
23 "REDIRECT_URI" => "/dispatch.fcgi",
24 "SCRIPT_NAME" => "/dispatch.fcgi",
25 "SERVER_ADDR" => "207.7.108.53",
26 "REMOTE_ADDR" => "207.7.108.53",
27 "REMOTE_HOST" => "google.com",
28 "REMOTE_IDENT" => "kevin",
29 "REMOTE_USER" => "kevin",
30 "SERVER_SOFTWARE" => "lighttpd/1.4.5",
31 "HTTP_COOKIE" => "_session_id=c84ace84796670c052c6ceb2451fb0f2; is_admin=yes",
32 "HTTP_X_FORWARDED_SERVER" => "glu.ttono.us",
33 "REQUEST_URI" => "/admin",
34 "DOCUMENT_ROOT" => "/home/kevinc/sites/typo/public",
35 "PATH_TRANSLATED" => "/home/kevinc/sites/typo/public/homepage/",
36 "SERVER_PORT" => "8007",
37 "QUERY_STRING" => "",
38 "REMOTE_PORT" => "63137",
39 "GATEWAY_INTERFACE" => "CGI/1.1",
40 "HTTP_X_FORWARDED_FOR" => "65.88.180.234",
41 "HTTP_ACCEPT" => "*/*",
42 "SCRIPT_FILENAME" => "/home/kevinc/sites/typo/public/dispatch.fcgi",
43 "REDIRECT_STATUS" => "200",
44 "REQUEST_METHOD" => "GET"
45 }
46 @request = ActionController::Request.new(@env)
47 # some Nokia phone browsers omit the space after the semicolon separator.
48 # some developers have grown accustomed to using comma in cookie values.
49 @alt_cookie_fmt_request = ActionController::Request.new(@env.merge({"HTTP_COOKIE"=>"_session_id=c84ace847,96670c052c6ceb2451fb0f2;is_admin=yes"}))
50 end
51
52 def default_test; end
53
54 private
55
56 def set_content_data(data)
57 @request.env['REQUEST_METHOD'] = 'POST'
58 @request.env['CONTENT_LENGTH'] = data.length
59 @request.env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=utf-8'
60 @request.env['rack.input'] = StringIO.new(data)
61 end
62 end
63
64 class RackRequestTest < BaseRackTest
65 def test_proxy_request
66 assert_equal 'glu.ttono.us', @request.host_with_port
67 end
68
69 def test_http_host
70 @env.delete "HTTP_X_FORWARDED_HOST"
71 @env['HTTP_HOST'] = "rubyonrails.org:8080"
72 assert_equal "rubyonrails.org", @request.host
73 assert_equal "rubyonrails.org:8080", @request.host_with_port
74
75 @env['HTTP_X_FORWARDED_HOST'] = "www.firsthost.org, www.secondhost.org"
76 assert_equal "www.secondhost.org", @request.host
77 end
78
79 def test_http_host_with_default_port_overrides_server_port
80 @env.delete "HTTP_X_FORWARDED_HOST"
81 @env['HTTP_HOST'] = "rubyonrails.org"
82 assert_equal "rubyonrails.org", @request.host_with_port
83 end
84
85 def test_host_with_port_defaults_to_server_name_if_no_host_headers
86 @env.delete "HTTP_X_FORWARDED_HOST"
87 @env.delete "HTTP_HOST"
88 assert_equal "glu.ttono.us:8007", @request.host_with_port
89 end
90
91 def test_host_with_port_falls_back_to_server_addr_if_necessary
92 @env.delete "HTTP_X_FORWARDED_HOST"
93 @env.delete "HTTP_HOST"
94 @env.delete "SERVER_NAME"
95 assert_equal "207.7.108.53", @request.host
96 assert_equal 8007, @request.port
97 assert_equal "207.7.108.53:8007", @request.host_with_port
98 end
99
100 def test_host_with_port_if_http_standard_port_is_specified
101 @env['HTTP_X_FORWARDED_HOST'] = "glu.ttono.us:80"
102 assert_equal "glu.ttono.us", @request.host_with_port
103 end
104
105 def test_host_with_port_if_https_standard_port_is_specified
106 @env['HTTP_X_FORWARDED_PROTO'] = "https"
107 @env['HTTP_X_FORWARDED_HOST'] = "glu.ttono.us:443"
108 assert_equal "glu.ttono.us", @request.host_with_port
109 end
110
111 def test_host_if_ipv6_reference
112 @env.delete "HTTP_X_FORWARDED_HOST"
113 @env['HTTP_HOST'] = "[2001:1234:5678:9abc:def0::dead:beef]"
114 assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host
115 end
116
117 def test_host_if_ipv6_reference_with_port
118 @env.delete "HTTP_X_FORWARDED_HOST"
119 @env['HTTP_HOST'] = "[2001:1234:5678:9abc:def0::dead:beef]:8008"
120 assert_equal "[2001:1234:5678:9abc:def0::dead:beef]", @request.host
121 end
122
123 def test_cgi_environment_variables
124 assert_equal "Basic", @request.auth_type
125 assert_equal 0, @request.content_length
126 assert_equal nil, @request.content_type
127 assert_equal "CGI/1.1", @request.gateway_interface
128 assert_equal "*/*", @request.accept
129 assert_equal "UTF-8", @request.accept_charset
130 assert_equal "gzip, deflate", @request.accept_encoding
131 assert_equal "en", @request.accept_language
132 assert_equal "no-cache, max-age=0", @request.cache_control
133 assert_equal "googlebot", @request.from
134 assert_equal "glu.ttono.us", @request.host
135 assert_equal "trans", @request.negotiate
136 assert_equal "no-cache", @request.pragma
137 assert_equal "http://www.google.com/search?q=glu.ttono.us", @request.referer
138 assert_equal "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en)", @request.user_agent
139 assert_equal "/homepage/", @request.path_info
140 assert_equal "/home/kevinc/sites/typo/public/homepage/", @request.path_translated
141 assert_equal "", @request.query_string
142 assert_equal "207.7.108.53", @request.remote_addr
143 assert_equal "google.com", @request.remote_host
144 assert_equal "kevin", @request.remote_ident
145 assert_equal "kevin", @request.remote_user
146 assert_equal :get, @request.request_method
147 assert_equal "/dispatch.fcgi", @request.script_name
148 assert_equal "glu.ttono.us", @request.server_name
149 assert_equal 8007, @request.server_port
150 assert_equal "HTTP/1.1", @request.server_protocol
151 assert_equal "lighttpd", @request.server_software
152 end
153
154 def test_cookie_syntax_resilience
155 cookies = @request.cookies
156 assert_equal "c84ace84796670c052c6ceb2451fb0f2", cookies["_session_id"], cookies.inspect
157 assert_equal "yes", cookies["is_admin"], cookies.inspect
158
159 alt_cookies = @alt_cookie_fmt_request.cookies
160 #assert_equal "c84ace847,96670c052c6ceb2451fb0f2", alt_cookies["_session_id"], alt_cookies.inspect
161 assert_equal "yes", alt_cookies["is_admin"], alt_cookies.inspect
162 end
163 end
164
165 class RackRequestParamsParsingTest < BaseRackTest
166 def test_doesnt_break_when_content_type_has_charset
167 set_content_data 'flamenco=love'
168
169 assert_equal({"flamenco"=> "love"}, @request.request_parameters)
170 end
171
172 def test_doesnt_interpret_request_uri_as_query_string_when_missing
173 @request.env['REQUEST_URI'] = 'foo'
174 assert_equal({}, @request.query_parameters)
175 end
176 end
177
178 class RackRequestContentTypeTest < BaseRackTest
179 def test_html_content_type_verification
180 @request.env['CONTENT_TYPE'] = Mime::HTML.to_s
181 assert @request.content_type.verify_request?
182 end
183
184 def test_xml_content_type_verification
185 @request.env['CONTENT_TYPE'] = Mime::XML.to_s
186 assert !@request.content_type.verify_request?
187 end
188 end
189
190 class RackRequestNeedsRewoundTest < BaseRackTest
191 def test_body_should_be_rewound
192 data = 'foo'
193 @env['rack.input'] = StringIO.new(data)
194 @env['CONTENT_LENGTH'] = data.length
195 @env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=utf-8'
196
197 # Read the request body by parsing params.
198 request = ActionController::Request.new(@env)
199 request.request_parameters
200
201 # Should have rewound the body.
202 assert_equal 0, request.body.pos
203 end
204 end
205
206 class RackResponseTest < BaseRackTest
207 def setup
208 super
209 @response = ActionController::Response.new
210 end
211
212 def test_simple_output
213 @response.body = "Hello, World!"
214 @response.prepare!
215
216 status, headers, body = @response.to_a
217 assert_equal 200, status
218 assert_equal({
219 "Content-Type" => "text/html; charset=utf-8",
220 "Cache-Control" => "private, max-age=0, must-revalidate",
221 "ETag" => '"65a8e27d8879283831b664bd8b7f0ad4"',
222 "Set-Cookie" => "",
223 "Content-Length" => "13"
224 }, headers)
225
226 parts = []
227 body.each { |part| parts << part }
228 assert_equal ["Hello, World!"], parts
229 end
230
231 def test_utf8_output
232 @response.body = [1090, 1077, 1089, 1090].pack("U*")
233 @response.prepare!
234
235 status, headers, body = @response.to_a
236 assert_equal 200, status
237 assert_equal({
238 "Content-Type" => "text/html; charset=utf-8",
239 "Cache-Control" => "private, max-age=0, must-revalidate",
240 "ETag" => '"ebb5e89e8a94e9dd22abf5d915d112b2"',
241 "Set-Cookie" => "",
242 "Content-Length" => "8"
243 }, headers)
244 end
245
246 def test_streaming_block
247 @response.body = Proc.new do |response, output|
248 5.times { |n| output.write(n) }
249 end
250 @response.prepare!
251
252 status, headers, body = @response.to_a
253 assert_equal 200, status
254 assert_equal({
255 "Content-Type" => "text/html; charset=utf-8",
256 "Cache-Control" => "no-cache",
257 "Set-Cookie" => ""
258 }, headers)
259
260 parts = []
261 body.each { |part| parts << part }
262 assert_equal ["0", "1", "2", "3", "4"], parts
263 end
264 end
265
266 class RackResponseHeadersTest < BaseRackTest
267 def setup
268 super
269 @response = ActionController::Response.new
270 @response.status = "200 OK"
271 end
272
273 def test_content_type
274 [204, 304].each do |c|
275 @response.status = c.to_s
276 assert !response_headers.has_key?("Content-Type"), "#{c} should not have Content-Type header"
277 end
278
279 [200, 302, 404, 500].each do |c|
280 @response.status = c.to_s
281 assert response_headers.has_key?("Content-Type"), "#{c} did not have Content-Type header"
282 end
283 end
284
285 def test_status
286 assert !response_headers.has_key?('Status')
287 end
288
289 private
290 def response_headers
291 @response.prepare!
292 @response.to_a[1]
293 end
294 end