Merge branch 'add-before-redirect-hook' into 'master'

Add `before_redirect` to `follow_redirects` plugin

Closes #272

See merge request os85/httpx!296
This commit is contained in:
HoneyryderChuck 2023-12-02 00:05:32 +00:00
commit 9bab254710
3 changed files with 21 additions and 0 deletions

View File

@ -34,6 +34,12 @@ module HTTPX
def option_allow_auth_to_other_origins(value)
value
end
def option_redirect_on(value)
raise TypeError, ":redirect_on must be callable" unless value.respond_to?(:call)
value
end
end
module InstanceMethods
@ -57,6 +63,11 @@ module HTTPX
# build redirect request
redirect_uri = __get_location_from_response(response)
if options.redirect_on
redirect_allowed = options.redirect_on.call(redirect_uri)
return response unless redirect_allowed
end
if response.status == 305 && options.respond_to?(:proxy)
# The requested resource MUST be accessed through the proxy given by
# the Location field. The Location field gives the URI of the proxy.

View File

@ -12,6 +12,8 @@ module HTTPX
def follow_insecure_redirects: () -> bool?
def allow_auth_to_other_origins: () -> bool?
def redirect_on: (http_uri) -> bool?
end
def self.extra_options: (Options) -> (Options & _FollowRedirectsOptions)

View File

@ -104,6 +104,14 @@ module Requests
assert body["headers"].key?("Authorization")
end
def test_plugin_follow_redirects_redirect_on
session = HTTPX.plugin(:follow_redirects).with(redirect_on: ->(location_uri) { !location_uri.path.end_with?("1") })
redirect_response = session.get(max_redirect_uri(3))
verify_status(redirect_response, 302)
verify_header(redirect_response.headers, "location", "/relative-redirect/1")
end
private
def redirect_uri(redirect_uri = redirect_location)