Skip to content

Commit

Permalink
Simplified UT and fixed the bug
Browse files Browse the repository at this point in the history
Signed-off-by: Nathalie Jonathan <[email protected]>
  • Loading branch information
nathaliellenaa committed Nov 21, 2024
1 parent 405d704 commit da8cb58
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
6 changes: 5 additions & 1 deletion opensearchpy/_async/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,17 @@ async def perform_request(
method, params, body, ignore, timeout
)

from urllib.parse import unquote

decoded_url = unquote(url)

for attempt in range(self.max_retries + 1):
connection = self.get_connection()

try:
status, headers_response, data = await connection.perform_request(
method,
url,
decoded_url,
params,
body,
headers=headers,
Expand Down
33 changes: 12 additions & 21 deletions test_opensearchpy/test_async/test_signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import pytest
from _pytest.mark.structures import MarkDecorator

# from urllib.parse import quote, unquote


pytestmark: MarkDecorator = pytest.mark.asyncio


Expand Down Expand Up @@ -134,7 +137,7 @@ async def test_aws_signer_async_consitent_url(self) -> None:
doc_id = "test:123"
url = f"https://search-domain.region.es.amazonaws.com:9200/index/_doc/{doc_id}"

# Create a mock signer to capture the signed URL
# Create a mock signer class to capture the signed URL
class MockSigner(AWSV4SignerAsyncAuth):
def _sign_request(
self,
Expand All @@ -145,12 +148,12 @@ def _sign_request(
) -> Dict[str, str]:
nonlocal signed_url
signed_url = url
return super()._sign_request(method, url, query_string, body)
return {}

# Create a custom connection class to capture the sent URL
class CapturingConnection(AsyncHttpConnection):
# Create a mock connection class to capture the sent URL
class MockConnection(AsyncHttpConnection):
async def perform_request(
self: "CapturingConnection",
self: "MockConnection",
method: str,
url: str,
params: Optional[Mapping[str, Any]] = None,
Expand All @@ -160,10 +163,8 @@ async def perform_request(
headers: Optional[Mapping[str, str]] = None,
) -> Tuple[int, Mapping[str, str], str]:
nonlocal sent_url
host = self.host
base_url = f"{host}"
sent_url = f"{base_url}{url}"
return 200, {"content-type": "application/json"}, "{}"
sent_url = f"{self.host}{url}"
return 200, {}, "{}"

auth = MockSigner(self.mock_session(), region)
auth("GET", url)
Expand All @@ -173,19 +174,9 @@ async def perform_request(
http_auth=auth,
use_ssl=True,
verify_certs=True,
connection_class=CapturingConnection,
connection_class=MockConnection,
)

try:
await client.index("index", {"test": "data"}, id=doc_id)
except Exception as e:
print(f"Expected exception: {e}")

# Verify URLs
assert signed_url is not None, "Signed URL was not captured"
assert sent_url is not None, "Sent URL was not captured"
print(f"Signed URL: {signed_url}")
print(f"Sent URL: {sent_url}")
await client.index("index", {"test": "data"}, id=doc_id)

# Verify URLs match
assert signed_url == sent_url, "URLs don't match"

0 comments on commit da8cb58

Please sign in to comment.