Skip to content
This repository has been archived by the owner on Jan 30, 2019. It is now read-only.

Commit

Permalink
adding specs for Pingfm::Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Dale Campbell committed Sep 28, 2008
1 parent 3f11aab commit ce0b3a5
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
135 changes: 135 additions & 0 deletions spec/pingfm_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# $Id$
# TODO: LOTS of repetition here that can probably be refactored a bit.

require File.join(File.dirname(__FILE__), %w[spec_helper])

Expand Down Expand Up @@ -47,6 +48,26 @@
result['services'].first['id'].should eql('twitter')
end

it "should list the system services" do
init_system_services_response
uri = URI.parse "#{Pingfm::API_URL}/#{@service_type}"

# mock the http call
http_resp = mock('response')
http_resp.should_receive(:body).and_return(@response)
Net::HTTP.should_receive(:post_form).with(uri, @params).and_return(http_resp)

# call and verify
result = @client.system_services
result.should_not be_empty
result['status'].should_not be_nil
result['status'].should eql('OK')
result['services'].should_not be_nil
result['services'].should_not be_empty
result['services'].length.should eql(2)
result['services'].first['id'].should eql('bebo')
end

it "should list the user's custom triggers" do
init_trigger_response

Expand Down Expand Up @@ -88,6 +109,7 @@
result['messages'].should_not be_empty
result['messages'].length.should eql(3)
result['messages'].first['id'].should eql('12345')
result['messages'].last['location'].should_not be_empty
end

it "should post a message to the service" do
Expand Down Expand Up @@ -154,6 +176,119 @@
result['status'].should eql('FAIL')
result['message'].should_not be_nil
end

it "should handle a failed system services cleanly" do
init_fail_response 'system.services'

uri = URI.parse "#{Pingfm::API_URL}/#{@service_type}"

# mock the http call
http_resp = mock('response')
http_resp.should_receive(:body).and_return(@response)
Net::HTTP.should_receive(:post_form).with(uri, @params).and_return(http_resp)

# call and verify
result = @client.system_services
result.should_not be_empty
result['status'].should_not be_nil
result['status'].should eql('FAIL')
result['message'].should_not be_nil
end

it "should handle a failed user's services cleanly" do
init_fail_response 'user.services'

uri = URI.parse "#{Pingfm::API_URL}/#{@service_type}"

# mock the http call
http_resp = mock('response')
http_resp.should_receive(:body).and_return(@response)
Net::HTTP.should_receive(:post_form).with(uri, @params).and_return(http_resp)

# call and verify
result = @client.services
result.should_not be_empty
result['status'].should_not be_nil
result['status'].should eql('FAIL')
result['message'].should_not be_nil
end

it "should handle a failed user's triggers cleanly" do
init_fail_response 'user.triggers'

uri = URI.parse "#{Pingfm::API_URL}/#{@service_type}"

# mock the http call
http_resp = mock('response')
http_resp.should_receive(:body).and_return(@response)
Net::HTTP.should_receive(:post_form).with(uri, @params).and_return(http_resp)

# call and verify
result = @client.triggers
result.should_not be_empty
result['status'].should_not be_nil
result['status'].should eql('FAIL')
result['message'].should_not be_nil
end

it "should handle a failed user's latest messages cleanly" do
init_fail_response 'user.latest'

uri = URI.parse "#{Pingfm::API_URL}/#{@service_type}"

# mock the http call
http_resp = mock('response')
http_resp.should_receive(:body).and_return(@response)
@params.merge!('order' => 'DESC', 'limit' => 25)
Net::HTTP.should_receive(:post_form).with(uri, @params).and_return(http_resp)

# call and verify
result = @client.latest
result.should_not be_empty
result['status'].should_not be_nil
result['status'].should eql('FAIL')
result['message'].should_not be_nil
end

it "should handle a failed user post cleanly" do
init_fail_response 'user.post'

uri = URI.parse "#{Pingfm::API_URL}/#{@service_type}"

# mock the http call
http_resp = mock('response')
http_resp.should_receive(:body).and_return(@response)
@params.merge!({'post_method' => 'default', 'title' => '',
'service' => '', 'body' => 'test message', 'debug' => 0})
Net::HTTP.should_receive(:post_form).with(uri, @params).and_return(http_resp)

# call and verify
result = @client.post('test message')
result.should_not be_empty
result['status'].should_not be_nil
result['status'].should eql('FAIL')
result['message'].should_not be_nil
end

it "should handle a failed user trigger post cleanly" do
init_fail_response 'user.tpost'

uri = URI.parse "#{Pingfm::API_URL}/#{@service_type}"

# mock the http call
http_resp = mock('response')
http_resp.should_receive(:body).and_return(@response)
@params.merge!({'title' => '', 'body' => 'test message',
'trigger' => '@trigger', 'debug' => 0})
Net::HTTP.should_receive(:post_form).with(uri, @params).and_return(http_resp)

# call and verify
result = @client.tpost('test message', '@trigger')
result.should_not be_empty
result['status'].should_not be_nil
result['status'].should eql('FAIL')
result['message'].should_not be_nil
end
end

# EOF
23 changes: 23 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ def init_service_response
EOXML
end

def init_system_services_response
@service_type = 'system.services'
@response = <<EOXML
<rsp status="OK">
<transaction>12345</transaction>
<method>system.services</method>
<services>
<service id="bebo" name="Bebo">
<trigger>@be</trigger>
<url>http://www.bebo.com/</url>
<icon>http://p.ping.fm/static/icons/bebo.png</icon>
</service>
<service id="blogger" name="Blogger">
<trigger>@bl</trigger>
<url>http://www.blogger.com/</url>
<icon>http://p.ping.fm/static/icons/blogger.png</icon>
</service>
</services>
</rsp>
EOXML
end

def init_trigger_response
@service_type = 'user.triggers'
@response = <<EOXML
Expand Down Expand Up @@ -121,6 +143,7 @@ def init_latest_response
<content>
<body>aXMgdGVzdGluZyBQaW5nLmZtIQ==</body>
</content>
<location>VHVsc2EsIE9L</location>
</message>
</messages>
</rsp>
Expand Down

0 comments on commit ce0b3a5

Please sign in to comment.