ruby-web 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. data/ChangeLog +474 -0
  2. data/INSTALL.txt +9 -0
  3. data/InstalledFiles +180 -0
  4. data/LICENSE.txt +74 -0
  5. data/Rakefile +529 -0
  6. data/TODO +65 -0
  7. data/doc/additional.xml +149 -0
  8. data/doc/core.xml +652 -0
  9. data/doc/credits/index.xml +52 -0
  10. data/doc/credits/php.contributors.xml +118 -0
  11. data/doc/credits/php.language-snippets.ent +622 -0
  12. data/doc/install/index.xml +136 -0
  13. data/doc/install/mac/index.xml +21 -0
  14. data/doc/install/ruby-web.install.rb.instructions.xml +7 -0
  15. data/doc/install/unix/index.xml +46 -0
  16. data/doc/install/win/apache1.xml +166 -0
  17. data/doc/install/win/apache2.xml +141 -0
  18. data/doc/install/win/iis.xml +162 -0
  19. data/doc/install/win/index.xml +24 -0
  20. data/doc/install/win/installer.xml +31 -0
  21. data/doc/install/win/manual.xml +43 -0
  22. data/doc/manual.xml +69 -0
  23. data/doc/old/apache_cgi.txt +23 -0
  24. data/doc/old/fastcgi.txt +23 -0
  25. data/doc/old/mod_ruby.txt +21 -0
  26. data/doc/old/snippets.rdoc +183 -0
  27. data/doc/old/webrick.txt +23 -0
  28. data/doc/old/windows_cgi.txt +9 -0
  29. data/doc/tutorial.xml +14 -0
  30. data/doc/xsl/manual-multi.xsl +10 -0
  31. data/doc/xsl/manual-pdf.xsl +6 -0
  32. data/doc/xsl/manual-single.xsl +6 -0
  33. data/doc/xsl/manual.css +22 -0
  34. data/install.rb +1022 -0
  35. data/lib/formatter.rb +314 -0
  36. data/lib/html-parser.rb +429 -0
  37. data/lib/htmlrepair.rb +113 -0
  38. data/lib/htmlsplit.rb +842 -0
  39. data/lib/sgml-parser.rb +332 -0
  40. data/lib/web.rb +68 -0
  41. data/lib/web/assertinclude.rb +129 -0
  42. data/lib/web/config.rb +50 -0
  43. data/lib/web/connection.rb +1070 -0
  44. data/lib/web/convenience.rb +154 -0
  45. data/lib/web/formreader.rb +318 -0
  46. data/lib/web/htmlparser/html-parser.rb +429 -0
  47. data/lib/web/htmlparser/sgml-parser.rb +332 -0
  48. data/lib/web/htmltools/element.rb +296 -0
  49. data/lib/web/htmltools/stparser.rb +276 -0
  50. data/lib/web/htmltools/tags.rb +286 -0
  51. data/lib/web/htmltools/tree.rb +139 -0
  52. data/lib/web/htmltools/xmltree.rb +160 -0
  53. data/lib/web/htmltools/xpath.rb +71 -0
  54. data/lib/web/info.rb +63 -0
  55. data/lib/web/load.rb +210 -0
  56. data/lib/web/mime.rb +87 -0
  57. data/lib/web/phprb.rb +340 -0
  58. data/lib/web/resources/test/cookie.rb +33 -0
  59. data/lib/web/resources/test/counter.rb +20 -0
  60. data/lib/web/resources/test/multipart.rb +14 -0
  61. data/lib/web/resources/test/redirect.rb +8 -0
  62. data/lib/web/resources/test/stock.rb +33 -0
  63. data/lib/web/sapi/apache.rb +129 -0
  64. data/lib/web/sapi/fastcgi.rb +22 -0
  65. data/lib/web/sapi/install/apache.rb +180 -0
  66. data/lib/web/sapi/install/iis.rb +93 -0
  67. data/lib/web/sapi/install/macosx.rb +90 -0
  68. data/lib/web/sapi/webrick.rb +86 -0
  69. data/lib/web/session.rb +83 -0
  70. data/lib/web/shim/cgi.rb +129 -0
  71. data/lib/web/shim/rails.rb +175 -0
  72. data/lib/web/stringio.rb +78 -0
  73. data/lib/web/strscanparser.rb +24 -0
  74. data/lib/web/tagparser.rb +96 -0
  75. data/lib/web/testing.rb +666 -0
  76. data/lib/web/traceoutput.rb +75 -0
  77. data/lib/web/unit.rb +56 -0
  78. data/lib/web/upload.rb +59 -0
  79. data/lib/web/validate.rb +52 -0
  80. data/lib/web/wiki.rb +557 -0
  81. data/lib/web/wiki/linker.rb +72 -0
  82. data/lib/web/wiki/page.rb +201 -0
  83. data/lib/webunit.rb +27 -0
  84. data/lib/webunit/assert.rb +152 -0
  85. data/lib/webunit/converter.rb +154 -0
  86. data/lib/webunit/cookie.rb +118 -0
  87. data/lib/webunit/domwalker.rb +185 -0
  88. data/lib/webunit/exception.rb +14 -0
  89. data/lib/webunit/form.rb +116 -0
  90. data/lib/webunit/frame.rb +37 -0
  91. data/lib/webunit/htmlelem.rb +122 -0
  92. data/lib/webunit/image.rb +26 -0
  93. data/lib/webunit/jscript.rb +31 -0
  94. data/lib/webunit/link.rb +33 -0
  95. data/lib/webunit/params.rb +321 -0
  96. data/lib/webunit/parser.rb +229 -0
  97. data/lib/webunit/response.rb +464 -0
  98. data/lib/webunit/runtest.rb +41 -0
  99. data/lib/webunit/table.rb +148 -0
  100. data/lib/webunit/testcase.rb +45 -0
  101. data/lib/webunit/ui/cui/testrunner.rb +50 -0
  102. data/lib/webunit/utils.rb +68 -0
  103. data/lib/webunit/webunit.rb +28 -0
  104. data/test/dev/action.rb +83 -0
  105. data/test/dev/forms.rb +104 -0
  106. data/test/dev/forms2.rb +104 -0
  107. data/test/dev/parser.rb +17 -0
  108. data/test/dev/scripts/dump.rb +24 -0
  109. data/test/dev/scripts/makedist.rb +62 -0
  110. data/test/dev/scripts/uri.rb +41 -0
  111. data/test/dev/scripts/uri/common.rb +432 -0
  112. data/test/dev/scripts/uri/ftp.rb +149 -0
  113. data/test/dev/scripts/uri/generic.rb +1106 -0
  114. data/test/dev/scripts/uri/http.rb +76 -0
  115. data/test/dev/scripts/uri/https.rb +26 -0
  116. data/test/dev/scripts/uri/ldap.rb +238 -0
  117. data/test/dev/scripts/uri/mailto.rb +260 -0
  118. data/test/dev/scripts/urireg.rb +174 -0
  119. data/test/dev/simpledispatcher.rb +156 -0
  120. data/test/dev/test.action.rb +146 -0
  121. data/test/dev/test.formreader.rb +463 -0
  122. data/test/dev/test.simpledispatcher.rb +186 -0
  123. data/test/dev/webunit/conv/digit-0.rb +21 -0
  124. data/test/dev/webunit/conv/digit-1.rb +17 -0
  125. data/test/dev/webunit/conv/digit.rb +23 -0
  126. data/test/dev/webunit/conv/test_digit-0.rb +16 -0
  127. data/test/dev/webunit/conv/test_digit-1.rb +19 -0
  128. data/test/dev/webunit/conv/test_digit.rb +26 -0
  129. data/test/dev/webunit/conv/test_digit_view-0.rb +76 -0
  130. data/test/dev/webunit/conv/test_digit_view-1.rb +102 -0
  131. data/test/dev/webunit/conv/test_digit_view.rb +134 -0
  132. data/test/installation/htdocs/cgi_test.rb +296 -0
  133. data/test/installation/htdocs/test_install.rb +4 -0
  134. data/test/installation/runwebtest.rb +5 -0
  135. data/test/installation/test_cookie.rb +128 -0
  136. data/test/installation/test_form.rb +47 -0
  137. data/test/installation/test_multipart.rb +51 -0
  138. data/test/installation/test_request.rb +24 -0
  139. data/test/installation/test_response.rb +35 -0
  140. data/test/unit/htdocs/cookie.rb +32 -0
  141. data/test/unit/htdocs/multipart.rb +28 -0
  142. data/test/unit/htdocs/redirect.rb +12 -0
  143. data/test/unit/htdocs/simple.rb +13 -0
  144. data/test/unit/htdocs/stock.rb +33 -0
  145. data/test/unit/test_assert.rb +162 -0
  146. data/test/unit/test_cookie.rb +114 -0
  147. data/test/unit/test_domwalker.rb +77 -0
  148. data/test/unit/test_form.rb +42 -0
  149. data/test/unit/test_frame.rb +40 -0
  150. data/test/unit/test_htmlelem.rb +74 -0
  151. data/test/unit/test_image.rb +45 -0
  152. data/test/unit/test_jscript.rb +57 -0
  153. data/test/unit/test_link.rb +85 -0
  154. data/test/unit/test_multipart.rb +51 -0
  155. data/test/unit/test_params.rb +210 -0
  156. data/test/unit/test_parser.rb +53 -0
  157. data/test/unit/test_response.rb +150 -0
  158. data/test/unit/test_table.rb +70 -0
  159. data/test/unit/test_utils.rb +106 -0
  160. data/test/unit/test_webunit.rb +28 -0
  161. data/test/web/mod_ruby_stub.rb +39 -0
  162. data/test/web/test.assertinclude.rb +109 -0
  163. data/test/web/test.buffer.rb +182 -0
  164. data/test/web/test.code.loader.rb +78 -0
  165. data/test/web/test.config.rb +31 -0
  166. data/test/web/test.error.handling.rb +91 -0
  167. data/test/web/test.formreader-2.0.rb +352 -0
  168. data/test/web/test.load.rb +125 -0
  169. data/test/web/test.mime-type.rb +23 -0
  170. data/test/web/test.narf.cgi.rb +106 -0
  171. data/test/web/test.phprb.rb +239 -0
  172. data/test/web/test.request.rb +368 -0
  173. data/test/web/test.response.rb +637 -0
  174. data/test/web/test.ruby-web.rb +10 -0
  175. data/test/web/test.session.rb +50 -0
  176. data/test/web/test.shim.cgi.rb +96 -0
  177. data/test/web/test.tagparser.rb +65 -0
  178. data/test/web/test.template2.rb +297 -0
  179. data/test/web/test.testing2.rb +318 -0
  180. data/test/web/test.upload.rb +45 -0
  181. data/test/web/test.validate.rb +46 -0
  182. data/test/web/test.web.test.rb +495 -0
  183. data/test/wiki/test.history.rb +297 -0
  184. data/test/wiki/test.illustration_page.rb +287 -0
  185. data/test/wiki/test.linker.rb +197 -0
  186. data/test/wiki/test.tarpit.rb +56 -0
  187. data/test/wiki/test.wiki.rb +300 -0
  188. data/test/wikitestroot/admin.rb +7 -0
  189. data/test/wikitestroot/wiki.rb +6 -0
  190. metadata +234 -0
@@ -0,0 +1,318 @@
1
+ class TestWebTesting2 < Test::Unit::TestCase
2
+ include Web::Testing
3
+
4
+ def setup
5
+ @pwd = Dir.pwd
6
+ if File.exists?( temp_dir + "/test_dir/" )
7
+ rm_rf(temp_dir + "/test_dir")
8
+ end
9
+ Dir.mkdir(temp_dir + "/test_dir/")
10
+ Dir.chdir(temp_dir + "/test_dir/")
11
+ #@docroot = Web.get_docroot
12
+ #Web.set_docroot( temp_dir + "/test_dir/")
13
+ end
14
+
15
+ def teardown
16
+ Dir.chdir(@pwd)
17
+ rm_rf(temp_dir + "/test_dir")
18
+ #Web.set_docroot( @docroot )
19
+ end
20
+
21
+ def write_file file
22
+ File.open( temp_dir + "/test_dir/app.rb" , "w" ) { |f| f.write(file) }
23
+ end
24
+
25
+ def test_web_cgi_asserts
26
+ write_file <<-EOF
27
+ Web.print "hello tom"
28
+ Web.set_cookie( "username", "someonespecial" )
29
+ Web.add_header( "Special-Person", "Oh Yes" )
30
+ EOF
31
+
32
+ do_request "app.rb"
33
+
34
+ assert_content "hello tom"
35
+ assert_header "Special-Person", "Oh Yes"
36
+ assert_cookie "username", "someonespecial"
37
+ end
38
+
39
+ def test_path_info
40
+ write_file <<-EOF
41
+ Web::write Web::path_info
42
+ EOF
43
+ do_request "app.rb/path_info/"
44
+ assert_content( "/path_info/" )
45
+ end
46
+
47
+ def test_do_submit
48
+ form = "<html><body><form name='the_form'><input type='text' name='thing'></form></body></html>"
49
+
50
+ write_file <<-EOF
51
+ if Web["thing"] == "foo"
52
+ Web << "this is a foo page"
53
+ elsif Web["thing"] == "bar"
54
+ Web << "that is the bar page"
55
+ end
56
+ Web << "#{form}"
57
+ EOF
58
+
59
+ do_request "app.rb"
60
+ assert_content( form )
61
+
62
+ do_submit( "the_form", "thing" => "foo" )
63
+
64
+ assert_content( "this is a foo page" + form )
65
+
66
+ do_submit( "the_form", "thing" => "bar" )
67
+
68
+ assert_content( "that is the bar page" + form )
69
+ end
70
+
71
+ def test_do_submit_submits_hidden
72
+ form = "<html><body><form name='the_form'><input type='hidden' name='thing' value='val'></form></body></html>"
73
+
74
+ write_file <<-EOF
75
+ Web << "displayed '"
76
+ Web << Web['thing']
77
+ Web << "'#{form}"
78
+ EOF
79
+
80
+ do_request "app.rb"
81
+ assert_content( "displayed ''" + form)
82
+
83
+ do_submit( "the_form" )
84
+
85
+ assert_content( "displayed 'val'" + form)
86
+ end
87
+
88
+ def test_do_redirect
89
+ write_file <<-EOF
90
+ if Web["action"] == "finish"
91
+ Web.puts "We redirected!"
92
+ else
93
+ Web.set_redirect "app.rb?action=finish"
94
+ Web.puts "here"
95
+ end
96
+ EOF
97
+
98
+ do_request "app.rb"
99
+
100
+ assert_content( "We redirected!\n" )
101
+ end
102
+
103
+ def test_do_redirect_with_session
104
+ write_file <<-EOF
105
+ if Web["action"] == "finish"
106
+ Web.puts Web.session["name"]
107
+ else
108
+ Web.set_redirect "app.rb?action=finish"
109
+ Web.session["name"] = "Tom"
110
+ end
111
+ EOF
112
+
113
+ do_request( "app.rb" )
114
+
115
+ assert_content( "Tom\n" )
116
+ end
117
+
118
+
119
+ def test_do_multiple_redirect
120
+ write_file <<-EOF
121
+ if Web["action"] == "second"
122
+ Web.set_redirect "app.rb?action=third"
123
+ elsif Web["action"] == "third"
124
+ Web.puts "We redirected!"
125
+ else
126
+ Web.set_redirect "app.rb?action=second"
127
+ end
128
+ EOF
129
+
130
+ do_request "app.rb"
131
+
132
+ assert_content( "We redirected!\n" )
133
+ end
134
+
135
+ def test_do_redirect_on_submit
136
+ write_file <<-EOF
137
+ if Web["submitted"] == "true"
138
+ Web.set_redirect "app.rb?isredirect=true"
139
+ elsif Web["isredirect"] == "true"
140
+ Web.puts "We redirected!"
141
+ else
142
+ Web.puts "<html><body><form name='form' action='app.rb'>"
143
+ Web.puts "<input type='hidden' name='submitted' value='true'>"
144
+ Web.puts "</form></body></html>"
145
+ end
146
+ EOF
147
+
148
+ do_request "app.rb"
149
+ do_submit "form"
150
+
151
+ assert_content( "We redirected!\n" )
152
+ end
153
+
154
+ def test_form_exists
155
+ write_file <<-EOF
156
+ Web.puts "<html><body><form name='form' action='app.rb'>"
157
+ Web.puts "<input type='hidden' name='submitted' value='true'>"
158
+ Web.puts "</form></body></html>"
159
+ EOF
160
+
161
+ do_request "app.rb"
162
+ assert_raises( FormNotFoundException ) do
163
+ do_submit "not_this_form"
164
+ end
165
+ end
166
+
167
+ def test_all_fields_on_form
168
+ write_file <<-EOF
169
+ Web.puts "<html><body><form name='form' action='app.rb'>"
170
+ Web.puts "</form></body></html>"
171
+ EOF
172
+
173
+ do_request "app.rb"
174
+ assert_raises( FieldNotFoundException ) do
175
+ do_submit "form", "not_on_form" => "value"
176
+ end
177
+ end
178
+
179
+ # check that sessions & cookies are propagated
180
+
181
+ def test_two_hidden_forms_are_array
182
+ form = "<html><body><form name=\"the_form\">"+
183
+ "<input type=\"hidden\" name=\"thing[]\" value=\"val1\">"+
184
+ "<input type=\"hidden\" name=\"thing[]\" value=\"val2\">"+
185
+ "</form></body></html>"
186
+
187
+ write_file <<-EOF
188
+ Web << "submitted '"
189
+ Web << Web['thing[]'].sort.join(',')
190
+ Web << "'"
191
+ Web << '#{form}'
192
+ EOF
193
+
194
+ do_request "app.rb"
195
+ assert_content( "submitted ''" + form)
196
+
197
+ do_submit( "the_form" )
198
+
199
+ assert_content( "submitted 'val1,val2'" + form)
200
+ end
201
+
202
+
203
+ def test_cgi_optional
204
+ write_file <<-EOF
205
+ if Web["name"] == "patrick"
206
+ Web.write( "temp_patrick" )
207
+ elsif Web["name"] == "tom"
208
+ Web << "My name is "
209
+ Web << Web['name']
210
+ Web::puts " and also TOM"
211
+ end
212
+ EOF
213
+
214
+ do_request( "app.rb" , "name" => "patrick" )
215
+ assert_content "temp_patrick"
216
+
217
+ do_request( "app.rb", "name" => "tom" )
218
+ assert_content "My name is tom and also TOM\n"
219
+ end
220
+
221
+ def test_assert_form_includes_input_box
222
+ write_file <<-EOF
223
+ Web.puts "<html><body><form name='form'>"+
224
+ "<input type='text' name='test1' value='1'>"+
225
+ "<input type='text' name='test2' value='2'>"+
226
+ "</form></body></html>"
227
+ EOF
228
+ do_request "app.rb"
229
+
230
+ assert_form_includes("form" , { "test1" => "1", "test2" => "2" } )
231
+ assert_form_includes("form" , { "test1" => "1" } )
232
+
233
+ assert_raises( Test::Unit::AssertionFailedError ) {
234
+ assert_form_includes("form" , { "test1" => "1", "test2" => "2", "test3" => "3" } )
235
+ }
236
+ assert_raises( Test::Unit::AssertionFailedError ) {
237
+ assert_form_includes("form" , { "test1" => "1", "test2" => "1" } )
238
+ }
239
+ end
240
+
241
+ def test_assert_form_includes_checkbox
242
+ write_file <<-EOF
243
+ Web.puts "<html><body><form name='form'>
244
+ <input type='checkbox' name='test1' checked>
245
+ <input type='checkbox' name='test2'>
246
+ </form></body></html>"
247
+ EOF
248
+
249
+ do_request "app.rb"
250
+
251
+ assert_form_includes("form" , { "test1" => true, "test2" => false } )
252
+ end
253
+
254
+ def todo_test_assert_form_includes_checkbox_array
255
+ write_file <<-EOF
256
+ Web.puts "<html><body><form name='form'>"+
257
+ "<input type='checkbox' name='test22[]' value='2' checked>"+
258
+ "<input type='checkbox' name='test22[]' value='3' checked>"+
259
+ "</form></body></html>"
260
+ EOF
261
+ do_request "app.rb"
262
+
263
+ assert_form_includes("form" , { "test22[]" => ["2","3"] } )
264
+ end
265
+
266
+ def todo_test_submit_checkbox
267
+ write_file <<-EOF
268
+ Web.puts "<html><body><form name='form'>"+
269
+ "<input type='hidden' name='action' value='submit'>"+
270
+ "<input type='checkbox' name='test1' value='1'>"+
271
+ "<input type='checkbox' name='test2' value='2'>"+
272
+ "<input type='checkbox' name='test3' value='3' checked>"+
273
+ "<input type='checkbox' name='test4[]' value='4'>"+
274
+ "<input type='checkbox' name='test4[]' value='5'>"+
275
+ "<input type='checkbox' name='test4[]' value='6' checked>"+
276
+ "</form></body></html>"
277
+ EOF
278
+
279
+ do_request "app.rb"
280
+
281
+ actionPerformed = false
282
+ write_file <<-EOF
283
+ assert_equal "", Web["test1"]
284
+ assert_equal "2", Web["test2"]
285
+ assert_equal "3", Web["test3"]
286
+ assert_equal ["5","6"], Web["test4[]"].sort
287
+ actionPerformed=true
288
+ EOF
289
+
290
+ do_submit( "form", "test2" => "2", "test4[]" => "5" )
291
+
292
+ assert actionPerformed
293
+ end
294
+
295
+ def test_assert_form_includes_input_textarea1
296
+ write_file <<-EOF
297
+ Web.puts "<html><body><form name='form'>"+
298
+ "<textarea name='test1'>contents of text area</textarea>"+
299
+ "</form></body></html>"
300
+ EOF
301
+
302
+ do_request "app.rb"
303
+
304
+ assert_form_includes "form" , "test1" => "contents of text area"
305
+ end
306
+
307
+ def test_assert_form_includes_input_textarea2
308
+ write_file <<-EOF
309
+ Web::puts "<html><body><form name='form'>"+
310
+ "<textarea name='test1'>contents of text area</textarea>"+
311
+ "</form></body></html>"
312
+ EOF
313
+
314
+ do_request "app.rb"
315
+
316
+ assert_form_includes "form", "test1" => "contents of text area"
317
+ end
318
+ end
@@ -0,0 +1,45 @@
1
+ class TestUpload < Test::Unit::TestCase
2
+ attr_reader :original_filename, :content_type, :test_file, :move_to
3
+ def setup
4
+ @test_file = "test/web/testfiles/fallow_keep.png.450x450.2005-07-24.jpg"
5
+ @original_filename = "c:\\workspace\\fallow_keep.png.450x450.2005-07-24.jpg"
6
+ @content_type = "image/png"
7
+ @move_to = "test/web/testfiles/moved_fallow_keep.jpg"
8
+ end
9
+
10
+ def test_initialize
11
+ File.open( test_file ) { |file|
12
+ upload = Web::Upload.new( file, content_type, original_filename )
13
+
14
+ assert_equal( content_type, upload.content_type )
15
+ assert_equal( original_filename, upload.original_filename )
16
+ }
17
+ end
18
+
19
+ def test_initialize_with_filename
20
+ upload = Web::Upload.new( test_file, content_type, original_filename )
21
+
22
+ assert_equal( File.open( test_file, "r" ) { |f| f.read }, upload.read )
23
+ end
24
+
25
+ def test_read_open_and_write_file
26
+ expected_contents = File.open( test_file, "r" ) { |f| f.read }
27
+
28
+ upload = Web::Upload.new( test_file, content_type, original_filename )
29
+
30
+ assert_equal( expected_contents, upload.read )
31
+
32
+ upload.open{ |io|
33
+ assert_equal( expected_contents, io.read )
34
+ }
35
+
36
+ begin
37
+ upload.save( move_to )
38
+ File.open( move_to, "r" ) { |io|
39
+ assert_equal( expected_contents, io.read )
40
+ }
41
+ ensure
42
+ File.delete( move_to )
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,46 @@
1
+ require 'web/validate'
2
+
3
+ class TestValidation < Test::Unit::TestCase
4
+ def setup
5
+ Web::setup
6
+ end
7
+
8
+ def teardown
9
+ Web::teardown
10
+ end
11
+
12
+ # need to setup validation rules
13
+ def test_validate
14
+ Web::open(:out=>'') do
15
+ Web::rule( :message => 'Invalid Username',
16
+ :field => :username,
17
+ :regexp => /\w{1,8} / )
18
+
19
+ errors = Web::validate
20
+ assert_equal(1, errors.length )
21
+ assert( errors.first.kind_of?(Web::Rule) )
22
+ assert_equal( 'Invalid Username', errors.first.message )
23
+ assert_equal( 'username', errors.first.field )
24
+ end
25
+ end
26
+
27
+
28
+ def test_fatal
29
+ assert_raises( Web::FatalValidationError ) do
30
+ Web::open(:out=>'', :request => {:username => '123'} ) do
31
+ Web::rule( :message => 'Invalid Username',
32
+ :field => :username,
33
+ :regexp => /\w{1,8} /,
34
+ :malformed =>:fatal )
35
+
36
+ errors = Web::validate
37
+ end
38
+ end
39
+ end
40
+
41
+ def todo_test_javascript
42
+ #Web::get_javascript_validation
43
+ end
44
+
45
+
46
+ end
@@ -0,0 +1,495 @@
1
+ require 'web/testing'
2
+
3
+ class TestWebTest < Test::Unit::TestCase
4
+ include Web::Testing
5
+
6
+ def setup
7
+ if File.exists?( temp_dir + "/test_dir/" )
8
+ rm_rf(temp_dir + "/test_dir")
9
+ end
10
+ Dir.mkdir(temp_dir + "/test_dir/")
11
+ Dir.mkdir(temp_dir + "/test_dir/templates")
12
+ Web::docroot = temp_dir + "/test_dir/"
13
+ Web::setup
14
+ end
15
+
16
+ def teardown
17
+ rm_rf(temp_dir + "/test_dir")
18
+ Web::teardown
19
+ end
20
+
21
+ def write_file file
22
+ File.open( temp_dir + "/test_dir/app.rb" , "w" ) { |f| f.write(file) }
23
+ end
24
+
25
+ def test_error_handling_while_testing
26
+ # make sure Web::Testing#do_request throws exceptions
27
+ write_file <<-EOF
28
+ raise TestWebTest::FlaggedException
29
+ EOF
30
+
31
+ assert_raises( FlaggedException ) do
32
+ do_request "app.rb"
33
+ end
34
+ end
35
+
36
+ def test_path_info
37
+ write_file <<-EOF
38
+ Web::write Web::path_info
39
+ EOF
40
+
41
+ do_request "app.rb/test_path_info"
42
+ Web::assert_content("/test_path_info")
43
+ assert_equal( "/test_path_info", Web::env['PATH_INFO'] )
44
+
45
+ do_request "/app.rb/test_path_info"
46
+ Web::assert_content("/test_path_info")
47
+ assert_equal( "/test_path_info", Web::env['PATH_INFO'] )
48
+ end
49
+
50
+ # this is a tell tale exception
51
+ class FlaggedException < Exception
52
+ end
53
+
54
+ def test_passing_env
55
+ write_file <<-EOF
56
+ Web::write Web::env['test'].to_s
57
+ EOF
58
+
59
+ do_request "app.rb", :env => {"test" => "patrick"}
60
+ Web::assert_content( "patrick" )
61
+
62
+ ENV["test"] = "jenny"
63
+ do_request "app.rb", :env => { "ignore" => "this" }
64
+ Web::assert_content( "jenny" )
65
+ ENV.delete "test"
66
+
67
+ # test case sensitivity
68
+ write_file <<-EOF
69
+ Web::write Web::env['TeSt'].to_s
70
+ EOF
71
+
72
+ do_request "app.rb", :env => {"tEsT" => "patrick"}
73
+ Web::assert_content( "patrick" )
74
+
75
+ end
76
+
77
+
78
+
79
+ def test_must_have_docroot_for_absolute_paths
80
+ write_file <<-EOF
81
+ Web.puts "will never reach here"
82
+ EOF
83
+ Web::docroot = nil
84
+ assert_raises( Web::Testing::MustSetDocrootForAbsolutePathException ) do
85
+ do_request "/app.rb"
86
+ end
87
+ end
88
+
89
+ def test_web_cgi_asserts
90
+ write_file <<-EOF
91
+ Web.print "hello tom"
92
+ Web.set_cookie( "username", "someonespecial" )
93
+ Web.add_header( "Special-Person", "Oh Yes" )
94
+ EOF
95
+
96
+ do_request "/app.rb"
97
+
98
+ assert_content "hello tom"
99
+ assert_header "Special-Person", "Oh Yes"
100
+ assert_cookie "username", "someonespecial"
101
+ end
102
+
103
+ def test_do_submit
104
+ form = "<html><body><form name='the_form'><input type='text' name='thing'></form></body></html>"
105
+
106
+ write_file <<-EOF
107
+ if Web["thing"] == "foo"
108
+ Web << "this is a foo page"
109
+ elsif Web["thing"] == "bar"
110
+ Web << "that is the bar page"
111
+ end
112
+ Web << "#{form}"
113
+ EOF
114
+
115
+ do_request "/app.rb"
116
+ assert_content( form )
117
+
118
+ do_submit( "the_form", "thing" => "foo" )
119
+
120
+ assert_content( "this is a foo page" + form )
121
+
122
+ do_submit( "the_form", "thing" => "bar" )
123
+
124
+ assert_content( "that is the bar page" + form )
125
+ end
126
+
127
+ def test_do_submit_submits_hidden
128
+ form = "<html><body><form name='the_form'><input type='hidden' name='thing' value='val'></form></body></html>"
129
+
130
+ write_file <<-EOF
131
+ Web << "displayed '"
132
+ Web << Web['thing']
133
+ Web << "'#{form}"
134
+ EOF
135
+
136
+ do_request "/app.rb"
137
+ assert_content( "displayed ''" + form)
138
+
139
+ do_submit( "the_form" )
140
+
141
+ assert_content( "displayed 'val'" + form)
142
+ end
143
+
144
+ def test_do_redirect
145
+ write_file <<-EOF
146
+ if Web["action"] == "finish"
147
+ Web.puts "We redirected!"
148
+ else
149
+ Web.set_redirect "/app.rb?action=finish"
150
+ Web.puts "here"
151
+ end
152
+ EOF
153
+
154
+ do_request "/app.rb"
155
+
156
+ assert_content( "We redirected!\n" )
157
+ end
158
+
159
+ def test_do_redirect_with_session
160
+ write_file <<-EOF
161
+ if Web["action"] == "finish"
162
+ Web.puts Web.session["name"]
163
+ else
164
+ Web.set_redirect "/app.rb?action=finish"
165
+ Web.session["name"] = "Tom"
166
+ end
167
+ EOF
168
+
169
+ do_request( "/app.rb" )
170
+
171
+ assert_content( "Tom\n" )
172
+ end
173
+
174
+
175
+ def test_do_multiple_redirect
176
+ write_file <<-EOF
177
+ if Web["action"] == "second"
178
+ Web.set_redirect "/app.rb?action=third"
179
+ elsif Web["action"] == "third"
180
+ Web.puts "We redirected!"
181
+ else
182
+ Web.set_redirect "/app.rb?action=second"
183
+ end
184
+ EOF
185
+
186
+ do_request "/app.rb"
187
+
188
+ assert_content( "We redirected!\n" )
189
+ end
190
+
191
+ def test_do_redirect_on_submit
192
+ write_file <<-EOF
193
+ if Web["submitted"] == "true"
194
+ Web.set_redirect "/app.rb?isredirect=true"
195
+ elsif Web["isredirect"] == "true"
196
+ Web.puts "We redirected!"
197
+ else
198
+ Web.puts "<html><body><form name='form' action='/app.rb'>"
199
+ Web.puts "<input type='hidden' name='submitted' value='true'>"
200
+ Web.puts "</form></body></html>"
201
+ end
202
+ EOF
203
+
204
+ do_request "/app.rb"
205
+ do_submit "form"
206
+
207
+ assert_content( "We redirected!\n" )
208
+ end
209
+
210
+ def test_form_exists
211
+ write_file <<-EOF
212
+ Web.puts "<html><body><form name='form' action='/app.rb'>"
213
+ Web.puts "<input type='hidden' name='submitted' value='true'>"
214
+ Web.puts "</form></body></html>"
215
+ EOF
216
+
217
+ do_request "/app.rb"
218
+ assert_raises( Web::Testing::FormNotFoundException ) do
219
+ do_submit "not_this_form"
220
+ end
221
+ end
222
+
223
+ def test_all_fields_on_form
224
+ write_file <<-EOF
225
+ Web.puts "<html><body><form name='form' action='/app.rb'>"
226
+ Web.puts "</form></body></html>"
227
+ EOF
228
+
229
+ do_request "/app.rb"
230
+ assert_raises( Web::Testing::FieldNotFoundException ) do
231
+ do_submit( "form", "not_on_form" => "value" )
232
+ end
233
+ end
234
+
235
+ def test_select
236
+ write_file <<-EOF
237
+ Web.puts <<-FORM
238
+ <html>
239
+ <body>
240
+ <form name='form' action='/app.rb'>
241
+ <select name='aselect'>
242
+ <option selected>avalue</option>
243
+ <option value='another'>Another Value</option>
244
+ </select>
245
+ </form>
246
+ </body>
247
+ </html>
248
+ FORM
249
+ EOF
250
+
251
+ do_request "/app.rb"
252
+
253
+ assert_options( 'form', 'aselect' => ['another', 'avalue'] )
254
+ assert_form_includes( "form", 'aselect' => 'avalue' )
255
+ end
256
+
257
+ def test_radio
258
+ write_file <<-EOF
259
+ Web.puts <<-FORM
260
+ <html>
261
+ <body>
262
+ <form name='form' action='/app.rb'>
263
+ <input type="radio" name='aradio' value="avalue" checked>
264
+ <input type="radio" name='aradio' value="another">
265
+ </form>
266
+ </body>
267
+ </html>
268
+ FORM
269
+ EOF
270
+
271
+ do_request "/app.rb"
272
+
273
+ assert_options( 'form', 'aradio' => ['another', 'avalue'] )
274
+ assert_form_includes( 'form', 'aradio' => 'avalue' )
275
+ end
276
+
277
+ def test_multi_select
278
+ write_file <<-EOF
279
+ Web.puts <<-FORM
280
+ <html>
281
+ <body>
282
+ <form name='form' action='/app.rb'>
283
+ <select name='aselect[]' multiple>
284
+ <option>first</option>
285
+ <option selected>avalue</option>
286
+ <option value='another' selected>Another Value</option>
287
+ </select>
288
+ </form>
289
+ </body>
290
+ </html>
291
+ FORM
292
+ EOF
293
+
294
+ do_request "/app.rb"
295
+
296
+ assert_options( 'form', 'aselect[]' => ['first',
297
+ 'avalue',
298
+ 'another'] )
299
+ assert_form_includes( "form", 'aselect[]' => ['another','avalue'] )
300
+ end
301
+
302
+ def test_checkbox_group
303
+ write_file <<-EOF
304
+ Web.puts <<-FORM
305
+ <html>
306
+ <body>
307
+ <form name='form' action='/app.rb'>
308
+ <input type="checkbox" name='check[]' value="first">
309
+ <input type="checkbox" name='check[]' value="avalue" checked>
310
+ <input type="checkbox" name='check[]' value="another" checked>
311
+ </form>
312
+ </body>
313
+ </html>
314
+ FORM
315
+ EOF
316
+
317
+ do_request "/app.rb"
318
+ assert_options( 'form', 'check[]' => [ 'first', 'avalue', 'another' ] )
319
+ assert_form_includes( "form", 'check[]' => ['another', 'avalue'] )
320
+ end
321
+
322
+ # check that sessions & cookies are propagated
323
+
324
+ def test_two_hidden_forms_are_array
325
+ form = "<html><body><form name=\"the_form\">"+
326
+ "<input type=\"hidden\" name=\"thing[]\" value=\"val1\">"+
327
+ "<input type=\"hidden\" name=\"thing[]\" value=\"val2\">"+
328
+ "</form></body></html>"
329
+
330
+ write_file <<-EOF
331
+ Web << "submitted '"
332
+ Web << Web['thing[]'].sort.join(',')
333
+ Web << "'"
334
+ Web << '#{form}'
335
+ EOF
336
+
337
+ do_request "/app.rb"
338
+ assert_content( "submitted ''" + form)
339
+
340
+ do_submit( "the_form" )
341
+
342
+ assert_content( "submitted 'val1,val2'" + form)
343
+ end
344
+
345
+
346
+ def test_cgi_optional
347
+ write_file <<-EOF
348
+ if Web["name"] == "patrick"
349
+ Web.write( "temp_patrick" )
350
+ elsif Web["name"] == "tom"
351
+ Web << "My name is "
352
+ Web << Web['name']
353
+ Web::puts " and also TOM"
354
+ end
355
+ EOF
356
+
357
+ do_request( "/app.rb" , "name" => "patrick" )
358
+
359
+ assert_content "temp_patrick"
360
+
361
+ do_request( "/app.rb", "name" => "tom" )
362
+ assert_content "My name is tom and also TOM\n"
363
+ end
364
+
365
+ def test_assert_form_includes_input_box
366
+ write_file <<-EOF
367
+ Web.puts "<html><body><form name='form'>"+
368
+ "<input type='text' name='test1' value='1'>"+
369
+ "<input type='text' name='test2' value='2'>"+
370
+ "</form></body></html>"
371
+ EOF
372
+ do_request "/app.rb"
373
+
374
+ assert_form_includes("form" , { "test1" => "1", "test2" => "2" } )
375
+ assert_form_includes("form" , { "test1" => "1" } )
376
+
377
+ assert_raises( Test::Unit::AssertionFailedError ) {
378
+ assert_form_includes("form" , { "test1" => "1", "test2" => "2", "test3" => "3" } )
379
+ }
380
+ assert_raises( Test::Unit::AssertionFailedError ) {
381
+ assert_form_includes("form" , { "test1" => "1", "test2" => "1" } )
382
+ }
383
+ end
384
+
385
+ def test_assert_form_includes_checkbox
386
+ write_file <<-EOF
387
+ Web.puts "<html><body><form name='form'>"+
388
+ "<input type='checkbox' name='test1' checked>"+
389
+ "<input type='checkbox' name='test2'>"+
390
+ "</form></body></html>"
391
+ EOF
392
+
393
+ do_request "/app.rb"
394
+
395
+ assert_form_includes("form" , { "test1" => true, "test2" => false } )
396
+ end
397
+
398
+ # hahahhahaha tom. you crack me up. It always takes a while
399
+ # for me to catch up to you :-)
400
+ # I'm not sure how I would want to do this either. ~ patrick
401
+ #
402
+ # that's why I wrote formreader.rb. I'm merging it into the narf
403
+ # api now. all the select tests change, hopefully for the
404
+ # better. ~ patrick
405
+ def test_assert_form_includes_checkbox_array
406
+ write_file <<-EOF
407
+ Web.puts "<html><body><form name='form'>"+
408
+ "<input type='checkbox' name='test22[]' value='2' checked>"+
409
+ "<input type='checkbox' name='test22[]' value='3' checked>"+
410
+ "</form></body></html>"
411
+ EOF
412
+ do_request "/app.rb"
413
+ assert_options( 'form', 'test22[]' => ['2', '3' ] )
414
+ assert_form_includes("form" , "test22[]" => ["2", "3"] )
415
+ end
416
+
417
+ def test_submit_checkbox
418
+ write_file <<-EOF
419
+ Web.puts "<html><body><form name='form'>"+
420
+ "<input type='hidden' name='action' value='submit'>"+
421
+ "<input type='checkbox' name='test1' value='1'>"+
422
+ "<input type='checkbox' name='test2' value='2'>"+
423
+ "<input type='checkbox' name='test3' value='3' checked>"+
424
+ "<input type='checkbox' name='test4[]' value='4'>"+
425
+ "<input type='checkbox' name='test4[]' value='5'>"+
426
+ "<input type='checkbox' name='test4[]' value='6' checked>"+
427
+ "</form></body></html>"
428
+ EOF
429
+
430
+ do_request "/app.rb"
431
+ assert_options("form",
432
+ 'test1' => ["1"],
433
+ 'test2' => ["2"],
434
+ 'test3' => ["3"],
435
+ 'test4[]' => ['4','5','6'] )
436
+
437
+
438
+ assert_form_includes("form",
439
+ 'test1' => false,
440
+ 'test2' => false,
441
+ 'test3' => "3",
442
+ 'test4[]' => ['6'] )
443
+
444
+ actionPerformed = false
445
+ write_file <<-EOF
446
+ if ( ("" == Web["test1"]) &&
447
+ ("2" == Web["test2"]) &&
448
+ ("3" == Web["test3"]) &&
449
+ ( ["5","6"] == Web["test4[]"].sort ) )
450
+ actionPerformed=true
451
+ else
452
+ raise Web["test2"]
453
+ end
454
+ EOF
455
+
456
+ # this is ugly. that is why patrick is working on it
457
+ #do_submit( "form", "test2" => true, "test4[].5" => true )
458
+ #do_submit( "form", "test2" => true, "test4[]" => "5" )
459
+
460
+ #assert actionPerformed
461
+ end
462
+
463
+ def test_assert_form_includes_input_textarea1
464
+ write_file <<-EOF
465
+ Web.puts "<html><body><form name='form'>"+
466
+ "<textarea name='test1'>contents of text area</textarea>"+
467
+ "</form></body></html>"
468
+ EOF
469
+
470
+ do_request "/app.rb"
471
+
472
+ assert_form_includes "form" , "test1" => "contents of text area"
473
+ end
474
+
475
+ def test_assert_form_includes_input_textarea2
476
+ write_file <<-EOF
477
+ Web.puts "<html><body><form name='form'>"+
478
+ "<textarea name='test1'>contents of text area</textarea>"+
479
+ "</form></body></html>"
480
+ EOF
481
+
482
+ do_request "/app.rb"
483
+
484
+ assert_form_includes "form", "test1" => "contents of text area"
485
+ end
486
+
487
+ def test_global_puts_takes_array
488
+ Web::open(:out=>'') do
489
+ Web::puts "Hello", "There"
490
+ end
491
+
492
+ assert_content( "Hello\nThere\n" )
493
+ end
494
+
495
+ end