Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide implementation details #271

Merged
merged 6 commits into from
Dec 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/http/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Connection

# @param [HTTP::Request] req
# @param [HTTP::Options] options
# @raise [HTTP::ConnectionError] when failed to connect
def initialize(req, options)
@persistent = options.persistent?
@keep_alive_timeout = options.keep_alive_timeout.to_f
Expand All @@ -35,6 +36,8 @@ def initialize(req, options)
send_proxy_connect_request(req)
start_tls(req, options)
reset_timer
rescue SocketError, SystemCallError => e
raise ConnectionError, "failed to connect: #{e}"
end

# @see (HTTP::Response::Parser#status_code)
Expand Down Expand Up @@ -104,7 +107,7 @@ def read_headers!

set_keep_alive
rescue IOError, Errno::ECONNRESET, Errno::EPIPE => e
raise IOError, "problem making HTTP request: #{e}"
raise ConnectionError, "failed to read headers: #{e}"
end

# Callback for when we've reached the end of a response
Expand Down
3 changes: 3 additions & 0 deletions lib/http/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module HTTP
# Generic error
class Error < StandardError; end

# Generic Connection error
class ConnectionError < Error; end

# Generic Request error
class RequestError < Error; end

Expand Down
1 change: 1 addition & 0 deletions lib/http/response/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def #{symbol}? # def bad_request?
end

def __setobj__(obj)
fail TypeError, "Expected #{obj.inspect} to respond to #to_i" unless obj.respond_to? :to_i
@code = obj.to_i
end

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/http/request/writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
let(:body) { 123 }

it "raises an error" do
expect { writer }.to raise_error
expect { writer }.to raise_error(HTTP::RequestError)
end
end
end
Expand All @@ -59,12 +59,12 @@

context "when Transfer-Encoding not set" do
let(:headers) { HTTP::Headers.new }
specify { expect { writer.stream }.to raise_error }
specify { expect { writer.stream }.to raise_error(HTTP::RequestError) }
end

context "when Transfer-Encoding is not chunked" do
let(:headers) { HTTP::Headers.coerce "Transfer-Encoding" => "gzip" }
specify { expect { writer.stream }.to raise_error }
specify { expect { writer.stream }.to raise_error(HTTP::RequestError) }
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/http/response/status_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RSpec.describe HTTP::Response::Status do
describe ".new" do
it "fails if given value does not respond to #to_i" do
expect { described_class.new double }.to raise_error
expect { described_class.new double }.to raise_error TypeError
end

it "accepts any object that responds to #to_i" do
Expand Down
5 changes: 5 additions & 0 deletions spec/lib/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,5 +337,10 @@
client = HTTP.headers("Cookie" => "foo=bar").cookies(:baz => :moo)
expect(client.get(endpoint).to_s).to eq "foo: bar\nbaz: moo"
end

it "unifies socket errors into HTTP::ConnectionError" do
expect { HTTP.get "http://thishostshouldnotexists.com" }.to raise_error HTTP::ConnectionError
expect { HTTP.get "http://127.0.0.1:000" }.to raise_error HTTP::ConnectionError
end
end
end
2 changes: 1 addition & 1 deletion spec/support/connection_reuse_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
# rubocop:enable Style/RescueModifier

# Should error because we tried to use a bad socket
expect { client.get("#{server.endpoint}/socket").body.to_s }.to raise_error(IOError)
expect { client.get("#{server.endpoint}/socket").body.to_s }.to raise_error HTTP::ConnectionError

# Should succeed since we create a new socket
second_socket = client.get("#{server.endpoint}/socket").body.to_s
Expand Down
2 changes: 1 addition & 1 deletion spec/support/http_handling_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
# rubocop:enable Style/RescueModifier

# Should error because we tried to use a bad socket
expect { client.get("#{server.endpoint}/socket").body.to_s }.to raise_error(IOError)
expect { client.get("#{server.endpoint}/socket").body.to_s }.to raise_error HTTP::ConnectionError

# Should succeed since we create a new socket
second_socket_id = client.get("#{server.endpoint}/socket").body.to_s
Expand Down