chrisk/fakeweb — pull request #43 by dylanahsmith (2014-01-09 00:15:04+00:00)
Title: Fix reconnect on keep alive timeout when allowing all connections.

## Problem

Ruby 2.0 added a keep_alive_timeout variable to Net::HTTP for persistent. If keep_alive_timeout seconds has passed between requests, then Net::HTTP will close the socket and call connect. However, fakeweb replaces the `connect` method to have it do nothing (delaying it until request is called), so the socket is closed without reconnecting on a keep_alive_timeout.

The following script will result in a `closed stream (IOError)` exception being raised, but will run without exceptions after removing `require 'fakeweb'`.

```
require 'net/http'
require 'fakeweb'

uri = URI("http://www.google.com/")
req = Net::HTTP::Get.new(uri)

Net::HTTP.new(uri.host, uri.port).start do |http|
http.request(req)
sleep 2 # default keep_alive_timeout
http.request(req)
end
```

fakeweb sets `FakeWeb.allow_net_connect = true` on initialization. In this state, we should avoid changing the behaviour of Net::HTTP so that it works in cases that fakeweb wasn't designed for.

## Solution

Add `FakeWeb.allow_all_connections?` which returns true if fakeweb isn't intercepting any any requests (i.e. `FakeWeb.allow_net_connect = true`). In this state, the Net::HTTP monkey patch returns the behaviour of the `connect` and `request` method by just calling the real methods.

## Remaining Problem

fakeweb will still have this problem for persistent http connections when uri's are registered, but I didn't want to complicate this pull request by trying to solve this use case. I wanted to start by making sure fakeweb isn't disruptive when it is required but isn't being used, or when it is disabled for certain tests.