em-http-request 0.2.9 → 0.3.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.
@@ -1,21 +1,20 @@
1
- HTTP/1.0 200 OK
2
- Date: Mon, 16 Nov 2009 20:39:15 GMT
3
- Expires: -1
4
- Cache-Control: private, max-age=0
5
- Content-Type: text/html; charset=ISO-8859-1
6
- Set-Cookie: PREF=ID=9454187d21c4a6a6:TM=1258403955:LM=1258403955:S=2-mf1n5oV5yAeT9-; expires=Wed, 16-Nov-2011 20:39:15 GMT; path=/; domain=.google.ca
7
- Set-Cookie: NID=28=lvxxVdiBQkCetu_WFaUxLyB7qPlHXS5OdAGYTqge_laVlCKVN8VYYeVBh4bNZiK_Oan2gm8oP9GA-FrZfMPC3ZMHeNq37MG2JH8AIW9LYucU8brOeuggMEbLNNXuiWg4; expires=Tue, 18-May-2010 20:39:15 GMT; path=/; domain=.google.ca; HttpOnly
8
- Server: gws
9
- X-XSS-Protection: 0
10
- X-Cache: MISS from .
11
- Via: 1.0 .:80 (squid)
12
- Connection: close
13
-
14
- <!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"><title>Google</title><script>window.google={kEI:"eLgBS4LROqCQedKVvfUL",kEXPI:"17259,21329,21516,22107",kCSI:{e:"17259,21329,21516,22107",ei:"eLgBS4LROqCQedKVvfUL"},kHL:"en",time:function(){return(new Date).getTime()},log:function(b,d,c){var a=new Image,e=google,g=e.lc,f=e.li;a.onerror=(a.onload=(a.onabort=function(){delete g[f]}));g[f]=a;c=c||"/gen_204?atyp=i&ct="+b+"&cad="+d+"&zx="+google.time();a.src=c;e.li=f+1},lc:[],li:0};
15
- window.google.sn="webhp";window.google.timers={load:{t:{start:(new Date).getTime()}}};try{}catch(b){}window.google.jsrt_kill=1;
16
- var _gjwl=location;function _gjuc(){var e=_gjwl.href.indexOf("#");if(e>=0){var a=_gjwl.href.substring(e);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 f=a.substring(d,b);if(f.indexOf("fp=")==0){a=a.substring(0,c)+a.substring(b,a.length);b=c}else if(f=="cad=h")return 0;c=b}_gjwl.href="/search?"+a+"&cad=h";return 1}}}return 0}function _gjp(){!(window._gjwl.hash&&
17
- window._gjuc())&&setTimeout(_gjp,500)};
18
- window._gjp && _gjp()</script><style>td{line-height:.8em;}.gac_m td{line-height:17px;}form{margin-bottom:20px;}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}em{font-weight:bold;font-style:normal}.lst{font:17px arial,sans-serif;margin-bottom:.2em;vertical-align:bottom;}input{font-family:inherit}.lsb,.gac_sb{font-size:15px;height:1.85em!important;margin:.2em;}#gbar{height:22px}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}#guser{padding-bottom:7px !important;text-align:right}#gbar,#guser{font-size:13px;padding-top:1px !important}@media all{.gb1,.gb3{height:22px;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb3,a.gb4{color:#00c !important}.gb3{text-decoration:none}</style><script>google.y={};google.x=function(e,g){google.y[e.id]=[e,g];return false};</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_logo7.png'" topmargin=3 marginheight=3><textarea id=csi style=display:none></textarea><div id=gbar><nobr><b class=gb1>Web</b> <a href="http://images.google.ca/imghp?hl=en&tab=wi" class=gb1>Images</a> <a href="http://video.google.ca/?hl=en&tab=wv" class=gb1>Videos</a> <a href="http://maps.google.ca/maps?hl=en&tab=wl" class=gb1>Maps</a> <a href="http://news.google.ca/nwshp?hl=en&tab=wn" class=gb1>News</a> <a href="http://groups.google.ca/grphp?hl=en&tab=wg" class=gb1>Groups</a> <a href="http://mail.google.com/mail/?hl=en&tab=wm" class=gb1>Gmail</a> <a href="http://www.google.ca/intl/en/options/" class=gb3><u>more</u> &raquo;</a></nobr></div><div id=guser width=100%><nobr><a href="/url?sa=p&pref=ig&pval=3&q=http://www.google.ca/ig%3Fhl%3Den%26source%3Diglk&usg=AFQjCNG2Kt7TgMZuV7Fl3FeeTOmTWMvggA" class=gb4>iGoogle</a> | <a href="/preferences?hl=en" class=gb4>Search settings</a> | <a href="https://www.google.com/accounts/Login?hl=en&continue=http://www.google.ca/" class=gb4>Sign in</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div><center><br clear=all id=lgpd><img alt="Google" height=110 src="/intl/en_ca/images/logo.gif" width=276 id=logo onload="window.lol&&lol()"><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 name=source type=hidden value=hp><input type=hidden name=ie value="ISO-8859-1"><input autocomplete="off" maxlength=2048 name=q size=55 class=lst title="Google Search" value=""><br><input name=btnG type=submit value="Google Search" class=lsb><input name=btnI type=submit value="I'm Feeling Lucky" class=lsb></td><td nowrap width=25% align=left><font size=-2>&nbsp;&nbsp;<a href=/advanced_search?hl=en>Advanced Search</a><br>&nbsp;&nbsp;<a href=/language_tools?hl=en>Language Tools</a></font></td></tr><tr><td align=center colspan=3><font size=-1><span style="text-align:left">Search: <input id=all type=radio name=meta value="" checked><label for=all> the web </label> <input id=cty type=radio name=meta value="cr=countryCA"><label for=cty> pages from Canada </label> </span></font></td></tr></table></form><br><font size=-1>Google.ca offered in: <a href="http://www.google.ca/setprefs?sig=0_XtMil90_yvnNEW8dIglASaHCVhU=&hl=fr">fran?ais</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> - <a href="http://www.google.com/ncr">Go to Google.com</a></font><p><font size=-2>&copy;2009 - <a href="/intl/en/privacy.html">Privacy</a></font></p></center><div id=xjsd></div><div id=xjsi><script>if(google.y)google.y.first=[];if(google.y)google.y.first=[];google.dstr=[];google.rein=[];window.setTimeout(function(){var a=document.createElement("script");a.src="/extern_js/f/CgJlbhICY2EgACswCjhBQB0sKzAOOAksKzAYOAQsKzAlOMmIASwrMCY4BywrMCc4Aiw/n_sssePDGvc.js";(document.getElementById("xjsd")||document.body).appendChild(a)},0);
19
- ;google.y.first.push(function(){google.ac.b=true;google.ac.i(document.f,document.f.q,'','')});google.xjs&&google.j&&google.j.xi&&google.j.xi()</script></div><script>(function(){
20
- function a(){google.timers.load.t.ol=(new Date).getTime();google.report&&google.timers.load.t.xjs&&google.report(google.timers.load,google.kCSI)}if(window.addEventListener)window.addEventListener("load",a,false);else if(window.attachEvent)window.attachEvent("onload",a);google.timers.load.t.prt=(new Date).getTime();
21
- })();
1
+ HTTP/1.1 200 OK
2
+ Date: Sat, 14 Aug 2010 21:58:26 GMT
3
+ Expires: -1
4
+ Cache-Control: private, max-age=0
5
+ Content-Type: text/html; charset=ISO-8859-1
6
+ Set-Cookie: PREF=ID=11955ae9690fd292:TM=1281823106:LM=1281823106:S=wHdloFqGQ_OLSE92; expires=Mon, 13-Aug-2012 21:58:26 GMT; path=/; domain=.google.ca
7
+ Set-Cookie: NID=37=USTdOsxOSMbLjphkJ3S5Ueua3Yc23COXuK_pbztcHx7JoyhomwQySrvebCf3_u8eyrBiLWssVzaZcEOiKGEJbNdy8lRhnq_mfrdz693LaMjNPh__ccW4sgn1ZO6nQltE; expires=Sun, 13-Feb-2011 21:58:26 GMT; path=/; domain=.google.ca; HttpOnly
8
+ Server: gws
9
+ X-XSS-Protection: 1; mode=block
10
+
11
+ <!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"><title>Google</title><script>window.google={kEI:"ghFnTJiGG9WhnQf4o9X8BA",kEXPI:"25913,25976,26060",kCSI:{e:"25913,25976,26060",ei:"ghFnTJiGG9WhnQf4o9X8BA",expi:"25913,25976,26060"},ml:function(){},kHL:"en",time:function(){return(new Date).getTime()},log:function(b,d,c){var a=new Image,e=google,g=e.lc,f=e.li;a.onerror=(a.onload=(a.onabort=function(){delete g[f]}));g[f]=a;c=c||"/gen_204?atyp=i&ct="+b+"&cad="+d+"&zx="+google.time();a.src=c;e.li=f+1},lc:[],li:0,Toolbelt:{}};
12
+ window.google.sn="webhp";window.google.timers={load:{t:{start:(new Date).getTime()}}};try{}catch(u){}window.google.jsrt_kill=1;
13
+ var _gjwl=location;function _gjuc(){var e=_gjwl.href.indexOf("#");if(e>=0){var a=_gjwl.href.substring(e);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 f=a.substring(d,b);if(f.indexOf("fp=")==0){a=a.substring(0,c)+a.substring(b,a.length);b=c}else if(f=="cad=h")return 0;c=b}_gjwl.href="/search?"+a+"&cad=h";return 1}}}return 0}function _gjp(){!(window._gjwl.hash&&
14
+ window._gjuc())&&setTimeout(_gjp,500)};
15
+ window._gjp && _gjp()</script><style id=gstyle>body{margin:0}#gog{padding:3px 8px 0}td{line-height:.8em;}.gac_m td{line-height:17px;}form{margin-bottom:20px;}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}em{font-weight:bold;font-style:normal}.lst{width:496px}.tiah{width:458px}input{font-family:inherit}a.gb1,a.gb2,a.gb3,a.gb4{color:#11c !important}#gbar,#guser{font-size:13px;padding-top:1px !important}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{color:#00c !important}body{background:#fff;color:black}input{-moz-box-sizing:content-box}a{color:#11c;text-decoration:none}a:hover,a:active{text-decoration:underline}.fl a{color:#4272db}a:visited{color:#551a8b}a.gb1,a.gb4{text-decoration:underline}a.gb3:hover{text-decoration:none}#ghead a.gb2:hover{color:#fff!important}.ds{display:-moz-inline-box}.ds{border-bottom:solid 1px #e7e7e7;border-right:solid 1px #e7e7e7;display:inline-block;margin:3px 0 4px;margin-left:4px}.sblc{padding-top:5px}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px;}.lsbb{background:#eee;border:solid 1px;border-color:#ccc #999 #999 #ccc;height:30px;display:block}.lsb{background:url(/images/srpr/nav_logo14.png) bottom;font:15px arial,sans-serif;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;vertical-align:top}.lsb:active{background:#ccc}.lst:focus{outline:none}.ftl,#fll a{margin:0 12px}#addlang a{padding:0 3px}.gac_v div{display:none}.gac_v .gac_v2,.gac_bt{display:block!important}</style><script>google.y={};google.x=function(e,g){google.y[e.id]=[e,g];return false};</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/srpr/nav_logo14.png'" ><textarea id=csi style=display:none></textarea><div id=ghead><div id=gbar><nobr><b class=gb1>Web</b> <a onclick=gbar.qs(this) href="http://www.google.ca/imghp?hl=en&tab=wi" class=gb1>Images</a> <a onclick=gbar.qs(this) href="http://video.google.ca/?hl=en&tab=wv" class=gb1>Videos</a> <a onclick=gbar.qs(this) href="http://maps.google.ca/maps?hl=en&tab=wl" class=gb1>Maps</a> <a onclick=gbar.qs(this) href="http://news.google.ca/nwshp?hl=en&tab=wn" class=gb1>News</a> <a onclick=gbar.qs(this) href="http://books.google.ca/bkshp?hl=en&tab=wp" class=gb1>Books</a> <a href="http://mail.google.com/mail/?hl=en&tab=wm" class=gb1>Gmail</a> <a href="http://www.google.ca/intl/en/options/" class=gb1 style="text-decoration:none"><u>more</u> &raquo;</a></nobr></div><div id=guser width=100%><nobr><span id=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe><a href="/url?sa=p&pref=ig&pval=3&q=http://www.google.ca/ig%3Fhl%3Den%26source%3Diglk&usg=AFQjCNG2Kt7TgMZuV7Fl3FeeTOmTWMvggA" class=gb4>iGoogle</a> | </span><a href="/preferences?hl=en" class=gb4>Settings</a> | <a href="https://www.google.com/accounts/Login?hl=en&continue=http://www.google.ca/" class=gb4>Sign in</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div></div> <center><br clear=all id=lgpd><div id=lga><table cellpadding=0 cellspacing=0 border=0><tr><td align=right valign=bottom><img src=images/hp0.gif width=158 height=78 alt="Google"></td><td valign=bottom><img src=images/hp1.gif width=50 height=78 alt=""></td><td valign=bottom><img src=images/hp2.gif width=68 height=78 alt=""></td></tr><tr><td class=h align=right valign=top><b></b></td><td valign=top><img src=images/hp3.gif width=50 height=32 alt=""></td><td valign=top class=h><font color=#666666 style=font-size:16px><b>Canada</b></font></td></tr></table><br></div><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 name=source type=hidden value=hp><input type=hidden name=ie value="ISO-8859-1"><div class=ds style="height:32px;margin:4px 0"><input autocomplete="off" maxlength=2048 name=q class="lst" title="Google Search" value="" size=57 style="background:#fff;border:1px solid #ccc;border-bottom-color:#999;border-right-color:#999;color:#000;font:18px arial,sans-serif bold;height:25px;margin:0;padding:5px 8px 0 6px;vertical-align:top"></div><br style="line-height:0"><span class=ds><span class=lsbb><input name=btnG type=submit value="Google Search" class=lsb></span></span><span class=ds><span class=lsbb><input name=btnI type=submit value="I&#39;m Feeling Lucky" class=lsb></span></span></td><td nowrap width=25% align=left class=sblc><a href="/advanced_search?hl=en">Advanced Search</a><a href="/language_tools?hl=en">Language Tools</a></td></tr></table></form><div style="font-size:83%;min-height:3.5em"><br><div id=als><font size=-1 id=addlang>Google.ca offered in: <a href="http://www.google.ca/setprefs?sig=0_h3wwdBzWMwIKco5_3UM-S8-NI00=&amp;hl=fr">fran�ais</a></font><br><br></div></div><div id=res></div><span id=footer><center id=fctr><div style="font-size:10pt"><div id=fll style="margin:19px auto 19px auto;text-align:center"><a href="/intl/en/ads/">Advertising&nbsp;Programs</a><a href="/services/">Business Solutions</a><a href="/intl/en/about.html">About Google</a><a href="http://www.google.com/ncr">Go to Google.com</a></div></div><p style="color:#767676;font-size:8pt">&copy; 2010 - <a href="/intl/en/privacy.html">Privacy</a></p></center></span> <div id=xjsd></div><div id=xjsi><script>if(google.y)google.y.first=[];if(google.y)google.y.first=[];google.dstr=[];google.rein=[];window.setTimeout(function(){var a=document.createElement("script");a.src="/extern_js/f/CgJlbhICY2EgACswRTgBLCswWjgDLCswDjgXLCswFzgHLCswJzgELCswPDgDLCswCjhzQB0sKzAlOMqIASwrMEA4EiwrMEE4BSwrMFQ4ACwrMBg4BSwrMCY4DSyAAheQAhY/shQ1OaVztmo.js";(document.getElementById("xjsd")||document.body).appendChild(a);if(google.timers&&google.timers.load.t)google.timers.load.t.xjsls=(new Date).getTime();},0);
16
+ ;google.neegg=1;google.y.first.push(function(){var form=document.f||document.f||document.gs;google.ac.i(form,form.q,'','','',{o:1,sw:1});google.History&&google.History.initialize('/')});if(google.j&&google.j.en&&google.j.xi){window.setTimeout(google.j.xi,0);google.fade=null;}</script></div><script>(function(){
17
+ var b,d,e,f;function g(a,c){if(a.removeEventListener){a.removeEventListener("load",c,false);a.removeEventListener("error",c,false)}else{a.detachEvent("onload",c);a.detachEvent("onerror",c)}}function h(a){f=(new Date).getTime();++d;a=a||window.event;var c=a.target||a.srcElement;g(c,h)}var i=document.getElementsByTagName("img");b=i.length;d=0;for(var j=0,k;j<b;++j){k=i[j];if(k.complete||typeof k.src!="string"||!k.src)++d;else if(k.addEventListener){k.addEventListener("load",h,false);k.addEventListener("error",
18
+ h,false)}else{k.attachEvent("onload",h);k.attachEvent("onerror",h)}}e=b-d;function l(){if(!google.timers.load.t)return;google.timers.load.t.ol=(new Date).getTime();google.timers.load.t.iml=f;google.kCSI.imc=d;google.kCSI.imn=b;google.kCSI.imp=e;google.report&&google.report(google.timers.load,google.kCSI)}if(window.addEventListener)window.addEventListener("load",l,false);else if(window.attachEvent)window.attachEvent("onload",l);google.timers.load.t.prt=(f=(new Date).getTime());
19
+ })();
20
+ </script>
data/spec/helper.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  require 'rubygems'
2
- require 'spec'
2
+ require 'rspec'
3
3
  require 'pp'
4
4
 
5
- require 'lib/em-http'
6
- require 'lib/em-http/mock'
7
- require 'em-websocket'
5
+ $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+
7
+ require 'em-http'
8
+ require 'em-websocket'
data/spec/mock_spec.rb CHANGED
@@ -1,27 +1,73 @@
1
- require 'spec/helper'
1
+ require 'helper'
2
2
 
3
3
  describe 'em-http mock' do
4
4
 
5
+ before(:all) do
6
+ EventMachine::MockHttpRequest.activate!
7
+ end
8
+
9
+ after(:all) do
10
+ EventMachine::MockHttpRequest.deactivate!
11
+ end
12
+
5
13
  before(:each) do
6
14
  EventMachine::MockHttpRequest.reset_registry!
7
15
  EventMachine::MockHttpRequest.reset_counts!
8
16
  end
9
-
17
+
18
+ it "should serve a fake http request from a proc" do
19
+ EventMachine::HttpRequest.register('http://www.google.ca:80/', :get) { |req|
20
+ req.response_header.http_status = 200
21
+ req.response_header['SOME_WACKY_HEADER'] = 'WACKY_HEADER_VALUE'
22
+ req.response = "Well, now this is fun."
23
+ }
24
+ EM.run {
25
+ http = EventMachine::HttpRequest.new('http://www.google.ca/').get
26
+ http.errback { fail }
27
+ http.callback {
28
+ http.response_header.status.should == 200
29
+ http.response_header['SOME_WACKY_HEADER'].should == 'WACKY_HEADER_VALUE'
30
+ http.response.should == "Well, now this is fun."
31
+ EventMachine.stop
32
+ }
33
+ }
34
+
35
+ EventMachine::HttpRequest.count('http://www.google.ca:80/', :get, {}).should == 1
36
+ end
37
+
38
+ it "should serve a fake http request from a proc with raw data" do
39
+ EventMachine::HttpRequest.register('http://www.google.ca:80/', :get) { |req|
40
+ req.receive_data(File.read(File.join(File.dirname(__FILE__), 'fixtures', 'google.ca')))
41
+ }
42
+ EM.run {
43
+ http = EventMachine::HttpRequest.new('http://www.google.ca/').get
44
+ http.errback { fail }
45
+ http.callback {
46
+ http.response_header.status.should == 200
47
+ http.response.should == File.read(File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'), :encoding => 'ISO-8859-1').split("\r\n\r\n", 2).last
48
+ http.response.encoding.to_s.should == 'ISO-8859-1'
49
+ EventMachine::HttpRequest.count('http://www.google.ca:80/', :get, {}).should == 1
50
+ EventMachine.stop
51
+ }
52
+ }
53
+ end
54
+
10
55
  it "should serve a fake http request from a file" do
11
- EventMachine::MockHttpRequest.register_file('http://www.google.ca:80/', :get, {}, File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'))
56
+ EventMachine::HttpRequest.register_file('http://www.google.ca:80/', :get, {}, File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'))
12
57
  EM.run {
13
-
14
- http = EventMachine::MockHttpRequest.new('http://www.google.ca/').get
58
+
59
+ http = EventMachine::HttpRequest.new('http://www.google.ca/').get
15
60
  http.errback { fail }
16
61
  http.callback {
17
- http.response.should == File.read(File.join(File.dirname(__FILE__), 'fixtures', 'google.ca')).split("\n\n", 2).last
62
+ http.response_header.status.should == 200
63
+ http.response.should == File.read(File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'), :encoding => 'ISO-8859-1').split("\r\n\r\n", 2).last
64
+ http.response.encoding.to_s.should == 'ISO-8859-1'
65
+ EventMachine::HttpRequest.count('http://www.google.ca:80/', :get, {}).should == 1
18
66
  EventMachine.stop
19
67
  }
20
68
  }
21
-
22
- EventMachine::MockHttpRequest.count('http://www.google.ca:80/', :get, {}).should == 1
23
69
  end
24
-
70
+
25
71
  it "should serve a fake http request from a string" do
26
72
  data = <<-HEREDOC
27
73
  HTTP/1.0 200 OK
@@ -46,70 +92,73 @@ window._gjp && _gjp()</script><style>td{line-height:.8em;}.gac_m td{line-height:
46
92
  function a(){google.timers.load.t.ol=(new Date).getTime();google.report&&google.timers.load.t.xjs&&google.report(google.timers.load,google.kCSI)}if(window.addEventListener)window.addEventListener("load",a,false);else if(window.attachEvent)window.attachEvent("onload",a);google.timers.load.t.prt=(new Date).getTime();
47
93
  })();
48
94
  HEREDOC
49
- EventMachine::MockHttpRequest.register('http://www.google.ca:80/', :get, {}, data)
95
+ EventMachine::HttpRequest.register('http://www.google.ca:80/', :get, {}, data)
50
96
  EventMachine.run {
51
-
52
- http = EventMachine::MockHttpRequest.new('http://www.google.ca/').get
97
+
98
+ http = EventMachine::HttpRequest.new('http://www.google.ca/').get
53
99
  http.errback { fail }
54
100
  http.callback {
55
101
  http.response.should == data.split("\n\n", 2).last
102
+ EventMachine::HttpRequest.count('http://www.google.ca:80/', :get, {}).should == 1
56
103
  EventMachine.stop
57
104
  }
58
105
  }
59
-
60
- EventMachine::MockHttpRequest.count('http://www.google.ca:80/', :get, {}).should == 1
106
+
61
107
  end
62
108
 
63
109
  it "should serve a fake failing http request" do
64
- EventMachine::MockHttpRequest.register('http://www.google.ca:80/', :get, {}, :fail)
110
+ EventMachine::HttpRequest.register('http://www.google.ca:80/', :get, {}, :fail)
65
111
  error = false
66
112
 
67
113
  EventMachine.run {
68
- http = EventMachine::MockHttpRequest.new('http://www.google.ca/').get
114
+ http = EventMachine::HttpRequest.new('http://www.google.ca/').get
115
+ http.callback {
116
+ EventMachine.stop
117
+ fail
118
+ }
69
119
  http.errback {
70
- error = true
120
+ EventMachine::HttpRequest.count('http://www.google.ca:80/', :get, {}).should == 1
71
121
  EventMachine.stop
72
122
  }
73
123
  }
74
124
 
75
- error.should be_true
76
- EventMachine::MockHttpRequest.count('http://www.google.ca:80/', :get, {}).should == 1
77
125
  end
78
-
126
+
79
127
  it "should distinguish the cache by the given headers" do
80
- EventMachine::MockHttpRequest.register_file('http://www.google.ca:80/', :get, {:user_agent => 'BERT'}, File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'))
81
- EventMachine::MockHttpRequest.register_file('http://www.google.ca:80/', :get, {}, File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'))
128
+ EventMachine::HttpRequest.register_file('http://www.google.ca:80/', :get, {:user_agent => 'BERT'}, File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'))
129
+ EventMachine::HttpRequest.register_file('http://www.google.ca:80/', :get, {}, File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'))
82
130
  EM.run {
83
-
84
- http = EventMachine::MockHttpRequest.new('http://www.google.ca/').get
131
+ http = EventMachine::HttpRequest.new('http://www.google.ca/').get
85
132
  http.errback { fail }
86
133
  http.callback {
87
- http.response.should == File.read(File.join(File.dirname(__FILE__), 'fixtures', 'google.ca')).split("\n\n", 2).last
134
+ http.response_header.status.should == 200
135
+ http.response.should == File.read(File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'), :encoding => 'ISO-8859-1').split("\r\n\r\n", 2).last
136
+ http.response.encoding.to_s.should == 'ISO-8859-1'
137
+ EventMachine::HttpRequest.count('http://www.google.ca:80/', :get, {}).should == 1
138
+ EventMachine::HttpRequest.count('http://www.google.ca:80/', :get, {:user_agent => 'BERT'}).should == 0
88
139
  EventMachine.stop
89
140
  }
90
141
  }
91
-
92
- EventMachine::MockHttpRequest.count('http://www.google.ca:80/', :get, {}).should == 1
93
- EventMachine::MockHttpRequest.count('http://www.google.ca:80/', :get, {:user_agent => 'BERT'}).should == 0
94
-
142
+
95
143
  EM.run {
96
-
97
- http = EventMachine::MockHttpRequest.new('http://www.google.ca/').get({:head => {:user_agent => 'BERT'}})
144
+ http = EventMachine::HttpRequest.new('http://www.google.ca/').get({:head => {:user_agent => 'BERT'}})
98
145
  http.errback { fail }
99
146
  http.callback {
100
- http.response.should == File.read(File.join(File.dirname(__FILE__), 'fixtures', 'google.ca')).split("\n\n", 2).last
147
+ http.response_header.status.should == 200
148
+ http.response.should == File.read(File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'), :encoding => 'ISO-8859-1').split("\r\n\r\n", 2).last
149
+ http.response.encoding.to_s.should == 'ISO-8859-1'
150
+ EventMachine::HttpRequest.count('http://www.google.ca:80/', :get, {:user_agent => 'BERT'}).should == 1
101
151
  EventMachine.stop
102
152
  }
103
153
  }
104
-
105
- EventMachine::MockHttpRequest.count('http://www.google.ca:80/', :get, {:user_agent => 'BERT'}).should == 1
154
+
106
155
  end
107
156
 
108
157
  it "should raise an exception if pass-thru is disabled" do
109
- EventMachine::MockHttpRequest.pass_through_requests = false
158
+ EventMachine::HttpRequest.pass_through_requests = false
110
159
  EventMachine.run {
111
160
  proc {
112
- http = EventMachine::MockHttpRequest.new('http://www.google.ca/').get
161
+ http = EventMachine::HttpRequest.new('http://www.google.ca/').get
113
162
  }.should raise_error
114
163
  EventMachine.stop
115
164
  }
data/spec/multi_spec.rb CHANGED
@@ -1,51 +1,68 @@
1
- require 'spec/helper'
2
- require 'spec/stallion'
3
-
4
- describe EventMachine::MultiRequest do
5
-
6
- it "should submit multiple requests in parallel and return once all of them are complete" do
7
- EventMachine.run {
8
-
9
- # create an instance of multi-request handler, and the requests themselves
10
- multi = EventMachine::MultiRequest.new
11
-
12
- # add multiple requests to the multi-handler
13
- multi.add(EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get(:query => {:q => 'test'}))
14
- multi.add(EventMachine::HttpRequest.new('http://0.0.0.0:8083/').get(:timeout => 1))
15
-
16
- multi.callback {
17
- # verify successful request
18
- multi.responses[:succeeded].size.should == 1
19
- multi.responses[:succeeded].first.response.should match(/test/)
20
-
21
- # verify invalid requests
22
- multi.responses[:failed].size.should == 1
23
- multi.responses[:failed].first.response_header.status.should == 0
24
-
25
- EventMachine.stop
26
- }
27
- }
28
- end
29
-
30
- it "should handle multiple mock requests" do
31
- EventMachine::MockHttpRequest.register_file('http://127.0.0.1:8080/', :get, {}, File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'))
32
- EventMachine::MockHttpRequest.register_file('http://0.0.0.0:8083/', :get, {}, File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'))
33
-
34
- EventMachine.run {
35
-
36
- # create an instance of multi-request handler, and the requests themselves
37
- multi = EventMachine::MultiRequest.new
38
-
39
- # add multiple requests to the multi-handler
40
- multi.add(EventMachine::MockHttpRequest.new('http://127.0.0.1:8080/').get)
41
- multi.add(EventMachine::MockHttpRequest.new('http://0.0.0.0:8083/').get)
42
-
43
- multi.callback {
44
- # verify successful request
45
- multi.responses[:succeeded].size.should == 2
46
-
47
- EventMachine.stop
48
- }
49
- }
50
- end
51
- end
1
+ require 'helper'
2
+ require 'stallion'
3
+
4
+ describe EventMachine::MultiRequest do
5
+
6
+ it "should submit multiple requests in parallel and return once all of them are complete" do
7
+ EventMachine.run {
8
+
9
+ # create an instance of multi-request handler, and the requests themselves
10
+ multi = EventMachine::MultiRequest.new
11
+
12
+ # add multiple requests to the multi-handler
13
+ multi.add(EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get(:query => {:q => 'test'}))
14
+ multi.add(EventMachine::HttpRequest.new('http://0.0.0.0:8083/').get(:timeout => 1))
15
+
16
+ multi.callback {
17
+ # verify successful request
18
+ multi.responses[:succeeded].size.should == 1
19
+ multi.responses[:succeeded].first.response.should match(/test/)
20
+
21
+ # verify invalid requests
22
+ multi.responses[:failed].size.should == 1
23
+ multi.responses[:failed].first.response_header.status.should == 0
24
+
25
+ EventMachine.stop
26
+ }
27
+ }
28
+ end
29
+
30
+ it "should accept multiple open connections and return once all of them are complete" do
31
+ EventMachine.run {
32
+ http1 = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get(:query => {:q => 'test'})
33
+ http2 = EventMachine::HttpRequest.new('http://0.0.0.0:8083/').get(:timeout => 1)
34
+
35
+ multi = EventMachine::MultiRequest.new([http1, http2]) do
36
+ multi.responses[:succeeded].size.should == 1
37
+ multi.responses[:succeeded].first.response.should match(/test/)
38
+
39
+ multi.responses[:failed].size.should == 1
40
+ multi.responses[:failed].first.response_header.status.should == 0
41
+
42
+ EventMachine.stop
43
+ end
44
+ }
45
+ end
46
+
47
+ it "should handle multiple mock requests" do
48
+ EventMachine::MockHttpRequest.register_file('http://127.0.0.1:8080/', :get, {}, File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'))
49
+ EventMachine::MockHttpRequest.register_file('http://0.0.0.0:8083/', :get, {}, File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'))
50
+
51
+ EventMachine.run {
52
+
53
+ # create an instance of multi-request handler, and the requests themselves
54
+ multi = EventMachine::MultiRequest.new
55
+
56
+ # add multiple requests to the multi-handler
57
+ multi.add(EventMachine::MockHttpRequest.new('http://127.0.0.1:8080/').get)
58
+ multi.add(EventMachine::MockHttpRequest.new('http://0.0.0.0:8083/').get)
59
+
60
+ multi.callback {
61
+ # verify successful request
62
+ multi.responses[:succeeded].size.should == 2
63
+
64
+ EventMachine.stop
65
+ }
66
+ }
67
+ end
68
+ end