ruby-web 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +474 -0
- data/INSTALL.txt +9 -0
- data/InstalledFiles +180 -0
- data/LICENSE.txt +74 -0
- data/Rakefile +529 -0
- data/TODO +65 -0
- data/doc/additional.xml +149 -0
- data/doc/core.xml +652 -0
- data/doc/credits/index.xml +52 -0
- data/doc/credits/php.contributors.xml +118 -0
- data/doc/credits/php.language-snippets.ent +622 -0
- data/doc/install/index.xml +136 -0
- data/doc/install/mac/index.xml +21 -0
- data/doc/install/ruby-web.install.rb.instructions.xml +7 -0
- data/doc/install/unix/index.xml +46 -0
- data/doc/install/win/apache1.xml +166 -0
- data/doc/install/win/apache2.xml +141 -0
- data/doc/install/win/iis.xml +162 -0
- data/doc/install/win/index.xml +24 -0
- data/doc/install/win/installer.xml +31 -0
- data/doc/install/win/manual.xml +43 -0
- data/doc/manual.xml +69 -0
- data/doc/old/apache_cgi.txt +23 -0
- data/doc/old/fastcgi.txt +23 -0
- data/doc/old/mod_ruby.txt +21 -0
- data/doc/old/snippets.rdoc +183 -0
- data/doc/old/webrick.txt +23 -0
- data/doc/old/windows_cgi.txt +9 -0
- data/doc/tutorial.xml +14 -0
- data/doc/xsl/manual-multi.xsl +10 -0
- data/doc/xsl/manual-pdf.xsl +6 -0
- data/doc/xsl/manual-single.xsl +6 -0
- data/doc/xsl/manual.css +22 -0
- data/install.rb +1022 -0
- data/lib/formatter.rb +314 -0
- data/lib/html-parser.rb +429 -0
- data/lib/htmlrepair.rb +113 -0
- data/lib/htmlsplit.rb +842 -0
- data/lib/sgml-parser.rb +332 -0
- data/lib/web.rb +68 -0
- data/lib/web/assertinclude.rb +129 -0
- data/lib/web/config.rb +50 -0
- data/lib/web/connection.rb +1070 -0
- data/lib/web/convenience.rb +154 -0
- data/lib/web/formreader.rb +318 -0
- data/lib/web/htmlparser/html-parser.rb +429 -0
- data/lib/web/htmlparser/sgml-parser.rb +332 -0
- data/lib/web/htmltools/element.rb +296 -0
- data/lib/web/htmltools/stparser.rb +276 -0
- data/lib/web/htmltools/tags.rb +286 -0
- data/lib/web/htmltools/tree.rb +139 -0
- data/lib/web/htmltools/xmltree.rb +160 -0
- data/lib/web/htmltools/xpath.rb +71 -0
- data/lib/web/info.rb +63 -0
- data/lib/web/load.rb +210 -0
- data/lib/web/mime.rb +87 -0
- data/lib/web/phprb.rb +340 -0
- data/lib/web/resources/test/cookie.rb +33 -0
- data/lib/web/resources/test/counter.rb +20 -0
- data/lib/web/resources/test/multipart.rb +14 -0
- data/lib/web/resources/test/redirect.rb +8 -0
- data/lib/web/resources/test/stock.rb +33 -0
- data/lib/web/sapi/apache.rb +129 -0
- data/lib/web/sapi/fastcgi.rb +22 -0
- data/lib/web/sapi/install/apache.rb +180 -0
- data/lib/web/sapi/install/iis.rb +93 -0
- data/lib/web/sapi/install/macosx.rb +90 -0
- data/lib/web/sapi/webrick.rb +86 -0
- data/lib/web/session.rb +83 -0
- data/lib/web/shim/cgi.rb +129 -0
- data/lib/web/shim/rails.rb +175 -0
- data/lib/web/stringio.rb +78 -0
- data/lib/web/strscanparser.rb +24 -0
- data/lib/web/tagparser.rb +96 -0
- data/lib/web/testing.rb +666 -0
- data/lib/web/traceoutput.rb +75 -0
- data/lib/web/unit.rb +56 -0
- data/lib/web/upload.rb +59 -0
- data/lib/web/validate.rb +52 -0
- data/lib/web/wiki.rb +557 -0
- data/lib/web/wiki/linker.rb +72 -0
- data/lib/web/wiki/page.rb +201 -0
- data/lib/webunit.rb +27 -0
- data/lib/webunit/assert.rb +152 -0
- data/lib/webunit/converter.rb +154 -0
- data/lib/webunit/cookie.rb +118 -0
- data/lib/webunit/domwalker.rb +185 -0
- data/lib/webunit/exception.rb +14 -0
- data/lib/webunit/form.rb +116 -0
- data/lib/webunit/frame.rb +37 -0
- data/lib/webunit/htmlelem.rb +122 -0
- data/lib/webunit/image.rb +26 -0
- data/lib/webunit/jscript.rb +31 -0
- data/lib/webunit/link.rb +33 -0
- data/lib/webunit/params.rb +321 -0
- data/lib/webunit/parser.rb +229 -0
- data/lib/webunit/response.rb +464 -0
- data/lib/webunit/runtest.rb +41 -0
- data/lib/webunit/table.rb +148 -0
- data/lib/webunit/testcase.rb +45 -0
- data/lib/webunit/ui/cui/testrunner.rb +50 -0
- data/lib/webunit/utils.rb +68 -0
- data/lib/webunit/webunit.rb +28 -0
- data/test/dev/action.rb +83 -0
- data/test/dev/forms.rb +104 -0
- data/test/dev/forms2.rb +104 -0
- data/test/dev/parser.rb +17 -0
- data/test/dev/scripts/dump.rb +24 -0
- data/test/dev/scripts/makedist.rb +62 -0
- data/test/dev/scripts/uri.rb +41 -0
- data/test/dev/scripts/uri/common.rb +432 -0
- data/test/dev/scripts/uri/ftp.rb +149 -0
- data/test/dev/scripts/uri/generic.rb +1106 -0
- data/test/dev/scripts/uri/http.rb +76 -0
- data/test/dev/scripts/uri/https.rb +26 -0
- data/test/dev/scripts/uri/ldap.rb +238 -0
- data/test/dev/scripts/uri/mailto.rb +260 -0
- data/test/dev/scripts/urireg.rb +174 -0
- data/test/dev/simpledispatcher.rb +156 -0
- data/test/dev/test.action.rb +146 -0
- data/test/dev/test.formreader.rb +463 -0
- data/test/dev/test.simpledispatcher.rb +186 -0
- data/test/dev/webunit/conv/digit-0.rb +21 -0
- data/test/dev/webunit/conv/digit-1.rb +17 -0
- data/test/dev/webunit/conv/digit.rb +23 -0
- data/test/dev/webunit/conv/test_digit-0.rb +16 -0
- data/test/dev/webunit/conv/test_digit-1.rb +19 -0
- data/test/dev/webunit/conv/test_digit.rb +26 -0
- data/test/dev/webunit/conv/test_digit_view-0.rb +76 -0
- data/test/dev/webunit/conv/test_digit_view-1.rb +102 -0
- data/test/dev/webunit/conv/test_digit_view.rb +134 -0
- data/test/installation/htdocs/cgi_test.rb +296 -0
- data/test/installation/htdocs/test_install.rb +4 -0
- data/test/installation/runwebtest.rb +5 -0
- data/test/installation/test_cookie.rb +128 -0
- data/test/installation/test_form.rb +47 -0
- data/test/installation/test_multipart.rb +51 -0
- data/test/installation/test_request.rb +24 -0
- data/test/installation/test_response.rb +35 -0
- data/test/unit/htdocs/cookie.rb +32 -0
- data/test/unit/htdocs/multipart.rb +28 -0
- data/test/unit/htdocs/redirect.rb +12 -0
- data/test/unit/htdocs/simple.rb +13 -0
- data/test/unit/htdocs/stock.rb +33 -0
- data/test/unit/test_assert.rb +162 -0
- data/test/unit/test_cookie.rb +114 -0
- data/test/unit/test_domwalker.rb +77 -0
- data/test/unit/test_form.rb +42 -0
- data/test/unit/test_frame.rb +40 -0
- data/test/unit/test_htmlelem.rb +74 -0
- data/test/unit/test_image.rb +45 -0
- data/test/unit/test_jscript.rb +57 -0
- data/test/unit/test_link.rb +85 -0
- data/test/unit/test_multipart.rb +51 -0
- data/test/unit/test_params.rb +210 -0
- data/test/unit/test_parser.rb +53 -0
- data/test/unit/test_response.rb +150 -0
- data/test/unit/test_table.rb +70 -0
- data/test/unit/test_utils.rb +106 -0
- data/test/unit/test_webunit.rb +28 -0
- data/test/web/mod_ruby_stub.rb +39 -0
- data/test/web/test.assertinclude.rb +109 -0
- data/test/web/test.buffer.rb +182 -0
- data/test/web/test.code.loader.rb +78 -0
- data/test/web/test.config.rb +31 -0
- data/test/web/test.error.handling.rb +91 -0
- data/test/web/test.formreader-2.0.rb +352 -0
- data/test/web/test.load.rb +125 -0
- data/test/web/test.mime-type.rb +23 -0
- data/test/web/test.narf.cgi.rb +106 -0
- data/test/web/test.phprb.rb +239 -0
- data/test/web/test.request.rb +368 -0
- data/test/web/test.response.rb +637 -0
- data/test/web/test.ruby-web.rb +10 -0
- data/test/web/test.session.rb +50 -0
- data/test/web/test.shim.cgi.rb +96 -0
- data/test/web/test.tagparser.rb +65 -0
- data/test/web/test.template2.rb +297 -0
- data/test/web/test.testing2.rb +318 -0
- data/test/web/test.upload.rb +45 -0
- data/test/web/test.validate.rb +46 -0
- data/test/web/test.web.test.rb +495 -0
- data/test/wiki/test.history.rb +297 -0
- data/test/wiki/test.illustration_page.rb +287 -0
- data/test/wiki/test.linker.rb +197 -0
- data/test/wiki/test.tarpit.rb +56 -0
- data/test/wiki/test.wiki.rb +300 -0
- data/test/wikitestroot/admin.rb +7 -0
- data/test/wikitestroot/wiki.rb +6 -0
- metadata +234 -0
@@ -0,0 +1,368 @@
|
|
1
|
+
require 'test/web/mod_ruby_stub'
|
2
|
+
|
3
|
+
class TestRequest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def env
|
6
|
+
ENV
|
7
|
+
end
|
8
|
+
|
9
|
+
def raw_post_data
|
10
|
+
$stdin
|
11
|
+
end
|
12
|
+
|
13
|
+
def assert_keys( expected, found )
|
14
|
+
passes = true
|
15
|
+
expected.each { |k, v|
|
16
|
+
passes = false unless found[k] == v
|
17
|
+
}
|
18
|
+
passes
|
19
|
+
end
|
20
|
+
|
21
|
+
def setup
|
22
|
+
@stdin_back = $stdin
|
23
|
+
end
|
24
|
+
|
25
|
+
def teardown
|
26
|
+
ENV["QUERY_STRING" ] = nil
|
27
|
+
ENV["REQUEST_METHOD"] = nil
|
28
|
+
ENV["CONTENT_TYPE" ] = nil
|
29
|
+
ENV["CONTENT_LENGTH"] = nil
|
30
|
+
ENV["HTTP_COOKIE" ] = nil
|
31
|
+
ENV["COOKIE" ] = nil
|
32
|
+
|
33
|
+
@get = @post = @cookies = nil
|
34
|
+
|
35
|
+
$stdin = @stdin_back
|
36
|
+
|
37
|
+
if defined? MOD_RUBY
|
38
|
+
Object.module_eval{ remove_const :MOD_RUBY }
|
39
|
+
end
|
40
|
+
@cgi = nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def params
|
44
|
+
{ "string" => "aString",
|
45
|
+
"array[]" => ["one", "two", "three"],
|
46
|
+
"file" => Web::Upload.new( File.new( "test/web/testfiles/text" ),
|
47
|
+
"content-type",
|
48
|
+
"original-filename" ) }
|
49
|
+
end
|
50
|
+
|
51
|
+
def cookie
|
52
|
+
{ "aCookie" => "Peanut Butter" }
|
53
|
+
end
|
54
|
+
|
55
|
+
def cgi
|
56
|
+
@cgi ||= Web::Connection.new( :request => params,
|
57
|
+
:cookie => cookie,
|
58
|
+
:out => self.out )
|
59
|
+
end
|
60
|
+
|
61
|
+
def out
|
62
|
+
@out ||= ""
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_mock_cookies
|
66
|
+
assert_includes( Web::Connection.normalize(cookie), cgi.cookie )
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_convenience_defaults_to_param
|
70
|
+
assert_equal( "aString", cgi["string"] )
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_convenience_returns_array
|
74
|
+
assert_equal( ["one", "two", "three"], cgi["array[]"] )
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_setting_params
|
78
|
+
assert_equal( "aString", cgi["string"] )
|
79
|
+
assert_equal( ["one", "two", "three"], cgi["array[]"] )
|
80
|
+
|
81
|
+
cgi["array[]"] = ["four", "five", "six"]
|
82
|
+
cgi["string"] = "another_string"
|
83
|
+
|
84
|
+
assert_equal( "another_string", cgi["string"] )
|
85
|
+
assert_equal( ["four", "five", "six"], cgi["array[]"] )
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_single_param
|
90
|
+
assert_equal( "aString", cgi.single_param("string") )
|
91
|
+
assert_equal( "one,two,three", cgi.single_param("array[]") )
|
92
|
+
assert_equal( "original-filename", cgi.single_param("file").original_filename )
|
93
|
+
|
94
|
+
assert_equal( "", cgi.single_param("non-existent") )
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_multiple_params
|
98
|
+
assert_equal( ["aString"], cgi.request["string"] )
|
99
|
+
assert_equal( ["one", "two", "three"], cgi.request["array[]"] )
|
100
|
+
assert_equal( "original-filename", cgi.request["file"][0].original_filename )
|
101
|
+
assert_equal( 1, cgi.request["file"].length)
|
102
|
+
|
103
|
+
assert_equal( [], cgi.request["non-existent"] )
|
104
|
+
|
105
|
+
Web.connection = cgi
|
106
|
+
|
107
|
+
assert_equal( ["aString"], Web::request["string"] )
|
108
|
+
assert_equal( ["one", "two", "three"], Web::request["array[]"] )
|
109
|
+
assert_equal( "original-filename", Web::request["file"][0].original_filename )
|
110
|
+
assert_equal( 1, Web::request["file"].length)
|
111
|
+
|
112
|
+
assert_equal( [], Web::request["non-existent"] )
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_request
|
116
|
+
assert_equal( ["aString"], cgi.request["string"] )
|
117
|
+
assert_equal( ["one", "two", "three"], cgi.request["array[]"] )
|
118
|
+
assert_equal( "original-filename", cgi.request["file"][0].original_filename )
|
119
|
+
assert_equal( 1, cgi.request["file"].length)
|
120
|
+
|
121
|
+
assert_equal( [], cgi.request["non-existent"] )
|
122
|
+
|
123
|
+
Web.connection = cgi
|
124
|
+
|
125
|
+
assert_equal( ["aString"], Web::request["string"] )
|
126
|
+
assert_equal( ["one", "two", "three"], Web::request["array[]"] )
|
127
|
+
assert_equal( "original-filename", Web::request["file"][0].original_filename )
|
128
|
+
assert_equal( 1, Web::request["file"].length)
|
129
|
+
|
130
|
+
assert_equal( [], Web::request["non-existent"] )
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_keys
|
134
|
+
assert_equal( ["string", "array[]", "file"].sort, cgi.keys.sort )
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_has_key?
|
138
|
+
assert cgi.key?( "string" )
|
139
|
+
assert cgi.key?( "array[]" )
|
140
|
+
assert cgi.key?( "file" )
|
141
|
+
|
142
|
+
assert((cgi.key? "missing key") == false)
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_parse_query_string
|
146
|
+
assert_equal({}, {})
|
147
|
+
assert_keys( {"query1"=>["~value1"], "query2"=>["value1", "value2"] },
|
148
|
+
Web::Connection.parse_query_string('query1=%7Evalue1&query2=value1&query2=value2') )
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_parse_nil_cookie
|
153
|
+
assert_keys( {}, Web::Connection::CookieHash.new(nil) )
|
154
|
+
assert_equal( [], Web::Connection::CookieHash.new(nil).default )
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_parse_cookie
|
158
|
+
assert_equal( {"cookie1"=>["~value1"], "cookie2"=>["value1", "value2"]}.inspect,
|
159
|
+
Web::Connection::CookieHash.new("cookie1=%7Evalue1; cookie2=value1; cookie2=value2").inspect )
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_parse_multiple_cookies
|
163
|
+
assert_equal( {"cookie"=>["~value1", "value 2"]}.inspect,
|
164
|
+
Web::Connection::CookieHash.new("cookie=%7Evalue1&value%202").inspect )
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_parse_blank_cookie
|
168
|
+
assert_keys( {"cookie"=>[]},
|
169
|
+
Web::Connection::CookieHash.new("cookie=") )
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_parse_get_and_post
|
173
|
+
ENV["REQUEST_METHOD"] = "POST"
|
174
|
+
ENV["QUERY_STRING"] = "key=get_value_1&get_only=get_value_2"
|
175
|
+
ENV["CONTENT_LENGTH"] = "40"
|
176
|
+
File.open( "test/web/testfiles/post.and.get.data" ){ |post_data|
|
177
|
+
$stdin = post_data
|
178
|
+
|
179
|
+
cgi = Web::Connection.new
|
180
|
+
|
181
|
+
assert_equal( {"key" => ["get_value_1"], "get_only" => ["get_value_2"] },
|
182
|
+
cgi.get )
|
183
|
+
assert_equal( {"key" => ["post_value_1"], "post_only" => ["post_value_2"]},
|
184
|
+
cgi.post )
|
185
|
+
assert_equal( {"key" => ["post_value_1"], "get_only" => ["get_value_2"], "post_only" => ["post_value_2"]},
|
186
|
+
cgi.request )
|
187
|
+
}
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_cgi_parses_by_default
|
191
|
+
ENV["REQUEST_METHOD"] = "GET"
|
192
|
+
ENV["QUERY_STRING"] = "key=a%20value&anotherKey=another%20value"
|
193
|
+
assert_keys( {"key" => ["a value"], "anotherKey" => ["another value"] },
|
194
|
+
Web::Connection.new.get )
|
195
|
+
assert_keys( {"key" => ["a value"], "anotherKey" => ["another value"] },
|
196
|
+
Web::Connection.new.request )
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_cgi_parses_http_cookie
|
200
|
+
ENV['HTTP_COOKIE'] = "cookie1=%7Evalue1; cookie2=value1; cookie2=value2"
|
201
|
+
assert_keys( {"cookie1"=>["~value1"], "cookie2"=>["value1", "value2"]},
|
202
|
+
Web::Connection.new.cookie )
|
203
|
+
assert_keys( {"cookie1"=>["~value1"], "cookie2"=>["value1", "value2"]},
|
204
|
+
Web::Connection.new.request )
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_cgi_parses_cookie
|
208
|
+
ENV['COOKIE'] = "cookie1=%7Evalue1; cookie2=value1; cookie2=value2"
|
209
|
+
|
210
|
+
assert_keys( {"cookie1"=>["~value1"], "cookie2"=>["value1", "value2"]},
|
211
|
+
Web::Connection.new.cookie )
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_cgi_only_parses_cookies_once
|
215
|
+
ENV['HTTP_COOKIE'] = "cookie1=%7Evalue1; cookie2=value1; cookie2=value2"
|
216
|
+
ENV['COOKIE'] = "cookie1=%7Evalue1; cookie2=value1; cookie2=value2"
|
217
|
+
assert_keys( {"cookie1"=>["~value1"], "cookie2"=>["value1", "value2"]},
|
218
|
+
Web::Connection.new.cookie )
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_parse_empty_query_string
|
222
|
+
ENV["REQUEST_METHOD"] = "GET"
|
223
|
+
cgi = Web::Connection.new
|
224
|
+
assert_equal( {}, cgi.request )
|
225
|
+
end
|
226
|
+
|
227
|
+
def test_parse_get_query_string
|
228
|
+
ENV["REQUEST_METHOD"] = "GET"
|
229
|
+
ENV["QUERY_STRING"] = "key=a%20value&anotherKey=another%20value"
|
230
|
+
cgi = Web::Connection.new
|
231
|
+
assert_equal( {"key" => ["a value"], "anotherKey" => ["another value"]}, cgi.request )
|
232
|
+
end
|
233
|
+
|
234
|
+
def test_parse_get_multiples
|
235
|
+
ENV["REQUEST_METHOD"] = "GET"
|
236
|
+
ENV["QUERY_STRING"] = "key=a%20value&key=another%20value"
|
237
|
+
cgi = Web::Connection.new
|
238
|
+
assert_equal( {"key" => ["a value", "another value"]}, cgi.request )
|
239
|
+
end
|
240
|
+
|
241
|
+
def test_parse_head_like_get
|
242
|
+
ENV["REQUEST_METHOD"] = "HEAD"
|
243
|
+
cgi = Web::Connection.new
|
244
|
+
assert_equal( {}, cgi.request )
|
245
|
+
|
246
|
+
teardown
|
247
|
+
|
248
|
+
ENV["REQUEST_METHOD"] = "HEAD"
|
249
|
+
ENV["QUERY_STRING"] = "key=a%20value&anotherKey=another%20value"
|
250
|
+
cgi = Web::Connection.new
|
251
|
+
assert_equal( {"key" => ["a value"], "anotherKey" => ["another value"]},
|
252
|
+
cgi.request )
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_parse_post
|
256
|
+
ENV["REQUEST_METHOD"] = "POST"
|
257
|
+
ENV["CONTENT_LENGTH"] = "40"
|
258
|
+
File.open( "test/web/testfiles/post.data" ){ |post_data|
|
259
|
+
$stdin = post_data
|
260
|
+
cgi = Web::Connection.new
|
261
|
+
|
262
|
+
assert_equal( "key=a%20value&anotherKey=another%20value", cgi.raw_post_data.read )
|
263
|
+
assert_keys( {"key" => ["a value"], "anotherKey" => ["another value"]},
|
264
|
+
cgi.post )
|
265
|
+
assert_keys( {"key" => ["a value"], "anotherKey" => ["another value"]},
|
266
|
+
cgi.request )
|
267
|
+
}
|
268
|
+
end
|
269
|
+
|
270
|
+
# TODO: add multipart tests for mozilla, netscape 4, explorer 5
|
271
|
+
def test_parse_multiparts
|
272
|
+
multipart_test(177696,
|
273
|
+
'----------0xKhTmLbOuNdArY',
|
274
|
+
'mac-safari-fallow_dump.bin' )
|
275
|
+
end
|
276
|
+
|
277
|
+
def multipart_test( content_length, boundary, datafile )
|
278
|
+
ENV["REQUEST_METHOD"] = "POST"
|
279
|
+
ENV["CONTENT_LENGTH"] = content_length.to_s
|
280
|
+
ENV["CONTENT_TYPE" ] = "multipart/form-data, boundary=#{ boundary }"
|
281
|
+
|
282
|
+
@stdin = $stdin
|
283
|
+
|
284
|
+
File.open( "test/web/testfiles/#{ datafile }" ) do |post_data|
|
285
|
+
$stdin = post_data
|
286
|
+
multipart_asserts
|
287
|
+
end
|
288
|
+
ensure
|
289
|
+
$stdin = @stdin
|
290
|
+
end
|
291
|
+
|
292
|
+
def multipart_asserts
|
293
|
+
progress_events = [ ]
|
294
|
+
Web::config[:multipart_progress_hook] = lambda do |total, read|
|
295
|
+
progress_events.push [total, read]
|
296
|
+
end
|
297
|
+
params = Web::open(:out=>'') do |connection|
|
298
|
+
connection.request
|
299
|
+
end
|
300
|
+
|
301
|
+
image = 'fallow_keep.png.450x450.2005-07-24.jpg'
|
302
|
+
assert_equal( ["key", "file"], params.keys )
|
303
|
+
assert_equal( "value", params["key"] )
|
304
|
+
assert_match( /#{image}/, params["file"].original_filename )
|
305
|
+
assert_match( /image/, params["file"].content_type )
|
306
|
+
assert_match( /jpeg/, params["file"].content_type )
|
307
|
+
File.open( "test/web/testfiles/#{image}" ) { |test_image|
|
308
|
+
test_image.binmode
|
309
|
+
assert_equal( test_image.read, params["file"].read )
|
310
|
+
}
|
311
|
+
assert_equal( [ [177667, 0],
|
312
|
+
[177667, 10240],
|
313
|
+
[177667, 177667] ],
|
314
|
+
progress_events )
|
315
|
+
ensure
|
316
|
+
Web::config[:multipart_progress_hook] = nil
|
317
|
+
end
|
318
|
+
|
319
|
+
### TODO: What happens if you upload two files?
|
320
|
+
### What if they have the same name?
|
321
|
+
def test_parse_two_uploaded_files
|
322
|
+
|
323
|
+
end
|
324
|
+
|
325
|
+
def test_downcase_env
|
326
|
+
ENV["KEY"] = "value"
|
327
|
+
env = Web::Connection::downcase_env( ENV )
|
328
|
+
["key", "kEy", "kEY", "KEY", "KEy", "Key", "keY"].each{ |k|
|
329
|
+
assert( env.has_key?(k) )
|
330
|
+
assert_equal( "value", env[k] )
|
331
|
+
}
|
332
|
+
end
|
333
|
+
|
334
|
+
def options
|
335
|
+
{}
|
336
|
+
end
|
337
|
+
|
338
|
+
def test_mac_ie_multipart_bug
|
339
|
+
require 'yaml'
|
340
|
+
test_env = nil
|
341
|
+
File.open("test/web/testfiles/mac_ie_env.yaml", "r") { |f|
|
342
|
+
test_env = YAML.load(f)
|
343
|
+
}
|
344
|
+
|
345
|
+
backup_env = {}
|
346
|
+
test_env.each{ |k,v|
|
347
|
+
backup_env[k.downcase] = ENV[k.downcase]
|
348
|
+
ENV[k.downcase] = v
|
349
|
+
}
|
350
|
+
|
351
|
+
begin
|
352
|
+
File.open("test/web/testfiles/mac_ie_stdin.bin", "r") { |f|
|
353
|
+
$stdin = f
|
354
|
+
|
355
|
+
cgi.parse_request
|
356
|
+
|
357
|
+
assert_equal "test", cgi.post["text"].first
|
358
|
+
assert_equal Web::Upload, cgi.post["small"].first.class
|
359
|
+
assert_equal 15, cgi.post["button.x"].first.to_i
|
360
|
+
assert_equal 11, cgi.post["button.y"].first.to_i
|
361
|
+
}
|
362
|
+
ensure
|
363
|
+
backup_env.each{ |k,v|
|
364
|
+
ENV[k] = v
|
365
|
+
}
|
366
|
+
end
|
367
|
+
end
|
368
|
+
end
|
@@ -0,0 +1,637 @@
|
|
1
|
+
require 'test/web/mod_ruby_stub'
|
2
|
+
|
3
|
+
class TestResponse < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def output
|
6
|
+
@output = StringIO.new unless @output
|
7
|
+
@output
|
8
|
+
end
|
9
|
+
|
10
|
+
def unbuffered_response
|
11
|
+
@unbuffered_response ||= Web::Connection.new( :out => output, :buffered => false, :session => {} )
|
12
|
+
@unbuffered_response
|
13
|
+
end
|
14
|
+
|
15
|
+
def nph_response
|
16
|
+
@nph_response ||= Web::Connection.new( :out => output, :nph => true, :session => {} )
|
17
|
+
@nph_response
|
18
|
+
end
|
19
|
+
|
20
|
+
def response
|
21
|
+
@response ||= Web::Connection.new( :out => output, :session => {} )
|
22
|
+
@response
|
23
|
+
end
|
24
|
+
|
25
|
+
def mod_response
|
26
|
+
require 'web/sapi/apache'
|
27
|
+
@mod_response ||= Web::ModRubyConnection.new( :out => output,
|
28
|
+
:session => {} )
|
29
|
+
@mod_response
|
30
|
+
end
|
31
|
+
|
32
|
+
def setup
|
33
|
+
@old_record = $\
|
34
|
+
@old_newline = $/
|
35
|
+
@old_readline = $_
|
36
|
+
@old_stdin = $stdin
|
37
|
+
@output = nil
|
38
|
+
end
|
39
|
+
|
40
|
+
def teardown
|
41
|
+
$\ = @old_record
|
42
|
+
$/ = @old_newline
|
43
|
+
$_ = @old_readline
|
44
|
+
$stdin = @old_stdin
|
45
|
+
@output = @response = @nph_response = @unbuffered_response = @mod_response = nil
|
46
|
+
ENV["SCRIPT_NAME"] = nil
|
47
|
+
ENV["SERVER_PROTOCOL"] = nil
|
48
|
+
ENV["SERVER_SOFTWARE"] = nil
|
49
|
+
Apache.reset
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_noheader
|
53
|
+
mockio = ""
|
54
|
+
response = Web::Connection.new( :out => mockio, :session => {}, :noheader => true )
|
55
|
+
response.add_header("Location", "/home.html" )
|
56
|
+
response.print "Should be no headers"
|
57
|
+
response.flush
|
58
|
+
expected = "Should be no headers"
|
59
|
+
assert_equal( expected, mockio )
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_outputs
|
63
|
+
mockio = ""
|
64
|
+
response = Web::Connection.new( :out => mockio, :session => {} )
|
65
|
+
response.content_type = "text/html"
|
66
|
+
response.add_header("Location", "/home.html" )
|
67
|
+
response.print "He's just a NoWhere Man"
|
68
|
+
response.clear
|
69
|
+
response.print "Hello, World"
|
70
|
+
response.flush
|
71
|
+
|
72
|
+
expected = "Content-Type: text/html\r\n"
|
73
|
+
expected += "Location: /home.html\r\n"
|
74
|
+
expected += "Status: 200 OK\r\n"
|
75
|
+
expected += "\r\n"
|
76
|
+
expected += "Hello, World"
|
77
|
+
|
78
|
+
assert_equal( expected, mockio )
|
79
|
+
|
80
|
+
# flushing a second time doesn't send more output
|
81
|
+
response.flush
|
82
|
+
assert_equal( expected, mockio )
|
83
|
+
|
84
|
+
# can't clear output after headers have been sent
|
85
|
+
assert_raises( Exception ) {
|
86
|
+
response.clear
|
87
|
+
}
|
88
|
+
expected
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_send_file
|
93
|
+
# send file, and set a reasonable header
|
94
|
+
cgi = Web::Connection.new(:out=>"")
|
95
|
+
Web::connection = cgi
|
96
|
+
cgi.send_file( "test/web/test_send_file.txt" )
|
97
|
+
cgi.assert_content( "test_send_file\n" )
|
98
|
+
cgi.assert_header( "Content-Type", "text/plain" )
|
99
|
+
|
100
|
+
cgi.write "no more output should be written"
|
101
|
+
cgi.assert_content( "test_send_file\n" )
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
# test we don't clobber an existing header
|
106
|
+
cgi = Web::Connection.new(:out=>"")
|
107
|
+
Web::connection = cgi
|
108
|
+
cgi.content_type = "xapplication/ms-excel"
|
109
|
+
cgi.send_file( "test/web/test_send_file.txt" )
|
110
|
+
cgi.assert_content( "test_send_file\n" )
|
111
|
+
cgi.assert_header( "Content-Type", "xapplication/ms-excel" )
|
112
|
+
|
113
|
+
# this method should be useful for packagers
|
114
|
+
cgi = Web::Connection.new(:out=>"")
|
115
|
+
Web::connection = cgi
|
116
|
+
cgi.content_type = 'text/html'
|
117
|
+
Web::send_lib_file( "test_send_file.txt" )
|
118
|
+
cgi.assert_content( "test_send_file\n" )
|
119
|
+
cgi.assert_header( "Content-Type", "text/plain" )
|
120
|
+
|
121
|
+
# test we don't clobber an existing header
|
122
|
+
cgi = Web::Connection.new(:out=>"")
|
123
|
+
Web::connection = cgi
|
124
|
+
cgi.content_type = "xapplication/ms-excel"
|
125
|
+
Web::send_lib_file( "test_send_file.txt" )
|
126
|
+
cgi.assert_content( "test_send_file\n" )
|
127
|
+
cgi.assert_header( "Content-Type", "xapplication/ms-excel" )
|
128
|
+
|
129
|
+
assert_equal( "test_send_file\n",
|
130
|
+
Web::lib_file_contents( "test_send_file.txt" ) )
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_default_out
|
135
|
+
response = Web::Connection.new
|
136
|
+
|
137
|
+
assert_equal( $stdout, response.output )
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_default_content_type
|
141
|
+
response.flush
|
142
|
+
assert_equal( "Content-Type: text/html\r\nStatus: 200 OK\r\n\r\n", output.string )
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_set_content_type
|
146
|
+
response.content_type = "text/plain"
|
147
|
+
response.flush
|
148
|
+
assert_equal( "text/plain", response.content_type )
|
149
|
+
assert_equal( "Content-Type: text/plain\r\nStatus: 200 OK\r\n\r\n", output.string )
|
150
|
+
|
151
|
+
assert_raises( Web::Error ) {
|
152
|
+
response.content_type = "text/something"
|
153
|
+
}
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_set_charset_after_flush_raises_cgiexception
|
157
|
+
response.flush
|
158
|
+
assert_raises( Web::Error ) {
|
159
|
+
response.charset = "foo"
|
160
|
+
}
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_set_location
|
164
|
+
assert_equal( nil, response.location )
|
165
|
+
response.location = "/somewhere"
|
166
|
+
assert_equal( "/somewhere", response.location )
|
167
|
+
response.flush
|
168
|
+
assert_equal( "Content-Type: text/html\r\nLocation: /somewhere\r\nStatus: 200 OK\r\n\r\n", output.string )
|
169
|
+
|
170
|
+
assert_raises( Web::Error ) {
|
171
|
+
response.location = "foo"
|
172
|
+
}
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_encoding_sends_header
|
176
|
+
response.encoding = "7bit"
|
177
|
+
response.flush
|
178
|
+
assert_equal( "Content-Encoding: 7bit\r\nContent-Type: text/html\r\nStatus: 200 OK\r\n\r\n", output.string )
|
179
|
+
|
180
|
+
assert_raises( Web::Error ) {
|
181
|
+
response.encoding = "foo"
|
182
|
+
}
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_status_sends_header
|
186
|
+
response.status = "300"
|
187
|
+
response.flush
|
188
|
+
assert_equal( "Content-Type: text/html\r\nStatus: 300 Multiple Choices\r\n\r\n", output.string )
|
189
|
+
|
190
|
+
assert_raises( Web::Error ) {
|
191
|
+
response.status = "500"
|
192
|
+
}
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_status_message
|
196
|
+
Web::Connection::HTTP_STATUS.each{ |code, message|
|
197
|
+
setup
|
198
|
+
response.status = code
|
199
|
+
response.flush
|
200
|
+
assert_equal( "Content-Type: text/html\r\n" +
|
201
|
+
"Status: #{ code } #{ message }\r\n\r\n",
|
202
|
+
output.string )
|
203
|
+
teardown
|
204
|
+
}
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_set_status_with_i
|
208
|
+
Web::Connection::HTTP_STATUS.each{ |code, message|
|
209
|
+
setup
|
210
|
+
response.status = code.to_i
|
211
|
+
response.flush
|
212
|
+
assert_equal( "Content-Type: text/html\r\n" +
|
213
|
+
"Status: #{ code } #{ message }\r\n\r\n",
|
214
|
+
output.string )
|
215
|
+
teardown
|
216
|
+
}
|
217
|
+
end
|
218
|
+
|
219
|
+
def test_get_header
|
220
|
+
# test no result
|
221
|
+
assert_equal( [], response.get_header("not there") )
|
222
|
+
|
223
|
+
response.add_header( "patrick", "dufus" )
|
224
|
+
response.add_header( "tom", "smarty-pants" )
|
225
|
+
response.add_header( "tom", "drinker" )
|
226
|
+
|
227
|
+
# test single
|
228
|
+
assert_equal( ["dufus"], response.get_header("patrick") )
|
229
|
+
# test multiple
|
230
|
+
assert_equal( ["smarty-pants", "drinker"], response.get_header("tom") )
|
231
|
+
# test state unaffected (return value, not reference)
|
232
|
+
response.get_header("patrick").push "drunk"
|
233
|
+
assert_equal( ["dufus"], response.get_header("patrick") )
|
234
|
+
# test case insensitivity
|
235
|
+
assert_equal( ["dufus"], response.get_header("PAtrIck") )
|
236
|
+
end
|
237
|
+
|
238
|
+
def test_assert_header
|
239
|
+
# missing header
|
240
|
+
assert_raises( Test::Unit::AssertionFailedError ){
|
241
|
+
response.assert_header( "non-existent", "this header doesn't exist" )
|
242
|
+
}
|
243
|
+
|
244
|
+
# incorrect header
|
245
|
+
response.add_header( "patrick", "dufus" );
|
246
|
+
assert_raises( Test::Unit::AssertionFailedError ){
|
247
|
+
response.assert_header( "patrick", "genius" )
|
248
|
+
}
|
249
|
+
|
250
|
+
# works with single header
|
251
|
+
response.assert_header( "patrick", "dufus" );
|
252
|
+
# or array
|
253
|
+
response.assert_header( "patrick", ["dufus"] );
|
254
|
+
|
255
|
+
response.add_header( "patrick", "dumbo" );
|
256
|
+
|
257
|
+
# multiple headers need the whole array
|
258
|
+
assert_raises( Test::Unit::AssertionFailedError ){
|
259
|
+
response.assert_header( "patrick", "dufus" )
|
260
|
+
}
|
261
|
+
|
262
|
+
# full array works
|
263
|
+
response.assert_header( "patrick", ["dufus", "dumbo"] )
|
264
|
+
|
265
|
+
# order matters
|
266
|
+
assert_raises( Test::Unit::AssertionFailedError ){
|
267
|
+
response.assert_header( "patrick", ["dumbo", "dufus"] )
|
268
|
+
}
|
269
|
+
|
270
|
+
end
|
271
|
+
|
272
|
+
def test_add_header_only_adds_one
|
273
|
+
response.add_header( "Content-Type", "text/plain" )
|
274
|
+
response.add_header( "Content-Type", "text/fancy" )
|
275
|
+
response.add_header( "charset", "foo" )
|
276
|
+
response.add_header( "charset", "yoo" )
|
277
|
+
response.add_header( "Status", "300" )
|
278
|
+
response.add_header( "status", "404" )
|
279
|
+
response.add_header( "Content-Encoding", "7bit" )
|
280
|
+
response.add_header( "contEnt-enCoding", "16bit")
|
281
|
+
response.add_header( "location", "somewhere" )
|
282
|
+
response.add_header( "LocaTion", "somewhere/else" )
|
283
|
+
|
284
|
+
response.flush
|
285
|
+
|
286
|
+
assert_equal( "Content-Encoding: 16bit\r\n" +
|
287
|
+
"Content-Type: text/fancy; charset=yoo\r\n" +
|
288
|
+
"Location: somewhere/else\r\n" +
|
289
|
+
"Status: 404 Not Found\r\n\r\n",
|
290
|
+
output.string )
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_send_headers
|
294
|
+
assert_equal( "", output.string )
|
295
|
+
response.flush
|
296
|
+
assert_equal( "Content-Type: text/html\r\nStatus: 200 OK\r\n\r\n",
|
297
|
+
output.string )
|
298
|
+
assert_raises( Web::Error ) {
|
299
|
+
response.add_header("Set-Cookie", "something")
|
300
|
+
}
|
301
|
+
response << "Hello World"
|
302
|
+
response.flush
|
303
|
+
assert_equal( "Content-Type: text/html\r\nStatus: 200 OK\r\n\r\nHello World",
|
304
|
+
output.string )
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_send_multiple_headers
|
308
|
+
response.add_header( "Patrick", "Genius" )
|
309
|
+
response.add_header( "Patrick", "Dufus" )
|
310
|
+
response.flush
|
311
|
+
|
312
|
+
expected = "Content-Type: text/html\r\nPatrick: Genius\r\nPatrick: Dufus\r\nStatus: 200 OK\r\n\r\n"
|
313
|
+
assert_equal( expected, output.string )
|
314
|
+
end
|
315
|
+
|
316
|
+
def test_send_nph_headers
|
317
|
+
nph_response.flush
|
318
|
+
assert_match( %r|HTTP/1.0: 200 OK\r\nDate: |, output.string )
|
319
|
+
end
|
320
|
+
|
321
|
+
def test_nph_uses_env_server_protocol
|
322
|
+
ENV["SERVER_PROTOCOL"] = "HTTP/1.1"
|
323
|
+
nph_response.flush
|
324
|
+
assert_match( %r|HTTP/1.1: 200 OK\r\nDate: |, output.string )
|
325
|
+
end
|
326
|
+
|
327
|
+
def test_nph_uses_status
|
328
|
+
nph_response.status = "300"
|
329
|
+
nph_response.flush
|
330
|
+
assert_match( %r|HTTP/1.0: 300 Multiple Choices\r\nDate: |, output.string )
|
331
|
+
assert_nil( /Status: 300/ =~ output.string )
|
332
|
+
end
|
333
|
+
|
334
|
+
def test_nph_sets_server_software
|
335
|
+
ENV["SERVER_SOFTWARE"] = "Narf"
|
336
|
+
nph_response.flush
|
337
|
+
assert_match( /Server: Narf/, output.string )
|
338
|
+
end
|
339
|
+
|
340
|
+
def test_nph_sets_server_software_to_default
|
341
|
+
nph_response.flush
|
342
|
+
assert_match( /Server: /, output.string )
|
343
|
+
end
|
344
|
+
|
345
|
+
def test_nph_doesnt_overwrite_server_software
|
346
|
+
ENV["SERVER_SOFTWARE"] = "Narf"
|
347
|
+
nph_response.add_header( "Server", "MyNarf" )
|
348
|
+
nph_response.flush
|
349
|
+
assert_match( /Server: MyNarf/, output.string )
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_nph_mode_with_IIS
|
353
|
+
ENV["SERVER_SOFTWARE"] = "IIS"
|
354
|
+
response.flush
|
355
|
+
assert_match( %r|HTTP/1.0: 200 OK|, output.string )
|
356
|
+
end
|
357
|
+
|
358
|
+
def test_send_headers_with_mod_ruby
|
359
|
+
mod_response.set_cookie( "cookie", "monster" )
|
360
|
+
mod_response.set_cookie( "bert", "ernie" )
|
361
|
+
mod_response.add_header( "Patrick", "cool" )
|
362
|
+
|
363
|
+
mod_response.send_header
|
364
|
+
|
365
|
+
headers = Apache::request.headers_out
|
366
|
+
assert_equal( ["cookie=monster; path=", "bert=ernie; path="],
|
367
|
+
headers["Set-Cookie"] )
|
368
|
+
assert_equal( "cool", headers["Patrick"] )
|
369
|
+
assert_equal( "200", Apache.request.status_line )
|
370
|
+
assert_equal( "text/html", Apache.request.content_type )
|
371
|
+
assert_equal( nil, Apache.request.content_encoding )
|
372
|
+
assert( Apache.request.sent? )
|
373
|
+
end
|
374
|
+
|
375
|
+
def test_send_charset_with_mod_ruby
|
376
|
+
mod_response.charset = "English"
|
377
|
+
mod_response.send_header
|
378
|
+
assert_equal( "text/html; charset=English", Apache.request.content_type )
|
379
|
+
assert( !Apache.request.headers_out.has_key?("Content-Type") )
|
380
|
+
end
|
381
|
+
|
382
|
+
def test_send_content_type_with_mod_ruby
|
383
|
+
mod_response.content_type = "text/something"
|
384
|
+
mod_response.send_header
|
385
|
+
assert_equal( "text/something", Apache.request.content_type )
|
386
|
+
assert( !Apache.request.headers_out.has_key?("Content-Type") )
|
387
|
+
end
|
388
|
+
|
389
|
+
def test_send_status_with_mod_ruby
|
390
|
+
mod_response.status = "300"
|
391
|
+
mod_response.send_header
|
392
|
+
assert_equal( "300", Apache::request.status_line )
|
393
|
+
assert( !Apache.request.headers_out.has_key?("Status") )
|
394
|
+
end
|
395
|
+
|
396
|
+
def test_send_encoding_with_mod_ruby
|
397
|
+
mod_response.encoding = "7bit"
|
398
|
+
mod_response.send_header
|
399
|
+
assert_equal( "7bit", Apache::request.content_encoding )
|
400
|
+
assert( !Apache.request.headers_out.has_key?("Content-Encoding") )
|
401
|
+
end
|
402
|
+
|
403
|
+
def test_append_type_and_charset
|
404
|
+
response.charset = "ISO"
|
405
|
+
response.flush
|
406
|
+
assert_equal( "Content-Type: text/html; charset=ISO\r\nStatus: 200 OK\r\n\r\n",
|
407
|
+
output.string )
|
408
|
+
end
|
409
|
+
|
410
|
+
def test_charset
|
411
|
+
response.charset = "ISO"
|
412
|
+
assert_equal( "ISO", response.charset )
|
413
|
+
end
|
414
|
+
|
415
|
+
def test_redirect
|
416
|
+
eval <<-EVAL
|
417
|
+
def response.redirect
|
418
|
+
@header["Location"]
|
419
|
+
end
|
420
|
+
EVAL
|
421
|
+
|
422
|
+
response.set_redirect("/somewhere")
|
423
|
+
assert_equal( "/somewhere", response.location )
|
424
|
+
assert_equal( "302", response.status )
|
425
|
+
end
|
426
|
+
|
427
|
+
def test_set_cookie
|
428
|
+
response.set_cookie("myCookie", "myValue",
|
429
|
+
:path => "/path/" ,
|
430
|
+
:domain => "mydomain.com" ,
|
431
|
+
:expires => Time.gm(2002,2,3),
|
432
|
+
:secure => true)
|
433
|
+
assert_equal( "myCookie=myValue; path=/path/; domain=mydomain.com; expires=Sun, 03 Feb 2002 00:00:00 GMT; secure", response.header["Set-Cookie"].to_s )
|
434
|
+
end
|
435
|
+
|
436
|
+
|
437
|
+
def test_get_cookie
|
438
|
+
assert_equal( [ ], response.get_cookie( "not there" ) )
|
439
|
+
assert_equal( [ ], response.cookie["not there"] )
|
440
|
+
|
441
|
+
response.set_cookie("myCookie", "myValue",
|
442
|
+
:path => "/path/" ,
|
443
|
+
:domain => "mydomain.com",
|
444
|
+
:expires => Time.gm(2002,2,3),
|
445
|
+
:secure => true)
|
446
|
+
response.set_cookie("key", "anything" )
|
447
|
+
response.set_cookie("key", "something" )
|
448
|
+
|
449
|
+
assert_equal( [ ], response.get_cookie( "myCookie" ) )
|
450
|
+
assert_equal( [ ], response.get_cookie( "key" ) )
|
451
|
+
|
452
|
+
if false
|
453
|
+
# do I really want to set @cookie values when we called set_cookie?
|
454
|
+
# it seems to make a mess, esp. with multiple cookies.
|
455
|
+
# ~ patrick
|
456
|
+
|
457
|
+
# test single
|
458
|
+
assert_equal( "myValue", response.get_cookie( "myCookie" ) )
|
459
|
+
assert_equal( "myValue", response.cookie["myCookie"] )
|
460
|
+
|
461
|
+
response.set_cookie( "myCookie", "another" )
|
462
|
+
assert_equal( "another", response.get_cookie( "myCookie" ) )
|
463
|
+
assert_equal( "another", response.cookie["myCookie"] )
|
464
|
+
|
465
|
+
# test multiple
|
466
|
+
assert_equal( "something", response.get_cookie("key") )
|
467
|
+
assert_equal( "something", response.cookie["key"] )
|
468
|
+
end
|
469
|
+
end
|
470
|
+
|
471
|
+
def test_assert_cookie
|
472
|
+
# no cookie fails
|
473
|
+
assert_raises(Test::Unit::AssertionFailedError){
|
474
|
+
response.assert_cookie( "no cookie", "monster hungry!" )
|
475
|
+
}
|
476
|
+
|
477
|
+
# no cookie passes
|
478
|
+
response.assert_cookie( "no cookie", nil )
|
479
|
+
|
480
|
+
response.set_cookie( "cookie", "monster" )
|
481
|
+
|
482
|
+
# single cookie passes
|
483
|
+
response.assert_cookie( "cookie", "monster" )
|
484
|
+
|
485
|
+
# single cookie array passes
|
486
|
+
response.assert_cookie( "cookie", ["monster"] )
|
487
|
+
end
|
488
|
+
|
489
|
+
def test_insecure_cookie
|
490
|
+
response.set_cookie("myCookie", "myValue",
|
491
|
+
:path => "/path/" ,
|
492
|
+
:domain => "mydomain.com",
|
493
|
+
:expires => Time.gm(2002,2,3) )
|
494
|
+
|
495
|
+
assert_equal( "myCookie=myValue; path=/path/; domain=mydomain.com; expires=Sun, 03 Feb 2002 00:00:00 GMT", response.header["Set-Cookie"].to_s )
|
496
|
+
|
497
|
+
# clear for the next test
|
498
|
+
response.header["Set-Cookie"] = []
|
499
|
+
|
500
|
+
response.set_cookie("myCookie", "myValue",
|
501
|
+
:path => "/path/",
|
502
|
+
:domain => "mydomain.com",
|
503
|
+
:expires => Time.gm(2002,2,3),
|
504
|
+
:secure => false)
|
505
|
+
|
506
|
+
assert_equal( "myCookie=myValue; path=/path/; domain=mydomain.com; expires=Sun, 03 Feb 2002 00:00:00 GMT", response.header["Set-Cookie"].to_s )
|
507
|
+
end
|
508
|
+
|
509
|
+
def test_leave_domain_off_cookie
|
510
|
+
response.set_cookie("myCookie", "myValue",
|
511
|
+
:path => "/path/" ,
|
512
|
+
:expires => Time.gm(2002,2,3),
|
513
|
+
:secure => true)
|
514
|
+
|
515
|
+
assert_equal( "myCookie=myValue; path=/path/; expires=Sun, 03 Feb 2002 00:00:00 GMT; secure", response.header["Set-Cookie"].to_s )
|
516
|
+
end
|
517
|
+
|
518
|
+
def test_leave_path_off_cookie
|
519
|
+
response.set_cookie("myCookie", "myValue",
|
520
|
+
:domain => "mydomain.com",
|
521
|
+
:expires => Time.gm(2002,2,3),
|
522
|
+
:secure => true)
|
523
|
+
|
524
|
+
assert_equal( "myCookie=myValue; path=; domain=mydomain.com; expires=Sun, 03 Feb 2002 00:00:00 GMT; secure", response.header["Set-Cookie"].to_s )
|
525
|
+
end
|
526
|
+
|
527
|
+
def test_leave_expires_off_cookie
|
528
|
+
response.set_cookie("myCookie", "myValue",
|
529
|
+
:path => "/path/" ,
|
530
|
+
:domain => "mydomain.com",
|
531
|
+
:secure => true)
|
532
|
+
|
533
|
+
assert_equal( "myCookie=myValue; path=/path/; domain=mydomain.com; secure", response.header["Set-Cookie"].to_s )
|
534
|
+
end
|
535
|
+
|
536
|
+
def test_ie_path_support
|
537
|
+
ENV["SCRIPT_NAME"] = "http://www.localhost.com/cgi-bin/script.rb"
|
538
|
+
response.set_cookie("myCookie", "myValue" )
|
539
|
+
assert_equal( "myCookie=myValue; path=http://www.localhost.com/cgi-bin/", response.header["Set-Cookie"].to_s )
|
540
|
+
end
|
541
|
+
|
542
|
+
def test_values_are_escaped
|
543
|
+
response.set_cookie("angryCookie", "!@$#%^&*")
|
544
|
+
assert_equal( "angryCookie=%21%40%24%23%25%5E%26%2A; path=", response.header["Set-Cookie"].to_s )
|
545
|
+
end
|
546
|
+
|
547
|
+
def test_array_of_values
|
548
|
+
response.set_cookie( "meals", ["breakfast", "lunch", "dinner"] )
|
549
|
+
assert_equal( "meals=breakfast&lunch&dinner; path=", response.header["Set-Cookie"].to_s )
|
550
|
+
end
|
551
|
+
|
552
|
+
def test_setting_two_cookies
|
553
|
+
response.set_cookie( "first", "one" )
|
554
|
+
response.set_cookie( "next", "two" )
|
555
|
+
assert_equal( ["first=one; path=", "next=two; path="], response.header["Set-Cookie"] )
|
556
|
+
end
|
557
|
+
|
558
|
+
def test_buffered_output
|
559
|
+
response.encoding = "kanji"
|
560
|
+
response.write( "Your page moved!" )
|
561
|
+
assert_equal( "", output.string )
|
562
|
+
response.set_redirect("/somewhere")
|
563
|
+
assert_equal( "", output.string )
|
564
|
+
|
565
|
+
response.flush
|
566
|
+
|
567
|
+
assert_equal( "Content-Encoding: kanji\r\n" +
|
568
|
+
"Content-Type: text/html\r\n" +
|
569
|
+
"Location: /somewhere\r\n" +
|
570
|
+
"Status: 302 Found\r\n" +
|
571
|
+
"\r\nYour page moved!",
|
572
|
+
output.string )
|
573
|
+
end
|
574
|
+
|
575
|
+
def test_unbuffered_output
|
576
|
+
unbuffered_response << "Hello World"
|
577
|
+
|
578
|
+
assert_raises( Web::Error ) {
|
579
|
+
unbuffered_response.add_header( "Patrick", "May" )
|
580
|
+
}
|
581
|
+
|
582
|
+
assert_equal( add_header("Hello World"), output.string )
|
583
|
+
|
584
|
+
# test flush doesn't affect unbuffered output
|
585
|
+
unbuffered_response.flush
|
586
|
+
assert_equal( add_header("Hello World"), output.string )
|
587
|
+
end
|
588
|
+
|
589
|
+
#-----------------------------------------
|
590
|
+
# test output methods
|
591
|
+
#-----------------------------------------
|
592
|
+
|
593
|
+
def add_header( string )
|
594
|
+
"Content-Type: text/html\r\nStatus: 200 OK\r\n\r\n#{ string }"
|
595
|
+
end
|
596
|
+
|
597
|
+
def test_append
|
598
|
+
unbuffered_response << "Hello World"
|
599
|
+
assert_equal( add_header("Hello World"), output.string )
|
600
|
+
end
|
601
|
+
|
602
|
+
def test_write
|
603
|
+
unbuffered_response.write( "Hello World" )
|
604
|
+
assert_equal( add_header("Hello World"), output.string )
|
605
|
+
end
|
606
|
+
|
607
|
+
# I had trouble testing this
|
608
|
+
# def test_default_print
|
609
|
+
# end
|
610
|
+
|
611
|
+
def test_print_multiple_args
|
612
|
+
$\ = nil
|
613
|
+
unbuffered_response.print( "hello ", "world ", "im ", "here" )
|
614
|
+
assert_equal( add_header("hello world im here"), output.string )
|
615
|
+
end
|
616
|
+
|
617
|
+
def test_print_non_strings
|
618
|
+
unbuffered_response.print( "hello ", 43770 )
|
619
|
+
assert_equal( add_header("hello 43770"), output.string )
|
620
|
+
end
|
621
|
+
|
622
|
+
def test_puts
|
623
|
+
$\ = "\n"
|
624
|
+
unbuffered_response.puts( "hello", 43770, "anyone?" )
|
625
|
+
assert_equal( add_header("hello\n43770\nanyone?\n"), output.string )
|
626
|
+
end
|
627
|
+
|
628
|
+
def test_stdin
|
629
|
+
c = nil
|
630
|
+
Web::open( :out => output ) do
|
631
|
+
c = Web::connection
|
632
|
+
Kernel::puts "Hello World"
|
633
|
+
end
|
634
|
+
c.assert_content( "Hello World\n" )
|
635
|
+
end
|
636
|
+
|
637
|
+
end
|