From 23443b7c4e432194137731408d01b642bd7858db Mon Sep 17 00:00:00 2001
From: peppyheppy <paul.hepworth@peppyheppy.com>
Date: Tue, 16 Jun 2015 22:50:20 -0700
Subject: [PATCH] added/fixed support for the timeout option so the
 documentation in the README is correct

---
 lib/rack/reverse_proxy.rb       |  3 +++
 spec/rack/reverse_proxy_spec.rb | 28 ++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/lib/rack/reverse_proxy.rb b/lib/rack/reverse_proxy.rb
index 4d3de34..55d5419 100644
--- a/lib/rack/reverse_proxy.rb
+++ b/lib/rack/reverse_proxy.rb
@@ -71,6 +71,9 @@ def proxy(env, source_request, matcher)
       # Create a streaming response (the actual network communication is deferred, a.k.a. streamed)
       target_response = HttpStreamingResponse.new(target_request, uri.host, uri.port)
 
+      # pass the timeout configuration through
+      target_response.read_timeout = options[:timeout] if options[:timeout].to_i > 0
+
       target_response.use_ssl = "https" == uri.scheme
 
       # Let rack set the transfer-encoding header
diff --git a/spec/rack/reverse_proxy_spec.rb b/spec/rack/reverse_proxy_spec.rb
index ea35a55..adfcbdd 100644
--- a/spec/rack/reverse_proxy_spec.rb
+++ b/spec/rack/reverse_proxy_spec.rb
@@ -80,6 +80,34 @@ def app
       end
     end
 
+    describe "with timeout configuration" do
+      def app
+        Rack::ReverseProxy.new(dummy_app) do
+          reverse_proxy '/test/slow', 'http://example.com/', {:timeout => 99}
+        end
+      end
+
+      it "should make request with basic auth" do
+        stub_request(:get, "http://example.com/test/slow")
+        Rack::HttpStreamingResponse.any_instance.should_receive(:read_timeout=).with(99)
+        get '/test/slow'
+      end
+    end
+
+    describe "without timeout configuration" do
+      def app
+        Rack::ReverseProxy.new(dummy_app) do
+          reverse_proxy '/test/slow', 'http://example.com/'
+        end
+      end
+
+      it "should make request with basic auth" do
+        stub_request(:get, "http://example.com/test/slow")
+        Rack::HttpStreamingResponse.any_instance.should_not_receive(:read_timeout=)
+        get '/test/slow'
+      end
+    end
+
     describe "with basic auth turned on" do
       def app
         Rack::ReverseProxy.new(dummy_app) do