Skip to content

Commit 0303672

Browse files
committed
TEST: Use pytest-httpserver to test validate_from_url
1 parent 8aec289 commit 0303672

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

nibabel/tests/test_image_api.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,28 @@ def validate_to_from_bytes(self, imaker, params):
572572
del img_a
573573
del img_b
574574

575+
@pytest.fixture(autouse=True)
576+
def setup(self, httpserver):
577+
"""Make pytest fixtures available to validate functions"""
578+
self.httpserver = httpserver
579+
580+
def validate_from_url(self, imaker, params):
581+
server = self.httpserver
582+
583+
img = imaker()
584+
img_bytes = img.to_bytes()
585+
586+
server.expect_oneshot_request("/img").respond_with_data(img_bytes)
587+
url = server.url_for("/img")
588+
assert url.startswith("http://") # Check we'll trigger an HTTP handler
589+
rt_img = img.__class__.from_url(url)
590+
591+
assert rt_img.to_bytes() == img_bytes
592+
assert self._header_eq(img.header, rt_img.header)
593+
assert np.array_equal(img.get_fdata(), rt_img.get_fdata())
594+
del img
595+
del rt_img
596+
575597
@staticmethod
576598
def _header_eq(header_a, header_b):
577599
""" Header equality check that can be overridden by a subclass of this test
@@ -583,7 +605,6 @@ def _header_eq(header_a, header_b):
583605
return header_a == header_b
584606

585607

586-
587608
class LoadImageAPI(GenericImageAPI,
588609
DataInterfaceMixin,
589610
AffineMixin,

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ test =
6161
pytest !=5.3.4
6262
pytest-cov
6363
pytest-doctestplus
64+
pytest-httpserver
6465
zstd =
6566
pyzstd >= 0.14.3
6667
all =

0 commit comments

Comments
 (0)