Repository: yt-dlp/yt-dlp
File: test/test_networking.py (Python)

Code before:

        def mock_read(*args, **kwargs):
            raise raised()
        monkeypatch.setattr(res.fp, 'read', mock_read)

        with pytest.raises(expected, match=match) as exc_info:
            res.read()

        assert exc_info.type is expected

    def test_close(self, handler, monkeypatch):
        rh = handler()
        session = rh._get_instance(cookiejar=rh.cookiejar)
        called = False
        original_close = session.close

        def mock_close(*args, **kwargs):
            nonlocal called
            called = True
            return original_close(*args, **kwargs)

        monkeypatch.setattr(session, 'close', mock_close)

        rh.close()

        assert called is True

    def test_remove_logging_handler(self, handler):
        # Ensure logging handler, containing YoutubeDL instance, is removed when we close the request handler
        # https://github.com/yt-dlp/yt-dlp/issues/8922
        logging_handlers = logging.getLogger('urllib3').handlers
        before_count = len(logging_handlers)
        rh = handler()
        assert len(logging_handlers) == before_count + 1
        rh.close()
        assert len(logging_handlers) == before_count


def run_validation(handler, error, req, **handler_kwargs):
    with handler(**handler_kwargs) as rh:
        if error:
            with pytest.raises(error):
                rh.validate(req)
        else:
            rh.validate(req)


class TestRequestHandlerValidation:

    class ValidationRH(RequestHandler):
        def _send(self, request):

Review comment (suggestion):
I think this is enough, especially since the values come from us. Same for the other ones
```suggestion
        assert called
```

Code after:
        (
            lambda: urllib3.exceptions.ProtocolError('error', http.client.IncompleteRead(partial=b'abc', expected=4)),
            IncompleteRead,
            '3 bytes read, 4 more expected'
        ),
        (
            lambda: urllib3.exceptions.ProtocolError('error', urllib3.exceptions.IncompleteRead(partial=3, expected=5)),
            IncompleteRead,
            '3 bytes read, 5 more expected'
        ),
    ])
    def test_response_error_mapping(self, handler, monkeypatch, raised, expected, match):
        from requests.models import Response as RequestsResponse
        from urllib3.response import HTTPResponse as Urllib3Response

        from yt_dlp.networking._requests import RequestsResponseAdapter
        requests_res = RequestsResponse()
        requests_res.raw = Urllib3Response(body=b'', status=200)
        res = RequestsResponseAdapter(requests_res)

        def mock_read(*args, **kwargs):
            raise raised()
        monkeypatch.setattr(res.fp, 'read', mock_read)

        with pytest.raises(expected, match=match) as exc_info:
            res.read()

        assert exc_info.type is expected

    def test_close(self, handler, monkeypatch):
        rh = handler()
        session = rh._get_instance(cookiejar=rh.cookiejar)
        called = False
        original_close = session.close

        def mock_close(*args, **kwargs):
            nonlocal called
            called = True
            return original_close(*args, **kwargs)

        monkeypatch.setattr(session, 'close', mock_close)
        rh.close()
        assert called


def run_validation(handler, error, req, **handler_kwargs):
    with handler(**handler_kwargs) as rh:
        if error:
            with pytest.raises(error):
                rh.validate(req)
        else: