Added 'allow_redirects' parameter in perform_request function for RequestsHttpConnection (#401)
Signed-off-by: saimedhi <saimedhi@amazon.com>
This commit is contained in:
committed by
GitHub
parent
237ce9daf6
commit
f1b5706ebd
@@ -9,6 +9,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
||||
- Added 'point in time' APIs to the pyi files in sync and async client ([#378](https://github.com/opensearch-project/opensearch-py/pull/378))
|
||||
- Added MacOS and Windows CI workflows ([#390](https://github.com/opensearch-project/opensearch-py/pull/390))
|
||||
- Compatibility with OpenSearch 2.1.0 - 2.6.0 ([#381](https://github.com/opensearch-project/opensearch-py/pull/381))
|
||||
- Added 'allow_redirects' parameter in perform_request function for RequestsHttpConnection ([#401](https://github.com/opensearch-project/opensearch-py/pull/401))
|
||||
### Changed
|
||||
- Upgrading pytest-asyncio to latest version - 0.21.0 ([#339](https://github.com/opensearch-project/opensearch-py/pull/339))
|
||||
- Fixed flaky CI tests by replacing httpbin with a simple http_server ([#395](https://github.com/opensearch-project/opensearch-py/pull/395))
|
||||
|
||||
@@ -155,7 +155,15 @@ class RequestsHttpConnection(Connection):
|
||||
)
|
||||
|
||||
def perform_request(
|
||||
self, method, url, params=None, body=None, timeout=None, ignore=(), headers=None
|
||||
self,
|
||||
method,
|
||||
url,
|
||||
params=None,
|
||||
body=None,
|
||||
timeout=None,
|
||||
allow_redirects=True,
|
||||
ignore=(),
|
||||
headers=None,
|
||||
):
|
||||
url = self.base_url + url
|
||||
headers = headers or {}
|
||||
@@ -173,7 +181,10 @@ class RequestsHttpConnection(Connection):
|
||||
settings = self.session.merge_environment_settings(
|
||||
prepared_request.url, {}, None, None, None
|
||||
)
|
||||
send_kwargs = {"timeout": timeout or self.timeout}
|
||||
send_kwargs = {
|
||||
"timeout": timeout or self.timeout,
|
||||
"allow_redirects": allow_redirects,
|
||||
}
|
||||
send_kwargs.update(settings)
|
||||
try:
|
||||
response = self.session.send(prepared_request, **send_kwargs)
|
||||
|
||||
@@ -14,9 +14,16 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
|
||||
class TestHTTPRequestHandler(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
self.send_response(200)
|
||||
headers = self.headers
|
||||
self.send_header("Content-type", "application/json")
|
||||
|
||||
if self.path == "/redirect":
|
||||
new_location = "http://localhost:8090"
|
||||
self.send_response(302)
|
||||
self.send_header("Location", new_location)
|
||||
else:
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", "application/json")
|
||||
|
||||
self.end_headers()
|
||||
|
||||
Headers = {}
|
||||
|
||||
@@ -1020,6 +1020,46 @@ class TestConnectionHttpServer:
|
||||
conn.perform_request("GET", "/")
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
sys.version_info < (3, 0),
|
||||
reason="http_server is only available from python 3.x",
|
||||
)
|
||||
class TestRequestsConnectionRedirect:
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
# Start servers
|
||||
cls.server1 = TestHTTPServer(port=8080)
|
||||
cls.server1.start()
|
||||
cls.server2 = TestHTTPServer(port=8090)
|
||||
cls.server2.start()
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
# Stop servers
|
||||
cls.server2.stop()
|
||||
cls.server1.stop()
|
||||
|
||||
# allow_redirects = False
|
||||
def test_redirect_failure_when_allow_redirect_false(self):
|
||||
conn = RequestsHttpConnection("localhost", port=8080, use_ssl=False, timeout=60)
|
||||
with pytest.raises(TransportError) as e:
|
||||
conn.perform_request("GET", "/redirect", allow_redirects=False)
|
||||
assert e.value.status_code == 302
|
||||
|
||||
# allow_redirects = True (Default)
|
||||
def test_redirect_success_when_allow_redirect_true(self):
|
||||
conn = RequestsHttpConnection("localhost", port=8080, use_ssl=False, timeout=60)
|
||||
user_agent = conn._get_default_user_agent()
|
||||
status, headers, data = conn.perform_request("GET", "/redirect")
|
||||
assert status == 200
|
||||
data = json.loads(data)
|
||||
assert data["headers"] == {
|
||||
"Host": "localhost:8090",
|
||||
"Accept-Encoding": "identity",
|
||||
"User-Agent": user_agent,
|
||||
}
|
||||
|
||||
|
||||
def test_default_connection_is_returned_by_default():
|
||||
c = connections.Connections()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user