Skip to content

Commit

Permalink
report error if server closes keepalive (fix #225)
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Jun 22, 2013
1 parent ee337a5 commit 9615041
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/em-http/client.rb
Expand Up @@ -115,7 +115,7 @@ def unbind(reason = nil)
end

else
on_error(reason)
on_error(reason || 'connection closed by server')
end
end

Expand Down
6 changes: 4 additions & 2 deletions lib/em-http/http_connection.rb
Expand Up @@ -52,7 +52,9 @@ def conn=(c)

def activate_connection(client)
begin
EventMachine.bind_connect(@connopts.bind, @connopts.bind_port, @connopts.host, @connopts.port, HttpStubConnection) do |conn|
EventMachine.bind_connect(@connopts.bind, @connopts.bind_port,
@connopts.host, @connopts.port,
HttpStubConnection) do |conn|
post_init

@deferred = false
Expand Down Expand Up @@ -167,7 +169,7 @@ def redirect(client)
@pending.push client
end

def unbind(reason)
def unbind(reason = nil)
@clients.map { |c| c.unbind(reason) }

if r = @pending.shift
Expand Down
27 changes: 23 additions & 4 deletions spec/client_spec.rb
Expand Up @@ -761,22 +761,41 @@ def failed(http=nil)

it "should reconnect if connection was closed between requests" do
EventMachine.run {
conn = EM::HttpRequest.new('http://127.0.0.1:8090/', :inactivity_timeout => 0.5)
req = conn.get :keepalive => true
conn = EM::HttpRequest.new('http://127.0.0.1:8090/')
req = conn.get

req.callback do
EM.add_timer(1) do
req = conn.get
conn.close('client closing connection')

EM.next_tick do
req = conn.get :path => "/gzip"
req.callback do
req.response_header.status.should == 200
req.response.should match('compressed')
EventMachine.stop
end
end
end
}
end

it "should report error if connection was closed by server on client keepalive requests" do
EventMachine.run {
conn = EM::HttpRequest.new('http://127.0.0.1:8090/')
req = conn.get :keepalive => true

req.callback do
req = conn.get

req.callback { failed(http) }
req.errback do
req.error.should match('connection closed by server')
EventMachine.stop
end
end
}
end

it 'should handle malformed Content-Type header repetitions' do
EventMachine.run {
response =<<-HTTP.gsub(/^ +/, '').strip
Expand Down

0 comments on commit 9615041

Please sign in to comment.