Skip to content

Commit

Permalink
Support Net::HTTP::Persistent.write_timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
maurycy authored and flavorjones committed Nov 29, 2023
1 parent 119d13e commit 0dfaacb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/mechanize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,21 @@ def read_timeout= read_timeout
@agent.read_timeout = read_timeout
end

##
# Length of time to wait for data to be sent to the server

def write_timeout
@agent.write_timeout
end

##
# Sets the timeout for each chunk of data to be sent to the server to
# +write_timeout+. A single request may write many chunks of data.

def write_timeout= write_timeout
@agent.write_timeout = write_timeout
end

##
# Controls how mechanize deals with redirects. The following values are
# allowed:
Expand Down
7 changes: 7 additions & 0 deletions lib/mechanize/http/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ class Mechanize::HTTP::Agent
# Length of time to attempt to read data from the server
attr_accessor :read_timeout

# Length of time to attempt to write data to the server
attr_accessor :write_timeout

# :section:

# The cookies for this agent
Expand Down Expand Up @@ -161,6 +164,7 @@ def initialize(connection_name = 'mechanize')
@robots_mutex = Mutex.new
@user_agent = nil
@webrobots = nil
@write_timeout = nil

# HTTP Authentication
@auth_store = Mechanize::HTTP::AuthStore.new
Expand Down Expand Up @@ -274,6 +278,9 @@ def fetch uri, method = :get, headers = {}, params = [],
if @read_timeout && connection.respond_to?(:read_timeout=)
connection.read_timeout = @read_timeout
end
if @write_timeout && connection.respond_to?(:write_timeout=)
connection.write_timeout = @write_timeout
end

request_log request

Expand Down
6 changes: 6 additions & 0 deletions test/test_mechanize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,12 @@ def test_read_timeout_equals
assert_equal 5, @mech.read_timeout
end

def test_write_timeout_equals
@mech.write_timeout = 7

assert_equal 7, @mech.write_timeout
end

def test_timeouts_for_file_connection
uri = URI.parse "file://#{File.expand_path __FILE__}"
@mech.read_timeout = 5
Expand Down

0 comments on commit 0dfaacb

Please sign in to comment.