mechanize 0.6.3 → 0.6.4
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of mechanize might be problematic. Click here for more details.
- data/{CHANGELOG → CHANGELOG.txt} +17 -0
- data/{EXAMPLES → EXAMPLES.txt} +0 -0
- data/{GUIDE → GUIDE.txt} +0 -0
- data/{LICENSE → LICENSE.txt} +0 -0
- data/Manifest.txt +31 -0
- data/{NOTES → NOTES.txt} +14 -0
- data/{README → README.txt} +7 -3
- data/Rakefile +64 -0
- data/eg/flickr_upload.rb +23 -0
- data/eg/mech-dump.rb +7 -0
- data/eg/proxy_req.rb +9 -0
- data/eg/rubyforge.rb +21 -0
- data/eg/spider.rb +11 -0
- data/lib/mechanize/cookie.rb +15 -3
- data/lib/mechanize/errors.rb +1 -3
- data/lib/mechanize/form.rb +3 -1
- data/lib/mechanize/form_elements.rb +1 -1
- data/lib/mechanize/mech_version.rb +1 -1
- data/lib/mechanize/net-overrides/net/http.rb +1 -1
- data/lib/mechanize/page.rb +7 -10
- data/lib/mechanize/page_elements.rb +1 -0
- data/lib/mechanize/parsers/rexml_page.rb +8 -10
- data/lib/mechanize.rb +59 -37
- data/setup.rb +1585 -0
- data/test/htdocs/relative/tc_relative_links.html +19 -0
- data/test/htdocs/tc_relative_links.html +19 -0
- data/test/tc_checkboxes.rb +1 -1
- data/test/tc_cookie_class.rb +18 -2
- data/test/tc_form_as_hash.rb +6 -8
- data/test/tc_form_button.rb +36 -0
- data/test/tc_form_no_inputname.rb +2 -2
- data/test/tc_forms.rb +12 -0
- data/test/tc_no_attributes.rb +1 -1
- data/test/tc_relative_links.rb +40 -0
- data/test/tc_response_code.rb +20 -0
- data/test/tc_subclass.rb +28 -0
- data/test/tc_upload.rb +11 -11
- data/test/{ts_mech.rb → test_all.rb} +24 -34
- data/test/test_includes.rb +112 -0
- data/test/{servlets.rb → test_servlets.rb} +4 -6
- metadata +55 -59
- data/test/README +0 -7
- data/test/proxy.rb +0 -30
- data/test/server.rb +0 -43
- data/test/ssl_server.rb +0 -48
@@ -0,0 +1,19 @@
|
|
1
|
+
<html>
|
2
|
+
<body>
|
3
|
+
<a href="../tc_relative_links.html">dot dot slash</a>
|
4
|
+
<FRAMESET cols="20%, 80%">
|
5
|
+
<FRAMESET rows="100, 200, 200">
|
6
|
+
<FRAME name="frame1" src="../tc_relative_links.html">
|
7
|
+
<FRAME name="frame2" src="../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,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>
|
data/test/tc_checkboxes.rb
CHANGED
data/test/tc_cookie_class.rb
CHANGED
@@ -7,8 +7,8 @@ require 'test_includes'
|
|
7
7
|
|
8
8
|
module Enumerable
|
9
9
|
def combine
|
10
|
-
masks = inject([[], 1]){|(ar, m), e| [ar<<m, m<<1]}[0]
|
11
|
-
all = masks.inject(0){|al, m| al|m}
|
10
|
+
masks = inject([[], 1]){|(ar, m), e| [ar << m, m << 1 ] }[0]
|
11
|
+
all = masks.inject(0){ |al, m| al|m }
|
12
12
|
|
13
13
|
result = []
|
14
14
|
for i in 1..all do
|
@@ -53,6 +53,22 @@ class CookieClassTest < Test::Unit::TestCase
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
+
def test_parse_bad_version
|
57
|
+
bad_cookie = 'PRETANET=TGIAqbFXtt; Name=/PRETANET; Path=/; Version=1.2; Content-type=text/html; Domain=192.168.6.196; expires=Friday, 13-November-2026 23:01:46 GMT;'
|
58
|
+
url = URI.parse('http://localhost/')
|
59
|
+
WWW::Mechanize::Cookie.parse(url, bad_cookie) { |cookie|
|
60
|
+
assert_nil(cookie.version)
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_parse_bad_max_age
|
65
|
+
bad_cookie = 'PRETANET=TGIAqbFXtt; Name=/PRETANET; Path=/; Max-Age=1.2; Content-type=text/html; Domain=192.168.6.196; expires=Friday, 13-November-2026 23:01:46 GMT;'
|
66
|
+
url = URI.parse('http://localhost/')
|
67
|
+
WWW::Mechanize::Cookie.parse(url, bad_cookie) { |cookie|
|
68
|
+
assert_nil(cookie.max_age)
|
69
|
+
}
|
70
|
+
end
|
71
|
+
|
56
72
|
def test_parse_date_fail
|
57
73
|
url = URI.parse('http://localhost/')
|
58
74
|
|
data/test/tc_form_as_hash.rb
CHANGED
@@ -11,11 +11,11 @@ class TestFormHash < Test::Unit::TestCase
|
|
11
11
|
|
12
12
|
def setup
|
13
13
|
@agent = WWW::Mechanize.new
|
14
|
+
@page = @agent.get('http://localhost/form_multival.html')
|
14
15
|
end
|
15
16
|
|
16
17
|
def test_form_hash
|
17
|
-
|
18
|
-
form = page.forms.name('post_form').first
|
18
|
+
form = @page.forms.name('post_form').first
|
19
19
|
|
20
20
|
assert_not_nil(form)
|
21
21
|
field_length = form.fields.length
|
@@ -27,8 +27,7 @@ class TestFormHash < Test::Unit::TestCase
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def test_add_field_via_hash
|
30
|
-
|
31
|
-
form = page.forms.name('post_form').first
|
30
|
+
form = @page.forms.name('post_form').first
|
32
31
|
|
33
32
|
assert_not_nil(form)
|
34
33
|
field_length = form.fields.length
|
@@ -40,8 +39,7 @@ class TestFormHash < Test::Unit::TestCase
|
|
40
39
|
end
|
41
40
|
|
42
41
|
def test_fields_as_hash
|
43
|
-
|
44
|
-
form = page.forms.name('post_form').first
|
42
|
+
form = @page.forms.name('post_form').first
|
45
43
|
|
46
44
|
assert_not_nil(form)
|
47
45
|
assert_equal(2, form.fields.name('first').length)
|
@@ -52,8 +50,8 @@ class TestFormHash < Test::Unit::TestCase
|
|
52
50
|
end
|
53
51
|
|
54
52
|
def test_keys
|
55
|
-
page = @agent.get(
|
56
|
-
form = page.forms.first
|
53
|
+
@page = @agent.get('http://localhost/empty_form.html')
|
54
|
+
form = @page.forms.first
|
57
55
|
|
58
56
|
assert_not_nil(form)
|
59
57
|
assert_equal(false, form.has_field?('name'))
|
@@ -0,0 +1,36 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'mechanize'
|
6
|
+
require 'test_includes'
|
7
|
+
|
8
|
+
class TestFormButtons < Test::Unit::TestCase
|
9
|
+
include TestMethods
|
10
|
+
|
11
|
+
def setup
|
12
|
+
@agent = WWW::Mechanize.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_submit_button
|
16
|
+
html = <<-END
|
17
|
+
<html><body>
|
18
|
+
<form><input type="submit" value="submit" /></form>
|
19
|
+
</body></html>
|
20
|
+
END
|
21
|
+
page = WWW::Mechanize::Page.new( nil, html_response, html, 200, @agent )
|
22
|
+
assert_equal(1, page.forms.length)
|
23
|
+
assert_equal(1, page.forms.first.buttons.length)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_button_button
|
27
|
+
html = <<-END
|
28
|
+
<html><body>
|
29
|
+
<form><input type="button" value="submit" /></form>
|
30
|
+
</body></html>
|
31
|
+
END
|
32
|
+
page = WWW::Mechanize::Page.new( nil, html_response, html, 200, @agent )
|
33
|
+
assert_equal(1, page.forms.length)
|
34
|
+
assert_equal(1, page.forms.first.buttons.length)
|
35
|
+
end
|
36
|
+
end
|
@@ -10,11 +10,11 @@ class FormNoInputNameTest < Test::Unit::TestCase
|
|
10
10
|
|
11
11
|
def setup
|
12
12
|
@agent = WWW::Mechanize.new
|
13
|
+
@page = @agent.get('http://localhost/form_no_input_name.html')
|
13
14
|
end
|
14
15
|
|
15
16
|
def test_no_input_name
|
16
|
-
|
17
|
-
form = page.forms.first
|
17
|
+
form = @page.forms.first
|
18
18
|
assert_equal(0, form.fields.length)
|
19
19
|
assert_equal(0, form.radiobuttons.length)
|
20
20
|
assert_equal(0, form.checkboxes.length)
|
data/test/tc_forms.rb
CHANGED
@@ -511,4 +511,16 @@ class FormsMechTest < Test::Unit::TestCase
|
|
511
511
|
assert_not_nil(f)
|
512
512
|
assert_equal(true, form.has_field?('intarweb'))
|
513
513
|
end
|
514
|
+
|
515
|
+
def test_field_error
|
516
|
+
@page = @agent.get('http://localhost/empty_form.html')
|
517
|
+
form = @page.forms.first
|
518
|
+
assert_raise(NoMethodError) {
|
519
|
+
form.foo = 'asdfasdf'
|
520
|
+
}
|
521
|
+
|
522
|
+
assert_raise(NoMethodError) {
|
523
|
+
form.foo
|
524
|
+
}
|
525
|
+
end
|
514
526
|
end
|
data/test/tc_no_attributes.rb
CHANGED
@@ -14,7 +14,7 @@ class TestNoAttributes < Test::Unit::TestCase
|
|
14
14
|
|
15
15
|
def test_parse_no_attributes
|
16
16
|
assert_nothing_raised do
|
17
|
-
page = @agent.get(
|
17
|
+
page = @agent.get('http://localhost/tc_no_attributes.html')
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'mechanize'
|
6
|
+
require 'test_includes'
|
7
|
+
|
8
|
+
class TestRelativeLinks < Test::Unit::TestCase
|
9
|
+
include TestMethods
|
10
|
+
|
11
|
+
def setup
|
12
|
+
@agent = WWW::Mechanize.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_dot_dot_slash
|
16
|
+
@page = @agent.get("http://localhost/relative/tc_relative_links.html")
|
17
|
+
page = @page.links.first.click
|
18
|
+
assert_equal('http://localhost/tc_relative_links.html', @agent.current_page.uri.to_s)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_go_forward
|
22
|
+
@page = @agent.get("http://localhost/tc_relative_links.html")
|
23
|
+
@page = @page.links.first.click
|
24
|
+
assert_equal('http://localhost/relative/tc_relative_links.html', @agent.current_page.uri.to_s)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_frame_dot_dot_slash
|
28
|
+
@page = @agent.get("http://localhost/relative/tc_relative_links.html")
|
29
|
+
page = @agent.click(@page.frames.text('frame1'))
|
30
|
+
assert_equal('http://localhost/tc_relative_links.html', @agent.current_page.uri.to_s)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_frame_forward_back_forward
|
34
|
+
@page = @agent.get("http://localhost/tc_relative_links.html")
|
35
|
+
page1 = @agent.click @page.frames.name('frame1')
|
36
|
+
assert_equal('http://localhost/relative/tc_relative_links.html', @agent.current_page.uri.to_s)
|
37
|
+
page2 = @agent.click @page.frames.name('frame2')
|
38
|
+
assert_equal('http://localhost/relative/tc_relative_links.html', @agent.current_page.uri.to_s)
|
39
|
+
end
|
40
|
+
end
|
data/test/tc_response_code.rb
CHANGED
@@ -13,6 +13,10 @@ class ResponseCodeMechTest < Test::Unit::TestCase
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_redirect
|
16
|
+
@agent.get("http://localhost:#{PORT}/response_code?code=300")
|
17
|
+
assert_equal("http://localhost:#{PORT}/index.html",
|
18
|
+
@agent.current_page.uri.to_s)
|
19
|
+
|
16
20
|
@agent.get("http://localhost:#{PORT}/response_code?code=301")
|
17
21
|
assert_equal("http://localhost:#{PORT}/index.html",
|
18
22
|
@agent.current_page.uri.to_s)
|
@@ -20,6 +24,22 @@ class ResponseCodeMechTest < Test::Unit::TestCase
|
|
20
24
|
@agent.get("http://localhost:#{PORT}/response_code?code=302")
|
21
25
|
assert_equal("http://localhost:#{PORT}/index.html",
|
22
26
|
@agent.current_page.uri.to_s)
|
27
|
+
|
28
|
+
@agent.get("http://localhost:#{PORT}/response_code?code=303")
|
29
|
+
assert_equal("http://localhost:#{PORT}/index.html",
|
30
|
+
@agent.current_page.uri.to_s)
|
31
|
+
|
32
|
+
@agent.get("http://localhost:#{PORT}/response_code?code=307")
|
33
|
+
assert_equal("http://localhost:#{PORT}/index.html",
|
34
|
+
@agent.current_page.uri.to_s)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_do_not_follow_redirect
|
38
|
+
@agent.redirect_ok = false
|
39
|
+
|
40
|
+
@agent.get("http://localhost:#{PORT}/response_code?code=302")
|
41
|
+
assert_equal("http://localhost:#{PORT}/response_code?code=302",
|
42
|
+
@agent.current_page.uri.to_s)
|
23
43
|
end
|
24
44
|
|
25
45
|
def test_error
|
data/test/tc_subclass.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'mechanize'
|
6
|
+
require 'test_includes'
|
7
|
+
|
8
|
+
class MechSubclass < WWW::Mechanize
|
9
|
+
def set_headers(uri, request, cur_page)
|
10
|
+
super(uri, request, cur_page)
|
11
|
+
request.add_field('Cookie', 'name=Aaron')
|
12
|
+
request
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestSubclass < Test::Unit::TestCase
|
17
|
+
include TestMethods
|
18
|
+
|
19
|
+
def setup
|
20
|
+
@agent = MechSubclass.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_send_cookie
|
24
|
+
page = @agent.get("http://localhost:#{PORT}/send_cookies")
|
25
|
+
assert_equal(1, page.links.length)
|
26
|
+
assert_not_nil(page.links.find { |l| l.text == "name:Aaron" })
|
27
|
+
end
|
28
|
+
end
|
data/test/tc_upload.rb
CHANGED
@@ -17,14 +17,14 @@ class UploadMechTest < Test::Unit::TestCase
|
|
17
17
|
assert_equal('multipart/form-data', @page.forms[0].enctype)
|
18
18
|
|
19
19
|
form = @page.forms.first
|
20
|
-
form.file_uploads.first.file_name = "
|
20
|
+
form.file_uploads.first.file_name = "#{BASE_DIR}/test_all.rb"
|
21
21
|
form.file_uploads.first.mime_type = "text/plain"
|
22
22
|
form.file_uploads.first.file_data = "Hello World\n\n"
|
23
23
|
|
24
24
|
@page = @agent.submit(form)
|
25
25
|
|
26
26
|
assert_match(
|
27
|
-
"Content-Disposition: form-data; name=\"userfile1\"; filename=\"
|
27
|
+
"Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{BASE_DIR}/test_all.rb\"",
|
28
28
|
@page.body
|
29
29
|
)
|
30
30
|
assert_match(
|
@@ -40,14 +40,14 @@ class UploadMechTest < Test::Unit::TestCase
|
|
40
40
|
assert_equal('multipart/form-data', @page.forms[1].enctype)
|
41
41
|
|
42
42
|
form = @page.forms[1]
|
43
|
-
form.file_uploads.first.file_name = "
|
43
|
+
form.file_uploads.first.file_name = "#{BASE_DIR}/test_all.rb"
|
44
44
|
form.file_uploads.first.mime_type = "text/plain"
|
45
45
|
form.file_uploads.first.file_data = "Hello World\n\n"
|
46
46
|
|
47
47
|
@page = @agent.submit(form)
|
48
48
|
|
49
49
|
assert_match(
|
50
|
-
"Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"
|
50
|
+
"Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"#{BASE_DIR}/test_all.rb\"",
|
51
51
|
@page.body
|
52
52
|
)
|
53
53
|
end
|
@@ -56,13 +56,13 @@ class UploadMechTest < Test::Unit::TestCase
|
|
56
56
|
assert_equal('multipart/form-data', @page.forms[1].enctype)
|
57
57
|
|
58
58
|
form = @page.forms[1]
|
59
|
-
form.file_uploads.first.file_name = "
|
59
|
+
form.file_uploads.first.file_name = "#{BASE_DIR}/test_all.rb"
|
60
60
|
|
61
61
|
@page = @agent.submit(form)
|
62
62
|
|
63
|
-
contents = File.open("
|
63
|
+
contents = File.open("#{BASE_DIR}/test_all.rb", 'rb') { |f| f.read }
|
64
64
|
assert_match(
|
65
|
-
"Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"
|
65
|
+
"Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"#{BASE_DIR}/test_all.rb\"",
|
66
66
|
@page.body
|
67
67
|
)
|
68
68
|
assert_match(contents, @page.body)
|
@@ -72,14 +72,14 @@ class UploadMechTest < Test::Unit::TestCase
|
|
72
72
|
assert_equal('multipart/form-data', @page.forms[1].enctype)
|
73
73
|
|
74
74
|
form = @page.forms[1]
|
75
|
-
form.file_uploads.first.file_name = "
|
76
|
-
form.file_uploads.first.file_data = File.open("
|
75
|
+
form.file_uploads.first.file_name = "#{BASE_DIR}/test_all.rb"
|
76
|
+
form.file_uploads.first.file_data = File.open("#{BASE_DIR}/test_all.rb", 'rb')
|
77
77
|
|
78
78
|
@page = @agent.submit(form)
|
79
79
|
|
80
|
-
contents = File.open("
|
80
|
+
contents = File.open("#{BASE_DIR}/test_all.rb", 'rb') { |f| f.read }
|
81
81
|
assert_match(
|
82
|
-
"Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"
|
82
|
+
"Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"#{BASE_DIR}/test_all.rb\"",
|
83
83
|
@page.body
|
84
84
|
)
|
85
85
|
assert_match(contents, @page.body)
|
@@ -1,47 +1,37 @@
|
|
1
1
|
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
2
|
$:.unshift File.join(File.dirname(__FILE__), "..", "test")
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
}
|
7
|
-
|
8
|
-
#Thread.new {
|
9
|
-
# require 'ssl_server'
|
10
|
-
#}
|
11
|
-
|
12
|
-
require 'test/unit'
|
13
|
-
require 'tc_cookies'
|
14
|
-
require 'tc_forms'
|
15
|
-
require 'tc_mech'
|
16
|
-
require 'tc_links'
|
17
|
-
require 'tc_response_code'
|
18
|
-
require 'tc_upload'
|
19
|
-
require 'tc_forms'
|
20
|
-
require 'tc_watches'
|
21
|
-
require 'tc_authenticate'
|
4
|
+
require 'tc_bad_links'
|
5
|
+
require 'tc_checkboxes'
|
22
6
|
require 'tc_cookie_class'
|
23
7
|
require 'tc_cookie_jar'
|
8
|
+
require 'tc_cookies'
|
24
9
|
require 'tc_errors'
|
25
|
-
require '
|
26
|
-
require '
|
27
|
-
require '
|
28
|
-
require 'tc_page'
|
10
|
+
require 'tc_form_action'
|
11
|
+
require 'tc_form_as_hash'
|
12
|
+
require 'tc_form_button'
|
29
13
|
require 'tc_form_no_inputname'
|
14
|
+
require 'tc_forms'
|
15
|
+
require 'tc_gzipping'
|
16
|
+
require 'tc_links'
|
17
|
+
require 'tc_mech'
|
30
18
|
require 'tc_multi_select'
|
19
|
+
require 'tc_no_attributes'
|
20
|
+
require 'tc_page'
|
21
|
+
require 'tc_pluggable_parser'
|
22
|
+
require 'tc_post_form'
|
23
|
+
require 'tc_pretty_print'
|
24
|
+
require 'tc_radiobutton'
|
25
|
+
require 'tc_referer'
|
26
|
+
require 'tc_relative_links'
|
27
|
+
require 'tc_response_code'
|
28
|
+
require 'tc_save_file'
|
29
|
+
require 'tc_select'
|
31
30
|
require 'tc_select_all'
|
32
31
|
require 'tc_select_none'
|
33
|
-
require 'tc_select'
|
34
32
|
require 'tc_select_noopts'
|
35
33
|
require 'tc_set_fields'
|
36
|
-
require '
|
37
|
-
require 'tc_radiobutton'
|
38
|
-
require 'tc_checkboxes'
|
39
|
-
require 'tc_pretty_print'
|
34
|
+
require 'tc_subclass'
|
40
35
|
require 'tc_textarea'
|
41
|
-
require '
|
42
|
-
require '
|
43
|
-
require 'tc_referer'
|
44
|
-
require 'tc_form_as_hash'
|
45
|
-
#require 'tc_proxy'
|
46
|
-
#require 'tc_ssl_server'
|
47
|
-
|
36
|
+
require 'tc_upload'
|
37
|
+
require 'tc_watches'
|
data/test/test_includes.rb
CHANGED
@@ -1,5 +1,117 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'test_servlets'
|
3
|
+
require 'webrick/httputils'
|
4
|
+
|
5
|
+
BASE_DIR = File.dirname(__FILE__)
|
6
|
+
|
7
|
+
class Net::HTTP
|
8
|
+
#def self.new(*args)
|
9
|
+
# obj = allocate
|
10
|
+
# return obj
|
11
|
+
#end
|
12
|
+
|
13
|
+
alias :old_do_start :do_start
|
14
|
+
|
15
|
+
def do_start
|
16
|
+
@started = true
|
17
|
+
end
|
18
|
+
|
19
|
+
SERVLETS = {
|
20
|
+
'/gzip' => GzipServlet,
|
21
|
+
'/form_post' => FormTest,
|
22
|
+
'/form post' => FormTest,
|
23
|
+
'/response_code' => ResponseCodeTest,
|
24
|
+
'/bad_content_type' => BadContentTypeTest,
|
25
|
+
'/content_type_test' => ContentTypeTest,
|
26
|
+
'/referer' => RefererServlet,
|
27
|
+
'/file_upload' => FileUploadTest,
|
28
|
+
'/one_cookie' => OneCookieTest,
|
29
|
+
'/one_cookie_no_space' => OneCookieNoSpacesTest,
|
30
|
+
'/many_cookies' => ManyCookiesTest,
|
31
|
+
'/many_cookies_as_string' => ManyCookiesAsStringTest,
|
32
|
+
'/send_cookies' => SendCookiesTest,
|
33
|
+
}
|
34
|
+
|
35
|
+
PAGE_CACHE = {}
|
36
|
+
|
37
|
+
alias :old_request :request
|
38
|
+
|
39
|
+
def request(request, *data, &block)
|
40
|
+
url = URI.parse(request.path)
|
41
|
+
path = url.path.gsub('%20', ' ')
|
42
|
+
|
43
|
+
path = '/index.html' if path == '/'
|
44
|
+
|
45
|
+
res = Response.new
|
46
|
+
request.query = WEBrick::HTTPUtils.parse_query(url.query)
|
47
|
+
request.cookies = WEBrick::Cookie.parse(request['Cookie'])
|
48
|
+
if SERVLETS[path]
|
49
|
+
if request.method == "POST"
|
50
|
+
if request['Content-Type'] =~ /^multipart\/form-data/
|
51
|
+
request.body = data.first
|
52
|
+
else
|
53
|
+
request.query = WEBrick::HTTPUtils.parse_query(data.first)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
SERVLETS[path].new({}).send("do_#{request.method}", request, res)
|
57
|
+
else
|
58
|
+
filename = "htdocs#{path}"
|
59
|
+
unless PAGE_CACHE[filename]
|
60
|
+
File.open("#{BASE_DIR}/#{filename}", 'rb') { |file|
|
61
|
+
PAGE_CACHE[filename] = file.read
|
62
|
+
}
|
63
|
+
end
|
64
|
+
res.body = PAGE_CACHE[filename]
|
65
|
+
end
|
66
|
+
|
67
|
+
res['Content-Type'] ||= 'text/html'
|
68
|
+
res['Content-Length'] ||= res.body.length.to_s
|
69
|
+
res.code ||= "200"
|
70
|
+
|
71
|
+
res.cookies.each do |cookie|
|
72
|
+
res.add_field('Set-Cookie', cookie.to_s)
|
73
|
+
end
|
74
|
+
yield res
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class Net::HTTPRequest
|
79
|
+
attr_accessor :query, :body, :cookies
|
80
|
+
end
|
81
|
+
|
82
|
+
class Response
|
83
|
+
include Net::HTTPHeader
|
84
|
+
|
85
|
+
attr_reader :code
|
86
|
+
attr_accessor :body, :query, :cookies
|
87
|
+
|
88
|
+
def code=(c)
|
89
|
+
@code = c.to_s
|
90
|
+
end
|
91
|
+
|
92
|
+
alias :status :code
|
93
|
+
alias :status= :code=
|
94
|
+
|
95
|
+
def initialize
|
96
|
+
@header = {}
|
97
|
+
@body = ''
|
98
|
+
@code = nil
|
99
|
+
@query = nil
|
100
|
+
@cookies = []
|
101
|
+
end
|
102
|
+
|
103
|
+
def read_body
|
104
|
+
yield body
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
|
1
109
|
module TestMethods
|
2
110
|
PORT = 2000
|
3
111
|
PROXYPORT = 2001
|
4
112
|
SSLPORT = 2002
|
113
|
+
|
114
|
+
def html_response
|
115
|
+
{ 'content-type' => 'text/html' }
|
116
|
+
end
|
5
117
|
end
|
@@ -7,19 +7,19 @@ require 'stringio'
|
|
7
7
|
class RefererServlet < WEBrick::HTTPServlet::AbstractServlet
|
8
8
|
def do_GET(req, res)
|
9
9
|
res['Content-Type'] = "text/html"
|
10
|
-
res.body = req['Referer']
|
10
|
+
res.body = req['Referer'] || ''
|
11
11
|
end
|
12
12
|
|
13
13
|
def do_POST(req, res)
|
14
14
|
res['Content-Type'] = "text/html"
|
15
|
-
res.body = req['Referer']
|
15
|
+
res.body = req['Referer'] || ''
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
class GzipServlet < WEBrick::HTTPServlet::AbstractServlet
|
20
20
|
def do_GET(req, res)
|
21
21
|
if req['Accept-Encoding'] =~ /gzip/
|
22
|
-
File.open("htdocs/#{req.query['file']}", 'r') do |file|
|
22
|
+
File.open("#{BASE_DIR}/htdocs/#{req.query['file']}", 'r') do |file|
|
23
23
|
string = ""
|
24
24
|
zipped = StringIO.new string, 'w'
|
25
25
|
gz = Zlib::GzipWriter.new(zipped)
|
@@ -62,9 +62,7 @@ class ResponseCodeTest < WEBrick::HTTPServlet::AbstractServlet
|
|
62
62
|
if req.query['code']
|
63
63
|
code = req.query['code'].to_i
|
64
64
|
case code
|
65
|
-
when 301
|
66
|
-
res['Location'] = "/index.html"
|
67
|
-
when 302
|
65
|
+
when 300, 301, 302, 303, 304, 305, 307
|
68
66
|
res['Location'] = "/index.html"
|
69
67
|
end
|
70
68
|
res.status = code
|