Skip to content

Commit f1b5706

Browse files
authored
Added 'allow_redirects' parameter in perform_request function for RequestsHttpConnection (opensearch-project#401)
Signed-off-by: saimedhi <saimedhi@amazon.com>
1 parent 237ce9d commit f1b5706

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
99
- Added 'point in time' APIs to the pyi files in sync and async client ([#378](https://github.com/opensearch-project/opensearch-py/pull/378))
1010
- Added MacOS and Windows CI workflows ([#390](https://github.com/opensearch-project/opensearch-py/pull/390))
1111
- Compatibility with OpenSearch 2.1.0 - 2.6.0 ([#381](https://github.com/opensearch-project/opensearch-py/pull/381))
12+
- Added 'allow_redirects' parameter in perform_request function for RequestsHttpConnection ([#401](https://github.com/opensearch-project/opensearch-py/pull/401))
1213
### Changed
1314
- Upgrading pytest-asyncio to latest version - 0.21.0 ([#339](https://github.com/opensearch-project/opensearch-py/pull/339))
1415
- Fixed flaky CI tests by replacing httpbin with a simple http_server ([#395](https://github.com/opensearch-project/opensearch-py/pull/395))

opensearchpy/connection/http_requests.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,15 @@ def __init__(
155155
)
156156

157157
def perform_request(
158-
self, method, url, params=None, body=None, timeout=None, ignore=(), headers=None
158+
self,
159+
method,
160+
url,
161+
params=None,
162+
body=None,
163+
timeout=None,
164+
allow_redirects=True,
165+
ignore=(),
166+
headers=None,
159167
):
160168
url = self.base_url + url
161169
headers = headers or {}
@@ -173,7 +181,10 @@ def perform_request(
173181
settings = self.session.merge_environment_settings(
174182
prepared_request.url, {}, None, None, None
175183
)
176-
send_kwargs = {"timeout": timeout or self.timeout}
184+
send_kwargs = {
185+
"timeout": timeout or self.timeout,
186+
"allow_redirects": allow_redirects,
187+
}
177188
send_kwargs.update(settings)
178189
try:
179190
response = self.session.send(prepared_request, **send_kwargs)

test_opensearchpy/TestHttpServer.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@
1414

1515
class TestHTTPRequestHandler(BaseHTTPRequestHandler):
1616
def do_GET(self):
17-
self.send_response(200)
1817
headers = self.headers
19-
self.send_header("Content-type", "application/json")
18+
19+
if self.path == "/redirect":
20+
new_location = "http://localhost:8090"
21+
self.send_response(302)
22+
self.send_header("Location", new_location)
23+
else:
24+
self.send_response(200)
25+
self.send_header("Content-type", "application/json")
26+
2027
self.end_headers()
2128

2229
Headers = {}

test_opensearchpy/test_connection.py

+40
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,46 @@ def test_requests_connection_error(self):
10201020
conn.perform_request("GET", "/")
10211021

10221022

1023+
@pytest.mark.skipif(
1024+
sys.version_info < (3, 0),
1025+
reason="http_server is only available from python 3.x",
1026+
)
1027+
class TestRequestsConnectionRedirect:
1028+
@classmethod
1029+
def setup_class(cls):
1030+
# Start servers
1031+
cls.server1 = TestHTTPServer(port=8080)
1032+
cls.server1.start()
1033+
cls.server2 = TestHTTPServer(port=8090)
1034+
cls.server2.start()
1035+
1036+
@classmethod
1037+
def teardown_class(cls):
1038+
# Stop servers
1039+
cls.server2.stop()
1040+
cls.server1.stop()
1041+
1042+
# allow_redirects = False
1043+
def test_redirect_failure_when_allow_redirect_false(self):
1044+
conn = RequestsHttpConnection("localhost", port=8080, use_ssl=False, timeout=60)
1045+
with pytest.raises(TransportError) as e:
1046+
conn.perform_request("GET", "/redirect", allow_redirects=False)
1047+
assert e.value.status_code == 302
1048+
1049+
# allow_redirects = True (Default)
1050+
def test_redirect_success_when_allow_redirect_true(self):
1051+
conn = RequestsHttpConnection("localhost", port=8080, use_ssl=False, timeout=60)
1052+
user_agent = conn._get_default_user_agent()
1053+
status, headers, data = conn.perform_request("GET", "/redirect")
1054+
assert status == 200
1055+
data = json.loads(data)
1056+
assert data["headers"] == {
1057+
"Host": "localhost:8090",
1058+
"Accept-Encoding": "identity",
1059+
"User-Agent": user_agent,
1060+
}
1061+
1062+
10231063
def test_default_connection_is_returned_by_default():
10241064
c = connections.Connections()
10251065

0 commit comments

Comments
 (0)