Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Net::HTTP::Persistent.write_timeout #663

Merged
merged 3 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Mechanize CHANGELOG

## next / unreleased

* `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


## 2.13.0 / 2025-01-02

* Quash frozen string warnings in Ruby 3.4. (#661) @simpl1g
Expand Down
15 changes: 15 additions & 0 deletions lib/mechanize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,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
10 changes: 9 additions & 1 deletion test/test_mechanize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1016,8 +1016,16 @@ def test_put_redirect

def test_read_timeout_equals
@mech.read_timeout = 5

assert_equal 5, @mech.read_timeout
assert @mech.get('http://localhost/response_code?code=200')
assert_equal 5, @mech.agent.http.read_timeout
end

def test_write_timeout_equals
@mech.write_timeout = 7
assert_equal 7, @mech.write_timeout
assert @mech.get('http://localhost/response_code?code=200')
assert_equal 7, @mech.agent.http.write_timeout
end

def test_timeouts_for_file_connection
Expand Down
Loading