Skip to content

Commit

Permalink
Removed unused function from client. Added tests for pingback sending…
Browse files Browse the repository at this point in the history
… and receiving.
  • Loading branch information
Doug Youch committed Feb 11, 2010
1 parent 088fcac commit e33dbf0
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 5 deletions.
Expand Up @@ -34,11 +34,6 @@ def pingback_uri
@pingback_uri
end

def retrieve_target_content(target_uri)
return open(target_uri) if target_uri =~ /^http:\/\//
target_uri
end

def send_pingback
raise "pingback uri not found" unless self.pingback_uri

Expand Down
@@ -0,0 +1,34 @@
require File.expand_path(File.dirname(__FILE__)) + "/../../../../../../spec/spec_helper"

describe Feedback::PingbackSupport do

reset_domain_tables :feedback_pingbacks, :comments, :content_nodes, :content_node_values, :blog_posts

class Pinger
attr_accessor :id, :html
include Feedback::PingbackSupport
end

before(:each) do
@post = Blog::BlogPost.create
@link = '/test-post'
@type = ContentType.create :component => 'blog', :container_type => 'Blog::BlogBlog', :container_id => 1, :content_type => 'Blog::BlogPost', :content_name => 'Mock Blog', :title_field => 'title'
@node = @type.content_nodes.create :node => @post
@node_value = @node.content_node_values.create :content_type_id => @type.id, :link => @link
@source_uri = 'http://myexternal.test.site.com/test-post'
@target_uri = Configuration.domain_link @link
@pinger = Pinger.new
end

it "should do nothing if html of field are not specified" do
ContentNode.should_receive(:find_by_node_type_and_node_id).exactly(0).times
@pinger.send_pingbacks
end

it "should add pingbacks" do
@node.should_receive(:link).any_number_of_times.and_return(@link)
ContentNode.should_receive(:find_by_node_type_and_node_id).with(@pinger.class.to_s, anything()).and_return(@node)
FeedbackOutgoingPingback.should_receive(:add_pingback).with(@node, 'http://test.dev/test')
@pinger.send_pingbacks :html => '<html><body><p>Fake <a href="http://test.dev/test">link</a></p></body></html>'
end
end
@@ -0,0 +1,88 @@
require File.expand_path(File.dirname(__FILE__)) + "/../../../../../spec/spec_helper"

describe FeedbackOutgoingPingback do

reset_domain_tables :feedback_outgoing_pingbacks, :comments, :content_nodes, :content_node_values, :blog_posts

it "should require content and target" do
@comment = FeedbackOutgoingPingback.new
@comment.valid?
@comment.should have(1).errors_on(:content_node_id)
@comment.should have(1).errors_on(:target_uri)
end

describe "processing outgoing pingbacks" do
before(:each) do
@post = Blog::BlogPost.create
@link = '/test-post'
@type = ContentType.create :component => 'blog', :container_type => 'Blog::BlogBlog', :container_id => 1, :content_type => 'Blog::BlogPost', :content_name => 'Mock Blog', :title_field => 'title'
@node = @type.content_nodes.create :node => @post
@node_value = @node.content_node_values.create :content_type_id => @type.id, :link => @link
@source_uri = Configuration.domain_link @link
@target_uri = 'http://myexternal.test.site.com/test-post'
end

def fake_client(ok, param)
@client = mock
@client.should_receive(:send_pingback).once.and_return([ok, param])
FeedbackPingbackClient.should_receive(:new).and_return(@client)
end

def fake_fault(faultCode, faultString)
mock :faultCode => faultCode, :faultString => faultString
end

it "should detect if the target is on the same host" do
same_host_target = Configuration.domain_link '/same-host'
FeedbackOutgoingPingback.add_pingback(@node, same_host_target).should be_nil
end

it "should detect if the target is not a full url" do
target = '/same-host'
FeedbackOutgoingPingback.add_pingback(@node, target).should be_nil
end

it "should save the outgoing pingback" do
fake_client(true, 'Pingback accepted')
assert_difference 'FeedbackOutgoingPingback.count', 1 do
FeedbackOutgoingPingback.add_pingback(@node, @target_uri).should_not be_nil
end

pingback = FeedbackOutgoingPingback.find(:last)
pingback.content_node_id.should == @node.id
pingback.target_uri.should == @target_uri
pingback.accepted.should be_true
pingback.status.should == 'Pingback accepted'
end

it "should save the outgoing pingback even when it is not accepted" do
fake_client(false, fake_fault(17, 'Target URL not found'))
assert_difference 'FeedbackOutgoingPingback.count', 1 do
FeedbackOutgoingPingback.add_pingback(@node, @target_uri).should_not be_nil
end

pingback = FeedbackOutgoingPingback.find(:last)
pingback.content_node_id.should == @node.id
pingback.target_uri.should == @target_uri
pingback.accepted.should be_false
pingback.status.should == 'Target URL not found'
pingback.status_code.should == 17
end

it "should save the outgoing pingback even if send_pingback raise exception" do
@client = mock
@client.should_receive(:send_pingback).once.and_raise("pingback uri not found")
FeedbackPingbackClient.should_receive(:new).and_return(@client)

assert_difference 'FeedbackOutgoingPingback.count', 1 do
FeedbackOutgoingPingback.add_pingback(@node, @target_uri).should_not be_nil
end

pingback = FeedbackOutgoingPingback.find(:last)
pingback.content_node_id.should == @node.id
pingback.target_uri.should == @target_uri
pingback.accepted.should be_nil
pingback.status.should == 'pingback uri not found'
end
end
end
@@ -0,0 +1,57 @@
require File.expand_path(File.dirname(__FILE__)) + "/../../../../../spec/spec_helper"

describe FeedbackPingbackClient do

def fake_response(pingback_url, body)
@response = mock :body => body
@response.should_receive(:[]).with('X-Pingback').any_number_of_times.and_return(pingback_url)
@response
end

def fake_net_http(response)
@http = mock
@http.should_receive(:request_get).and_yield(response)
Net::HTTP.should_receive(:start).and_yield(@http)
end

before(:each) do
@source_uri = Configuration.domain_link @link
@target_uri = 'http://myexternal.test.site.com/test-post'
@client = FeedbackPingbackClient.new @source_uri, @target_uri
end

it "should find the pingback url in the response header" do
fake_net_http( fake_response('http://test.dev/xmlrpc/pingback', '<html></html>') )
@client.pingback_uri.should == 'http://test.dev/xmlrpc/pingback'
end

it "should find the pingback url in the <link> tag" do
fake_net_http( fake_response(nil, '<html><head><link rel="pingback" href="http://test.dev/xmlrpc/pingback" /></head><body></body></html>') )
@client.pingback_uri.should == 'http://test.dev/xmlrpc/pingback'
end

it "should return nil if pingback url is not found" do
fake_net_http( fake_response(nil, '<html><head></head><body></body></html>') )
@client.pingback_uri.should be_nil
end

it "should return nil if exception is raised why fetching target" do
@http = mock
@http.should_receive(:request_get).and_raise("page not found")
Net::HTTP.should_receive(:start).and_yield(@http)
@client.pingback_uri.should be_nil
end

it "should send the pingback" do
fake_net_http( fake_response('http://test.dev/xmlrpc/pingback', '<html></html>') )
@rpc_client = mock
@rpc_client.should_receive(:call2).with('pingback.ping', @source_uri, @target_uri).and_return([true, 'Pingback Accepted'])
XMLRPC::Client.should_receive(:new2).with('http://test.dev/xmlrpc/pingback').and_return(@rpc_client)
@client.send_pingback.should == [true, 'Pingback Accepted']
end

it "should raise exception if pingback url not found" do
fake_net_http( fake_response(nil, '<html></html>') )
lambda{ @client.send_pingback }.should raise_error
end
end

0 comments on commit e33dbf0

Please sign in to comment.