Skip to content

Commit

Permalink
PYTHON-5000 Fix httpx mocking (#927)
Browse files Browse the repository at this point in the history
* PYTHON-5000 Fix httpx mocking

* let target be inferred
  • Loading branch information
blink1073 authored Dec 20, 2024
1 parent 67b3a94 commit 5d5ffd2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
4 changes: 1 addition & 3 deletions bindings/python/.evergreen/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ else
export CRYPT_SHARED_PATH="../crypt_shared/lib/mongo_crypt_v1.so"
MACHINE=$(uname -m)
if [ $MACHINE == "aarch64" ]; then
TARGET=rhel82
PYTHONS=("/opt/mongodbtoolchain/v3/bin/python3"
"/opt/mongodbtoolchain/v4/bin/python3"
)
else
TARGET=rhel80
PYTHONS=("/opt/python/3.8/bin/python3"
"/opt/python/3.9/bin/python3"
"/opt/python/3.10/bin/python3"
Expand All @@ -66,7 +64,7 @@ else
)
fi
/opt/mongodbtoolchain/v3/bin/python3 drivers-evergreen-tools/.evergreen/mongodl.py --component \
crypt_shared --version latest --out ../crypt_shared/ --target $TARGET
crypt_shared --version latest --out ../crypt_shared/
fi

for PYTHON_BINARY in "${PYTHONS[@]}"; do
Expand Down
22 changes: 11 additions & 11 deletions bindings/python/test/test_mongocrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ def test_need_kms_gcp_credentials(self):
encrypter = AutoEncrypter(callback, opts)
self.addCleanup(encrypter.close)

with respx.mock() as router:
with respx.mock(using="httpx") as router:
data = {"access_token": "foo"}
url = "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token"
router.add(
Expand Down Expand Up @@ -748,7 +748,7 @@ async def test_need_kms_gcp_credentials(self):
encrypter = AsyncAutoEncrypter(callback, opts)
self.addAsyncCleanup(encrypter.close)

with respx.mock() as router:
with respx.mock(using="httpx") as router:
data = {"access_token": "foo"}
url = "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token"
router.add(
Expand Down Expand Up @@ -1019,7 +1019,7 @@ def get_encrypter(self, clear_cache=True):

def test_success(self):
encrypter = self.get_encrypter()
with respx.mock() as router:
with respx.mock(using="httpx") as router:
data = {"access_token": "foo", "expires_in": 4000}
url = "http://169.254.169.254/metadata/identity/oauth2/token"
router.add(
Expand All @@ -1034,7 +1034,7 @@ def test_success(self):

def test_empty_json(self):
encrypter = self.get_encrypter()
with respx.mock() as router:
with respx.mock(using="httpx") as router:
url = "http://169.254.169.254/metadata/identity/oauth2/token"
router.add(
respx.get(url=url).mock(return_value=httpx.Response(200, json={}))
Expand All @@ -1048,7 +1048,7 @@ def test_empty_json(self):

def test_bad_json(self):
encrypter = self.get_encrypter()
with respx.mock() as router:
with respx.mock(using="httpx") as router:
url = "http://169.254.169.254/metadata/identity/oauth2/token"
router.add(
respx.get(url=url).mock(return_value=httpx.Response(200, text="a'"))
Expand All @@ -1062,7 +1062,7 @@ def test_bad_json(self):

def test_http_404(self):
encrypter = self.get_encrypter()
with respx.mock() as router:
with respx.mock(using="httpx") as router:
url = "http://169.254.169.254/metadata/identity/oauth2/token"
router.add(respx.get(url=url).mock(return_value=httpx.Response(404)))
with self.assertRaisesRegex(
Expand All @@ -1074,7 +1074,7 @@ def test_http_404(self):

def test_http_500(self):
encrypter = self.get_encrypter()
with respx.mock() as router:
with respx.mock(using="httpx") as router:
url = "http://169.254.169.254/metadata/identity/oauth2/token"
router.add(respx.get(url=url).mock(return_value=httpx.Response(500)))
with self.assertRaisesRegex(
Expand All @@ -1086,7 +1086,7 @@ def test_http_500(self):

def test_slow_response(self):
encrypter = self.get_encrypter()
with respx.mock() as router:
with respx.mock(using="httpx") as router:
url = "http://169.254.169.254/metadata/identity/oauth2/token"
router.add(
respx.get(url=url).mock(side_effect=httpx._exceptions.ConnectTimeout)
Expand All @@ -1100,7 +1100,7 @@ def test_slow_response(self):

def test_cache(self):
encrypter = self.get_encrypter()
with respx.mock() as router:
with respx.mock(using="httpx") as router:
data = {"access_token": "foo", "expires_in": 4000}
url = "http://169.254.169.254/metadata/identity/oauth2/token"
router.add(
Expand All @@ -1121,7 +1121,7 @@ def test_cache(self):

def test_cache_expires_soon(self):
encrypter = self.get_encrypter()
with respx.mock() as router:
with respx.mock(using="httpx") as router:
data = {"access_token": "foo", "expires_in": 10}
url = "http://169.254.169.254/metadata/identity/oauth2/token"
router.add(
Expand All @@ -1137,7 +1137,7 @@ def test_cache_expires_soon(self):
# Should not use the cached value.
encrypter = self.get_encrypter(False)
self.assertIsNotNone(pymongocrypt.synchronous.credentials._azure_creds_cache)
with respx.mock() as router:
with respx.mock(using="httpx") as router:
url = "http://169.254.169.254/metadata/identity/oauth2/token"
router.add(
respx.get(url=url).mock(side_effect=httpx._exceptions.ConnectTimeout)
Expand Down

0 comments on commit 5d5ffd2

Please sign in to comment.