1 require 'abstract_unit'
3 class BaseRackTest
< Test
::Unit::TestCase
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",
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"
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"}))
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)
64 class RackRequestTest
< BaseRackTest
65 def test_proxy_request
66 assert_equal
'glu.ttono.us', @request.host_with_port
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
75 @env['HTTP_X_FORWARDED_HOST'] = "www.firsthost.org, www.secondhost.org"
76 assert_equal
"www.secondhost.org", @request.host
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
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
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
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
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
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
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
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
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
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
165 class RackRequestParamsParsingTest
< BaseRackTest
166 def test_doesnt_break_when_content_type_has_charset
167 set_content_data
'flamenco=love'
169 assert_equal({"flamenco"=> "love"}, @request.request_parameters
)
172 def test_doesnt_interpret_request_uri_as_query_string_when_missing
173 @request.env['REQUEST_URI'] = 'foo'
174 assert_equal({}, @request.query_parameters
)
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
?
184 def test_xml_content_type_verification
185 @request.env['CONTENT_TYPE'] = Mime
::XML.to_s
186 assert
!@request.content_type
.verify_request
?
190 class RackRequestNeedsRewoundTest
< BaseRackTest
191 def test_body_should_be_rewound
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'
197 # Read the request body by parsing params.
198 request
= ActionController
::Request.new(@env)
199 request
.request_parameters
201 # Should have rewound the body.
202 assert_equal
0, request
.body
.pos
206 class RackResponseTest
< BaseRackTest
209 @response = ActionController
::Response.new
212 def test_simple_output
213 @response.body
= "Hello, World!"
216 status
, headers
, body
= @response.to_a
217 assert_equal
200, status
219 "Content-Type" => "text/html; charset=utf-8",
220 "Cache-Control" => "private, max-age=0, must-revalidate",
221 "ETag" => '"65a8e27d8879283831b664bd8b7f0ad4"',
223 "Content-Length" => "13"
227 body
.each
{ |part
| parts
<< part
}
228 assert_equal
["Hello, World!"], parts
232 @response.body
= [1090, 1077, 1089, 1090].pack("U*")
235 status
, headers
, body
= @response.to_a
236 assert_equal
200, status
238 "Content-Type" => "text/html; charset=utf-8",
239 "Cache-Control" => "private, max-age=0, must-revalidate",
240 "ETag" => '"ebb5e89e8a94e9dd22abf5d915d112b2"',
242 "Content-Length" => "8"
246 def test_streaming_block
247 @response.body
= Proc
.new
do |response
, output
|
248 5.times
{ |n
| output
.write(n
) }
252 status
, headers
, body
= @response.to_a
253 assert_equal
200, status
255 "Content-Type" => "text/html; charset=utf-8",
256 "Cache-Control" => "no-cache",
261 body
.each
{ |part
| parts
<< part
}
262 assert_equal
["0", "1", "2", "3", "4"], parts
266 class RackResponseHeadersTest
< BaseRackTest
269 @response = ActionController
::Response.new
270 @response.status
= "200 OK"
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"
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"
286 assert
!response_headers
.has_key
?('Status')