fakeweb 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'stringio'
4
+
5
+ module Net #:nodoc: all
6
+
7
+ class BufferedIO
8
+ def initialize(io, debug_output = nil)
9
+ @read_timeout = 60
10
+ @rbuf = ''
11
+ @debug_output = debug_output
12
+
13
+ @io = case io
14
+ when Socket, OpenSSL::SSL::SSLSocket, IO
15
+ io
16
+ when String
17
+ File.exists?(io) ? File.open(io, "r") : StringIO.new(io)
18
+ end
19
+ raise "Unable to create local socket" unless @io
20
+ end
21
+ end
22
+
23
+ class HTTP
24
+ def self.socket_type
25
+ FakeWeb::StubSocket
26
+ end
27
+
28
+ alias :original_net_http_request :request
29
+ alias :original_net_http_connect :connect
30
+
31
+ def request(request, body = nil, &block)
32
+ protocol = use_ssl? ? "https" : "http"
33
+
34
+ path = request.path
35
+ path = URI.parse(request.path).request_uri if request.path =~ /^http/
36
+
37
+ uri = "#{protocol}://#{self.address}:#{self.port}#{path}"
38
+ method = request.method.downcase.to_sym
39
+
40
+ if FakeWeb.registered_uri?(method, uri)
41
+ @socket = Net::HTTP.socket_type.new
42
+ FakeWeb.response_for(method, uri, &block)
43
+ elsif FakeWeb.allow_net_connect?
44
+ original_net_http_connect
45
+ original_net_http_request(request, body, &block)
46
+ else
47
+ raise FakeWeb::NetConnectNotAllowedError,
48
+ "Real HTTP connections are disabled. Unregistered request: #{request.method} #{uri}"
49
+ end
50
+ end
51
+
52
+ def connect
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,78 @@
1
+ module FakeWeb
2
+ class Registry #:nodoc:
3
+ include Singleton
4
+
5
+ attr_accessor :uri_map
6
+
7
+ def initialize
8
+ clean_registry
9
+ end
10
+
11
+ def clean_registry
12
+ self.uri_map = Hash.new do |hash, key|
13
+ hash[key] = Hash.new(&hash.default_proc)
14
+ end
15
+ end
16
+
17
+ def register_uri(method, uri, options)
18
+ uri_map[normalize_uri(uri)][method] = [*[options]].flatten.collect do |option|
19
+ FakeWeb::Responder.new(method, uri, option, option[:times])
20
+ end
21
+ end
22
+
23
+ def registered_uri?(method, uri)
24
+ normalized_uri = normalize_uri(uri)
25
+ uri_map[normalized_uri].has_key?(method) || uri_map[normalized_uri].has_key?(:any)
26
+ end
27
+
28
+ def registered_uri(method, uri)
29
+ uri = normalize_uri(uri)
30
+ registered = registered_uri?(method, uri)
31
+ if registered && uri_map[uri].has_key?(method)
32
+ uri_map[uri][method]
33
+ elsif registered
34
+ uri_map[uri][:any]
35
+ else
36
+ nil
37
+ end
38
+ end
39
+
40
+ def response_for(method, uri, &block)
41
+ responses = registered_uri(method, uri)
42
+ return nil if responses.nil?
43
+
44
+ next_response = responses.last
45
+ responses.each do |response|
46
+ if response.times and response.times > 0
47
+ response.times -= 1
48
+ next_response = response
49
+ break
50
+ end
51
+ end
52
+
53
+ next_response.response(&block)
54
+ end
55
+
56
+ private
57
+
58
+ def normalize_uri(uri)
59
+ case uri
60
+ when URI then uri
61
+ else
62
+ uri = 'http://' + uri unless uri.match('^https?://')
63
+ parsed_uri = URI.parse(uri)
64
+ parsed_uri.query = sort_query_params(parsed_uri.query)
65
+ parsed_uri
66
+ end
67
+ end
68
+
69
+ def sort_query_params(query)
70
+ if query.nil? || query.empty?
71
+ nil
72
+ else
73
+ query.split('&').sort.join('&')
74
+ end
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,99 @@
1
+ module FakeWeb
2
+ class Responder #:nodoc:
3
+
4
+ attr_accessor :method, :uri, :options, :times
5
+
6
+ def initialize(method, uri, options, times)
7
+ self.method = method
8
+ self.uri = uri
9
+ self.options = options
10
+ self.times = times ? times : 1
11
+ end
12
+
13
+ def response(&block)
14
+ if has_baked_response?
15
+ response = baked_response
16
+ else
17
+ code, msg = meta_information
18
+ response = Net::HTTPResponse.send(:response_class, code.to_s).new(uri, code.to_s, msg)
19
+ response.instance_variable_set(:@body, content)
20
+ end
21
+
22
+ response.instance_variable_set(:@read, true)
23
+ response.extend FakeWeb::Response
24
+
25
+ optionally_raise(response)
26
+
27
+ yield response if block_given?
28
+
29
+ response
30
+ end
31
+
32
+ private
33
+
34
+ def content
35
+ [ :file, :string ].each do |map_option|
36
+ next unless options.has_key?(map_option)
37
+ return self.send("#{map_option}_response", options[map_option])
38
+ end
39
+
40
+ return ''
41
+ end
42
+
43
+ def file_response(path)
44
+ IO.read(path)
45
+ end
46
+
47
+ def string_response(string)
48
+ string
49
+ end
50
+
51
+ def baked_response
52
+ resp = case options[:response]
53
+ when Net::HTTPResponse then options[:response]
54
+ when String
55
+ socket = Net::BufferedIO.new(options[:response])
56
+ r = Net::HTTPResponse.read_new(socket)
57
+
58
+ # Store the oiriginal transfer-encoding
59
+ saved_transfer_encoding = r.instance_eval {
60
+ @header['transfer-encoding'] if @header.key?('transfer-encoding')
61
+ }
62
+
63
+ # read the body of response.
64
+ r.instance_eval { @header['transfer-encoding'] = nil }
65
+ r.reading_body(socket, true) {}
66
+
67
+ # Delete the transfer-encoding key from r.@header if there wasn't one,
68
+ # else restore the saved_transfer_encoding.
69
+ if saved_transfer_encoding.nil?
70
+ r.instance_eval { @header.delete('transfer-encoding') }
71
+ else
72
+ r.instance_eval { @header['transfer-encoding'] = saved_transfer_encoding }
73
+ end
74
+ r
75
+ else raise StandardError, "Handler unimplemented for response #{options[:response]}"
76
+ end
77
+ end
78
+
79
+ def has_baked_response?
80
+ options.has_key?(:response)
81
+ end
82
+
83
+ def optionally_raise(response)
84
+ return unless options.has_key?(:exception)
85
+ ex_alloc = options[:exception].allocate
86
+ ex_instance = case ex_alloc
87
+ when Net::HTTPError, OpenURI::HTTPError
88
+ options[:exception].new('Exception from FakeWeb', response)
89
+ else options[:exception].new
90
+ end
91
+ raise ex_instance
92
+ end
93
+
94
+ def meta_information
95
+ options.has_key?(:status) ? options[:status] : [200, 'OK']
96
+ end
97
+
98
+ end
99
+ 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,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,3 @@
1
+ line 1
2
+ line 2
3
+ line 3
@@ -0,0 +1,41 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class TestFakeWebAllowNetConnect < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @original_allow_net_connect = FakeWeb.allow_net_connect?
7
+ end
8
+
9
+ def teardown
10
+ FakeWeb.allow_net_connect = @original_allow_net_connect
11
+ end
12
+
13
+
14
+ def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_true
15
+ FakeWeb.allow_net_connect = true
16
+ setup_expectations_for_real_apple_hot_news_request
17
+ Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
18
+ end
19
+
20
+ def test_raises_for_unregistered_requests_when_allow_net_connect_is_false
21
+ FakeWeb.allow_net_connect = false
22
+ exception = assert_raise FakeWeb::NetConnectNotAllowedError do
23
+ Net::HTTP.get(URI.parse('http://example.com/'))
24
+ end
25
+ end
26
+
27
+ def test_question_mark_method_returns_true_after_setting_allow_net_connect_to_true
28
+ FakeWeb.allow_net_connect = true
29
+ assert FakeWeb.allow_net_connect?
30
+ end
31
+
32
+ def test_question_mark_method_returns_false_after_setting_allow_net_connect_to_false
33
+ FakeWeb.allow_net_connect = false
34
+ assert !FakeWeb.allow_net_connect?
35
+ end
36
+
37
+ def test_allow_net_connect_is_true_by_default
38
+ assert FakeWeb.allow_net_connect?
39
+ end
40
+
41
+ end
@@ -0,0 +1,489 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class TestFakeWeb < Test::Unit::TestCase
4
+
5
+ def setup
6
+ FakeWeb.allow_net_connect = true
7
+ FakeWeb.clean_registry
8
+ end
9
+
10
+ def test_register_uri
11
+ FakeWeb.register_uri('http://mock/test_example.txt', :string => "example")
12
+ assert FakeWeb.registered_uri?('http://mock/test_example.txt')
13
+ end
14
+
15
+ def test_register_uri_with_wrong_number_of_arguments
16
+ assert_raises ArgumentError do
17
+ FakeWeb.register_uri("http://example.com")
18
+ end
19
+ assert_raises ArgumentError do
20
+ FakeWeb.register_uri(:get, "http://example.com", "/example", :string => "example")
21
+ end
22
+ end
23
+
24
+ def test_registered_uri_with_wrong_number_of_arguments
25
+ assert_raises ArgumentError do
26
+ FakeWeb.registered_uri?
27
+ end
28
+ assert_raises ArgumentError do
29
+ FakeWeb.registered_uri?(:get, "http://example.com", "/example")
30
+ end
31
+ end
32
+
33
+ def test_response_for_with_wrong_number_of_arguments
34
+ assert_raises ArgumentError do
35
+ FakeWeb.response_for
36
+ end
37
+ assert_raises ArgumentError do
38
+ FakeWeb.response_for(:get, "http://example.com", "/example")
39
+ end
40
+ end
41
+
42
+ def test_register_uri_without_domain_name
43
+ assert_raises URI::InvalidURIError do
44
+ FakeWeb.register_uri('test_example2.txt', File.dirname(__FILE__) + '/fixtures/test_example.txt')
45
+ end
46
+ end
47
+
48
+ def test_register_uri_with_port_and_check_with_port
49
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'foo')
50
+ assert FakeWeb.registered_uri?('http://example.com:3000/')
51
+ end
52
+
53
+ def test_register_uri_with_port_and_check_without_port
54
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'foo')
55
+ assert !FakeWeb.registered_uri?('http://example.com/')
56
+ end
57
+
58
+ def test_register_uri_with_default_port_for_http_and_check_without_port
59
+ FakeWeb.register_uri('http://example.com:80/', :string => 'foo')
60
+ assert FakeWeb.registered_uri?('http://example.com/')
61
+ end
62
+
63
+ def test_register_uri_with_default_port_for_https_and_check_without_port
64
+ FakeWeb.register_uri('https://example.com:443/', :string => 'foo')
65
+ assert FakeWeb.registered_uri?('https://example.com/')
66
+ end
67
+
68
+ def test_register_uri_with_no_port_for_http_and_check_with_default_port
69
+ FakeWeb.register_uri('http://example.com/', :string => 'foo')
70
+ assert FakeWeb.registered_uri?('http://example.com:80/')
71
+ end
72
+
73
+ def test_register_uri_with_no_port_for_https_and_check_with_default_port
74
+ FakeWeb.register_uri('https://example.com/', :string => 'foo')
75
+ assert FakeWeb.registered_uri?('https://example.com:443/')
76
+ end
77
+
78
+ def test_register_uri_with_no_port_for_https_and_check_with_443_on_http
79
+ FakeWeb.register_uri('https://example.com/', :string => 'foo')
80
+ assert !FakeWeb.registered_uri?('http://example.com:443/')
81
+ end
82
+
83
+ def test_register_uri_with_no_port_for_http_and_check_with_80_on_https
84
+ FakeWeb.register_uri('http://example.com/', :string => 'foo')
85
+ assert !FakeWeb.registered_uri?('https://example.com:80/')
86
+ end
87
+
88
+ def test_register_uri_for_any_method_explicitly
89
+ FakeWeb.register_uri(:any, "http://example.com/rpc_endpoint", :string => "OK")
90
+ assert FakeWeb.registered_uri?(:get, "http://example.com/rpc_endpoint")
91
+ assert FakeWeb.registered_uri?(:post, "http://example.com/rpc_endpoint")
92
+ assert FakeWeb.registered_uri?(:put, "http://example.com/rpc_endpoint")
93
+ assert FakeWeb.registered_uri?(:delete, "http://example.com/rpc_endpoint")
94
+ assert FakeWeb.registered_uri?(:any, "http://example.com/rpc_endpoint")
95
+ assert FakeWeb.registered_uri?("http://example.com/rpc_endpoint")
96
+ end
97
+
98
+ def test_register_uri_for_get_method_only
99
+ FakeWeb.register_uri(:get, "http://example.com/users", :string => "User list")
100
+ assert FakeWeb.registered_uri?(:get, "http://example.com/users")
101
+ assert !FakeWeb.registered_uri?(:post, "http://example.com/users")
102
+ assert !FakeWeb.registered_uri?(:put, "http://example.com/users")
103
+ assert !FakeWeb.registered_uri?(:delete, "http://example.com/users")
104
+ assert !FakeWeb.registered_uri?(:any, "http://example.com/users")
105
+ assert !FakeWeb.registered_uri?("http://example.com/users")
106
+ end
107
+
108
+ def test_response_for_with_registered_uri
109
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
110
+ assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body
111
+ end
112
+
113
+ def test_response_for_with_unknown_uri
114
+ assert_equal nil, FakeWeb.response_for(:get, 'http://example.com/')
115
+ end
116
+
117
+ def test_response_for_with_put_method
118
+ FakeWeb.register_uri(:put, "http://example.com", :string => "response")
119
+ assert_equal 'response', FakeWeb.response_for(:put, "http://example.com").body
120
+ end
121
+
122
+ def test_response_for_with_any_method_explicitly
123
+ FakeWeb.register_uri(:any, "http://example.com", :string => "response")
124
+ assert_equal 'response', FakeWeb.response_for(:get, "http://example.com").body
125
+ assert_equal 'response', FakeWeb.response_for(:any, "http://example.com").body
126
+ end
127
+
128
+ def test_content_for_registered_uri_with_port_and_request_with_port
129
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'test example content')
130
+ Net::HTTP.start('example.com', 3000) do |http|
131
+ response = http.get('/')
132
+ assert_equal 'test example content', response.body
133
+ end
134
+ end
135
+
136
+ def test_content_for_registered_uri_with_default_port_for_http_and_request_without_port
137
+ FakeWeb.register_uri('http://example.com:80/', :string => 'test example content')
138
+ Net::HTTP.start('example.com') do |http|
139
+ response = http.get('/')
140
+ assert_equal 'test example content', response.body
141
+ end
142
+ end
143
+
144
+ def test_content_for_registered_uri_with_no_port_for_http_and_request_with_default_port
145
+ FakeWeb.register_uri('http://example.com/', :string => 'test example content')
146
+ Net::HTTP.start('example.com', 80) do |http|
147
+ response = http.get('/')
148
+ assert_equal 'test example content', response.body
149
+ end
150
+ end
151
+
152
+ def test_content_for_registered_uri_with_default_port_for_https_and_request_with_default_port
153
+ FakeWeb.register_uri('https://example.com:443/', :string => 'test example content')
154
+ http = Net::HTTP.new('example.com', 443)
155
+ http.use_ssl = true
156
+ response = http.get('/')
157
+ assert_equal 'test example content', response.body
158
+ end
159
+
160
+ def test_content_for_registered_uri_with_no_port_for_https_and_request_with_default_port
161
+ FakeWeb.register_uri('https://example.com/', :string => 'test example content')
162
+ http = Net::HTTP.new('example.com', 443)
163
+ http.use_ssl = true
164
+ response = http.get('/')
165
+ assert_equal 'test example content', response.body
166
+ end
167
+
168
+ def test_content_for_registered_uris_with_ports_on_same_domain_and_request_without_port
169
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'port 3000')
170
+ FakeWeb.register_uri('http://example.com/', :string => 'port 80')
171
+ Net::HTTP.start('example.com') do |http|
172
+ response = http.get('/')
173
+ assert_equal 'port 80', response.body
174
+ end
175
+ end
176
+
177
+ def test_content_for_registered_uris_with_ports_on_same_domain_and_request_with_port
178
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'port 3000')
179
+ FakeWeb.register_uri('http://example.com/', :string => 'port 80')
180
+ Net::HTTP.start('example.com', 3000) do |http|
181
+ response = http.get('/')
182
+ assert_equal 'port 3000', response.body
183
+ end
184
+ end
185
+
186
+ def test_content_for_registered_uri_with_get_method_only
187
+ FakeWeb.allow_net_connect = false
188
+ FakeWeb.register_uri(:get, "http://example.com/", :string => "test example content")
189
+ Net::HTTP.start('example.com') do |http|
190
+ assert_equal 'test example content', http.get('/').body
191
+ assert_raises(FakeWeb::NetConnectNotAllowedError) { http.post('/', nil) }
192
+ assert_raises(FakeWeb::NetConnectNotAllowedError) { http.put('/', nil) }
193
+ assert_raises(FakeWeb::NetConnectNotAllowedError) { http.delete('/', nil) }
194
+ end
195
+ end
196
+
197
+ def test_content_for_registered_uri_with_any_method_explicitly
198
+ FakeWeb.allow_net_connect = false
199
+ FakeWeb.register_uri(:any, "http://example.com/", :string => "test example content")
200
+ Net::HTTP.start('example.com') do |http|
201
+ assert_equal 'test example content', http.get('/').body
202
+ assert_equal 'test example content', http.post('/', nil).body
203
+ assert_equal 'test example content', http.put('/', nil).body
204
+ assert_equal 'test example content', http.delete('/').body
205
+ end
206
+ end
207
+
208
+ def test_content_for_registered_uri_with_any_method_implicitly
209
+ FakeWeb.allow_net_connect = false
210
+ FakeWeb.register_uri("http://example.com/", :string => "test example content")
211
+ Net::HTTP.start('example.com') do |http|
212
+ assert_equal 'test example content', http.get('/').body
213
+ assert_equal 'test example content', http.post('/', nil).body
214
+ assert_equal 'test example content', http.put('/', nil).body
215
+ assert_equal 'test example content', http.delete('/').body
216
+ end
217
+ end
218
+
219
+ def test_mock_request_with_block
220
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
221
+ Net::HTTP.start('mock') do |http|
222
+ response = http.get('/test_example.txt')
223
+ assert_equal 'test example content', response.body
224
+ end
225
+ end
226
+
227
+ def test_mock_request_with_undocumented_full_uri_argument_style
228
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
229
+ Net::HTTP.start('mock') do |query|
230
+ response = query.get('http://mock/test_example.txt')
231
+ assert_equal 'test example content', response.body
232
+ end
233
+ end
234
+
235
+ def test_mock_request_with_undocumented_full_uri_argument_style_and_query
236
+ FakeWeb.register_uri('http://mock/test_example.txt?a=b', :string => 'test query content')
237
+ Net::HTTP.start('mock') do |query|
238
+ response = query.get('http://mock/test_example.txt?a=b')
239
+ assert_equal 'test query content', response.body
240
+ end
241
+ end
242
+
243
+ def test_mock_post
244
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
245
+ response = nil
246
+ Net::HTTP.start('mock') do |query|
247
+ response = query.post('/test_example.txt', '')
248
+ end
249
+ assert_equal 'test example content', response.body
250
+ end
251
+
252
+ def test_mock_post_with_string_as_registered_uri
253
+ response = nil
254
+ FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
255
+ Net::HTTP.start('mock') do |query|
256
+ response = query.post('/test_string.txt', '')
257
+ end
258
+ assert_equal 'foo', response.body
259
+ end
260
+
261
+ def test_mock_get_with_request_as_registered_uri
262
+ fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
263
+ FakeWeb.register_uri('http://mock/test_response', :response => fake_response)
264
+ response = nil
265
+ Net::HTTP.start('mock') do |query|
266
+ response = query.get('/test_response')
267
+ end
268
+
269
+ assert_equal fake_response, response
270
+ end
271
+
272
+ def test_mock_get_with_request_from_file_as_registered_uri
273
+ FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
274
+ response = nil
275
+ Net::HTTP.start('www.google.com') do |query|
276
+ response = query.get('/')
277
+ end
278
+ assert_equal '200', response.code
279
+ assert response.body.include?('<title>Google</title>')
280
+ end
281
+
282
+ def test_mock_post_with_request_from_file_as_registered_uri
283
+ FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
284
+ response = nil
285
+ Net::HTTP.start('www.google.com') do |query|
286
+ response = query.post('/', '')
287
+ end
288
+ assert_equal "200", response.code
289
+ assert response.body.include?('<title>Google</title>')
290
+ end
291
+
292
+ def test_proxy_request
293
+ FakeWeb.register_uri('http://www.example.com/', :string => "hello world")
294
+ FakeWeb.register_uri('http://your.proxy.host/', :string => "lala")
295
+ proxy_addr = 'your.proxy.host'
296
+ proxy_port = 8080
297
+
298
+ Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.example.com') do |http|
299
+ response = http.get('/')
300
+ assert_equal "hello world", response.body
301
+ end
302
+ end
303
+
304
+ def test_https_request
305
+ FakeWeb.register_uri('https://www.example.com/', :string => "Hello World")
306
+ http = Net::HTTP.new('www.example.com', 443)
307
+ http.use_ssl = true
308
+ response = http.get('/')
309
+ assert_equal "Hello World", response.body
310
+ end
311
+
312
+ def test_register_unimplemented_response
313
+ FakeWeb.register_uri('http://mock/unimplemented', :response => 1)
314
+ assert_raises StandardError do
315
+ Net::HTTP.start('mock') { |q| q.get('/unimplemented') }
316
+ end
317
+ end
318
+
319
+ def test_real_http_request
320
+ setup_expectations_for_real_apple_hot_news_request
321
+
322
+ resp = nil
323
+ Net::HTTP.start('images.apple.com') do |query|
324
+ resp = query.get('/main/rss/hotnews/hotnews.rss')
325
+ end
326
+ assert resp.body.include?('Apple')
327
+ assert resp.body.include?('News')
328
+ end
329
+
330
+ def test_real_http_request_with_undocumented_full_uri_argument_style
331
+ setup_expectations_for_real_apple_hot_news_request(:path => 'http://images.apple.com/main/rss/hotnews/hotnews.rss')
332
+
333
+ resp = nil
334
+ Net::HTTP.start('images.apple.com') do |query|
335
+ resp = query.get('http://images.apple.com/main/rss/hotnews/hotnews.rss')
336
+ end
337
+ assert resp.body.include?('Apple')
338
+ assert resp.body.include?('News')
339
+ end
340
+
341
+ def test_real_https_request
342
+ setup_expectations_for_real_apple_hot_news_request(:port => 443)
343
+
344
+ http = Net::HTTP.new('images.apple.com', 443)
345
+ http.use_ssl = true
346
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE # silence certificate warning
347
+ response = http.get('/main/rss/hotnews/hotnews.rss')
348
+ assert response.body.include?('Apple')
349
+ assert response.body.include?('News')
350
+ end
351
+
352
+ def test_real_request_on_same_domain_as_mock
353
+ setup_expectations_for_real_apple_hot_news_request
354
+
355
+ FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
356
+
357
+ resp = nil
358
+ Net::HTTP.start('images.apple.com') do |query|
359
+ resp = query.get('/main/rss/hotnews/hotnews.rss')
360
+ end
361
+ assert resp.body.include?('Apple')
362
+ assert resp.body.include?('News')
363
+ end
364
+
365
+ def test_mock_request_on_real_domain
366
+ FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
367
+ resp = nil
368
+ Net::HTTP.start('images.apple.com') do |query|
369
+ resp = query.get('/test_string.txt')
370
+ end
371
+ assert_equal 'foo', resp.body
372
+ end
373
+
374
+ def test_mock_post_that_raises_exception
375
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
376
+ assert_raises(StandardError) do
377
+ Net::HTTP.start('mock') do |query|
378
+ query.post('/raising_exception.txt', 'some data')
379
+ end
380
+ end
381
+ end
382
+
383
+ def test_mock_post_that_raises_an_http_error
384
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError)
385
+ assert_raises(Net::HTTPError) do
386
+ Net::HTTP.start('mock') do |query|
387
+ query.post('/raising_exception.txt', '')
388
+ end
389
+ end
390
+ end
391
+
392
+ def test_mock_instance_syntax
393
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
394
+ response = nil
395
+ uri = URI.parse('http://mock/test_example.txt')
396
+ http = Net::HTTP.new(uri.host, uri.port)
397
+ response = http.start do
398
+ http.get(uri.path)
399
+ end
400
+
401
+ assert_equal 'test example content', response.body
402
+ end
403
+
404
+ def test_mock_via_nil_proxy
405
+ response = nil
406
+ proxy_address = nil
407
+ proxy_port = nil
408
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
409
+ uri = URI.parse('http://mock/test_example.txt')
410
+ http = Net::HTTP::Proxy(proxy_address, proxy_port).new(
411
+ uri.host, (uri.port or 80))
412
+ response = http.start do
413
+ http.get(uri.path)
414
+ end
415
+
416
+ assert_equal 'test example content', response.body
417
+ end
418
+
419
+ def test_response_type
420
+ FakeWeb.register_uri('http://mock/test_example.txt', :string => "test")
421
+ Net::HTTP.start('mock') do |http|
422
+ response = http.get('/test_example.txt')
423
+ assert_kind_of(Net::HTTPSuccess, response)
424
+ end
425
+ end
426
+
427
+ def test_mock_request_that_raises_an_http_error_with_a_specific_status
428
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
429
+ exception = assert_raises(Net::HTTPError) do
430
+ Net::HTTP.start('mock') { |http| response = http.get('/raising_exception.txt') }
431
+ end
432
+ assert_equal '404', exception.response.code
433
+ assert_equal 'Not Found', exception.response.msg
434
+ end
435
+
436
+ def test_mock_rotate_responses
437
+ FakeWeb.register_uri('http://mock/multiple_test_example.txt',
438
+ [ {:file => File.dirname(__FILE__) + '/fixtures/test_example.txt', :times => 2},
439
+ {:string => "thrice", :times => 3},
440
+ {:string => "ever_more"} ])
441
+
442
+ uri = URI.parse('http://mock/multiple_test_example.txt')
443
+ 2.times { assert_equal 'test example content', Net::HTTP.get(uri) }
444
+ 3.times { assert_equal 'thrice', Net::HTTP.get(uri) }
445
+ 4.times { assert_equal 'ever_more', Net::HTTP.get(uri) }
446
+ end
447
+
448
+ def test_mock_request_using_response_with_transfer_encoding_header_has_valid_transfer_encoding_header
449
+ FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_with_transfer_encoding')
450
+ response = nil
451
+ Net::HTTP.start('www.google.com') do |query|
452
+ response = query.get('/')
453
+ end
454
+ assert_not_nil response['transfer-encoding']
455
+ assert response['transfer-encoding'] == 'chunked'
456
+ end
457
+
458
+ def test_mock_request_using_response_without_transfer_encoding_header_does_not_have_a_transfer_encoding_header
459
+ FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
460
+ response = nil
461
+ Net::HTTP.start('www.google.com') do |query|
462
+ response = query.get('/')
463
+ end
464
+ assert !response.key?('transfer-encoding')
465
+ end
466
+
467
+ def test_mock_request_using_response_from_curl_has_original_transfer_encoding_header
468
+ FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_from_curl')
469
+ response = nil
470
+ Net::HTTP.start('www.google.com') do |query|
471
+ response = query.get('/')
472
+ end
473
+ assert_not_nil response['transfer-encoding']
474
+ assert response['transfer-encoding'] == 'chunked'
475
+ end
476
+
477
+ def test_txt_file_should_have_three_lines
478
+ FakeWeb.register_uri('http://www.google.com/', :file => File.dirname(__FILE__) + '/fixtures/test_txt_file')
479
+ response = nil
480
+ Net::HTTP.start('www.google.com') do |query|
481
+ response = query.get('/')
482
+ end
483
+ assert response.body.split(/\n/).size == 3, "response has #{response.body.split(/\n/).size} lines should have 3"
484
+ end
485
+
486
+ def test_requiring_fakeweb_instead_of_fake_web
487
+ require "fakeweb"
488
+ end
489
+ end