rubyist-fakeweb 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,88 @@
1
+ require 'strscan'
2
+ require 'cgi'
3
+
4
+ # This class was copied/adapted from ActionController
5
+ class UrlEncodedPairParser < StringScanner
6
+ attr_reader :top, :parent, :result
7
+
8
+ def initialize(pairs = [])
9
+ super('')
10
+ @result = {}
11
+ pairs.each { |key, value| parse(key, value) }
12
+ end
13
+
14
+ KEY_REGEXP = %r{([^\[\]=&]+)}
15
+ BRACKETED_KEY_REGEXP = %r{\[([^\[\]=&]+)\]}
16
+
17
+ # Parse the query string
18
+ def parse(key, value)
19
+ self.string = key
20
+ @top, @parent = result, nil
21
+
22
+ # First scan the bare key
23
+ key = scan(KEY_REGEXP) or return
24
+ key = post_key_check(key)
25
+
26
+ # Then scan as many nestings as present
27
+ until eos?
28
+ r = scan(BRACKETED_KEY_REGEXP) or return
29
+ key = self[1]
30
+ key = post_key_check(key)
31
+ end
32
+
33
+ bind(key, value)
34
+ end
35
+
36
+ private
37
+ def post_key_check(key)
38
+ if scan(/\[\]/)
39
+ container(key, Array)
40
+ nil
41
+ elsif check(/\[[^\]]/)
42
+ container(key, Hash)
43
+ nil
44
+ else
45
+ key
46
+ end
47
+ end
48
+
49
+ def container(key, klass)
50
+ type_conflict! klass, top[key] if top.is_a?(Hash) && top.key?(key) && ! top[key].is_a?(klass)
51
+ value = bind(key, klass.new)
52
+ type_conflict! klass, value unless value.is_a?(klass)
53
+ push(value)
54
+ end
55
+
56
+ def push(value)
57
+ @parent, @top = @top, value
58
+ end
59
+
60
+ def bind(key, value)
61
+ if top.is_a? Array
62
+ if key
63
+ if top[-1].is_a?(Hash) && ! top[-1].key?(key)
64
+ top[-1][key] = value
65
+ else
66
+ top << {key => value}
67
+ push top.last
68
+ value = top[key]
69
+ end
70
+ else
71
+ top << value
72
+ end
73
+ elsif top.is_a? Hash
74
+ key = CGI.unescape(key)
75
+ parent << (@top = {}) if top.key?(key) && parent.is_a?(Array)
76
+ top[key] ||= value
77
+ return top[key]
78
+ else
79
+ raise ArgumentError, "Don't know what to do: top is #{top.inspect}"
80
+ end
81
+
82
+ return value
83
+ end
84
+
85
+ def type_conflict!(klass, value)
86
+ raise TypeError, "Conflicting types for parameter containers."
87
+ end
88
+ 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,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,68 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class TestFakeAuthentication < Test::Unit::TestCase
4
+ def setup
5
+ FakeWeb.register_uri('http://user:pass@mock/auth.txt', :string => 'authorized')
6
+ FakeWeb.register_uri('http://user2:pass@mock/auth.txt', :string => 'wrong user')
7
+ FakeWeb.register_uri('http://mock/auth.txt', :string => 'unauthorized')
8
+ end
9
+
10
+ def test_register_uri_with_authentication
11
+ FakeWeb.register_uri('http://user:pass@mock/test_example.txt', :string => "example")
12
+ assert FakeWeb.registered_uri?('http://user:pass@mock/test_example.txt')
13
+ end
14
+
15
+ def test_register_uri_with_authentication_doesnt_trigger_without
16
+ FakeWeb.register_uri('http://user:pass@mock/test_example.txt', :string => "example")
17
+ assert !FakeWeb.registered_uri?('http://mock/test_example.txt')
18
+ end
19
+
20
+ def test_register_uri_with_authentication_doesnt_trigger_with_incorrect_credentials
21
+ FakeWeb.register_uri('http://user:pass@mock/test_example.txt', :string => "example")
22
+ assert !FakeWeb.registered_uri?('http://user:wrong@mock/test_example.txt')
23
+ end
24
+
25
+ def test_unauthenticated_request
26
+ http = Net::HTTP.new('mock', 80)
27
+ req = Net::HTTP::Get.new('/auth.txt')
28
+ assert_equal http.request(req).body, 'unauthorized'
29
+ end
30
+
31
+ def test_authenticated_request
32
+ http = Net::HTTP.new('mock',80)
33
+ req = Net::HTTP::Get.new('/auth.txt')
34
+ req.basic_auth 'user', 'pass'
35
+ assert_equal http.request(req).body, 'authorized'
36
+ end
37
+
38
+ def test_incorrectly_authenticated_request
39
+ http = Net::HTTP.new('mock',80)
40
+ req = Net::HTTP::Get.new('/auth.txt')
41
+ req.basic_auth 'user2', 'pass'
42
+ assert_equal http.request(req).body, 'wrong user'
43
+ end
44
+
45
+ def test_basic_auth_support_is_transparent_to_oauth
46
+ FakeWeb.register_uri(:get, "http://sp.example.com/protected", :string => "secret")
47
+
48
+ # from http://oauth.net/core/1.0/#auth_header
49
+ auth_header = <<-HEADER
50
+ OAuth realm="http://sp.example.com/",
51
+ oauth_consumer_key="0685bd9184jfhq22",
52
+ oauth_token="ad180jjd733klru7",
53
+ oauth_signature_method="HMAC-SHA1",
54
+ oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",
55
+ oauth_timestamp="137131200",
56
+ oauth_nonce="4572616e48616d6d65724c61686176",
57
+ oauth_version="1.0"
58
+ HEADER
59
+ auth_header.gsub!(/\s+/, " ").strip!
60
+
61
+ http = Net::HTTP.new("sp.example.com", 80)
62
+ response = nil
63
+ http.start do |request|
64
+ response = request.get("/protected", {"authorization" => auth_header})
65
+ end
66
+ assert_equal "secret", response.body
67
+ end
68
+ end
@@ -0,0 +1,617 @@
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_block
16
+ FakeWeb.register_uri('http://mock/test_example.txt', {}) { 'foo' }
17
+ assert FakeWeb.registered_uri?('http://mock/test_example.txt')
18
+ end
19
+
20
+ def test_register_uri_with_wrong_number_of_arguments
21
+ assert_raises ArgumentError do
22
+ FakeWeb.register_uri("http://example.com")
23
+ end
24
+ assert_raises ArgumentError do
25
+ FakeWeb.register_uri(:get, "http://example.com", "/example", :string => "example")
26
+ end
27
+ end
28
+
29
+ def test_registered_uri_with_wrong_number_of_arguments
30
+ assert_raises ArgumentError do
31
+ FakeWeb.registered_uri?
32
+ end
33
+ assert_raises ArgumentError do
34
+ FakeWeb.registered_uri?(:get, "http://example.com", "/example")
35
+ end
36
+ end
37
+
38
+ def test_response_for_with_wrong_number_of_arguments
39
+ assert_raises ArgumentError do
40
+ FakeWeb.response_for
41
+ end
42
+ assert_raises ArgumentError do
43
+ FakeWeb.response_for(nil, :get, "http://example.com", "/example")
44
+ end
45
+ end
46
+
47
+ def test_register_uri_without_domain_name
48
+ assert_raises URI::InvalidURIError do
49
+ FakeWeb.register_uri('test_example2.txt', File.dirname(__FILE__) + '/fixtures/test_example.txt')
50
+ end
51
+ end
52
+
53
+ def test_register_uri_with_port_and_check_with_port
54
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'foo')
55
+ assert FakeWeb.registered_uri?('http://example.com:3000/')
56
+ end
57
+
58
+ def test_register_uri_with_port_and_check_without_port
59
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'foo')
60
+ assert !FakeWeb.registered_uri?('http://example.com/')
61
+ end
62
+
63
+ def test_register_uri_with_default_port_for_http_and_check_without_port
64
+ FakeWeb.register_uri('http://example.com:80/', :string => 'foo')
65
+ assert FakeWeb.registered_uri?('http://example.com/')
66
+ end
67
+
68
+ def test_register_uri_with_default_port_for_https_and_check_without_port
69
+ FakeWeb.register_uri('https://example.com:443/', :string => 'foo')
70
+ assert FakeWeb.registered_uri?('https://example.com/')
71
+ end
72
+
73
+ def test_register_uri_with_no_port_for_http_and_check_with_default_port
74
+ FakeWeb.register_uri('http://example.com/', :string => 'foo')
75
+ assert FakeWeb.registered_uri?('http://example.com:80/')
76
+ end
77
+
78
+ def test_register_uri_with_no_port_for_https_and_check_with_default_port
79
+ FakeWeb.register_uri('https://example.com/', :string => 'foo')
80
+ assert FakeWeb.registered_uri?('https://example.com:443/')
81
+ end
82
+
83
+ def test_register_uri_with_no_port_for_https_and_check_with_443_on_http
84
+ FakeWeb.register_uri('https://example.com/', :string => 'foo')
85
+ assert !FakeWeb.registered_uri?('http://example.com:443/')
86
+ end
87
+
88
+ def test_register_uri_with_no_port_for_http_and_check_with_80_on_https
89
+ FakeWeb.register_uri('http://example.com/', :string => 'foo')
90
+ assert !FakeWeb.registered_uri?('https://example.com:80/')
91
+ end
92
+
93
+ def test_register_uri_for_any_method_explicitly
94
+ FakeWeb.register_uri(:any, "http://example.com/rpc_endpoint", :string => "OK")
95
+ assert FakeWeb.registered_uri?(:get, "http://example.com/rpc_endpoint")
96
+ assert FakeWeb.registered_uri?(:post, "http://example.com/rpc_endpoint")
97
+ assert FakeWeb.registered_uri?(:put, "http://example.com/rpc_endpoint")
98
+ assert FakeWeb.registered_uri?(:delete, "http://example.com/rpc_endpoint")
99
+ assert FakeWeb.registered_uri?(:any, "http://example.com/rpc_endpoint")
100
+ assert FakeWeb.registered_uri?("http://example.com/rpc_endpoint")
101
+ end
102
+
103
+ def test_register_uri_for_get_method_only
104
+ FakeWeb.register_uri(:get, "http://example.com/users", :string => "User list")
105
+ assert FakeWeb.registered_uri?(:get, "http://example.com/users")
106
+ assert !FakeWeb.registered_uri?(:post, "http://example.com/users")
107
+ assert !FakeWeb.registered_uri?(:put, "http://example.com/users")
108
+ assert !FakeWeb.registered_uri?(:delete, "http://example.com/users")
109
+ assert !FakeWeb.registered_uri?(:any, "http://example.com/users")
110
+ assert !FakeWeb.registered_uri?("http://example.com/users")
111
+ end
112
+
113
+ def test_response_for_with_registered_uri
114
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
115
+ assert_equal 'test example content', FakeWeb.response_for(nil, 'http://mock/test_example.txt').body
116
+ end
117
+
118
+ def test_response_for_with_unknown_uri
119
+ assert_equal nil, FakeWeb.response_for(:get, 'http://example.com/')
120
+ end
121
+
122
+ def test_response_for_with_put_method
123
+ FakeWeb.register_uri(:put, "http://example.com", :string => "response")
124
+ assert_equal 'response', FakeWeb.response_for(nil, :put, "http://example.com").body
125
+ end
126
+
127
+ def test_response_for_with_any_method_explicitly
128
+ FakeWeb.register_uri(:any, "http://example.com", :string => "response")
129
+ assert_equal 'response', FakeWeb.response_for(:get, "http://example.com").body
130
+ assert_equal 'response', FakeWeb.response_for(:any, "http://example.com").body
131
+ end
132
+
133
+ def test_content_for_registered_uri_with_port_and_request_with_port
134
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'test example content')
135
+ Net::HTTP.start('example.com', 3000) do |http|
136
+ response = http.get('/')
137
+ assert_equal 'test example content', response.body
138
+ end
139
+ end
140
+
141
+ def test_content_for_registered_uri_with_block
142
+ FakeWeb.register_uri('http://example.com:3000/', {}) { |params| 'test example content' }
143
+
144
+ Net::HTTP.start('example.com', 3000) do |http|
145
+ response = http.get('/')
146
+ assert_equal 'test example content', response.body
147
+ end
148
+ end
149
+
150
+ def test_content_for_registered_uri_with_default_port_for_http_and_request_without_port
151
+ FakeWeb.register_uri('http://example.com:80/', :string => 'test example content')
152
+ Net::HTTP.start('example.com') do |http|
153
+ response = http.get('/')
154
+ assert_equal 'test example content', response.body
155
+ end
156
+ end
157
+
158
+ def test_content_for_registered_uri_with_no_port_for_http_and_request_with_default_port
159
+ FakeWeb.register_uri('http://example.com/', :string => 'test example content')
160
+ Net::HTTP.start('example.com', 80) do |http|
161
+ response = http.get('/')
162
+ assert_equal 'test example content', response.body
163
+ end
164
+ end
165
+
166
+ def test_content_for_registered_uri_with_default_port_for_https_and_request_with_default_port
167
+ FakeWeb.register_uri('https://example.com:443/', :string => 'test example content')
168
+ http = Net::HTTP.new('example.com', 443)
169
+ http.use_ssl = true
170
+ response = http.get('/')
171
+ assert_equal 'test example content', response.body
172
+ end
173
+
174
+ def test_content_for_registered_uri_with_no_port_for_https_and_request_with_default_port
175
+ FakeWeb.register_uri('https://example.com/', :string => 'test example content')
176
+ http = Net::HTTP.new('example.com', 443)
177
+ http.use_ssl = true
178
+ response = http.get('/')
179
+ assert_equal 'test example content', response.body
180
+ end
181
+
182
+ def test_content_for_registered_uris_with_ports_on_same_domain_and_request_without_port
183
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'port 3000')
184
+ FakeWeb.register_uri('http://example.com/', :string => 'port 80')
185
+ Net::HTTP.start('example.com') do |http|
186
+ response = http.get('/')
187
+ assert_equal 'port 80', response.body
188
+ end
189
+ end
190
+
191
+ def test_content_for_registered_uris_with_ports_on_same_domain_and_request_with_port
192
+ FakeWeb.register_uri('http://example.com:3000/', :string => 'port 3000')
193
+ FakeWeb.register_uri('http://example.com/', :string => 'port 80')
194
+ Net::HTTP.start('example.com', 3000) do |http|
195
+ response = http.get('/')
196
+ assert_equal 'port 3000', response.body
197
+ end
198
+ end
199
+
200
+ def test_content_for_registered_uri_with_get_method_only
201
+ FakeWeb.allow_net_connect = false
202
+ FakeWeb.register_uri(:get, "http://example.com/", :string => "test example content")
203
+ Net::HTTP.start('example.com') do |http|
204
+ assert_equal 'test example content', http.get('/').body
205
+ assert_raises(FakeWeb::NetConnectNotAllowedError) { http.post('/', nil) }
206
+ assert_raises(FakeWeb::NetConnectNotAllowedError) { http.put('/', nil) }
207
+ assert_raises(FakeWeb::NetConnectNotAllowedError) { http.delete('/', nil) }
208
+ end
209
+ end
210
+
211
+ def test_content_for_registered_uri_with_any_method_explicitly
212
+ FakeWeb.allow_net_connect = false
213
+ FakeWeb.register_uri(:any, "http://example.com/", :string => "test example content")
214
+ Net::HTTP.start('example.com') do |http|
215
+ assert_equal 'test example content', http.get('/').body
216
+ assert_equal 'test example content', http.post('/', nil).body
217
+ assert_equal 'test example content', http.put('/', nil).body
218
+ assert_equal 'test example content', http.delete('/').body
219
+ end
220
+ end
221
+
222
+ def test_content_for_registered_uri_with_any_method_implicitly
223
+ FakeWeb.allow_net_connect = false
224
+ FakeWeb.register_uri("http://example.com/", :string => "test example content")
225
+ Net::HTTP.start('example.com') do |http|
226
+ assert_equal 'test example content', http.get('/').body
227
+ assert_equal 'test example content', http.post('/', nil).body
228
+ assert_equal 'test example content', http.put('/', nil).body
229
+ assert_equal 'test example content', http.delete('/').body
230
+ end
231
+ end
232
+
233
+ def test_params_for_registered_uri_with_block_using_get_without_params
234
+ FakeWeb.register_uri(:get, 'http://example.com/', {}) do |params|
235
+ assert_equal({}, params)
236
+ ''
237
+ end
238
+
239
+ Net::HTTP.start('example.com') do |http|
240
+ response = http.get('/')
241
+ end
242
+ end
243
+
244
+ def test_params_for_registered_uri_with_block_using_get
245
+ FakeWeb.register_uri(:get, 'http://example.com/?foo=bar', {}) do |params|
246
+ assert_equal({'foo' => 'bar'}, params)
247
+ ''
248
+ end
249
+
250
+ Net::HTTP.start('example.com') do |http|
251
+ response = http.get('/?foo=bar')
252
+ end
253
+ end
254
+
255
+ def test_params_for_registered_uri_with_block_using_post_without_params
256
+ FakeWeb.register_uri(:post, 'http://example.com/', {}) do |params|
257
+ assert_equal({}, params)
258
+ ''
259
+ end
260
+
261
+ Net::HTTP.post_form(URI.parse('http://example.com/'), {})
262
+ end
263
+
264
+ def test_params_for_registered_uri_with_block_using_post
265
+ FakeWeb.register_uri(:post, 'http://example.com/', {}) do |params|
266
+ assert_equal({'foo' => 'bar'}, params)
267
+ ''
268
+ end
269
+
270
+ Net::HTTP.post_form(URI.parse('http://example.com/'), {'foo' => 'bar'})
271
+ end
272
+
273
+ def test_params_for_registered_uri_with_block_using_post_without_params
274
+ FakeWeb.register_uri(:post, 'http://example.com/', {}) do |params|
275
+ assert_equal({}, params)
276
+ ''
277
+ end
278
+
279
+ Net::HTTP.post_form(URI.parse('http://example.com/'), {})
280
+ end
281
+
282
+ def test_param_decoding
283
+ FakeWeb.register_uri(:post, 'http://example.com/', {}) do |params|
284
+ assert_equal({'user' => {'name' => 'foo'}}, params)
285
+ end
286
+
287
+ Net::HTTP.post_form(URI.parse('http://example.com/'), {'user[name]' => 'foo'})
288
+ end
289
+
290
+ def test_params_for_registered_uri_with_block_using_put_without_params
291
+ FakeWeb.register_uri(:put, 'http://example.com/', {}) do |params|
292
+ assert_equal({}, params)
293
+ ''
294
+ end
295
+
296
+ uri = URI.parse("http://example.com/")
297
+ Net::HTTP.start(uri.host, uri.port) do |http|
298
+ headers = {'Content-Type' => 'text/plain; charset=utf-8'}
299
+ response = http.send_request('PUT', uri.request_uri, '', headers)
300
+ end
301
+ end
302
+
303
+ def test_params_for_registered_uri_with_block_using_put
304
+ FakeWeb.register_uri(:put, 'http://example.com/', {}) do |params|
305
+ assert_equal({'foo' => 'bar'}, params)
306
+ ''
307
+ end
308
+
309
+ uri = URI.parse("http://example.com/")
310
+ Net::HTTP.start(uri.host, uri.port) do |http|
311
+ headers = {'Content-Type' => 'text/plain; charset=utf-8'}
312
+ put_data = "foo=bar"
313
+ response = http.send_request('PUT', uri.request_uri, put_data, headers)
314
+ end
315
+ end
316
+
317
+ def test_mock_request_with_block
318
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
319
+ Net::HTTP.start('mock') do |http|
320
+ response = http.get('/test_example.txt')
321
+ assert_equal 'test example content', response.body
322
+ end
323
+ end
324
+
325
+ def test_mock_request_with_undocumented_full_uri_argument_style
326
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
327
+ Net::HTTP.start('mock') do |query|
328
+ response = query.get('http://mock/test_example.txt')
329
+ assert_equal 'test example content', response.body
330
+ end
331
+ end
332
+
333
+ def test_mock_request_with_undocumented_full_uri_argument_style_and_query
334
+ FakeWeb.register_uri('http://mock/test_example.txt?a=b', :string => 'test query content')
335
+ Net::HTTP.start('mock') do |query|
336
+ response = query.get('http://mock/test_example.txt?a=b')
337
+ assert_equal 'test query content', response.body
338
+ end
339
+ end
340
+
341
+ def test_mock_post
342
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
343
+ response = nil
344
+ Net::HTTP.start('mock') do |query|
345
+ response = query.post('/test_example.txt', '')
346
+ end
347
+ assert_equal 'test example content', response.body
348
+ end
349
+
350
+ def test_mock_post_with_string_as_registered_uri
351
+ response = nil
352
+ FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
353
+ Net::HTTP.start('mock') do |query|
354
+ response = query.post('/test_string.txt', '')
355
+ end
356
+ assert_equal 'foo', response.body
357
+ end
358
+
359
+ def test_mock_get_with_request_as_registered_uri
360
+ fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
361
+ FakeWeb.register_uri('http://mock/test_response', :response => fake_response)
362
+ response = nil
363
+ Net::HTTP.start('mock') do |query|
364
+ response = query.get('/test_response')
365
+ end
366
+
367
+ assert_equal fake_response, response
368
+ end
369
+
370
+ def test_mock_get_with_request_from_file_as_registered_uri
371
+ FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
372
+ response = nil
373
+ Net::HTTP.start('www.google.com') do |query|
374
+ response = query.get('/')
375
+ end
376
+ assert_equal '200', response.code
377
+ assert response.body.include?('<title>Google</title>')
378
+ end
379
+
380
+ def test_mock_post_with_request_from_file_as_registered_uri
381
+ FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
382
+ response = nil
383
+ Net::HTTP.start('www.google.com') do |query|
384
+ response = query.post('/', '')
385
+ end
386
+ assert_equal "200", response.code
387
+ assert response.body.include?('<title>Google</title>')
388
+ end
389
+
390
+ def test_proxy_request
391
+ FakeWeb.register_uri('http://www.example.com/', :string => "hello world")
392
+ FakeWeb.register_uri('http://your.proxy.host/', :string => "lala")
393
+ proxy_addr = 'your.proxy.host'
394
+ proxy_port = 8080
395
+
396
+ Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.example.com') do |http|
397
+ response = http.get('/')
398
+ assert_equal "hello world", response.body
399
+ end
400
+ end
401
+
402
+ def test_https_request
403
+ FakeWeb.register_uri('https://www.example.com/', :string => "Hello World")
404
+ http = Net::HTTP.new('www.example.com', 443)
405
+ http.use_ssl = true
406
+ response = http.get('/')
407
+ assert_equal "Hello World", response.body
408
+ end
409
+
410
+ def test_register_unimplemented_response
411
+ FakeWeb.register_uri('http://mock/unimplemented', :response => 1)
412
+ assert_raises StandardError do
413
+ Net::HTTP.start('mock') { |q| q.get('/unimplemented') }
414
+ end
415
+ end
416
+
417
+ def test_real_http_request
418
+ setup_expectations_for_real_apple_hot_news_request
419
+
420
+ resp = nil
421
+ Net::HTTP.start('images.apple.com') do |query|
422
+ resp = query.get('/main/rss/hotnews/hotnews.rss')
423
+ end
424
+ assert resp.body.include?('Apple')
425
+ assert resp.body.include?('News')
426
+ end
427
+
428
+ def test_real_http_request_with_undocumented_full_uri_argument_style
429
+ setup_expectations_for_real_apple_hot_news_request(:path => 'http://images.apple.com/main/rss/hotnews/hotnews.rss')
430
+
431
+ resp = nil
432
+ Net::HTTP.start('images.apple.com') do |query|
433
+ resp = query.get('http://images.apple.com/main/rss/hotnews/hotnews.rss')
434
+ end
435
+ assert resp.body.include?('Apple')
436
+ assert resp.body.include?('News')
437
+ end
438
+
439
+ def test_real_https_request
440
+ setup_expectations_for_real_apple_hot_news_request(:port => 443)
441
+
442
+ http = Net::HTTP.new('images.apple.com', 443)
443
+ http.use_ssl = true
444
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE # silence certificate warning
445
+ response = http.get('/main/rss/hotnews/hotnews.rss')
446
+ assert response.body.include?('Apple')
447
+ assert response.body.include?('News')
448
+ end
449
+
450
+ def test_real_request_on_same_domain_as_mock
451
+ setup_expectations_for_real_apple_hot_news_request
452
+
453
+ FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
454
+
455
+ resp = nil
456
+ Net::HTTP.start('images.apple.com') do |query|
457
+ resp = query.get('/main/rss/hotnews/hotnews.rss')
458
+ end
459
+ assert resp.body.include?('Apple')
460
+ assert resp.body.include?('News')
461
+ end
462
+
463
+ def test_mock_request_on_real_domain
464
+ FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
465
+ resp = nil
466
+ Net::HTTP.start('images.apple.com') do |query|
467
+ resp = query.get('/test_string.txt')
468
+ end
469
+ assert_equal 'foo', resp.body
470
+ end
471
+
472
+ def test_mock_post_that_raises_exception
473
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
474
+ assert_raises(StandardError) do
475
+ Net::HTTP.start('mock') do |query|
476
+ query.post('/raising_exception.txt', 'some data')
477
+ end
478
+ end
479
+ end
480
+
481
+ def test_mock_post_that_raises_an_http_error
482
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError)
483
+ assert_raises(Net::HTTPError) do
484
+ Net::HTTP.start('mock') do |query|
485
+ query.post('/raising_exception.txt', '')
486
+ end
487
+ end
488
+ end
489
+
490
+ def test_raising_an_exception_that_requires_an_argument_to_instantiate
491
+ FakeWeb.register_uri(:get, "http://example.com/timeout.txt", :exception => Timeout::Error)
492
+ assert_raises(Timeout::Error) do
493
+ Net::HTTP.get(URI.parse("http://example.com/timeout.txt"))
494
+ end
495
+ end
496
+
497
+ def test_mock_instance_syntax
498
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
499
+ response = nil
500
+ uri = URI.parse('http://mock/test_example.txt')
501
+ http = Net::HTTP.new(uri.host, uri.port)
502
+ response = http.start do
503
+ http.get(uri.path)
504
+ end
505
+
506
+ assert_equal 'test example content', response.body
507
+ end
508
+
509
+ def test_mock_via_nil_proxy
510
+ response = nil
511
+ proxy_address = nil
512
+ proxy_port = nil
513
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
514
+ uri = URI.parse('http://mock/test_example.txt')
515
+ http = Net::HTTP::Proxy(proxy_address, proxy_port).new(
516
+ uri.host, (uri.port or 80))
517
+ response = http.start do
518
+ http.get(uri.path)
519
+ end
520
+
521
+ assert_equal 'test example content', response.body
522
+ end
523
+
524
+ def test_response_type
525
+ FakeWeb.register_uri('http://mock/test_example.txt', :string => "test")
526
+ Net::HTTP.start('mock') do |http|
527
+ response = http.get('/test_example.txt')
528
+ assert_kind_of(Net::HTTPSuccess, response)
529
+ end
530
+ end
531
+
532
+ def test_mock_request_that_raises_an_http_error_with_a_specific_status
533
+ FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
534
+ exception = assert_raises(Net::HTTPError) do
535
+ Net::HTTP.start('mock') { |http| response = http.get('/raising_exception.txt') }
536
+ end
537
+ assert_equal '404', exception.response.code
538
+ assert_equal 'Not Found', exception.response.msg
539
+ end
540
+
541
+ def test_mock_rotate_responses
542
+ FakeWeb.register_uri('http://mock/multiple_test_example.txt',
543
+ [ {:file => File.dirname(__FILE__) + '/fixtures/test_example.txt', :times => 2},
544
+ {:string => "thrice", :times => 3},
545
+ {:string => "ever_more"} ])
546
+
547
+ uri = URI.parse('http://mock/multiple_test_example.txt')
548
+ 2.times { assert_equal 'test example content', Net::HTTP.get(uri) }
549
+ 3.times { assert_equal 'thrice', Net::HTTP.get(uri) }
550
+ 4.times { assert_equal 'ever_more', Net::HTTP.get(uri) }
551
+ end
552
+
553
+ def test_mock_request_using_response_with_transfer_encoding_header_has_valid_transfer_encoding_header
554
+ FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_with_transfer_encoding')
555
+ response = nil
556
+ Net::HTTP.start('www.google.com') do |query|
557
+ response = query.get('/')
558
+ end
559
+ assert_not_nil response['transfer-encoding']
560
+ assert response['transfer-encoding'] == 'chunked'
561
+ end
562
+
563
+ def test_mock_request_using_response_without_transfer_encoding_header_does_not_have_a_transfer_encoding_header
564
+ FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
565
+ response = nil
566
+ Net::HTTP.start('www.google.com') do |query|
567
+ response = query.get('/')
568
+ end
569
+ assert !response.key?('transfer-encoding')
570
+ end
571
+
572
+ def test_mock_request_using_response_from_curl_has_original_transfer_encoding_header
573
+ FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_from_curl')
574
+ response = nil
575
+ Net::HTTP.start('www.google.com') do |query|
576
+ response = query.get('/')
577
+ end
578
+ assert_not_nil response['transfer-encoding']
579
+ assert response['transfer-encoding'] == 'chunked'
580
+ end
581
+
582
+ def test_txt_file_should_have_three_lines
583
+ FakeWeb.register_uri('http://www.google.com/', :file => File.dirname(__FILE__) + '/fixtures/test_txt_file')
584
+ response = nil
585
+ Net::HTTP.start('www.google.com') do |query|
586
+ response = query.get('/')
587
+ end
588
+ assert response.body.split(/\n/).size == 3, "response has #{response.body.split(/\n/).size} lines should have 3"
589
+ end
590
+
591
+ def test_requiring_fakeweb_instead_of_fake_web
592
+ require "fakeweb"
593
+ end
594
+
595
+ def test_registering_using_response_with_string_containing_null_byte
596
+ # Regression test for File.exists? raising an ArgumentError ("string
597
+ # contains null byte") since :response first tries to find by filename.
598
+ # The string should be treated as a response body, instead, and an
599
+ # EOFError is raised when the byte is encountered.
600
+ FakeWeb.register_uri("http://example.com", :response => "test\0test")
601
+ assert_raise EOFError do
602
+ Net::HTTP.get(URI.parse("http://example.com"))
603
+ end
604
+ end
605
+
606
+ def test_http_version_from_string_response
607
+ FakeWeb.register_uri(:get, "http://example.com", :string => "example")
608
+ response = Net::HTTP.start("example.com") { |http| http.get("/") }
609
+ assert_equal "1.0", response.http_version
610
+ end
611
+
612
+ def test_http_version_from_file_response
613
+ FakeWeb.register_uri(:get, "http://example.com", :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
614
+ response = Net::HTTP.start("example.com") { |http| http.get("/") }
615
+ assert_equal "1.0", response.http_version
616
+ end
617
+ end