Skip to content

Commit 5c99be6

Browse files
authored
Merge pull request #663 from sparklemotion/flavorjones-write-timeout
Support Net::HTTP::Persistent.write_timeout
2 parents 0b23841 + 805451f commit 5c99be6

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Mechanize CHANGELOG
22

3+
## next / unreleased
4+
5+
* `Mechanize` exposes a `write_timeout` attribute, which is set on the connection if it's supported (e.g., Net::HTTP::Persistent.write_timeout). (#586) @maurycy
6+
7+
38
## 2.13.0 / 2025-01-02
49

510
* Quash frozen string warnings in Ruby 3.4. (#661) @simpl1g

lib/mechanize.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,21 @@ def read_timeout= read_timeout
951951
@agent.read_timeout = read_timeout
952952
end
953953

954+
##
955+
# Length of time to wait for data to be sent to the server
956+
957+
def write_timeout
958+
@agent.write_timeout
959+
end
960+
961+
##
962+
# Sets the timeout for each chunk of data to be sent to the server to
963+
# +write_timeout+. A single request may write many chunks of data.
964+
965+
def write_timeout= write_timeout
966+
@agent.write_timeout = write_timeout
967+
end
968+
954969
##
955970
# Controls how mechanize deals with redirects. The following values are
956971
# allowed:

lib/mechanize/http/agent.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ class Mechanize::HTTP::Agent
106106
# Length of time to attempt to read data from the server
107107
attr_accessor :read_timeout
108108

109+
# Length of time to attempt to write data to the server
110+
attr_accessor :write_timeout
111+
109112
# :section:
110113

111114
# The cookies for this agent
@@ -161,6 +164,7 @@ def initialize(connection_name = 'mechanize')
161164
@robots_mutex = Mutex.new
162165
@user_agent = nil
163166
@webrobots = nil
167+
@write_timeout = nil
164168

165169
# HTTP Authentication
166170
@auth_store = Mechanize::HTTP::AuthStore.new
@@ -274,6 +278,9 @@ def fetch uri, method = :get, headers = {}, params = [],
274278
if @read_timeout && connection.respond_to?(:read_timeout=)
275279
connection.read_timeout = @read_timeout
276280
end
281+
if @write_timeout && connection.respond_to?(:write_timeout=)
282+
connection.write_timeout = @write_timeout
283+
end
277284

278285
request_log request
279286

test/test_mechanize.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,8 +1016,16 @@ def test_put_redirect
10161016

10171017
def test_read_timeout_equals
10181018
@mech.read_timeout = 5
1019-
10201019
assert_equal 5, @mech.read_timeout
1020+
assert @mech.get('http://localhost/response_code?code=200')
1021+
assert_equal 5, @mech.agent.http.read_timeout
1022+
end
1023+
1024+
def test_write_timeout_equals
1025+
@mech.write_timeout = 7
1026+
assert_equal 7, @mech.write_timeout
1027+
assert @mech.get('http://localhost/response_code?code=200')
1028+
assert_equal 7, @mech.agent.http.write_timeout
10211029
end
10221030

10231031
def test_timeouts_for_file_connection

0 commit comments

Comments
 (0)