corntrace-fakeweb 1.2.9
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +9 -0
- data/CHANGELOG +197 -0
- data/LICENSE.txt +281 -0
- data/README.rdoc +222 -0
- data/Rakefile +70 -0
- data/fakeweb.gemspec +123 -0
- data/lib/fake_web.rb +179 -0
- data/lib/fake_web/VERSION +1 -0
- data/lib/fake_web/ext/net_http.rb +81 -0
- data/lib/fake_web/registry.rb +114 -0
- data/lib/fake_web/responder.rb +120 -0
- data/lib/fake_web/response.rb +10 -0
- data/lib/fake_web/stub_socket.rb +15 -0
- data/lib/fake_web/utility.rb +65 -0
- data/lib/fakeweb.rb +2 -0
- data/test/fixtures/google_response_from_curl +12 -0
- data/test/fixtures/google_response_with_transfer_encoding +17 -0
- data/test/fixtures/google_response_without_transfer_encoding +11 -0
- data/test/fixtures/test_example.txt +1 -0
- data/test/fixtures/test_txt_file +3 -0
- data/test/test_allow_net_connect.rb +85 -0
- data/test/test_deprecations.rb +54 -0
- data/test/test_fake_authentication.rb +92 -0
- data/test/test_fake_web.rb +553 -0
- data/test/test_fake_web_open_uri.rb +58 -0
- data/test/test_helper.rb +87 -0
- data/test/test_missing_open_uri.rb +25 -0
- data/test/test_missing_pathname.rb +37 -0
- data/test/test_other_net_http_libraries.rb +36 -0
- data/test/test_precedence.rb +79 -0
- data/test/test_query_string.rb +45 -0
- data/test/test_regexes.rb +157 -0
- data/test/test_response_headers.rb +73 -0
- data/test/test_trailing_slashes.rb +53 -0
- data/test/test_utility.rb +76 -0
- data/test/vendor/right_http_connection-1.2.4/History.txt +59 -0
- data/test/vendor/right_http_connection-1.2.4/Manifest.txt +7 -0
- data/test/vendor/right_http_connection-1.2.4/README.txt +54 -0
- data/test/vendor/right_http_connection-1.2.4/Rakefile +103 -0
- data/test/vendor/right_http_connection-1.2.4/lib/net_fix.rb +160 -0
- data/test/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb +435 -0
- data/test/vendor/right_http_connection-1.2.4/setup.rb +1585 -0
- data/test/vendor/samuel-0.2.1/.document +5 -0
- data/test/vendor/samuel-0.2.1/.gitignore +5 -0
- data/test/vendor/samuel-0.2.1/LICENSE +20 -0
- data/test/vendor/samuel-0.2.1/README.rdoc +70 -0
- data/test/vendor/samuel-0.2.1/Rakefile +62 -0
- data/test/vendor/samuel-0.2.1/VERSION +1 -0
- data/test/vendor/samuel-0.2.1/lib/samuel.rb +52 -0
- data/test/vendor/samuel-0.2.1/lib/samuel/net_http.rb +10 -0
- data/test/vendor/samuel-0.2.1/lib/samuel/request.rb +96 -0
- data/test/vendor/samuel-0.2.1/samuel.gemspec +69 -0
- data/test/vendor/samuel-0.2.1/test/request_test.rb +193 -0
- data/test/vendor/samuel-0.2.1/test/samuel_test.rb +42 -0
- data/test/vendor/samuel-0.2.1/test/test_helper.rb +66 -0
- data/test/vendor/samuel-0.2.1/test/thread_test.rb +32 -0
- metadata +167 -0
@@ -0,0 +1,11 @@
|
|
1
|
+
HTTP/1.0 200 OK
|
2
|
+
Cache-Control: private, max-age=0
|
3
|
+
Date: Sun, 01 Feb 2009 01:55:33 GMT
|
4
|
+
Expires: -1
|
5
|
+
Content-Type: text/html; charset=ISO-8859-1
|
6
|
+
Set-Cookie: PREF=ID=3c140c3eb4c4f516:TM=1233453333:LM=1233453333:S=OH7sElk2hOWkb9ot; expires=Tue, 01-Feb-2011 01:55:33 GMT; path=/; domain=.google.com
|
7
|
+
Server: gws
|
8
|
+
|
9
|
+
<html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"><title>Google</title><script>var _gjwl=location;function _gjuc(){var a=_gjwl.hash;if(a.indexOf("&q=")>0||a.indexOf("#q=")>=0){a=a.substring(1);if(a.indexOf("#")==-1){for(var c=0;c<a.length;){var d=c;if(a.charAt(d)=="&")++d;var b=a.indexOf("&",d);if(b==-1)b=a.length;var e=a.substring(d,b);if(e.indexOf("fp=")==0){a=a.substring(0,c)+a.substring(b,a.length);b=c}else if(e=="cad=h")return 0;c=b}_gjwl.href="search?"+a+"&cad=h";return 1}}return 0};
|
10
|
+
window._gjuc && location.hash && _gjuc();</script><style>body,td,a,p,.h{font-family:arial,sans-serif}.h{color:#36c;font-size:20px}.q{color:#00c}.ts td{padding:0}.ts{border-collapse:collapse}#gbar{height:22px;padding-left:2px}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}#gbi,#gbs{background:#fff;left:0;position:absolute;top:24px;visibility:hidden;z-index:1000}#gbi{border:1px solid;border-color:#c9d7f1 #36c #36c #a2bae7;z-index:1001}#guser{padding-bottom:7px !important}#gbar,#guser{font-size:13px;padding-top:1px !important}@media all{.gb1,.gb3{height:22px;margin-right:.73em;vertical-align:top}#gbar{float:left}}.gb2{display:block;padding:.2em .5em}a.gb1,a.gb2,a.gb3{color:#00c !important}.gb2,.gb3{text-decoration:none}a.gb2:hover{background:#36c;color:#fff !important}</style><script>window.google={kEI:"FQGFSY2rG5eSswOKpsHeDQ",kEXPI:"17259",kHL:"en"};
|
11
|
+
google.y={};google.x=function(e,g){google.y[e.id]=[e,g];return false};window.gbar={};(function(){var b=window.gbar,f,h;b.qs=function(a){var c=window.encodeURIComponent&&(document.forms[0].q||"").value;if(c)a.href=a.href.replace(/([?&])q=[^&]*|$/,function(i,g){return(g||"&")+"q="+encodeURIComponent(c)})};function j(a,c){a.visibility=h?"hidden":"visible";a.left=c+"px"}b.tg=function(a){a=a||window.event;var c=0,i,g=window.navExtra,d=document.getElementById("gbi"),e=a.target||a.srcElement;a.cancelBubble=true;if(!f){f=document.createElement(Array.every||window.createPopup?"iframe":"div");f.frameBorder="0";f.src="#";d.parentNode.appendChild(f).id="gbs";if(g)for(i in g)d.insertBefore(g[i],d.firstChild).className="gb2";document.onclick=b.close}if(e.className!="gb3")e=e.parentNode;do c+=e.offsetLeft;while(e=e.offsetParent);j(d.style,c);f.style.width=d.offsetWidth+"px";f.style.height=d.offsetHeight+"px";j(f.style,c);h=!h};b.close=function(a){h&&b.tg(a)}})();</script></head><body bgcolor=#ffffff text=#000000 link=#0000cc vlink=#551a8b alink=#ff0000 onload="document.f.q.focus();if(document.images)new Image().src='/images/nav_logo3.png'" topmargin=3 marginheight=3><div id=gbar><nobr><b class=gb1>Web</b> <a href="http://images.google.com/imghp?hl=en&tab=wi" onclick=gbar.qs(this) class=gb1>Images</a> <a href="http://maps.google.com/maps?hl=en&tab=wl" onclick=gbar.qs(this) class=gb1>Maps</a> <a href="http://news.google.com/nwshp?hl=en&tab=wn" onclick=gbar.qs(this) class=gb1>News</a> <a href="http://www.google.com/prdhp?hl=en&tab=wf" onclick=gbar.qs(this) class=gb1>Shopping</a> <a href="http://mail.google.com/mail/?hl=en&tab=wm" class=gb1>Gmail</a> <a href="http://www.google.com/intl/en/options/" onclick="this.blur();gbar.tg(event);return !1" class=gb3><u>more</u> <small>▼</small></a><div id=gbi> <a href="http://video.google.com/?hl=en&tab=wv" onclick=gbar.qs(this) class=gb2>Video</a> <a href="http://groups.google.com/grphp?hl=en&tab=wg" onclick=gbar.qs(this) class=gb2>Groups</a> <a href="http://books.google.com/bkshp?hl=en&tab=wp" onclick=gbar.qs(this) class=gb2>Books</a> <a href="http://scholar.google.com/schhp?hl=en&tab=ws" onclick=gbar.qs(this) class=gb2>Scholar</a> <a href="http://finance.google.com/finance?hl=en&tab=we" onclick=gbar.qs(this) class=gb2>Finance</a> <a href="http://blogsearch.google.com/?hl=en&tab=wb" onclick=gbar.qs(this) class=gb2>Blogs</a> <div class=gb2><div class=gbd></div></div> <a href="http://www.youtube.com/?hl=en&tab=w1" onclick=gbar.qs(this) class=gb2>YouTube</a> <a href="http://www.google.com/calendar/render?hl=en&tab=wc" class=gb2>Calendar</a> <a href="http://picasaweb.google.com/home?hl=en&tab=wq" onclick=gbar.qs(this) class=gb2>Photos</a> <a href="http://docs.google.com/?hl=en&tab=wo" class=gb2>Documents</a> <a href="http://www.google.com/reader/view/?hl=en&tab=wy" class=gb2>Reader</a> <a href="http://sites.google.com/?hl=en&tab=w3" class=gb2>Sites</a> <div class=gb2><div class=gbd></div></div> <a href="http://www.google.com/intl/en/options/" class=gb2>even more »</a></div> </nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div><div align=right id=guser style="font-size:84%;padding:0 0 4px" width=100%><nobr><a href="/url?sa=p&pref=ig&pval=3&q=http://www.google.com/ig%3Fhl%3Den%26source%3Diglk&usg=AFQjCNFA18XPfgb7dKnXfKz7x7g1GDH1tg">iGoogle</a> | <a href="https://www.google.com/accounts/Login?continue=http://www.google.com/&hl=en">Sign in</a></nobr></div><center><br clear=all id=lgpd><img alt="Google" height=110 src="/intl/en_ALL/images/logo.gif" width=276><br><br><form action="/search" name=f><table cellpadding=0 cellspacing=0><tr valign=top><td width=25%> </td><td align=center nowrap><input name=hl type=hidden value=en><input type=hidden name=ie value="ISO-8859-1"><input autocomplete="off" maxlength=2048 name=q size=55 title="Google Search" value=""><br><input name=btnG type=submit value="Google Search"><input name=btnI type=submit value="I'm Feeling Lucky"></td><td nowrap width=25%><font size=-2> <a href=/advanced_search?hl=en>Advanced Search</a><br> <a href=/preferences?hl=en>Preferences</a><br> <a href=/language_tools?hl=en>Language Tools</a></font></td></tr></table></form><br><font size=-1>Share what you know. <a href="/aclk?sa=L&ai=ClyBp_v-EScTWD4W2tQOxoqSkB_v77nqP3YC9CsHZnNkTEAEgwVRQypDftPn_____AWDJBqoECU_QphTjHaZ5QA&num=1&sig=AGiWqtwtBqZ-zra3DJd_1chQKhKGf7lMVg&q=http://knol.google.com">Write a Knol</a>.</font><br><br><br><font size=-1><a href="/intl/en/ads/">Advertising Programs</a> - <a href="/services/">Business Solutions</a> - <a href="/intl/en/about.html">About Google</a></font><p><font size=-2>©2009 - <a href="/intl/en/privacy.html">Privacy</a></font></p></center></body><script>if(google.y)google.y.first=[];window.setTimeout(function(){var xjs=document.createElement('script');xjs.src='/extern_js/f/CgJlbhICdXMgACswCjgNLCswDjgELCswGDgDLA/oTKXc0xdkmY.js';document.getElementsByTagName('head')[0].appendChild(xjs)},0);google.y.first.push(function(){google.ac.i(document.f,document.f.q,'','')})</script><script>function _gjp() {!(location.hash && _gjuc()) && setTimeout(_gjp, 500);}window._gjuc && _gjp();</script></html>
|
@@ -0,0 +1 @@
|
|
1
|
+
test example content
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestFakeWebAllowNetConnect < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_true
|
6
|
+
FakeWeb.allow_net_connect = true
|
7
|
+
setup_expectations_for_real_apple_hot_news_request
|
8
|
+
Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_raises_for_unregistered_requests_when_allow_net_connect_is_false
|
12
|
+
FakeWeb.allow_net_connect = false
|
13
|
+
exception = assert_raise FakeWeb::NetConnectNotAllowedError do
|
14
|
+
Net::HTTP.get(URI.parse("http://example.com/"))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_exception_message_includes_unregistered_request_method_and_uri_but_no_default_port
|
19
|
+
FakeWeb.allow_net_connect = false
|
20
|
+
exception = assert_raise FakeWeb::NetConnectNotAllowedError do
|
21
|
+
Net::HTTP.get(URI.parse("http://example.com/"))
|
22
|
+
end
|
23
|
+
assert exception.message.include?("GET http://example.com/")
|
24
|
+
|
25
|
+
exception = assert_raise FakeWeb::NetConnectNotAllowedError do
|
26
|
+
http = Net::HTTP.new("example.com", 443)
|
27
|
+
http.use_ssl = true
|
28
|
+
http.get("/")
|
29
|
+
end
|
30
|
+
assert exception.message.include?("GET https://example.com/")
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_exception_message_includes_unregistered_request_port_when_not_default
|
34
|
+
FakeWeb.allow_net_connect = false
|
35
|
+
exception = assert_raise FakeWeb::NetConnectNotAllowedError do
|
36
|
+
Net::HTTP.start("example.com", 8000) { |http| http.get("/") }
|
37
|
+
end
|
38
|
+
assert exception.message.include?("GET http://example.com:8000/")
|
39
|
+
|
40
|
+
exception = assert_raise FakeWeb::NetConnectNotAllowedError do
|
41
|
+
http = Net::HTTP.new("example.com", 4433)
|
42
|
+
http.use_ssl = true
|
43
|
+
http.get("/")
|
44
|
+
end
|
45
|
+
assert exception.message.include?("GET https://example.com:4433/")
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_exception_message_includes_unregistered_request_port_when_not_default_with_path
|
49
|
+
FakeWeb.allow_net_connect = false
|
50
|
+
exception = assert_raise FakeWeb::NetConnectNotAllowedError do
|
51
|
+
Net::HTTP.start("example.com", 8000) { |http| http.get("/test") }
|
52
|
+
end
|
53
|
+
assert exception.message.include?("GET http://example.com:8000/test")
|
54
|
+
|
55
|
+
exception = assert_raise FakeWeb::NetConnectNotAllowedError do
|
56
|
+
http = Net::HTTP.new("example.com", 4433)
|
57
|
+
http.use_ssl = true
|
58
|
+
http.get("/test")
|
59
|
+
end
|
60
|
+
assert exception.message.include?("GET https://example.com:4433/test")
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_question_mark_method_returns_true_after_setting_allow_net_connect_to_true
|
64
|
+
FakeWeb.allow_net_connect = true
|
65
|
+
assert FakeWeb.allow_net_connect?
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_question_mark_method_returns_false_after_setting_allow_net_connect_to_false
|
69
|
+
FakeWeb.allow_net_connect = false
|
70
|
+
assert !FakeWeb.allow_net_connect?
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
class TestFakeWebAllowNetConnectWithCleanState < Test::Unit::TestCase
|
77
|
+
# Our test_helper.rb sets allow_net_connect = false in an inherited #setup
|
78
|
+
# method. Disable that here to test the default setting.
|
79
|
+
def setup; end
|
80
|
+
def teardown; end
|
81
|
+
|
82
|
+
def test_allow_net_connect_is_true_by_default
|
83
|
+
assert FakeWeb.allow_net_connect?
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestDeprecations < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_register_uri_without_method_argument_prints_deprecation_warning
|
6
|
+
warning = capture_stderr do
|
7
|
+
FakeWeb.register_uri("http://example.com", :body => "test")
|
8
|
+
end
|
9
|
+
assert_match %r(deprecation warning: fakeweb)i, warning
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_registered_uri_without_method_argument_prints_deprecation_warning
|
13
|
+
warning = capture_stderr do
|
14
|
+
FakeWeb.registered_uri?("http://example.com")
|
15
|
+
end
|
16
|
+
assert_match %r(deprecation warning: fakeweb)i, warning
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_response_for_without_method_argument_prints_deprecation_warning
|
20
|
+
warning = capture_stderr do
|
21
|
+
FakeWeb.response_for("http://example.com")
|
22
|
+
end
|
23
|
+
assert_match %r(deprecation warning: fakeweb)i, warning
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_register_uri_without_method_argument_prints_deprecation_warning_with_correct_caller
|
27
|
+
warning = capture_stderr do
|
28
|
+
FakeWeb.register_uri("http://example.com", :body => "test")
|
29
|
+
end
|
30
|
+
assert_match %r(Called at.*?test_deprecations\.rb)i, warning
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_register_uri_with_string_option_prints_deprecation_warning
|
34
|
+
warning = capture_stderr do
|
35
|
+
FakeWeb.register_uri(:get, "http://example.com", :string => "test")
|
36
|
+
end
|
37
|
+
assert_match %r(deprecation warning: fakeweb's :string option)i, warning
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_register_uri_with_file_option_prints_deprecation_warning
|
41
|
+
warning = capture_stderr do
|
42
|
+
FakeWeb.register_uri(:get, "http://example.com", :file => fixture_path("test_example.txt"))
|
43
|
+
end
|
44
|
+
assert_match %r(deprecation warning: fakeweb's :file option)i, warning
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_register_uri_with_string_option_prints_deprecation_warning_with_correct_caller
|
48
|
+
warning = capture_stderr do
|
49
|
+
FakeWeb.register_uri(:get, "http://example.com", :string => "test")
|
50
|
+
end
|
51
|
+
assert_match %r(Called at.*?test_deprecations\.rb)i, warning
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestFakeAuthentication < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_register_uri_with_authentication
|
6
|
+
FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :body => "example")
|
7
|
+
assert FakeWeb.registered_uri?(:get, 'http://user:pass@mock/test_example.txt')
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_register_uri_with_authentication_doesnt_trigger_without
|
11
|
+
FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :body => "example")
|
12
|
+
assert !FakeWeb.registered_uri?(:get, 'http://mock/test_example.txt')
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_register_uri_with_authentication_doesnt_trigger_with_incorrect_credentials
|
16
|
+
FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :body => "example")
|
17
|
+
assert !FakeWeb.registered_uri?(:get, 'http://user:wrong@mock/test_example.txt')
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_unauthenticated_request
|
21
|
+
FakeWeb.register_uri(:get, 'http://mock/auth.txt', :body => 'unauthorized')
|
22
|
+
http = Net::HTTP.new('mock', 80)
|
23
|
+
req = Net::HTTP::Get.new('/auth.txt')
|
24
|
+
assert_equal 'unauthorized', http.request(req).body
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_authenticated_request
|
28
|
+
FakeWeb.register_uri(:get, 'http://user:pass@mock/auth.txt', :body => 'authorized')
|
29
|
+
http = Net::HTTP.new('mock',80)
|
30
|
+
req = Net::HTTP::Get.new('/auth.txt')
|
31
|
+
req.basic_auth 'user', 'pass'
|
32
|
+
assert_equal 'authorized', http.request(req).body
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_authenticated_request_where_only_userinfo_differs
|
36
|
+
FakeWeb.register_uri(:get, 'http://user:pass@mock/auth.txt', :body => 'first user')
|
37
|
+
FakeWeb.register_uri(:get, 'http://user2:pass@mock/auth.txt', :body => 'second user')
|
38
|
+
http = Net::HTTP.new('mock')
|
39
|
+
req = Net::HTTP::Get.new('/auth.txt')
|
40
|
+
req.basic_auth 'user2', 'pass'
|
41
|
+
assert_equal 'second user', http.request(req).body
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_basic_auth_support_is_transparent_to_oauth
|
45
|
+
FakeWeb.register_uri(:get, "http://sp.example.com/protected", :body => "secret")
|
46
|
+
|
47
|
+
# from http://oauth.net/core/1.0/#auth_header
|
48
|
+
auth_header = <<-HEADER
|
49
|
+
OAuth realm="http://sp.example.com/",
|
50
|
+
oauth_consumer_key="0685bd9184jfhq22",
|
51
|
+
oauth_token="ad180jjd733klru7",
|
52
|
+
oauth_signature_method="HMAC-SHA1",
|
53
|
+
oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",
|
54
|
+
oauth_timestamp="137131200",
|
55
|
+
oauth_nonce="4572616e48616d6d65724c61686176",
|
56
|
+
oauth_version="1.0"
|
57
|
+
HEADER
|
58
|
+
auth_header.gsub!(/\s+/, " ").strip!
|
59
|
+
|
60
|
+
http = Net::HTTP.new("sp.example.com", 80)
|
61
|
+
response = nil
|
62
|
+
http.start do |request|
|
63
|
+
response = request.get("/protected", {"authorization" => auth_header})
|
64
|
+
end
|
65
|
+
assert_equal "secret", response.body
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_basic_auth_when_userinfo_contains_allowed_unencoded_characters
|
69
|
+
FakeWeb.register_uri(:get, "http://roses&hel1o,(+$):so;longs=@example.com", :body => "authorized")
|
70
|
+
http = Net::HTTP.new("example.com")
|
71
|
+
request = Net::HTTP::Get.new("/")
|
72
|
+
request.basic_auth("roses&hel1o,(+$)", "so;longs=")
|
73
|
+
assert_equal "authorized", http.request(request).body
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_basic_auth_when_userinfo_contains_encoded_at_sign
|
77
|
+
FakeWeb.register_uri(:get, "http://user%40example.com:secret@example.com", :body => "authorized")
|
78
|
+
http = Net::HTTP.new("example.com")
|
79
|
+
request = Net::HTTP::Get.new("/")
|
80
|
+
request.basic_auth("user@example.com", "secret")
|
81
|
+
assert_equal "authorized", http.request(request).body
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_basic_auth_when_userinfo_contains_allowed_encoded_characters
|
85
|
+
FakeWeb.register_uri(:get, "http://us%20er:sec%20%2F%2Fret%3F@example.com", :body => "authorized")
|
86
|
+
http = Net::HTTP.new("example.com")
|
87
|
+
request = Net::HTTP::Get.new("/")
|
88
|
+
request.basic_auth("us er", "sec //ret?")
|
89
|
+
assert_equal "authorized", http.request(request).body
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
@@ -0,0 +1,553 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestFakeWeb < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_register_uri
|
6
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => "example")
|
7
|
+
assert FakeWeb.registered_uri?(:get, 'http://mock/test_example.txt')
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_register_uri_with_wrong_number_of_arguments
|
11
|
+
assert_raises ArgumentError do
|
12
|
+
FakeWeb.register_uri("http://example.com")
|
13
|
+
end
|
14
|
+
assert_raises ArgumentError do
|
15
|
+
FakeWeb.register_uri(:get, "http://example.com", "/example", :body => "example")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_registered_uri_with_wrong_number_of_arguments
|
20
|
+
assert_raises ArgumentError do
|
21
|
+
FakeWeb.registered_uri?
|
22
|
+
end
|
23
|
+
assert_raises ArgumentError do
|
24
|
+
FakeWeb.registered_uri?(:get, "http://example.com", "/example")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_response_for_with_wrong_number_of_arguments
|
29
|
+
assert_raises ArgumentError do
|
30
|
+
FakeWeb.response_for
|
31
|
+
end
|
32
|
+
assert_raises ArgumentError do
|
33
|
+
FakeWeb.response_for(:get, "http://example.com", "/example")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_register_uri_without_domain_name
|
38
|
+
assert_raises URI::InvalidURIError do
|
39
|
+
FakeWeb.register_uri(:get, 'test_example2.txt', fixture_path("test_example.txt"))
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_register_uri_with_port_and_check_with_port
|
44
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'foo')
|
45
|
+
assert FakeWeb.registered_uri?(:get, 'http://example.com:3000/')
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_register_uri_with_port_and_check_without_port
|
49
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'foo')
|
50
|
+
assert !FakeWeb.registered_uri?(:get, 'http://example.com/')
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_register_uri_with_default_port_for_http_and_check_without_port
|
54
|
+
FakeWeb.register_uri(:get, 'http://example.com:80/', :body => 'foo')
|
55
|
+
assert FakeWeb.registered_uri?(:get, 'http://example.com/')
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_register_uri_with_default_port_for_https_and_check_without_port
|
59
|
+
FakeWeb.register_uri(:get, 'https://example.com:443/', :body => 'foo')
|
60
|
+
assert FakeWeb.registered_uri?(:get, 'https://example.com/')
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_register_uri_with_no_port_for_http_and_check_with_default_port
|
64
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
|
65
|
+
assert FakeWeb.registered_uri?(:get, 'http://example.com:80/')
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_register_uri_with_no_port_for_https_and_check_with_default_port
|
69
|
+
FakeWeb.register_uri(:get, 'https://example.com/', :body => 'foo')
|
70
|
+
assert FakeWeb.registered_uri?(:get, 'https://example.com:443/')
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_register_uri_with_no_port_for_https_and_check_with_443_on_http
|
74
|
+
FakeWeb.register_uri(:get, 'https://example.com/', :body => 'foo')
|
75
|
+
assert !FakeWeb.registered_uri?(:get, 'http://example.com:443/')
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_register_uri_with_no_port_for_http_and_check_with_80_on_https
|
79
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
|
80
|
+
assert !FakeWeb.registered_uri?(:get, 'https://example.com:80/')
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_register_uri_for_any_method_explicitly
|
84
|
+
FakeWeb.register_uri(:any, "http://example.com/rpc_endpoint", :body => "OK")
|
85
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com/rpc_endpoint")
|
86
|
+
assert FakeWeb.registered_uri?(:post, "http://example.com/rpc_endpoint")
|
87
|
+
assert FakeWeb.registered_uri?(:put, "http://example.com/rpc_endpoint")
|
88
|
+
assert FakeWeb.registered_uri?(:delete, "http://example.com/rpc_endpoint")
|
89
|
+
assert FakeWeb.registered_uri?(:any, "http://example.com/rpc_endpoint")
|
90
|
+
capture_stderr do # silence deprecation warning
|
91
|
+
assert FakeWeb.registered_uri?("http://example.com/rpc_endpoint")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_register_uri_for_get_method_only
|
96
|
+
FakeWeb.register_uri(:get, "http://example.com/users", :body => "User list")
|
97
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com/users")
|
98
|
+
assert !FakeWeb.registered_uri?(:post, "http://example.com/users")
|
99
|
+
assert !FakeWeb.registered_uri?(:put, "http://example.com/users")
|
100
|
+
assert !FakeWeb.registered_uri?(:delete, "http://example.com/users")
|
101
|
+
assert !FakeWeb.registered_uri?(:any, "http://example.com/users")
|
102
|
+
capture_stderr do # silence deprecation warning
|
103
|
+
assert !FakeWeb.registered_uri?("http://example.com/users")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_clean_registry_affects_registered_uri
|
108
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => "registered")
|
109
|
+
assert FakeWeb.registered_uri?(:get, "http://example.com")
|
110
|
+
FakeWeb.clean_registry
|
111
|
+
assert !FakeWeb.registered_uri?(:get, "http://example.com")
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_clean_registry_affects_net_http_requests
|
115
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => "registered")
|
116
|
+
response = Net::HTTP.start("example.com") { |query| query.get("/") }
|
117
|
+
assert_equal "registered", response.body
|
118
|
+
FakeWeb.clean_registry
|
119
|
+
assert_raise FakeWeb::NetConnectNotAllowedError do
|
120
|
+
Net::HTTP.start("example.com") { |query| query.get("/") }
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_response_for_with_registered_uri
|
125
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
126
|
+
assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_response_for_with_unknown_uri
|
130
|
+
assert_nil FakeWeb.response_for(:get, 'http://example.com/')
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_response_for_with_put_method
|
134
|
+
FakeWeb.register_uri(:put, "http://example.com", :body => "response")
|
135
|
+
assert_equal 'response', FakeWeb.response_for(:put, "http://example.com").body
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_response_for_with_any_method_explicitly
|
139
|
+
FakeWeb.register_uri(:any, "http://example.com", :body => "response")
|
140
|
+
assert_equal 'response', FakeWeb.response_for(:get, "http://example.com").body
|
141
|
+
assert_equal 'response', FakeWeb.response_for(:any, "http://example.com").body
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_content_for_registered_uri_with_port_and_request_with_port
|
145
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'test example content')
|
146
|
+
response = Net::HTTP.start('example.com', 3000) { |http| http.get('/') }
|
147
|
+
assert_equal 'test example content', response.body
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_content_for_registered_uri_with_default_port_for_http_and_request_without_port
|
151
|
+
FakeWeb.register_uri(:get, 'http://example.com:80/', :body => 'test example content')
|
152
|
+
response = Net::HTTP.start('example.com') { |http| http.get('/') }
|
153
|
+
assert_equal 'test example content', response.body
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_content_for_registered_uri_with_no_port_for_http_and_request_with_default_port
|
157
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'test example content')
|
158
|
+
response = Net::HTTP.start('example.com', 80) { |http| http.get('/') }
|
159
|
+
assert_equal 'test example content', response.body
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_content_for_registered_uri_with_default_port_for_https_and_request_with_default_port
|
163
|
+
FakeWeb.register_uri(:get, 'https://example.com:443/', :body => 'test example content')
|
164
|
+
http = Net::HTTP.new('example.com', 443)
|
165
|
+
http.use_ssl = true
|
166
|
+
response = http.get('/')
|
167
|
+
assert_equal 'test example content', response.body
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_content_for_registered_uri_with_no_port_for_https_and_request_with_default_port
|
171
|
+
FakeWeb.register_uri(:get, 'https://example.com/', :body => 'test example content')
|
172
|
+
http = Net::HTTP.new('example.com', 443)
|
173
|
+
http.use_ssl = true
|
174
|
+
response = http.get('/')
|
175
|
+
assert_equal 'test example content', response.body
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_content_for_registered_uris_with_ports_on_same_domain_and_request_without_port
|
179
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'port 3000')
|
180
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'port 80')
|
181
|
+
response = Net::HTTP.start('example.com') { |http| http.get('/') }
|
182
|
+
assert_equal 'port 80', response.body
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_content_for_registered_uris_with_ports_on_same_domain_and_request_with_port
|
186
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'port 3000')
|
187
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'port 80')
|
188
|
+
response = Net::HTTP.start('example.com', 3000) { |http| http.get('/') }
|
189
|
+
assert_equal 'port 3000', response.body
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_content_for_registered_uri_with_get_method_only
|
193
|
+
FakeWeb.allow_net_connect = false
|
194
|
+
FakeWeb.register_uri(:get, "http://example.com/", :body => "test example content")
|
195
|
+
http = Net::HTTP.new('example.com')
|
196
|
+
assert_equal 'test example content', http.get('/').body
|
197
|
+
assert_raises(FakeWeb::NetConnectNotAllowedError) { http.post('/', nil) }
|
198
|
+
assert_raises(FakeWeb::NetConnectNotAllowedError) { http.put('/', nil) }
|
199
|
+
assert_raises(FakeWeb::NetConnectNotAllowedError) { http.delete('/') }
|
200
|
+
end
|
201
|
+
|
202
|
+
def test_content_for_registered_uri_with_any_method_explicitly
|
203
|
+
FakeWeb.allow_net_connect = false
|
204
|
+
FakeWeb.register_uri(:any, "http://example.com/", :body => "test example content")
|
205
|
+
http = Net::HTTP.new('example.com')
|
206
|
+
assert_equal 'test example content', http.get('/').body
|
207
|
+
assert_equal 'test example content', http.post('/', nil).body
|
208
|
+
assert_equal 'test example content', http.put('/', nil).body
|
209
|
+
assert_equal 'test example content', http.delete('/').body
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_content_for_registered_uri_with_any_method_implicitly
|
213
|
+
FakeWeb.allow_net_connect = false
|
214
|
+
capture_stderr do # silence deprecation warning
|
215
|
+
FakeWeb.register_uri("http://example.com/", :body => "test example content")
|
216
|
+
end
|
217
|
+
|
218
|
+
http = Net::HTTP.new('example.com')
|
219
|
+
assert_equal 'test example content', http.get('/').body
|
220
|
+
assert_equal 'test example content', http.post('/', nil).body
|
221
|
+
assert_equal 'test example content', http.put('/', nil).body
|
222
|
+
assert_equal 'test example content', http.delete('/').body
|
223
|
+
end
|
224
|
+
|
225
|
+
def test_mock_request_with_block
|
226
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
227
|
+
response = Net::HTTP.start('mock') { |http| http.get('/test_example.txt') }
|
228
|
+
assert_equal 'test example content', response.body
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_request_with_registered_body_yields_the_response_body_to_a_request_block
|
232
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => "content")
|
233
|
+
body = nil
|
234
|
+
Net::HTTP.start("example.com") do |http|
|
235
|
+
http.get("/") do |response_body|
|
236
|
+
body = response_body
|
237
|
+
end
|
238
|
+
end
|
239
|
+
assert_equal "content", body
|
240
|
+
end
|
241
|
+
|
242
|
+
def test_request_with_registered_response_yields_the_response_body_to_a_request_block
|
243
|
+
fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
|
244
|
+
fake_response.instance_variable_set(:@body, "content")
|
245
|
+
FakeWeb.register_uri(:get, 'http://example.com', :response => fake_response)
|
246
|
+
body = nil
|
247
|
+
Net::HTTP.start("example.com") do |http|
|
248
|
+
http.get("/") do |response_body|
|
249
|
+
body = response_body
|
250
|
+
end
|
251
|
+
end
|
252
|
+
assert_equal "content", body
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_mock_request_with_undocumented_full_uri_argument_style
|
256
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
257
|
+
response = Net::HTTP.start('mock') { |query| query.get('http://mock/test_example.txt') }
|
258
|
+
assert_equal 'test example content', response.body
|
259
|
+
end
|
260
|
+
|
261
|
+
def test_mock_request_with_undocumented_full_uri_argument_style_and_query
|
262
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt?a=b', :body => 'test query content')
|
263
|
+
response = Net::HTTP.start('mock') { |query| query.get('http://mock/test_example.txt?a=b') }
|
264
|
+
assert_equal 'test query content', response.body
|
265
|
+
end
|
266
|
+
|
267
|
+
def test_mock_post
|
268
|
+
FakeWeb.register_uri(:post, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
269
|
+
response = Net::HTTP.start('mock') { |query| query.post('/test_example.txt', '') }
|
270
|
+
assert_equal 'test example content', response.body
|
271
|
+
end
|
272
|
+
|
273
|
+
def test_mock_post_with_string_as_registered_uri
|
274
|
+
FakeWeb.register_uri(:post, 'http://mock/test_string.txt', :body => 'foo')
|
275
|
+
response = Net::HTTP.start('mock') { |query| query.post('/test_string.txt', '') }
|
276
|
+
assert_equal 'foo', response.body
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_mock_get_with_request_as_registered_uri
|
280
|
+
fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
|
281
|
+
FakeWeb.register_uri(:get, 'http://mock/test_response', :response => fake_response)
|
282
|
+
response = Net::HTTP.start('mock') { |query| query.get('/test_response') }
|
283
|
+
assert_equal fake_response, response
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_mock_get_with_request_from_file_as_registered_uri
|
287
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => fixture_path("google_response_without_transfer_encoding"))
|
288
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
289
|
+
assert_equal '200', response.code
|
290
|
+
assert response.body.include?('<title>Google</title>')
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_mock_post_with_request_from_file_as_registered_uri
|
294
|
+
FakeWeb.register_uri(:post, 'http://www.google.com/', :response => fixture_path("google_response_without_transfer_encoding"))
|
295
|
+
response = Net::HTTP.start('www.google.com') { |query| query.post('/', '') }
|
296
|
+
assert_equal "200", response.code
|
297
|
+
assert response.body.include?('<title>Google</title>')
|
298
|
+
end
|
299
|
+
|
300
|
+
def test_proxy_request
|
301
|
+
FakeWeb.register_uri(:get, 'http://www.example.com/', :body => "hello world")
|
302
|
+
FakeWeb.register_uri(:get, 'http://your.proxy.host/', :body => "lala")
|
303
|
+
|
304
|
+
response = nil
|
305
|
+
Net::HTTP::Proxy('your.proxy.host', 8080).start('www.example.com') do |http|
|
306
|
+
response = http.get('/')
|
307
|
+
end
|
308
|
+
assert_equal "hello world", response.body
|
309
|
+
end
|
310
|
+
|
311
|
+
def test_https_request
|
312
|
+
FakeWeb.register_uri(:get, 'https://www.example.com/', :body => "Hello World")
|
313
|
+
http = Net::HTTP.new('www.example.com', 443)
|
314
|
+
http.use_ssl = true
|
315
|
+
response = http.get('/')
|
316
|
+
assert_equal "Hello World", response.body
|
317
|
+
end
|
318
|
+
|
319
|
+
def test_register_unimplemented_response
|
320
|
+
FakeWeb.register_uri(:get, 'http://mock/unimplemented', :response => 1)
|
321
|
+
assert_raises StandardError do
|
322
|
+
Net::HTTP.start('mock') { |q| q.get('/unimplemented') }
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
def test_real_http_request
|
327
|
+
FakeWeb.allow_net_connect = true
|
328
|
+
setup_expectations_for_real_apple_hot_news_request
|
329
|
+
|
330
|
+
resp = nil
|
331
|
+
Net::HTTP.start('images.apple.com') do |query|
|
332
|
+
resp = query.get('/main/rss/hotnews/hotnews.rss')
|
333
|
+
end
|
334
|
+
assert resp.body.include?('Apple')
|
335
|
+
assert resp.body.include?('News')
|
336
|
+
end
|
337
|
+
|
338
|
+
def test_real_http_request_with_undocumented_full_uri_argument_style
|
339
|
+
FakeWeb.allow_net_connect = true
|
340
|
+
setup_expectations_for_real_apple_hot_news_request(:path => 'http://images.apple.com/main/rss/hotnews/hotnews.rss')
|
341
|
+
|
342
|
+
resp = nil
|
343
|
+
Net::HTTP.start('images.apple.com') do |query|
|
344
|
+
resp = query.get('http://images.apple.com/main/rss/hotnews/hotnews.rss')
|
345
|
+
end
|
346
|
+
assert resp.body.include?('Apple')
|
347
|
+
assert resp.body.include?('News')
|
348
|
+
end
|
349
|
+
|
350
|
+
def test_real_https_request
|
351
|
+
FakeWeb.allow_net_connect = true
|
352
|
+
setup_expectations_for_real_apple_hot_news_request(:port => 443)
|
353
|
+
|
354
|
+
http = Net::HTTP.new('images.apple.com', 443)
|
355
|
+
http.use_ssl = true
|
356
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # silence certificate warning
|
357
|
+
response = http.get('/main/rss/hotnews/hotnews.rss')
|
358
|
+
assert response.body.include?('Apple')
|
359
|
+
assert response.body.include?('News')
|
360
|
+
end
|
361
|
+
|
362
|
+
def test_real_request_on_same_domain_as_mock
|
363
|
+
FakeWeb.allow_net_connect = true
|
364
|
+
setup_expectations_for_real_apple_hot_news_request
|
365
|
+
|
366
|
+
FakeWeb.register_uri(:get, 'http://images.apple.com/test_string.txt', :body => 'foo')
|
367
|
+
|
368
|
+
resp = nil
|
369
|
+
Net::HTTP.start('images.apple.com') do |query|
|
370
|
+
resp = query.get('/main/rss/hotnews/hotnews.rss')
|
371
|
+
end
|
372
|
+
assert resp.body.include?('Apple')
|
373
|
+
assert resp.body.include?('News')
|
374
|
+
end
|
375
|
+
|
376
|
+
def test_mock_request_on_real_domain
|
377
|
+
FakeWeb.register_uri(:get, 'http://images.apple.com/test_string.txt', :body => 'foo')
|
378
|
+
resp = nil
|
379
|
+
Net::HTTP.start('images.apple.com') do |query|
|
380
|
+
resp = query.get('/test_string.txt')
|
381
|
+
end
|
382
|
+
assert_equal 'foo', resp.body
|
383
|
+
end
|
384
|
+
|
385
|
+
def test_mock_post_that_raises_exception
|
386
|
+
FakeWeb.register_uri(:post, 'http://mock/raising_exception.txt', :exception => StandardError)
|
387
|
+
assert_raises(StandardError) do
|
388
|
+
Net::HTTP.start('mock') do |query|
|
389
|
+
query.post('/raising_exception.txt', 'some data')
|
390
|
+
end
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
def test_mock_post_that_raises_an_http_error
|
395
|
+
FakeWeb.register_uri(:post, 'http://mock/raising_exception.txt', :exception => Net::HTTPError)
|
396
|
+
assert_raises(Net::HTTPError) do
|
397
|
+
Net::HTTP.start('mock') do |query|
|
398
|
+
query.post('/raising_exception.txt', '')
|
399
|
+
end
|
400
|
+
end
|
401
|
+
end
|
402
|
+
|
403
|
+
def test_raising_an_exception_that_requires_an_argument_to_instantiate
|
404
|
+
FakeWeb.register_uri(:get, "http://example.com/timeout.txt", :exception => Timeout::Error)
|
405
|
+
assert_raises(Timeout::Error) do
|
406
|
+
Net::HTTP.get(URI.parse("http://example.com/timeout.txt"))
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
def test_mock_instance_syntax
|
411
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
412
|
+
response = nil
|
413
|
+
uri = URI.parse('http://mock/test_example.txt')
|
414
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
415
|
+
response = http.start do
|
416
|
+
http.get(uri.path)
|
417
|
+
end
|
418
|
+
|
419
|
+
assert_equal 'test example content', response.body
|
420
|
+
end
|
421
|
+
|
422
|
+
def test_mock_via_nil_proxy
|
423
|
+
response = nil
|
424
|
+
proxy_address = nil
|
425
|
+
proxy_port = nil
|
426
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
|
427
|
+
uri = URI.parse('http://mock/test_example.txt')
|
428
|
+
http = Net::HTTP::Proxy(proxy_address, proxy_port).new(
|
429
|
+
uri.host, (uri.port or 80))
|
430
|
+
response = http.start do
|
431
|
+
http.get(uri.path)
|
432
|
+
end
|
433
|
+
|
434
|
+
assert_equal 'test example content', response.body
|
435
|
+
end
|
436
|
+
|
437
|
+
def test_response_type
|
438
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => "test")
|
439
|
+
response = Net::HTTP.start('mock') { |http| http.get('/test_example.txt') }
|
440
|
+
assert_kind_of Net::HTTPSuccess, response
|
441
|
+
end
|
442
|
+
|
443
|
+
def test_mock_request_that_raises_an_http_error_with_a_specific_status
|
444
|
+
FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
|
445
|
+
exception = assert_raises(Net::HTTPError) do
|
446
|
+
Net::HTTP.start('mock') { |http| http.get('/raising_exception.txt') }
|
447
|
+
end
|
448
|
+
assert_equal '404', exception.response.code
|
449
|
+
assert_equal 'Not Found', exception.response.msg
|
450
|
+
end
|
451
|
+
|
452
|
+
def test_mock_rotate_responses
|
453
|
+
FakeWeb.register_uri(:get, 'http://mock/multiple_test_example.txt',
|
454
|
+
[ {:body => fixture_path("test_example.txt"), :times => 2},
|
455
|
+
{:body => "thrice", :times => 3},
|
456
|
+
{:body => "ever_more"} ])
|
457
|
+
|
458
|
+
uri = URI.parse('http://mock/multiple_test_example.txt')
|
459
|
+
2.times { assert_equal 'test example content', Net::HTTP.get(uri) }
|
460
|
+
3.times { assert_equal 'thrice', Net::HTTP.get(uri) }
|
461
|
+
4.times { assert_equal 'ever_more', Net::HTTP.get(uri) }
|
462
|
+
end
|
463
|
+
|
464
|
+
def test_mock_request_using_response_with_transfer_encoding_header_has_valid_transfer_encoding_header
|
465
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => fixture_path("google_response_with_transfer_encoding"))
|
466
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
467
|
+
assert_not_nil response['transfer-encoding']
|
468
|
+
assert response['transfer-encoding'] == 'chunked'
|
469
|
+
end
|
470
|
+
|
471
|
+
def test_mock_request_using_response_without_transfer_encoding_header_does_not_have_a_transfer_encoding_header
|
472
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => fixture_path("google_response_without_transfer_encoding"))
|
473
|
+
response = nil
|
474
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
475
|
+
assert !response.key?('transfer-encoding')
|
476
|
+
end
|
477
|
+
|
478
|
+
def test_mock_request_using_response_from_curl_has_original_transfer_encoding_header
|
479
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => fixture_path("google_response_from_curl"))
|
480
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
481
|
+
assert_not_nil response['transfer-encoding']
|
482
|
+
assert response['transfer-encoding'] == 'chunked'
|
483
|
+
end
|
484
|
+
|
485
|
+
def test_txt_file_should_have_three_lines
|
486
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :body => fixture_path("test_txt_file"))
|
487
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
488
|
+
assert response.body.split(/\n/).size == 3, "response has #{response.body.split(/\n/).size} lines should have 3"
|
489
|
+
end
|
490
|
+
|
491
|
+
def test_requiring_fakeweb_instead_of_fake_web
|
492
|
+
require "fakeweb"
|
493
|
+
end
|
494
|
+
|
495
|
+
def test_registering_with_string_containing_null_byte
|
496
|
+
# Regression test for File.exists? raising an ArgumentError ("string
|
497
|
+
# contains null byte") since :response first tries to find by filename.
|
498
|
+
# The string should be treated as a response body, instead, and an
|
499
|
+
# EOFError is raised when the byte is encountered.
|
500
|
+
FakeWeb.register_uri(:get, "http://example.com", :response => "test\0test")
|
501
|
+
assert_raise EOFError do
|
502
|
+
Net::HTTP.get(URI.parse("http://example.com"))
|
503
|
+
end
|
504
|
+
|
505
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => "test\0test")
|
506
|
+
body = Net::HTTP.get(URI.parse("http://example.com"))
|
507
|
+
assert_equal "test\0test", body
|
508
|
+
end
|
509
|
+
|
510
|
+
def test_registering_with_string_that_is_a_directory_name
|
511
|
+
# Similar to above, but for Errno::EISDIR being raised since File.exists?
|
512
|
+
# returns true for directories
|
513
|
+
FakeWeb.register_uri(:get, "http://example.com", :response => File.dirname(__FILE__))
|
514
|
+
assert_raise EOFError do
|
515
|
+
body = Net::HTTP.get(URI.parse("http://example.com"))
|
516
|
+
end
|
517
|
+
|
518
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => File.dirname(__FILE__))
|
519
|
+
body = Net::HTTP.get(URI.parse("http://example.com"))
|
520
|
+
assert_equal File.dirname(__FILE__), body
|
521
|
+
end
|
522
|
+
|
523
|
+
def test_registering_with_a_body_pointing_to_a_pathname
|
524
|
+
path = Pathname.new(fixture_path("test_example.txt"))
|
525
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => path)
|
526
|
+
response = Net::HTTP.start("example.com") { |http| http.get("/") }
|
527
|
+
assert_equal "test example content", response.body
|
528
|
+
end
|
529
|
+
|
530
|
+
def test_registering_with_a_response_pointing_to_a_pathname
|
531
|
+
path = Pathname.new(fixture_path("google_response_without_transfer_encoding"))
|
532
|
+
FakeWeb.register_uri(:get, "http://google.com", :response => path)
|
533
|
+
response = Net::HTTP.start("google.com") { |http| http.get("/") }
|
534
|
+
assert response.body.include?("<title>Google</title>")
|
535
|
+
end
|
536
|
+
|
537
|
+
def test_http_version_from_string_response
|
538
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => "example")
|
539
|
+
response = Net::HTTP.start("example.com") { |http| http.get("/") }
|
540
|
+
assert_equal "1.0", response.http_version
|
541
|
+
end
|
542
|
+
|
543
|
+
def test_http_version_from_file_response
|
544
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => fixture_path("test_example.txt"))
|
545
|
+
response = Net::HTTP.start("example.com") { |http| http.get("/") }
|
546
|
+
assert_equal "1.0", response.http_version
|
547
|
+
end
|
548
|
+
|
549
|
+
def test_version
|
550
|
+
assert_equal "1.2.7", FakeWeb::VERSION
|
551
|
+
end
|
552
|
+
|
553
|
+
end
|