ping_spec.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:6k
源码类别:

Ajax

开发平台:

Others

  1. require File.dirname(__FILE__) + '/../spec_helper'
  2. describe 'Given a post which references a pingback enabled article' do
  3.   def pingback_target; 'http://anotherblog.org/xml-rpc'; end
  4.   def referenced_url; 'http://anotherblog.org/a-post'; end
  5.   def referrer_url; 'http://myblog.net/referring-post'; end
  6.   before(:each) do
  7.     @mock_response = mock('response')
  8.     @mock_xmlrpc_response = mock('xmlrpc_response')
  9.   end
  10.   it 'Pingback sent to url found in referenced header' do
  11.     @mock_response.should_receive(:[]).with('X-Pingback').at_least(:once).and_return(pingback_target)
  12.     @mock_xmlrpc_response.should_receive(:call).with('pingback.ping', referrer_url, referenced_url)
  13.     make_and_send_ping
  14.   end
  15.   it 'Pingback sent to url found in referenced body' do
  16.     @mock_response.should_receive(:[]).with('X-Pingback').at_least(:once).and_return(nil)
  17.     @mock_response.should_receive(:body).at_least(:once)
  18.       .and_return(%{<link rel="pingback" href="http://anotherblog.org/xml-rpc" />})
  19.     @mock_xmlrpc_response.should_receive(:call).with('pingback.ping', referrer_url, referenced_url)
  20.     make_and_send_ping
  21.   end
  22.   it 'Pingback sent when new article is saved' do
  23.     ActiveRecord::Base.observers.should include(:email_notifier)
  24.     ActiveRecord::Base.observers.should include(:web_notifier)
  25.     blog = Blog.default
  26.     blog.should_not be_send_outbound_pings
  27.     blog.send_outbound_pings = 1
  28.     blog.save!
  29.     blog.should be_send_outbound_pings
  30.     a = Article.new 
  31.       :body => '<a href="http://anotherblog.org/a-post">',
  32.       :title => 'Test the pinging',
  33.       :published => true
  34.     Net::HTTP.should_receive(:get_response).and_return(@mock_response)
  35.     XMLRPC::Client.should_receive(:new2).with(pingback_target).and_return(@mock_xmlrpc_response)
  36.     @mock_response.should_receive(:[]).with('X-Pingback').at_least(:once).and_return(pingback_target)
  37.     @mock_xmlrpc_response.should_receive(:call)
  38.       .with('pingback.ping',
  39.             %r{http://myblog.net/d{4}/d{2}/d{2}/test-the-pinging},
  40.             referenced_url)
  41.     a.should have(1).html_urls
  42.     a.save!
  43.     a.should be_just_published
  44.     a = Article.find(a.id)
  45.     a.should_not be_just_published
  46.     # Saving again will not resend the pings
  47.     a.save
  48.   end
  49.   def make_and_send_ping
  50.     Net::HTTP.should_receive(:get_response).and_return(@mock_response)
  51.     XMLRPC::Client.should_receive(:new2).with(pingback_target).and_return(@mock_xmlrpc_response)
  52.     ping = contents(:article1).pings.build("url" => referenced_url)
  53.     ping.should be_instance_of(Ping)
  54.     ping.url.should == referenced_url
  55.     ping.send_pingback_or_trackback(referrer_url)
  56.   end
  57. end
  58. describe "An article links to another article, which contains a trackback URL" do
  59.   def referenced_url;  'http://anotherblog.org/a-post'; end
  60.   def trackback_url;  "http://anotherblog.org/a-post/trackback"; end
  61.   it 'Trackback URL is detected and pinged' do
  62.     referrer_url = 'http://myblog.net/referring-post'
  63.     post = "title=Article+1%21&excerpt=body&url=http://myblog.net/referring-post&blog_name=test+blog"
  64.     make_and_send_ping(post, contents(:article1), referrer_url)
  65.   end
  66.   it 'sends a trackback without html tag in excerpt' do
  67.     # TODO: Assert the following:
  68.     # contents(:xmltest).body = originally seen on <a href="http://blog.rubyonrails.org/">blog.rubyonrails.org</a>
  69.     post = "title=#{CGI.escape("Associations aren't :dependent => true anymore")}"
  70.     post << "&excerpt=#{CGI.escape("originally seen on blog.rubyonrails.org")}" # not original text see if normal ?
  71.     post << "&url=#{contents(:xmltest).permalink_url}"
  72.     post << "&blog_name=#{CGI.escape('test blog')}"
  73.     make_and_send_ping(post, contents(:xmltest), contents(:xmltest).permalink_url)
  74.   end
  75.   it 'sends a trackback without markdown tag in excerpt' do
  76.     # TODO: Assert the following:
  77.     # contents(:markdown_article) #in markdown formatn * wen * usen [ok](http://blog.ok.com) to define a link
  78.     post = "title=#{CGI.escape("How made link with markdown")}"
  79.     post << "&excerpt=#{CGI.escape("in markdown format we use ok to define a link")}" # not original text see if normal ?
  80.     post << "&url=#{contents(:markdown_article).permalink_url}"
  81.     post << "&blog_name=#{CGI.escape('test blog')}"
  82.     make_and_send_ping(post, contents(:markdown_article), contents(:markdown_article).permalink_url)
  83.   end
  84.   def make_and_send_ping(post, article, article_url)
  85.     @mock = mock('html_response')
  86.     Net::HTTP.should_receive(:get_response).with(URI.parse(referenced_url)).and_return(@mock)
  87.     @mock.should_receive(:[]).with('X-Pingback').at_least(:once)
  88.     @mock.should_receive(:body).twice.and_return(referenced_body)
  89.     Net::HTTP.should_receive(:start).with(URI.parse(trackback_url).host, 80).and_yield(@mock)
  90.     @mock.should_receive(:post) 
  91.       .with('/a-post/trackback', post,
  92.             'Content-type' => 'application/x-www-form-urlencoded; charset=utf-8') 
  93.       .and_return(@mock)
  94.     ping = article.pings.build(:url => referenced_url)
  95.     ping.send_pingback_or_trackback(article_url)
  96.   end
  97.   def referenced_body
  98.     <<-eobody
  99.     <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  100.            xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"
  101.            xmlns:dc="http://purl.org/dc/elements/1.1/">
  102.     <rdf:Description
  103.         rdf:about=""
  104.         trackback:ping="http://anotherblog.org/a-post/trackback"
  105.         dc:title="Track me, track me!"
  106.         dc:identifier="http://anotherblog.org/a-post"
  107.         dc:description="Track me 'til I fart!'"
  108.         dc:creator="pdcawley"
  109.         dc:date="2006-03-01T04:31:00-05:00" />
  110.     </rdf:RDF>
  111.     eobody
  112.   end
  113. end
  114. describe 'Given a remote site to notify, eg technorati' do
  115.   it 'we can ping them correctly' do
  116.     mock = mock('response')
  117.     XMLRPC::Client.should_receive(:new2).with('http://rpc.technorati.com/rpc/ping').and_return(mock)
  118.     mock.should_receive(:call).with('weblogUpdates.ping', 'test blog',
  119.                                     'http://myblog.net', 'http://myblog.net/new-post')
  120.     ping = contents(:article1).pings.build("url" => "http://rpc.technorati.com/rpc/ping")
  121.     ping.send_weblogupdatesping('http://myblog.net', 'http://myblog.net/new-post')
  122.   end
  123. end