Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved error handling tests to Client generic specs

  • Loading branch information...
commit 8ef65241a5d8dcbdbcea5956eef3bcab8f3bf144 1 parent 19f83a0
@mloughran mloughran authored
Showing with 71 additions and 77 deletions.
  1. +0 −65 spec/channel_spec.rb
  2. +71 −12 spec/client_spec.rb
View
65 spec/channel_spec.rb
@@ -70,55 +70,6 @@ def stub_post_to_raise(e)
end
end
end
-
- def trigger
- lambda { @channel.trigger!('new_event', 'Some data') }
- end
-
- it "should catch all Net::HTTP exceptions and raise a Pusher::HTTPError, exposing the original error if required" do
- stub_post_to_raise Timeout::Error
- error_raised = nil
-
- begin
- trigger.call
- rescue => e
- error_raised = e
- end
-
- error_raised.class.should == Pusher::HTTPError
- error_raised.message.should == 'Exception from WebMock (Timeout::Error)'
- error_raised.original_error.class.should == Timeout::Error
- end
-
-
- it "should raise Pusher::Error if pusher returns 400" do
- stub_post 400
- trigger.should raise_error(Pusher::Error)
- end
-
- it "should raise AuthenticationError if pusher returns 401" do
- stub_post 401
- trigger.should raise_error(Pusher::AuthenticationError)
- end
-
- it "should raise Pusher::Error if pusher returns 404" do
- stub_post 404
- trigger.should raise_error(Pusher::Error, 'Resource not found: app_id is probably invalid')
- end
-
- it "should raise Pusher::Error if pusher returns 407" do
- WebMock.stub_request(
- :post, %r{/apps/20/events}
- ).to_return(:status => 407)
- lambda {
- @client['test_channel'].trigger!('new_event', 'Some data')
- }.should raise_error(Pusher::Error, 'Proxy Authentication Required')
- end
-
- it "should raise Pusher::Error if pusher returns 500" do
- stub_post 500, "some error"
- trigger.should raise_error(Pusher::Error, 'Unknown error (status code 500): some error')
- end
end
describe '#trigger' do
@@ -173,22 +124,6 @@ def trigger
}
}
end
-
- it "should return a deferrable which fails (with exception) in fail case" do
- stub_post 401
-
- EM.run {
- d = @channel.trigger_async('new_event', 'Some data')
- d.callback {
- fail
- }
- d.errback { |error|
- WebMock.should have_requested(:post, pusher_url_regexp)
- error.should be_kind_of(Pusher::AuthenticationError)
- EM.stop
- }
- }
- end
end
describe '#info' do
View
83 spec/client_spec.rb
@@ -233,44 +233,90 @@
[:get, :post].each do |verb|
describe "##{verb}" do
before :each do
- WebMock.stub_request(verb, %r{api.pusherapp.com}).
+ @url_regexp = %r{api.pusherapp.com}
+ stub_request(verb, @url_regexp).
to_return(:status => 200, :body => "{}")
end
+ let(:call_api) { @client.send(verb, '/path') }
+
it "should use http by default" do
- @client.send(verb, '/path')
+ call_api
WebMock.should have_requested(verb, %r{http://api.pusherapp.com/apps/20/path})
end
it "should use https if configured" do
@client.encrypted = true
- @client.send(verb, '/path')
+ call_api
WebMock.should have_requested(verb, %r{https://api.pusherapp.com})
end
it "should format the respose hash with symbols at first level" do
- WebMock.stub_request(verb, %r{api.pusherapp.com}).to_return({
+ stub_request(verb, @url_regexp).to_return({
:status => 200,
:body => MultiJson.encode({'something' => {'a' => 'hash'}})
})
- response = @client.send(verb, '/path')
- response.should == {
+ call_api.should == {
:something => {'a' => 'hash'}
}
end
+
+ it "should catch all Net::HTTP exceptions and raise a Pusher::HTTPError wrapping the original error" do
+ stub_request(verb, @url_regexp).to_raise(Timeout::Error)
+
+ error = nil
+ begin
+ call_api
+ rescue => e
+ error = e
+ end
+
+ error.class.should == Pusher::HTTPError
+ error.should be_kind_of(Pusher::Error)
+ error.message.should == 'Exception from WebMock (Timeout::Error)'
+ error.original_error.class.should == Timeout::Error
+ end
+
+ it "should raise Pusher::Error if call returns 400" do
+ stub_request(verb, @url_regexp).to_return({:status => 400})
+ lambda { call_api }.should raise_error(Pusher::Error)
+ end
+
+ it "should raise AuthenticationError if pusher returns 401" do
+ stub_request(verb, @url_regexp).to_return({:status => 401})
+ lambda { call_api }.should raise_error(Pusher::AuthenticationError)
+ end
+
+ it "should raise Pusher::Error if pusher returns 404" do
+ stub_request(verb, @url_regexp).to_return({:status => 404})
+ lambda { call_api }.should raise_error(Pusher::Error, 'Resource not found: app_id is probably invalid')
+ end
+
+ it "should raise Pusher::Error if pusher returns 407" do
+ stub_request(verb, @url_regexp).to_return({:status => 407})
+ lambda { call_api }.should raise_error(Pusher::Error, 'Proxy Authentication Required')
+ end
+
+ it "should raise Pusher::Error if pusher returns 500" do
+ stub_request(verb, @url_regexp).to_return({:status => 500, :body => "some error"})
+ lambda { call_api }.should raise_error(Pusher::Error, 'Unknown error (status code 500): some error')
+ end
end
end
- [[:get, :get_async]].each do |verb, method|
+ [[:get, :get_async], [:post, :post_async]].each do |verb, method|
describe "##{method}" do
before :each do
- WebMock.stub_request(verb, %r{api.pusherapp.com}).
+ @url_regexp = %r{api.pusherapp.com}
+ stub_request(verb, @url_regexp).
to_return(:status => 200, :body => "{}")
end
+ let(:call_api) { @client.send(method, '/path') }
+
it "should use http by default" do
EM.run {
- @client.send(method, '/path').callback {
+ call_api.callback {
WebMock.should have_requested(verb, %r{http://api.pusherapp.com/apps/20/path})
EM.stop
}
@@ -280,7 +326,7 @@
it "should use https if configured" do
EM.run {
@client.encrypted = true
- @client.send(method, '/path').callback {
+ call_api.callback {
WebMock.should have_requested(verb, %r{https://api.pusherapp.com})
EM.stop
}
@@ -289,11 +335,11 @@
it "should format the respose hash with symbols at first level" do
EM.run {
- WebMock.stub_request(verb, %r{api.pusherapp.com}).to_return({
+ stub_request(verb, @url_regexp).to_return({
:status => 200,
:body => MultiJson.encode({'something' => {'a' => 'hash'}})
})
- @client.send(method, '/path').callback { |response|
+ call_api.callback { |response|
response.should == {
:something => {'a' => 'hash'}
}
@@ -301,6 +347,19 @@
}
}
end
+
+ it "should errback with Pusher::Error on unsuccessful response" do
+ EM.run {
+ stub_request(verb, @url_regexp).to_return({:status => 400})
+
+ call_api.errback { |e|
+ e.class.should == Pusher::Error
+ EM.stop
+ }.callback {
+ fail
+ }
+ }
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.