diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index f2f5a1f8b..8ae04f3f1 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -1186,14 +1186,21 @@ def test_it_retrieves_unstored_refs_via_requests(self): def test_it_retrieves_unstored_refs_via_urlopen(self): ref = "http://bar#baz" schema = {"baz": 12} + resource_manager_mock = mock.MagicMock() + (resource_manager_mock. + __enter__.return_value. + read.return_value. + decode.return_value) = json.dumps(schema).encode("utf8") + urlopen_mock = mock.MagicMock(return_value=resource_manager_mock) with MockImport("requests", None): - with mock.patch("jsonschema.validators.urlopen") as urlopen: - urlopen.return_value.read.return_value = ( - json.dumps(schema).encode("utf8")) + with mock.patch("jsonschema.validators.urlopen", urlopen_mock): with self.resolver.resolving(ref) as resolved: self.assertEqual(resolved, 12) - urlopen.assert_called_once_with("http://bar") + + urlopen_mock.assert_called_once_with("http://bar") + self.assertEqual(resource_manager_mock.__exit__.call_count, 1) + self.assertEqual(resource_manager_mock.__enter__.call_count, 1) def test_it_can_construct_a_base_uri_from_a_schema(self): schema = {"id": "foo"} diff --git a/jsonschema/validators.py b/jsonschema/validators.py index a47c3aefa..9d4ed3cf2 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -753,7 +753,8 @@ def resolve_remote(self, uri): result = requests.get(uri).json else: # Otherwise, pass off to urllib and assume utf-8 - result = json.loads(urlopen(uri).read().decode("utf-8")) + with urlopen(uri) as url: + result = json.loads(url.read().decode("utf-8")) if self.cache_remote: self.store[uri] = result