From 7218ff202cb2d9043b1f0d6dca496ea793fbae83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Redzy=C5=84ski?= Date: Fri, 15 Nov 2019 11:08:22 +0100 Subject: [PATCH] remote: http: rename http error, refactor test --- dvc/exceptions.py | 7 +++---- dvc/remote/http.py | 7 ++----- tests/func/test_repro.py | 3 +-- tests/unit/remote/test_http.py | 20 ++++++++++---------- tests/utils/httpd.py | 1 - 5 files changed, 16 insertions(+), 22 deletions(-) diff --git a/dvc/exceptions.py b/dvc/exceptions.py index 3778be592..3ad8c72b8 100644 --- a/dvc/exceptions.py +++ b/dvc/exceptions.py @@ -350,9 +350,8 @@ class NoOutputInExternalRepoError(DvcException): ) -class HTTPErrorStatusCodeException(DvcException): +class HTTPError(DvcException): def __init__(self, code, reason): - super(HTTPErrorStatusCodeException, self).__init__( - "Server responded with error status code: '{}' and message: " - "'{}'".format(code, reason) + super(HTTPError, self).__init__( + "HTTP error: '{} {}'".format(code, reason) ) diff --git a/dvc/remote/http.py b/dvc/remote/http.py index d4ff1cd04..4d5d15faa 100644 --- a/dvc/remote/http.py +++ b/dvc/remote/http.py @@ -7,8 +7,7 @@ from funcy import cached_property, wrap_prop from dvc.config import Config from dvc.config import ConfigError -from dvc.exceptions import DvcException -from dvc.exceptions import HTTPErrorStatusCodeException +from dvc.exceptions import DvcException, HTTPError from dvc.progress import Tqdm from dvc.remote.base import RemoteBASE from dvc.scheme import Schemes @@ -40,9 +39,7 @@ class RemoteHTTP(RemoteBASE): def _download(self, from_info, to_file, name=None, no_progress_bar=False): response = self._request("GET", from_info.url, stream=True) if response.status_code != 200: - raise HTTPErrorStatusCodeException( - response.status_code, response.reason - ) + raise HTTPError(response.status_code, response.reason) with Tqdm( total=None if no_progress_bar else self._content_length(from_info), leave=False, diff --git a/tests/func/test_repro.py b/tests/func/test_repro.py index f6e9a23a9..4e46fdde9 100644 --- a/tests/func/test_repro.py +++ b/tests/func/test_repro.py @@ -43,8 +43,7 @@ from tests.func.test_data_cloud import _should_test_ssh from tests.func.test_data_cloud import get_ssh_url from tests.func.test_data_cloud import TEST_AWS_REPO_BUCKET from tests.func.test_data_cloud import TEST_GCP_REPO_BUCKET -from tests.utils.httpd import ContentMD5Handler -from tests.utils.httpd import StaticFileServer +from tests.utils.httpd import StaticFileServer, ContentMD5Handler class TestRepro(TestDvc): diff --git a/tests/unit/remote/test_http.py b/tests/unit/remote/test_http.py index d1c2a5efa..924a05ae2 100644 --- a/tests/unit/remote/test_http.py +++ b/tests/unit/remote/test_http.py @@ -1,8 +1,12 @@ +try: + from http.server import BaseHTTPRequestHandler +except ImportError: + from BaseHTTPServer import BaseHTTPRequestHandler + import pytest -from BaseHTTPServer import BaseHTTPRequestHandler from dvc.config import ConfigError -from dvc.exceptions import HTTPErrorStatusCodeException +from dvc.exceptions import HTTPError from dvc.path_info import URLInfo from dvc.remote.http import RemoteHTTP from tests.utils.httpd import StaticFileServer @@ -19,11 +23,10 @@ def test_no_traverse_compatibility(dvc_repo): RemoteHTTP(dvc_repo, config) -@pytest.mark.parametrize("response_code", [404, 403, 500]) -def test_download_fails_on_error_code(response_code, dvc_repo): +def test_download_fails_on_error_code(dvc_repo): class ErrorStatusRequestHandler(BaseHTTPRequestHandler): def do_GET(self): - self.send_response(response_code, message="Error message") + self.send_response(404, message="Not found") self.end_headers() with StaticFileServer(ErrorStatusRequestHandler) as httpd: @@ -31,9 +34,6 @@ def test_download_fails_on_error_code(response_code, dvc_repo): config = {"url": url} remote = RemoteHTTP(dvc_repo, config) - import os - with pytest.raises(HTTPErrorStatusCodeException): - remote._download( - URLInfo(os.path.join(url, "file.txt")), "file.txt" - ) + with pytest.raises(HTTPError): + remote._download(URLInfo(url) / "file.txt", "file.txt") diff --git a/tests/utils/httpd.py b/tests/utils/httpd.py index 5883db7b7..92027ae75 100644 --- a/tests/utils/httpd.py +++ b/tests/utils/httpd.py @@ -47,7 +47,6 @@ class StaticFileServer: def __init__(self, handler_class=ETagHandler): self._lock.acquire() - self.response_handler = handler_class self._httpd = HTTPServer(("localhost", 0), handler_class) self._thread = None