cotweet-fakeweb 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/.autotest +5 -0
  2. data/.gitignore +7 -0
  3. data/CHANGELOG +215 -0
  4. data/LICENSE.txt +19 -0
  5. data/README.rdoc +189 -0
  6. data/Rakefile +67 -0
  7. data/fakeweb.gemspec +126 -0
  8. data/lib/fake_web.rb +215 -0
  9. data/lib/fake_web/ext/net_http.rb +72 -0
  10. data/lib/fake_web/registry.rb +127 -0
  11. data/lib/fake_web/responder.rb +122 -0
  12. data/lib/fake_web/response.rb +10 -0
  13. data/lib/fake_web/stub_socket.rb +15 -0
  14. data/lib/fake_web/utility.rb +90 -0
  15. data/lib/fakeweb.rb +2 -0
  16. data/test/fixtures/google_response_from_curl +12 -0
  17. data/test/fixtures/google_response_with_transfer_encoding +17 -0
  18. data/test/fixtures/google_response_without_transfer_encoding +11 -0
  19. data/test/fixtures/test_example.txt +1 -0
  20. data/test/fixtures/test_txt_file +3 -0
  21. data/test/test_allow_net_connect.rb +168 -0
  22. data/test/test_deprecations.rb +54 -0
  23. data/test/test_fake_authentication.rb +92 -0
  24. data/test/test_fake_web.rb +590 -0
  25. data/test/test_fake_web_open_uri.rb +58 -0
  26. data/test/test_helper.rb +90 -0
  27. data/test/test_last_request.rb +29 -0
  28. data/test/test_missing_open_uri.rb +25 -0
  29. data/test/test_missing_pathname.rb +37 -0
  30. data/test/test_other_net_http_libraries.rb +36 -0
  31. data/test/test_precedence.rb +79 -0
  32. data/test/test_query_string.rb +45 -0
  33. data/test/test_regexes.rb +157 -0
  34. data/test/test_response_headers.rb +79 -0
  35. data/test/test_trailing_slashes.rb +53 -0
  36. data/test/test_utility.rb +83 -0
  37. data/test/vendor/right_http_connection-1.2.4/History.txt +59 -0
  38. data/test/vendor/right_http_connection-1.2.4/Manifest.txt +7 -0
  39. data/test/vendor/right_http_connection-1.2.4/README.txt +54 -0
  40. data/test/vendor/right_http_connection-1.2.4/Rakefile +103 -0
  41. data/test/vendor/right_http_connection-1.2.4/lib/net_fix.rb +160 -0
  42. data/test/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb +435 -0
  43. data/test/vendor/right_http_connection-1.2.4/setup.rb +1585 -0
  44. data/test/vendor/samuel-0.2.1/.document +5 -0
  45. data/test/vendor/samuel-0.2.1/.gitignore +5 -0
  46. data/test/vendor/samuel-0.2.1/LICENSE +20 -0
  47. data/test/vendor/samuel-0.2.1/README.rdoc +70 -0
  48. data/test/vendor/samuel-0.2.1/Rakefile +62 -0
  49. data/test/vendor/samuel-0.2.1/VERSION +1 -0
  50. data/test/vendor/samuel-0.2.1/lib/samuel.rb +52 -0
  51. data/test/vendor/samuel-0.2.1/lib/samuel/net_http.rb +10 -0
  52. data/test/vendor/samuel-0.2.1/lib/samuel/request.rb +96 -0
  53. data/test/vendor/samuel-0.2.1/samuel.gemspec +69 -0
  54. data/test/vendor/samuel-0.2.1/test/request_test.rb +193 -0
  55. data/test/vendor/samuel-0.2.1/test/samuel_test.rb +42 -0
  56. data/test/vendor/samuel-0.2.1/test/test_helper.rb +66 -0
  57. data/test/vendor/samuel-0.2.1/test/thread_test.rb +32 -0
  58. metadata +167 -0
@@ -0,0 +1,122 @@
1
+ module FakeWeb
2
+ class Responder #:nodoc:
3
+
4
+ attr_accessor :method, :uri, :options, :times
5
+ KNOWN_OPTIONS = [:body, :exception, :response, :status].freeze
6
+
7
+ def initialize(method, uri, options, times)
8
+ self.method = method
9
+ self.uri = uri
10
+ self.options = options
11
+ self.times = times ? times : 1
12
+
13
+ if options.has_key?(:file) || options.has_key?(:string)
14
+ print_file_string_options_deprecation_warning
15
+ options[:body] = options.delete(:file) || options.delete(:string)
16
+ end
17
+ end
18
+
19
+ def response(&block)
20
+ if has_baked_response?
21
+ response = baked_response
22
+ else
23
+ code, msg = meta_information
24
+ response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg)
25
+ response.instance_variable_set(:@body, body)
26
+ headers_extracted_from_options.each do |name, value|
27
+ if value.respond_to?(:each)
28
+ value.each { |v| response.add_field(name, v) }
29
+ else
30
+ response[name] = value
31
+ end
32
+ end
33
+ end
34
+
35
+ response.instance_variable_set(:@read, true)
36
+ response.extend FakeWeb::Response
37
+
38
+ optionally_raise(response)
39
+
40
+ yield response if block_given?
41
+
42
+ response
43
+ end
44
+
45
+ private
46
+
47
+ def headers_extracted_from_options
48
+ options.reject {|name, _| KNOWN_OPTIONS.include?(name) }.map { |name, value|
49
+ [name.to_s.split("_").map { |segment| segment.capitalize }.join("-"), value]
50
+ }
51
+ end
52
+
53
+ def body
54
+ return '' if options[:body].nil?
55
+
56
+ options[:body] = options[:body].to_s if defined?(Pathname) && options[:body].is_a?(Pathname)
57
+
58
+ if !options[:body].include?("\0") && File.exists?(options[:body]) && !File.directory?(options[:body])
59
+ File.read(options[:body])
60
+ else
61
+ options[:body]
62
+ end
63
+ end
64
+
65
+ def baked_response
66
+ return options[:response] if options[:response].is_a?(Net::HTTPResponse)
67
+
68
+ if options[:response].is_a?(String) || (defined?(Pathname) && options[:response].is_a?(Pathname))
69
+ socket = Net::BufferedIO.new(options[:response].to_s)
70
+ r = Net::HTTPResponse.read_new(socket)
71
+
72
+ # Store the original transfer-encoding
73
+ saved_transfer_encoding = r.instance_eval {
74
+ @header['transfer-encoding'] if @header.key?('transfer-encoding')
75
+ }
76
+
77
+ # Read the body of response
78
+ r.instance_eval { @header['transfer-encoding'] = nil }
79
+ r.reading_body(socket, true) {}
80
+
81
+ # Delete the transfer-encoding key from r.@header if there wasn't one;
82
+ # otherwise, restore the saved_transfer_encoding
83
+ if saved_transfer_encoding.nil?
84
+ r.instance_eval { @header.delete('transfer-encoding') }
85
+ else
86
+ r.instance_eval { @header['transfer-encoding'] = saved_transfer_encoding }
87
+ end
88
+ r
89
+ else
90
+ raise StandardError, "Handler unimplemented for response #{options[:response]}"
91
+ end
92
+ end
93
+
94
+ def has_baked_response?
95
+ options.has_key?(:response)
96
+ end
97
+
98
+ def optionally_raise(response)
99
+ return unless options.has_key?(:exception)
100
+
101
+ case options[:exception].to_s
102
+ when "Net::HTTPError", "OpenURI::HTTPError"
103
+ raise options[:exception].new('Exception from FakeWeb', response)
104
+ else
105
+ raise options[:exception].new('Exception from FakeWeb')
106
+ end
107
+ end
108
+
109
+ def meta_information
110
+ options.has_key?(:status) ? options[:status] : [200, 'OK']
111
+ end
112
+
113
+ def print_file_string_options_deprecation_warning
114
+ which = options.has_key?(:file) ? :file : :string
115
+ $stderr.puts
116
+ $stderr.puts "Deprecation warning: FakeWeb's :#{which} option has been renamed to :body."
117
+ $stderr.puts "Just replace :#{which} with :body in your FakeWeb.register_uri calls."
118
+ $stderr.puts "Called at #{caller[6]}"
119
+ end
120
+
121
+ end
122
+ end
@@ -0,0 +1,10 @@
1
+ module FakeWeb
2
+ module Response #:nodoc:
3
+
4
+ def read_body(*args, &block)
5
+ yield @body if block_given?
6
+ @body
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ module FakeWeb
2
+ class StubSocket #:nodoc:
3
+
4
+ def initialize(*args)
5
+ end
6
+
7
+ def closed?
8
+ @closed ||= true
9
+ end
10
+
11
+ def readuntil(*args)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,90 @@
1
+ module FakeWeb
2
+ module Utility #:nodoc:
3
+
4
+ def self.decode_userinfo_from_header(header)
5
+ header.sub(/^Basic /, "").unpack("m").first
6
+ end
7
+
8
+ def self.encode_unsafe_chars_in_userinfo(userinfo)
9
+ unsafe_in_userinfo = /[^#{URI::REGEXP::PATTERN::UNRESERVED};&=+$,]|^(#{URI::REGEXP::PATTERN::ESCAPED})/
10
+ userinfo.split(":").map { |part| uri_escape(part, unsafe_in_userinfo) }.join(":")
11
+ end
12
+
13
+ def self.strip_default_port_from_uri(uri)
14
+ case uri
15
+ when %r{^http://} then uri.sub(%r{:80(/|$)}, '\1')
16
+ when %r{^https://} then uri.sub(%r{:443(/|$)}, '\1')
17
+ else uri
18
+ end
19
+ end
20
+
21
+ # Returns a string with a normalized version of a Net::HTTP request's URI.
22
+ def self.request_uri_as_string(net_http, request)
23
+ protocol = net_http.use_ssl? ? "https" : "http"
24
+
25
+ path = request.path
26
+ path = URI.parse(request.path).request_uri if request.path =~ /^http/
27
+
28
+ if request["authorization"] =~ /^Basic /
29
+ userinfo = FakeWeb::Utility.decode_userinfo_from_header(request["authorization"])
30
+ userinfo = FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo) + "@"
31
+ else
32
+ userinfo = ""
33
+ end
34
+
35
+ #added to make it work with gem koala (0.8.0) delete_object( )
36
+ path.insert(0, '/') unless path.start_with?('/')
37
+
38
+ uri = "#{protocol}://#{userinfo}#{net_http.address}:#{net_http.port}#{path}"
39
+ end
40
+
41
+ # Wrapper for URI escaping that switches between URI::Parser#escape and
42
+ # URI.escape for 1.9-compatibility
43
+ def self.uri_escape(*args)
44
+ if URI.const_defined?(:Parser)
45
+ URI::Parser.new.escape(*args)
46
+ else
47
+ URI.escape(*args)
48
+ end
49
+ end
50
+
51
+ def self.produce_side_effects_of_net_http_request(request, body)
52
+ request.set_body_internal(body)
53
+ request.content_length = request.body.length unless request.body.nil?
54
+ end
55
+
56
+ def self.puts_warning_for_net_http_around_advice_libs_if_needed
57
+ libs = {"Samuel" => defined?(Samuel)}
58
+ warnings = libs.select { |_, loaded| loaded }.map do |name, _|
59
+ <<-TEXT.gsub(/ {10}/, '')
60
+ \e[1mWarning: FakeWeb was loaded after #{name}\e[0m
61
+ * #{name}'s code is being ignored when a request is handled by FakeWeb,
62
+ because both libraries work by patching Net::HTTP.
63
+ * To fix this, just reorder your requires so that FakeWeb is before #{name}.
64
+ TEXT
65
+ end
66
+ $stderr.puts "\n" + warnings.join("\n") + "\n" if warnings.any?
67
+ end
68
+
69
+ def self.record_loaded_net_http_replacement_libs
70
+ libs = {"RightHttpConnection" => defined?(RightHttpConnection)}
71
+ @loaded_net_http_replacement_libs = libs.map { |name, loaded| name if loaded }.compact
72
+ end
73
+
74
+ def self.puts_warning_for_net_http_replacement_libs_if_needed
75
+ libs = {"RightHttpConnection" => defined?(RightHttpConnection)}
76
+ warnings = libs.select { |_, loaded| loaded }.
77
+ reject { |name, _| @loaded_net_http_replacement_libs.include?(name) }.
78
+ map do |name, _|
79
+ <<-TEXT.gsub(/ {10}/, '')
80
+ \e[1mWarning: #{name} was loaded after FakeWeb\e[0m
81
+ * FakeWeb's code is being ignored, because #{name} replaces parts of
82
+ Net::HTTP without deferring to other libraries. This will break Net::HTTP requests.
83
+ * To fix this, just reorder your requires so that #{name} is before FakeWeb.
84
+ TEXT
85
+ end
86
+ $stderr.puts "\n" + warnings.join("\n") + "\n" if warnings.any?
87
+ end
88
+
89
+ end
90
+ end
data/lib/fakeweb.rb ADDED
@@ -0,0 +1,2 @@
1
+ # So you can require "fakeweb" instead of "fake_web"
2
+ require "fake_web"
@@ -0,0 +1,12 @@
1
+ HTTP/1.1 200 OK
2
+ Cache-Control: private, max-age=0
3
+ Date: Sun, 01 Feb 2009 02:16:24 GMT
4
+ Expires: -1
5
+ Content-Type: text/html; charset=ISO-8859-1
6
+ Set-Cookie: PREF=ID=a6d9b5f5a4056dfe:TM=1233454584:LM=1233454584:S=U9pSwSu4eQwOPenX; expires=Tue, 01-Feb-2011 02:16:24 GMT; path=/; domain=.google.com
7
+ Server: gws
8
+ Transfer-Encoding: chunked
9
+
10
+ <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};
11
+ 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:"-AWFSZ6qFYuUswO9j5HIDQ",kEXPI:"17259,19547",kHL:"en"};
12
+ 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>&#9660;</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 &raquo;</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%>&nbsp;</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>&nbsp;&nbsp;<a href=/advanced_search?hl=en>Advanced Search</a><br>&nbsp;&nbsp;<a href=/preferences?hl=en>Preferences</a><br>&nbsp;&nbsp;<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=CYhslHwSFSZH6LIHusAPEsc2eBfv77nqP3YC9CsHZnNkTEAEgwVRQypDftPn_____AWDJBqoECU_QbUVlfOdxZw&num=1&sig=AGiWqtwRgqw8y_kza6RGKxBrCstaXkDJ7A&q=http://knol.google.com">Write a Knol</a>.</font><br><br><br><font size=-1><a href="/intl/en/ads/">Advertising&nbsp;Programs</a> - <a href="/services/">Business Solutions</a> - <a href="/intl/en/about.html">About Google</a></font><p><font size=-2>&copy;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/CgJlbhICdXMgACswCjgVLCswDjgELCswGDgDLA/L3N5xu59nDE.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,17 @@
1
+ HTTP/1.1 200 OK
2
+ Cache-Control: private, max-age=0
3
+ Date: Sun, 01 Feb 2009 01:54:36 GMT
4
+ Expires: -1
5
+ Content-Type: text/html; charset=ISO-8859-1
6
+ Set-Cookie: PREF=ID=4320bcaa30d097de:TM=1233453276:LM=1233453276:S=Eio39bg_nIabTxzL; expires=Tue, 01-Feb-2011 01:54:36 GMT; path=/; domain=.google.com
7
+ Server: gws
8
+ Transfer-Encoding: chunked
9
+
10
+ fef
11
+ <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};
12
+ 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:"3ACFSYC6EKTcswOL4_nBDQ",kEXPI:"17259,19463",kHL:"en"};
13
+ 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>&#9660;</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
14
+ a27
15
+ ://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 &raquo;</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%>&nbsp;</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>&nbsp;&nbsp;<a href=/advanced_search?hl=en>Advanced Search</a><br>&nbsp;&nbsp;<a href=/preferences?hl=en>Preferences</a><br>&nbsp;&nbsp;<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=CFL7HzwCFSZCnCJSwsQPm842HB_v77nqP3YC9CsHZnNkTEAEgwVRQypDftPn_____AWDJBqoECU_Q1sTewQNSbw&num=1&sig=AGiWqtyz-UiOD3EpsSp4k3n8A7zooeg48g&q=http://knol.google.com">Write a Knol</a>.</font><br><br><br><font size=-1><a href="/intl/en/ads/">Advertising&nbsp;Programs</a> - <a href="/services/">Business Solutions</a> - <a href="/intl/en/about.html">About Google</a></font><p><font size=-2>&copy;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/CgJlbhICdXMgACswCjgVLCswDjgELCswGDgDLA/L3N5xu59nDE.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>
16
+ 0
17
+
@@ -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>&#9660;</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 &raquo;</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%>&nbsp;</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>&nbsp;&nbsp;<a href=/advanced_search?hl=en>Advanced Search</a><br>&nbsp;&nbsp;<a href=/preferences?hl=en>Preferences</a><br>&nbsp;&nbsp;<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&nbsp;Programs</a> - <a href="/services/">Business Solutions</a> - <a href="/intl/en/about.html">About Google</a></font><p><font size=-2>&copy;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,3 @@
1
+ line 1
2
+ line 2
3
+ line 3
@@ -0,0 +1,168 @@
1
+ require 'test_helper'
2
+
3
+ class TestFakeWebAllowNetConnect < Test::Unit::TestCase
4
+ def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_true
5
+ FakeWeb.allow_net_connect = true
6
+ setup_expectations_for_real_apple_hot_news_request
7
+ Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
8
+ end
9
+
10
+ def test_raises_for_unregistered_requests_when_allow_net_connect_is_false
11
+ FakeWeb.allow_net_connect = false
12
+ assert_raise FakeWeb::NetConnectNotAllowedError do
13
+ Net::HTTP.get(URI.parse("http://example.com/"))
14
+ end
15
+ end
16
+
17
+ def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_the_same_string
18
+ FakeWeb.allow_net_connect = "http://images.apple.com/main/rss/hotnews/hotnews.rss"
19
+ setup_expectations_for_real_apple_hot_news_request
20
+ Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
21
+ end
22
+
23
+ def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_the_same_string_with_default_port
24
+ FakeWeb.allow_net_connect = "http://images.apple.com:80/main/rss/hotnews/hotnews.rss"
25
+ setup_expectations_for_real_apple_hot_news_request
26
+ Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
27
+ end
28
+
29
+ def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_the_same_uri
30
+ FakeWeb.allow_net_connect = URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss")
31
+ setup_expectations_for_real_apple_hot_news_request
32
+ Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
33
+ end
34
+
35
+ def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_a_matching_regexp
36
+ FakeWeb.allow_net_connect = %r[^http://images\.apple\.com]
37
+ setup_expectations_for_real_apple_hot_news_request
38
+ Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
39
+ end
40
+
41
+ def test_raises_for_unregistered_requests_when_allow_net_connect_is_a_different_string
42
+ FakeWeb.allow_net_connect = "http://example.com"
43
+ assert_raise FakeWeb::NetConnectNotAllowedError do
44
+ Net::HTTP.get(URI.parse("http://example.com/path"))
45
+ end
46
+ end
47
+
48
+ def test_raises_for_unregistered_requests_when_allow_net_connect_is_a_different_uri
49
+ FakeWeb.allow_net_connect = URI.parse("http://example.com")
50
+ assert_raise FakeWeb::NetConnectNotAllowedError do
51
+ Net::HTTP.get(URI.parse("http://example.com/path"))
52
+ end
53
+ end
54
+
55
+ def test_raises_for_unregistered_requests_when_allow_net_connect_is_a_non_matching_regexp
56
+ FakeWeb.allow_net_connect = %r[example\.net]
57
+ assert_raise FakeWeb::NetConnectNotAllowedError do
58
+ Net::HTTP.get(URI.parse("http://example.com"))
59
+ end
60
+ end
61
+
62
+ def test_changing_allow_net_connect_from_string_to_false_corretly_removes_whitelist
63
+ FakeWeb.allow_net_connect = "http://example.com"
64
+ FakeWeb.allow_net_connect = false
65
+ assert_raise FakeWeb::NetConnectNotAllowedError do
66
+ Net::HTTP.get(URI.parse("http://example.com"))
67
+ end
68
+ end
69
+
70
+ def test_changing_allow_net_connect_from_true_to_string_corretly_limits_connections
71
+ FakeWeb.allow_net_connect = true
72
+ FakeWeb.allow_net_connect = "http://example.com"
73
+ assert_raise FakeWeb::NetConnectNotAllowedError do
74
+ Net::HTTP.get(URI.parse("http://example.net"))
75
+ end
76
+ end
77
+
78
+ def test_exception_message_includes_unregistered_request_method_and_uri_but_no_default_port
79
+ FakeWeb.allow_net_connect = false
80
+ exception = assert_raise FakeWeb::NetConnectNotAllowedError do
81
+ Net::HTTP.get(URI.parse("http://example.com/"))
82
+ end
83
+ assert exception.message.include?("GET http://example.com/")
84
+
85
+ exception = assert_raise FakeWeb::NetConnectNotAllowedError do
86
+ http = Net::HTTP.new("example.com", 443)
87
+ http.use_ssl = true
88
+ http.get("/")
89
+ end
90
+ assert exception.message.include?("GET https://example.com/")
91
+ end
92
+
93
+ def test_exception_message_includes_unregistered_request_port_when_not_default
94
+ FakeWeb.allow_net_connect = false
95
+ exception = assert_raise FakeWeb::NetConnectNotAllowedError do
96
+ Net::HTTP.start("example.com", 8000) { |http| http.get("/") }
97
+ end
98
+ assert exception.message.include?("GET http://example.com:8000/")
99
+
100
+ exception = assert_raise FakeWeb::NetConnectNotAllowedError do
101
+ http = Net::HTTP.new("example.com", 4433)
102
+ http.use_ssl = true
103
+ http.get("/")
104
+ end
105
+ assert exception.message.include?("GET https://example.com:4433/")
106
+ end
107
+
108
+ def test_exception_message_includes_unregistered_request_port_when_not_default_with_path
109
+ FakeWeb.allow_net_connect = false
110
+ exception = assert_raise FakeWeb::NetConnectNotAllowedError do
111
+ Net::HTTP.start("example.com", 8000) { |http| http.get("/test") }
112
+ end
113
+ assert exception.message.include?("GET http://example.com:8000/test")
114
+
115
+ exception = assert_raise FakeWeb::NetConnectNotAllowedError do
116
+ http = Net::HTTP.new("example.com", 4433)
117
+ http.use_ssl = true
118
+ http.get("/test")
119
+ end
120
+ assert exception.message.include?("GET https://example.com:4433/test")
121
+ end
122
+
123
+ def test_question_mark_method_returns_true_after_setting_allow_net_connect_to_true
124
+ FakeWeb.allow_net_connect = true
125
+ assert FakeWeb.allow_net_connect?
126
+ end
127
+
128
+ def test_question_mark_method_returns_false_after_setting_allow_net_connect_to_false
129
+ FakeWeb.allow_net_connect = false
130
+ assert !FakeWeb.allow_net_connect?
131
+ end
132
+
133
+ def test_question_mark_method_raises_with_no_argument_when_allow_net_connect_is_a_whitelist
134
+ FakeWeb.allow_net_connect = "http://example.com"
135
+ exception = assert_raise ArgumentError do
136
+ FakeWeb.allow_net_connect?
137
+ end
138
+ assert_equal "You must supply a URI to test", exception.message
139
+ end
140
+
141
+ def test_question_mark_method_returns_true_when_argument_is_same_uri_as_allow_net_connect_string
142
+ FakeWeb.allow_net_connect = "http://example.com"
143
+ assert FakeWeb.allow_net_connect?("http://example.com/")
144
+ end
145
+
146
+ def test_question_mark_method_returns_true_when_argument_matches_allow_net_connect_regexp
147
+ FakeWeb.allow_net_connect = %r[^https?://example.com/]
148
+ assert FakeWeb.allow_net_connect?("http://example.com/path")
149
+ assert FakeWeb.allow_net_connect?("https://example.com:443/")
150
+ end
151
+
152
+ def test_question_mark_method_returns_false_when_argument_does_not_match_allow_net_connect_regexp
153
+ FakeWeb.allow_net_connect = %r[^http://example.com/]
154
+ assert !FakeWeb.allow_net_connect?("http://example.com:8080")
155
+ end
156
+ end
157
+
158
+
159
+ class TestFakeWebAllowNetConnectWithCleanState < Test::Unit::TestCase
160
+ # Our test_helper.rb sets allow_net_connect = false in an inherited #setup
161
+ # method. Disable that here to test the default setting.
162
+ def setup; end
163
+ def teardown; end
164
+
165
+ def test_allow_net_connect_is_true_by_default
166
+ assert FakeWeb.allow_net_connect?
167
+ end
168
+ end