mechanize-ntlm 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. data/CHANGELOG.rdoc +480 -0
  2. data/EXAMPLES.rdoc +171 -0
  3. data/FAQ.rdoc +11 -0
  4. data/GUIDE.rdoc +122 -0
  5. data/LICENSE.rdoc +340 -0
  6. data/Manifest.txt +169 -0
  7. data/README.rdoc +60 -0
  8. data/Rakefile +44 -0
  9. data/examples/flickr_upload.rb +23 -0
  10. data/examples/mech-dump.rb +7 -0
  11. data/examples/proxy_req.rb +9 -0
  12. data/examples/rubyforge.rb +21 -0
  13. data/examples/spider.rb +11 -0
  14. data/lib/mechanize-ntlm.rb +7 -0
  15. data/lib/www/mechanize.rb +582 -0
  16. data/lib/www/mechanize/chain.rb +34 -0
  17. data/lib/www/mechanize/chain/auth_headers.rb +82 -0
  18. data/lib/www/mechanize/chain/body_decoding_handler.rb +43 -0
  19. data/lib/www/mechanize/chain/connection_resolver.rb +78 -0
  20. data/lib/www/mechanize/chain/custom_headers.rb +23 -0
  21. data/lib/www/mechanize/chain/handler.rb +9 -0
  22. data/lib/www/mechanize/chain/header_resolver.rb +48 -0
  23. data/lib/www/mechanize/chain/parameter_resolver.rb +23 -0
  24. data/lib/www/mechanize/chain/post_connect_hook.rb +0 -0
  25. data/lib/www/mechanize/chain/pre_connect_hook.rb +22 -0
  26. data/lib/www/mechanize/chain/request_resolver.rb +32 -0
  27. data/lib/www/mechanize/chain/response_body_parser.rb +40 -0
  28. data/lib/www/mechanize/chain/response_header_handler.rb +51 -0
  29. data/lib/www/mechanize/chain/response_reader.rb +41 -0
  30. data/lib/www/mechanize/chain/ssl_resolver.rb +36 -0
  31. data/lib/www/mechanize/chain/uri_resolver.rb +73 -0
  32. data/lib/www/mechanize/content_type_error.rb +16 -0
  33. data/lib/www/mechanize/cookie.rb +72 -0
  34. data/lib/www/mechanize/cookie_jar.rb +191 -0
  35. data/lib/www/mechanize/file.rb +73 -0
  36. data/lib/www/mechanize/file_response.rb +62 -0
  37. data/lib/www/mechanize/file_saver.rb +39 -0
  38. data/lib/www/mechanize/form.rb +359 -0
  39. data/lib/www/mechanize/form/button.rb +8 -0
  40. data/lib/www/mechanize/form/check_box.rb +13 -0
  41. data/lib/www/mechanize/form/field.rb +28 -0
  42. data/lib/www/mechanize/form/file_upload.rb +24 -0
  43. data/lib/www/mechanize/form/image_button.rb +23 -0
  44. data/lib/www/mechanize/form/multi_select_list.rb +69 -0
  45. data/lib/www/mechanize/form/option.rb +51 -0
  46. data/lib/www/mechanize/form/radio_button.rb +38 -0
  47. data/lib/www/mechanize/form/select_list.rb +45 -0
  48. data/lib/www/mechanize/headers.rb +12 -0
  49. data/lib/www/mechanize/history.rb +67 -0
  50. data/lib/www/mechanize/inspect.rb +90 -0
  51. data/lib/www/mechanize/monkey_patch.rb +37 -0
  52. data/lib/www/mechanize/page.rb +145 -0
  53. data/lib/www/mechanize/page/base.rb +10 -0
  54. data/lib/www/mechanize/page/frame.rb +22 -0
  55. data/lib/www/mechanize/page/link.rb +50 -0
  56. data/lib/www/mechanize/page/meta.rb +10 -0
  57. data/lib/www/mechanize/pluggable_parsers.rb +103 -0
  58. data/lib/www/mechanize/redirect_limit_reached_error.rb +18 -0
  59. data/lib/www/mechanize/redirect_not_get_or_head_error.rb +20 -0
  60. data/lib/www/mechanize/response_code_error.rb +25 -0
  61. data/lib/www/mechanize/unsupported_scheme_error.rb +10 -0
  62. data/lib/www/mechanize/util.rb +76 -0
  63. data/lib/www/ntlm-http/lib/net/ntlm_http.rb +854 -0
  64. data/mechanize.gemspec +24 -0
  65. data/test/chain/test_argument_validator.rb +14 -0
  66. data/test/chain/test_custom_headers.rb +18 -0
  67. data/test/chain/test_parameter_resolver.rb +35 -0
  68. data/test/chain/test_request_resolver.rb +29 -0
  69. data/test/chain/test_response_reader.rb +24 -0
  70. data/test/data/htpasswd +1 -0
  71. data/test/data/server.crt +16 -0
  72. data/test/data/server.csr +12 -0
  73. data/test/data/server.key +15 -0
  74. data/test/data/server.pem +15 -0
  75. data/test/helper.rb +127 -0
  76. data/test/htdocs/alt_text.html +10 -0
  77. data/test/htdocs/bad_form_test.html +9 -0
  78. data/test/htdocs/button.jpg +0 -0
  79. data/test/htdocs/empty_form.html +6 -0
  80. data/test/htdocs/file_upload.html +26 -0
  81. data/test/htdocs/find_link.html +41 -0
  82. data/test/htdocs/form_multi_select.html +16 -0
  83. data/test/htdocs/form_multival.html +37 -0
  84. data/test/htdocs/form_no_action.html +18 -0
  85. data/test/htdocs/form_no_input_name.html +16 -0
  86. data/test/htdocs/form_select.html +16 -0
  87. data/test/htdocs/form_select_all.html +16 -0
  88. data/test/htdocs/form_select_none.html +17 -0
  89. data/test/htdocs/form_select_noopts.html +10 -0
  90. data/test/htdocs/form_set_fields.html +14 -0
  91. data/test/htdocs/form_test.html +188 -0
  92. data/test/htdocs/frame_test.html +30 -0
  93. data/test/htdocs/google.html +13 -0
  94. data/test/htdocs/iframe_test.html +16 -0
  95. data/test/htdocs/index.html +6 -0
  96. data/test/htdocs/link with space.html +5 -0
  97. data/test/htdocs/meta_cookie.html +11 -0
  98. data/test/htdocs/no_title_test.html +6 -0
  99. data/test/htdocs/relative/tc_relative_links.html +21 -0
  100. data/test/htdocs/tc_bad_links.html +5 -0
  101. data/test/htdocs/tc_base_link.html +8 -0
  102. data/test/htdocs/tc_blank_form.html +11 -0
  103. data/test/htdocs/tc_checkboxes.html +19 -0
  104. data/test/htdocs/tc_encoded_links.html +5 -0
  105. data/test/htdocs/tc_follow_meta.html +8 -0
  106. data/test/htdocs/tc_form_action.html +48 -0
  107. data/test/htdocs/tc_links.html +18 -0
  108. data/test/htdocs/tc_no_attributes.html +16 -0
  109. data/test/htdocs/tc_pretty_print.html +17 -0
  110. data/test/htdocs/tc_radiobuttons.html +17 -0
  111. data/test/htdocs/tc_referer.html +10 -0
  112. data/test/htdocs/tc_relative_links.html +19 -0
  113. data/test/htdocs/tc_textarea.html +23 -0
  114. data/test/htdocs/unusual______.html +5 -0
  115. data/test/servlets.rb +339 -0
  116. data/test/ssl_server.rb +48 -0
  117. data/test/test_authenticate.rb +71 -0
  118. data/test/test_bad_links.rb +25 -0
  119. data/test/test_blank_form.rb +16 -0
  120. data/test/test_checkboxes.rb +61 -0
  121. data/test/test_content_type.rb +13 -0
  122. data/test/test_cookie_class.rb +338 -0
  123. data/test/test_cookie_jar.rb +343 -0
  124. data/test/test_cookies.rb +123 -0
  125. data/test/test_encoded_links.rb +20 -0
  126. data/test/test_errors.rb +49 -0
  127. data/test/test_follow_meta.rb +69 -0
  128. data/test/test_form_action.rb +44 -0
  129. data/test/test_form_as_hash.rb +61 -0
  130. data/test/test_form_button.rb +38 -0
  131. data/test/test_form_no_inputname.rb +15 -0
  132. data/test/test_forms.rb +575 -0
  133. data/test/test_frames.rb +25 -0
  134. data/test/test_get_headers.rb +52 -0
  135. data/test/test_gzipping.rb +22 -0
  136. data/test/test_hash_api.rb +45 -0
  137. data/test/test_history.rb +142 -0
  138. data/test/test_history_added.rb +16 -0
  139. data/test/test_html_unscape_forms.rb +39 -0
  140. data/test/test_if_modified_since.rb +20 -0
  141. data/test/test_keep_alive.rb +31 -0
  142. data/test/test_links.rb +120 -0
  143. data/test/test_mech.rb +259 -0
  144. data/test/test_mechanize_file.rb +47 -0
  145. data/test/test_multi_select.rb +106 -0
  146. data/test/test_no_attributes.rb +13 -0
  147. data/test/test_option.rb +18 -0
  148. data/test/test_page.rb +67 -0
  149. data/test/test_pluggable_parser.rb +145 -0
  150. data/test/test_post_form.rb +34 -0
  151. data/test/test_pretty_print.rb +22 -0
  152. data/test/test_radiobutton.rb +75 -0
  153. data/test/test_redirect_limit_reached.rb +41 -0
  154. data/test/test_redirect_verb_handling.rb +45 -0
  155. data/test/test_referer.rb +39 -0
  156. data/test/test_relative_links.rb +40 -0
  157. data/test/test_request.rb +13 -0
  158. data/test/test_response_code.rb +52 -0
  159. data/test/test_save_file.rb +48 -0
  160. data/test/test_scheme.rb +48 -0
  161. data/test/test_select.rb +106 -0
  162. data/test/test_select_all.rb +15 -0
  163. data/test/test_select_none.rb +15 -0
  164. data/test/test_select_noopts.rb +16 -0
  165. data/test/test_set_fields.rb +44 -0
  166. data/test/test_ssl_server.rb +20 -0
  167. data/test/test_subclass.rb +14 -0
  168. data/test/test_textarea.rb +45 -0
  169. data/test/test_upload.rb +109 -0
  170. data/test/test_verbs.rb +25 -0
  171. metadata +284 -0
@@ -0,0 +1,6 @@
1
+ <html>
2
+ <title></title>
3
+ <body>
4
+ No title in the title tag
5
+ </body>
6
+ </html>
@@ -0,0 +1,21 @@
1
+ <html>
2
+ <body>
3
+ <a href="../tc_relative_links.html">dot dot slash</a>
4
+ <a href="../../../../../tc_relative_links.html">too many dots</a>
5
+ <FRAMESET cols="20%, 80%">
6
+ <FRAMESET rows="100, 200, 200">
7
+ <FRAME name="frame1" src="../tc_relative_links.html">
8
+ <FRAME name="frame2" src="../tc_relative_links.html">
9
+ </FRAMESET>
10
+ <FRAMESET rows="100, 200">
11
+ <FRAME name="frame3" src="/file_upload.html">
12
+ <IFRAME src="http://google.com/" name="frame4">
13
+ [Your user agent does not support frames or is currently configured
14
+ not to display frames. However, you may visit
15
+ <A href="foo.html">the related document.</A>]
16
+ <a href="?a=b">just the query string</A>
17
+ </IFRAME>
18
+ </FRAMESET>
19
+ </FRAMESET>
20
+ </body>
21
+ </html>
@@ -0,0 +1,5 @@
1
+ <html>
2
+ <body>
3
+ <a href="/alt_text.html ">Alt Text</a>
4
+ </body>
5
+ </html>
@@ -0,0 +1,8 @@
1
+ <html>
2
+ <head>
3
+ <base href="http://localhost/">
4
+ </head>
5
+ <body>
6
+ <a href="index.html">test</a>
7
+ </body>
8
+ </html>
@@ -0,0 +1,11 @@
1
+ <html>
2
+ <body>
3
+ <form name="test" method="post">
4
+ <input type="hidden" name="hidden_blank" value=''/>
5
+ <input type="hidden" name="hidden_noval"/>
6
+ <input type="text" name="visible_blank" value=''/>
7
+ <input type="text" name="visible_noval"/>
8
+ <input type="submit"/>
9
+ </form>
10
+ </body>
11
+ </html>
@@ -0,0 +1,19 @@
1
+ <html>
2
+ <head><title>tc_checkboxbuttons.html</title></head>
3
+ <body>
4
+ <form name="form1" method="post" action="/form_post">
5
+ Gender:<br />
6
+ M: <input type="checkbox" name="male" /><br />
7
+ F: <input type="checkbox" name="female" /><br />
8
+ Your one favorite color:<br />
9
+ Green: <input type="checkbox" name="green" /><br />
10
+ Green: <input type="checkbox" name="green" /><br />
11
+ Red: <input type="checkbox" name="red" /><br />
12
+ Blue: <input type="checkbox" name="blue" /><br />
13
+ Yellow: <input type="checkbox" name="yellow" /><br />
14
+ Brown: <input type="checkbox" name="brown" /><br />
15
+ Purple: <input type="checkbox" name="purple" /><br />
16
+ <input type="submit" value="Submit" />
17
+ </form>
18
+ </body>
19
+ </html>
@@ -0,0 +1,5 @@
1
+ <html>
2
+ <body>
3
+ <a href="/form_post?a=b&amp;b=c">test link</a>
4
+ </body>
5
+ </html>
@@ -0,0 +1,8 @@
1
+ <html>
2
+ <head>
3
+ <meta http-equiv="refresh" content="0; url=http://localhost/index.html">
4
+ </head>
5
+ <body>
6
+ This page has a meta refresh.
7
+ </body>
8
+ </html>
@@ -0,0 +1,48 @@
1
+ <html>
2
+ <head><title>Page Title</title></head>
3
+ <body>
4
+ <h1>Post Form 1</h1>
5
+ <form name="post_form1" method="post" action="/form_post?a=b&amp;b=c">
6
+ <table>
7
+ <tr>
8
+ <td>First Name</td>
9
+ <td><input type="text" name="first_name" /></td>
10
+ </tr>
11
+ </table><br />
12
+ <input type="submit" value="Submit" />
13
+ </form>
14
+
15
+ <h1>Post Form 2</h1>
16
+ <form name="post_form2" method="get" action="/form_post?a=b&amp;b=c">
17
+ <table>
18
+ <tr>
19
+ <td>First Name</td>
20
+ <td><input type="text" name="first_name" /></td>
21
+ </tr>
22
+ </table><br />
23
+ <input type="submit" value="Submit" />
24
+ </form>
25
+
26
+ <h1>Post Form 3</h1>
27
+ <form name="post_form3" method="post" action="/form_post?a=b&b=c">
28
+ <table>
29
+ <tr>
30
+ <td>First Name</td>
31
+ <td><input type="text" name="first_name" /></td>
32
+ </tr>
33
+ </table><br />
34
+ <input type="submit" value="Submit" />
35
+ </form>
36
+
37
+ <h1>Post Form 4</h1>
38
+ <form name="post_form4" method="post" action="/form_post#1">
39
+ <table>
40
+ <tr>
41
+ <td>First Name</td>
42
+ <td><input type="text" name="first_name" /></td>
43
+ </tr>
44
+ </table><br />
45
+ <input type="submit" value="Submit" />
46
+ </form>
47
+ </body>
48
+ </html>
@@ -0,0 +1,18 @@
1
+ <html>
2
+ <body>
3
+ <a href="thing.html"><b>Bold Dude</b></a>
4
+ <a href="thing.html">Dude</a>
5
+ <a href="thing.html">Aaron <b>James</b> Patterson</a>
6
+ <a href="thing.html"><b>Aaron</b> Patterson</a>
7
+ <a href="thing.html">Ruby <b>Rocks!</b></a>
8
+ <!-- Testing a bug with escaped stuff in links:
9
+ http://rubyforge.org/pipermail/mechanize-users/2006-September/000002.html
10
+ -->
11
+ <a href="link%20with%20space.html">encoded space</a>
12
+ <a href="link with space.html">not encoded space</a>
13
+ <!-- End escaped bug -->
14
+ <a href="unusual&&%3F%3F%23%23.html">unusual characters</a>
15
+
16
+ <a href="javascript:new_page('1')">javascript link</a>
17
+ </body>
18
+ </html>
@@ -0,0 +1,16 @@
1
+ <html>
2
+ <meta>
3
+ <head><title></title>
4
+ <body>
5
+ <a>Hello</a>
6
+ <a><img /></a>
7
+ <form>
8
+ <input />
9
+ <select>
10
+ <option />
11
+ </select>
12
+ <textarea></textarea>
13
+ </form>
14
+ <frame></frame>
15
+ </body>
16
+ </html>
@@ -0,0 +1,17 @@
1
+ <html>
2
+ <head><title>tc_pretty_print.html</title></head>
3
+ <body>
4
+ <a href="http://google.com/">Google</a><br />
5
+ <form method="post" action="/form_post" name="form1">
6
+ <input type="text" name="first_name"/><br />
7
+ <input type="password" name="password" /><br />
8
+ <input type="hidden" name="secret" value="hey" />
9
+ <input type="checkbox" name="checkme" />
10
+ <input type="radio" name="r" value="one" /><br />
11
+ <input type="radio" name="r" value="two" /><br />
12
+ <input type="submit" value="Submit" />
13
+ </form>
14
+ <frame src="http://memepool.com/" />
15
+ <iframe src="http://memepool.com/" />
16
+ </body>
17
+ </html>
@@ -0,0 +1,17 @@
1
+ <html>
2
+ <head><title>tc_radiobuttons.html</title></head>
3
+ <body>
4
+ <form name="form1" method="post" action="/form_post">
5
+ Gender:<br />
6
+ M: <input type="radio" name="gender" value="male" /><br />
7
+ F: <input type="radio" name="gender" value="female" /><br />
8
+ Your one favorite color:<br />
9
+ Green: <input type="radio" name="color" value="green" /><br />
10
+ Red: <input type="radio" name="color" value="red" /><br />
11
+ Blue: <input type="radio" name="color" value="blue" /><br />
12
+ Yellow: <input type="radio" name="color" value="yellow" /><br />
13
+ Brown: <input type="radio" name="color" value="brown" /><br />
14
+ <input type="submit" value="Submit" />
15
+ </form>
16
+ </body>
17
+ </html>
@@ -0,0 +1,10 @@
1
+ <html>
2
+ <body>
3
+ <a href="/referer">Referer Servlet</a>
4
+ <br />
5
+ <form method="post" action="/referer">
6
+ <input type="text" name="first" /></br>
7
+ <input type="submit" value="Submit" />
8
+ </form>
9
+ </body>
10
+ </html>
@@ -0,0 +1,19 @@
1
+ <html>
2
+ <body>
3
+ <a href="relative/tc_relative_links.html">forward</a>
4
+ <FRAMESET cols="20%, 80%">
5
+ <FRAMESET rows="100, 200, 200">
6
+ <FRAME name="frame1" src="relative/tc_relative_links.html">
7
+ <FRAME name="frame2" src="relative/tc_relative_links.html">
8
+ </FRAMESET>
9
+ <FRAMESET rows="100, 200">
10
+ <FRAME name="frame3" src="/file_upload.html">
11
+ <IFRAME src="http://google.com/" name="frame4">
12
+ [Your user agent does not support frames or is currently configured
13
+ not to display frames. However, you may visit
14
+ <A href="foo.html">the related document.</A>]
15
+ </IFRAME>
16
+ </FRAMESET>
17
+ </FRAMESET>
18
+ </body>
19
+ </html>
@@ -0,0 +1,23 @@
1
+ <html>
2
+ <head><title>tc_textarea.html</title></head>
3
+ <body>
4
+ <form name="form1" method="post" action="/form_post">
5
+ <textarea name="text1"></textarea>
6
+ <br />
7
+ <input type="submit" value="Submit" />
8
+ </form>
9
+ <br />
10
+ <form name="form2" method="post" action="/form_post">
11
+ <textarea name="text1">sample text</textarea>
12
+ <br />
13
+ <input type="submit" value="Submit" />
14
+ </form>
15
+ <br />
16
+ <form name="form3" method="post" action="/form_post">
17
+ <textarea name="text1"></textarea>
18
+ <textarea name="text1">sample text</textarea>
19
+ <br />
20
+ <input type="submit" value="Submit" />
21
+ </form>
22
+ </body>
23
+ </html>
@@ -0,0 +1,5 @@
1
+ <html>
2
+ <body>
3
+ This is a webpage that has a very unusual name.
4
+ </body>
5
+ </html>
@@ -0,0 +1,339 @@
1
+ require 'webrick'
2
+ require 'logger'
3
+ require 'date'
4
+ require 'zlib'
5
+ require 'stringio'
6
+ require 'base64'
7
+
8
+ class VerbServlet < WEBrick::HTTPServlet::AbstractServlet
9
+ %w(HEAD GET POST PUT DELETE).each do |verb|
10
+ eval(<<-eomethod)
11
+ def do_#{verb}(req, res)
12
+ res.body = "method: #{verb}"
13
+ end
14
+ eomethod
15
+ end
16
+ end
17
+
18
+ class BasicAuthServlet < WEBrick::HTTPServlet::AbstractServlet
19
+ def do_GET(req,res)
20
+ htpd = WEBrick::HTTPAuth::Htpasswd.new('dot.htpasswd')
21
+ htpd.set_passwd('Blah', 'user', 'pass')
22
+ authenticator = WEBrick::HTTPAuth::BasicAuth.new({
23
+ :UserDB => htpd,
24
+ :Realm => 'Blah',
25
+ :Logger => Logger.new(nil)
26
+ }
27
+ )
28
+ begin
29
+ authenticator.authenticate(req,res)
30
+ res.body = 'You are authenticated'
31
+ rescue WEBrick::HTTPStatus::Unauthorized => ex
32
+ res.status = 401
33
+ end
34
+ FileUtils.rm('dot.htpasswd')
35
+ end
36
+ alias :do_POST :do_GET
37
+ end
38
+
39
+ class DigestAuthServlet < WEBrick::HTTPServlet::AbstractServlet
40
+ htpd = WEBrick::HTTPAuth::Htdigest.new('digest.htpasswd')
41
+ htpd.set_passwd('Blah', 'user', 'pass')
42
+ @@authenticator = WEBrick::HTTPAuth::DigestAuth.new({
43
+ :UserDB => htpd,
44
+ :Realm => 'Blah',
45
+ :Algorithm => 'MD5',
46
+ :Logger => Logger.new(nil)
47
+ }
48
+ )
49
+ def do_GET(req,res)
50
+ def req.request_time; Time.now; end
51
+ def req.request_uri; '/digest_auth'; end
52
+ def req.request_method; "GET"; end
53
+
54
+ begin
55
+ @@authenticator.authenticate(req,res)
56
+ res.body = 'You are authenticated'
57
+ rescue WEBrick::HTTPStatus::Unauthorized => ex
58
+ res.status = 401
59
+ end
60
+ FileUtils.rm('digest.htpasswd') if File.exists?('digest.htpasswd')
61
+ end
62
+ alias :do_POST :do_GET
63
+ end
64
+
65
+ class HeaderServlet < WEBrick::HTTPServlet::AbstractServlet
66
+ def do_GET(req, res)
67
+ res['Content-Type'] = "text/html"
68
+
69
+ req.query.each do |x,y|
70
+ res[x] = y
71
+ end
72
+
73
+ body = ''
74
+ req.each_header do |k,v|
75
+ body << "#{k}|#{v}\n"
76
+ end
77
+ res.body = body
78
+ end
79
+ end
80
+
81
+ class RefererServlet < WEBrick::HTTPServlet::AbstractServlet
82
+ def do_GET(req, res)
83
+ res['Content-Type'] = "text/html"
84
+ res.body = req['Referer'] || ''
85
+ end
86
+
87
+ def do_POST(req, res)
88
+ res['Content-Type'] = "text/html"
89
+ res.body = req['Referer'] || ''
90
+ end
91
+ end
92
+
93
+ class ModifiedSinceServlet < WEBrick::HTTPServlet::AbstractServlet
94
+ def do_GET(req, res)
95
+ s_time = 'Fri, 04 May 2001 00:00:38 GMT'
96
+
97
+ my_time = Time.parse(s_time)
98
+
99
+ if req['If-Modified-Since']
100
+ your_time = Time.parse(req['If-Modified-Since'])
101
+ if my_time > your_time
102
+ res.body = 'This page was updated since you requested'
103
+ else
104
+ res.status = 304
105
+ end
106
+ else
107
+ res.body = 'You did not send an If-Modified-Since header'
108
+ end
109
+
110
+ res['Last-Modified'] = s_time
111
+ end
112
+ end
113
+
114
+ class GzipServlet < WEBrick::HTTPServlet::AbstractServlet
115
+ def do_GET(req, res)
116
+ if req['Accept-Encoding'] =~ /gzip/
117
+ if req.query['file']
118
+ File.open("#{BASE_DIR}/htdocs/#{req.query['file']}", 'r') do |file|
119
+ string = ""
120
+ zipped = StringIO.new string, 'w'
121
+ gz = Zlib::GzipWriter.new(zipped)
122
+ gz.write file.read
123
+ gz.close
124
+ res.body = string
125
+ end
126
+ else
127
+ res.body = ''
128
+ end
129
+ res['Content-Encoding'] = 'gzip'
130
+ res['Content-Type'] = "text/html"
131
+ else
132
+ raise 'no gzip'
133
+ end
134
+ end
135
+ end
136
+
137
+ class BadContentTypeTest < WEBrick::HTTPServlet::AbstractServlet
138
+ def do_GET(req, res)
139
+ res['Content-Type'] = "text/xml"
140
+ res.body = "Hello World"
141
+ end
142
+ end
143
+
144
+ class ContentTypeTest < WEBrick::HTTPServlet::AbstractServlet
145
+ def do_GET(req, res)
146
+ ct = req.query['ct'] || "text/html; charset=utf-8"
147
+ res['Content-Type'] = ct
148
+ res.body = "Hello World"
149
+ end
150
+ end
151
+
152
+ class FileUploadTest < WEBrick::HTTPServlet::AbstractServlet
153
+ def do_POST(req, res)
154
+ res.body = req.body
155
+ end
156
+ end
157
+
158
+ class InfiniteRefreshTest < WEBrick::HTTPServlet::AbstractServlet
159
+ def do_GET(req, res)
160
+ res['Content-Type'] = req.query['ct'] || "text/html"
161
+ res.status = req.query['code'] ? req.query['code'].to_i : '302'
162
+ number = req.query['q'] ? req.query['q'].to_i : 0
163
+ res['Refresh'] = " 0;url=http://localhost/infinite_refresh?q=#{number + 1}\r\n";
164
+ end
165
+ end
166
+
167
+ class InfiniteRedirectTest < WEBrick::HTTPServlet::AbstractServlet
168
+ def do_GET(req, res)
169
+ res['Content-Type'] = req.query['ct'] || "text/html"
170
+ res.status = req.query['code'] ? req.query['code'].to_i : '302'
171
+ number = req.query['q'] ? req.query['q'].to_i : 0
172
+ res['Location'] = "/infinite_redirect?q=#{number + 1}"
173
+ end
174
+ alias :do_POST :do_GET
175
+ end
176
+
177
+ class RedirectTest < WEBrick::HTTPServlet::AbstractServlet
178
+ def do_GET(req, res)
179
+ res['Content-Type'] = req.query['ct'] || "text/html"
180
+ res.status = req.query['code'] ? req.query['code'].to_i : '302'
181
+ res['Location'] = "/verb"
182
+ end
183
+
184
+ alias :do_POST :do_GET
185
+ alias :do_HEAD :do_GET
186
+ alias :do_PUT :do_GET
187
+ alias :do_DELETE :do_GET
188
+ end
189
+
190
+ class ResponseCodeTest < WEBrick::HTTPServlet::AbstractServlet
191
+ def do_GET(req, res)
192
+ res['Content-Type'] = req.query['ct'] || "text/html"
193
+ if req.query['code']
194
+ code = req.query['code'].to_i
195
+ case code
196
+ when 300, 301, 302, 303, 304, 305, 307
197
+ res['Location'] = "/index.html"
198
+ end
199
+ res.status = code
200
+ else
201
+ end
202
+ end
203
+ end
204
+
205
+ class HttpRefreshTest < WEBrick::HTTPServlet::AbstractServlet
206
+ def do_GET(req, res)
207
+ res['Content-Type'] = req.query['ct'] || "text/html"
208
+ refresh_time = req.query['refresh_time'] || 0
209
+ refresh_url = req.query['refresh_url'] || '/index.html'
210
+ res['Refresh'] = " #{refresh_time};url=#{refresh_url}\r\n";
211
+ end
212
+ end
213
+
214
+ class FormTest < WEBrick::HTTPServlet::AbstractServlet
215
+ def do_GET(req, res)
216
+ res.body = "<HTML><body>"
217
+ req.query.each_key { |k|
218
+ req.query[k].each_data { |data|
219
+ res.body << "<a href=\"#\">#{URI.unescape(k)}:#{URI.unescape(data)}</a><br />"
220
+ }
221
+ }
222
+ res.body << "</body></HTML>"
223
+ res['Content-Type'] = "text/html"
224
+ end
225
+
226
+ def do_POST(req, res)
227
+ res.body = "<HTML><body>"
228
+ req.query.each_key { |k|
229
+ req.query[k].each_data { |data|
230
+ res.body << "<a href=\"#\">#{k}:#{data}</a><br />"
231
+ }
232
+ }
233
+ res.body << "</body></HTML>"
234
+ res['Content-Type'] = "text/html"
235
+ end
236
+ end
237
+
238
+ class OneCookieTest < WEBrick::HTTPServlet::AbstractServlet
239
+ def do_GET(req, res)
240
+ cookie = WEBrick::Cookie.new("foo", "bar")
241
+ cookie.path = "/"
242
+ cookie.expires = Time.now + 86400
243
+ res.cookies << cookie
244
+ res['Content-Type'] = "text/html"
245
+ res.body = "<html><body>hello</body></html>"
246
+ end
247
+ end
248
+
249
+ class OneCookieNoSpacesTest < WEBrick::HTTPServlet::AbstractServlet
250
+ def do_GET(req, res)
251
+ cookie = WEBrick::Cookie.new("foo", "bar")
252
+ cookie.path = "/"
253
+ cookie.expires = Time.now + 86400
254
+ res.cookies << cookie.to_s.gsub(/; /, ';')
255
+ res['Content-Type'] = "text/html"
256
+ res.body = "<html><body>hello</body></html>"
257
+ end
258
+ end
259
+
260
+ class ManyCookiesTest < WEBrick::HTTPServlet::AbstractServlet
261
+ def do_GET(req, res)
262
+ name_cookie = WEBrick::Cookie.new("name", "Aaron")
263
+ name_cookie.path = "/"
264
+ name_cookie.expires = Time.now + 86400
265
+ res.cookies << name_cookie
266
+ res.cookies << name_cookie
267
+ res.cookies << name_cookie
268
+ res.cookies << name_cookie
269
+
270
+ expired_cookie = WEBrick::Cookie.new("expired", "doh")
271
+ expired_cookie.path = "/"
272
+ expired_cookie.expires = Time.now - 86400
273
+ res.cookies << expired_cookie
274
+
275
+ different_path_cookie = WEBrick::Cookie.new("a_path", "some_path")
276
+ different_path_cookie.path = "/some_path"
277
+ different_path_cookie.expires = Time.now + 86400
278
+ res.cookies << different_path_cookie
279
+
280
+ no_path_cookie = WEBrick::Cookie.new("no_path", "no_path")
281
+ no_path_cookie.expires = Time.now + 86400
282
+ res.cookies << no_path_cookie
283
+
284
+ no_exp_path_cookie = WEBrick::Cookie.new("no_expires", "nope")
285
+ no_exp_path_cookie.path = "/"
286
+ res.cookies << no_exp_path_cookie
287
+
288
+ res['Content-Type'] = "text/html"
289
+ res.body = "<html><body>hello</body></html>"
290
+ end
291
+ end
292
+
293
+ class ManyCookiesAsStringTest < WEBrick::HTTPServlet::AbstractServlet
294
+ def do_GET(req, res)
295
+ cookies = []
296
+ name_cookie = WEBrick::Cookie.new("name", "Aaron")
297
+ name_cookie.path = "/"
298
+ name_cookie.expires = Time.now + 86400
299
+ name_cookie.domain = 'localhost'
300
+ cookies << name_cookie
301
+ cookies << name_cookie
302
+ cookies << name_cookie
303
+ cookies << "#{name_cookie}; HttpOnly"
304
+
305
+ expired_cookie = WEBrick::Cookie.new("expired", "doh")
306
+ expired_cookie.path = "/"
307
+ expired_cookie.expires = Time.now - 86400
308
+ cookies << expired_cookie
309
+
310
+ different_path_cookie = WEBrick::Cookie.new("a_path", "some_path")
311
+ different_path_cookie.path = "/some_path"
312
+ different_path_cookie.expires = Time.now + 86400
313
+ cookies << different_path_cookie
314
+
315
+ no_path_cookie = WEBrick::Cookie.new("no_path", "no_path")
316
+ no_path_cookie.expires = Time.now + 86400
317
+ cookies << no_path_cookie
318
+
319
+ no_exp_path_cookie = WEBrick::Cookie.new("no_expires", "nope")
320
+ no_exp_path_cookie.path = "/"
321
+ cookies << no_exp_path_cookie
322
+
323
+ res['Set-Cookie'] = cookies.join(', ')
324
+
325
+ res['Content-Type'] = "text/html"
326
+ res.body = "<html><body>hello</body></html>"
327
+ end
328
+ end
329
+
330
+ class SendCookiesTest < WEBrick::HTTPServlet::AbstractServlet
331
+ def do_GET(req, res)
332
+ res['Content-Type'] = "text/html"
333
+ res.body = "<html><body>"
334
+ req.cookies.each { |c|
335
+ res.body << "<a href=\"#\">#{c.name}:#{c.value}</a>"
336
+ }
337
+ res.body << "</body></html>"
338
+ end
339
+ end