Skip to content

Commit

Permalink
invoke fail callback on bad DNS
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Mar 21, 2009
1 parent 2a998f2 commit 3c32c82
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion em-http-request.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spec = Gem::Specification.new do |s|
s.name = 'em-http-request'
s.version = '0.1.2'
s.version = '0.1.3'
s.date = '2009-03-20'
s.summary = 'EventMachine based HTTP Request interface'
s.description = s.summary
Expand Down
21 changes: 13 additions & 8 deletions lib/em-http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ module EventMachine
#

class HttpRequest
attr_reader :response, :headers

def initialize(host, headers = {})
@headers = headers
Expand Down Expand Up @@ -52,13 +51,19 @@ def send_request(method, options)
raise ArgumentError, "invalid request path" unless /^\// === @uri.path

method = method.to_s.upcase

EventMachine.connect(@uri.host, @uri.port, EventMachine::HttpClient) { |c|
c.uri = @uri
c.method = method
c.options = options
c.comm_inactivity_timeout = options[:timeout] || 5
}
begin
EventMachine.connect(@uri.host, @uri.port, EventMachine::HttpClient) { |c|
c.uri = @uri
c.method = method
c.options = options
c.comm_inactivity_timeout = options[:timeout] || 5
}
rescue RuntimeError => e
raise e unless e.message == "no connection"
conn = EventMachine::HttpClient.new("")
conn.on_error("no connection")
conn
end
end
end
end
14 changes: 13 additions & 1 deletion test/test_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def failed
fail
end

it "should fail GET on invalid host" do
it "should fail GET on DNS timeout" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.1.1.1/').get
http.callback { failed }
Expand All @@ -19,6 +19,18 @@ def failed
}
end

it "should fail GET on invalid host" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://google1.com/').get
http.callback { failed }
http.errback {
http.response_header.status.should == 0
http.errors.should match(/no connection/)
EventMachine.stop
}
}
end

it "should fail GET on missing path" do
EventMachine.run {
lambda {
Expand Down

0 comments on commit 3c32c82

Please sign in to comment.