Permalink
Browse files

Raise a REST::DisconnectError when the client is disconnected from th…

…e server.
  • Loading branch information...
1 parent 2d976b7 commit f1e2eb9a204b89a3c7b32c3350d1e036dec76cbf @Manfred Manfred committed Jan 5, 2012
Showing with 29 additions and 1 deletion.
  1. +3 −0 lib/rest.rb
  2. +5 −1 lib/rest/request.rb
  3. +10 −0 test/functional_test.rb
  4. +2 −0 test/lib/http_server.rb
  5. +9 −0 test/rest_request_test.rb
View
@@ -3,6 +3,9 @@
# REST is basically a convenience wrapper around Net::HTTP. It defines a simple and consistant API for doing REST-style
# HTTP calls.
module REST
+ # Raised when the remote server disconnects when reading the response
+ class DisconnectedError < StandardError; end
+
# Performs a HEAD on a resource. See REST::Request.new for a complete discussion of options.
#
# response = REST.get('http://example.com/pigeons/12',
View
@@ -130,7 +130,11 @@ def perform
end
end
- response = http_request.start { |http| http.request(request) }
+ begin
+ response = http_request.start { |http| http.request(request) }
+ rescue EOFError => error
+ raise REST::DisconnectedError, error.message
+ end
REST::Response.new(response.code, response.instance_variable_get('@header'), response.body)
end
@@ -28,5 +28,15 @@
response = REST.get(BASE_URL + '/')
response.body.should == "OK!\n"
end
+
+ it "does stuff when the server disconnects" do
+ begin
+ REST.get(BASE_URL + '/disconnect')
+ rescue REST::DisconnectedError => e
+ e.message.should == 'end of file reached'
+ else
+ fail
+ end
+ end
end
end
@@ -40,6 +40,8 @@ def run
case path
when '/'
response(client, :ok, 'OK!')
+ when '/disconnect'
+ client.close
else
response(client, :not_found, "Unknown path: #{path}")
end
@@ -160,4 +160,13 @@
request.perform
}.should.raise(ArgumentError)
end
+
+ it "should raise a disconnect errro when the reading the response fails" do
+ http_request = Net::HTTP.new('example.com')
+ Net::HTTP.expects(:new).returns(http_request)
+ http_request.expects(:start).raises(EOFError.new('failed'))
+ lambda {
+ REST.get('/something')
+ }.should.raise(REST::DisconnectedError)
+ end
end

0 comments on commit f1e2eb9

Please sign in to comment.