Skip to content
18 changes: 16 additions & 2 deletions tests/external_httplib/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys

import pytest

try:
import urllib.request as urllib
except:
except ImportError:
import urllib

from testing_support.external_fixtures import cache_outgoing_headers, insert_incoming_headers
Expand All @@ -29,6 +29,14 @@

from newrelic.api.background_task import background_task

# Since Python 3.3, `urllib.URLopener()` has been deprecated in favor of
# `urllib.request.urlopen`. In Python 3.14, `urllib.URLopener()` will be
# removed. `urllib.request.urlopen` corresponds to the old `urllib2.urlopen`

SKIP_IF_PYTHON_3_14_OR_ABOVE = pytest.mark.skipif(
sys.version_info[0:2] >= (3, 14), reason="urllib.URLopener() is removed in Python 3.14 and above"
)


@pytest.fixture(scope="session")
def metrics(server):
Expand All @@ -44,6 +52,7 @@ def metrics(server):
return scoped, rollup


@SKIP_IF_PYTHON_3_14_OR_ABOVE
def test_urlopener_http_request(server, metrics):
@validate_transaction_metrics(
"test_urllib:test_urlopener_http_request",
Expand All @@ -59,6 +68,7 @@ def _test():
_test()


@SKIP_IF_PYTHON_3_14_OR_ABOVE
def test_urlopener_https_request(server, metrics):
@validate_transaction_metrics(
"test_urllib:test_urlopener_https_request",
Expand All @@ -77,6 +87,7 @@ def _test():
_test()


@SKIP_IF_PYTHON_3_14_OR_ABOVE
def test_urlopener_http_request_with_port(server):
scoped = [(f"External/localhost:{server.port}/urllib/", 1)]

Expand Down Expand Up @@ -110,6 +121,7 @@ def _test():
]


@SKIP_IF_PYTHON_3_14_OR_ABOVE
@validate_transaction_metrics(
"test_urllib:test_urlopener_file_request",
scoped_metrics=_test_urlopener_file_request_scoped_metrics,
Expand All @@ -123,6 +135,7 @@ def test_urlopener_file_request():
opener.open(file_uri)


@SKIP_IF_PYTHON_3_14_OR_ABOVE
@background_task()
@cache_outgoing_headers
@validate_cross_process_headers
Expand All @@ -131,6 +144,7 @@ def test_urlopener_cross_process_request(server):
opener.open(f"http://localhost:{server.port}/")


@SKIP_IF_PYTHON_3_14_OR_ABOVE
@cat_enabled
def test_urlopener_cross_process_response(server):
_test_urlopener_cross_process_response_scoped_metrics = [
Expand Down
1 change: 0 additions & 1 deletion tests/external_httplib/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import urllib.request as urllib2

import pytest
Expand Down
Loading