summaryrefslogtreecommitdiff
path: root/testing/test_pastebin.py
diff options
context:
space:
mode:
authorMichael Goerz <goerz@stanford.edu>2019-08-18 13:32:46 -0400
committerMichael Goerz <goerz@stanford.edu>2019-08-26 23:50:46 -0400
commitd47b9d04d4cf824150caef46c9c888779c1b3f58 (patch)
treea430824961335420523565f905e8a632e000920c /testing/test_pastebin.py
parent5bf9f9a71183886ea39db6f0a8b496bebb39d9a9 (diff)
downloadpytest-d47b9d04d4cf824150caef46c9c888779c1b3f58.tar.gz
Gracefully handle HTTP errors from pastebin
We find that the --pastebin option to pytest sometimes fails with "HTTP Error 400: Bad Request". We're still investigating the exact cause of these errors, but in the meantime, a failure to upload to the pastebin service should probably not crash pytest and cause a test failure in the continuous-integration. This patch catches exceptions like HTTPError that may be thrown while trying to communicate with the pastebin service, and reports them as a "bad response", without crashing with a backtrace or failing the entire test suite.
Diffstat (limited to 'testing/test_pastebin.py')
-rw-r--r--testing/test_pastebin.py56
1 files changed, 55 insertions, 1 deletions
diff --git a/testing/test_pastebin.py b/testing/test_pastebin.py
index 4e8bac56c..a1bc0622e 100644
--- a/testing/test_pastebin.py
+++ b/testing/test_pastebin.py
@@ -83,6 +83,47 @@ class TestPaste:
return request.config.pluginmanager.getplugin("pastebin")
@pytest.fixture
+ def mocked_urlopen_fail(self, monkeypatch):
+ """
+ monkeypatch the actual urlopen call to emulate a HTTP Error 400
+ """
+ calls = []
+
+ import urllib.error
+ import urllib.request
+
+ def mocked(url, data):
+ calls.append((url, data))
+ raise urllib.error.HTTPError(url, 400, "Bad request", None, None)
+
+ monkeypatch.setattr(urllib.request, "urlopen", mocked)
+ return calls
+
+ @pytest.fixture
+ def mocked_urlopen_invalid(self, monkeypatch):
+ """
+ monkeypatch the actual urlopen calls done by the internal plugin
+ function that connects to bpaste service, but return a url in an
+ unexpected format
+ """
+ calls = []
+
+ def mocked(url, data):
+ calls.append((url, data))
+
+ class DummyFile:
+ def read(self):
+ # part of html of a normal response
+ return b'View <a href="/invalid/3c0c6750bd">raw</a>.'
+
+ return DummyFile()
+
+ import urllib.request
+
+ monkeypatch.setattr(urllib.request, "urlopen", mocked)
+ return calls
+
+ @pytest.fixture
def mocked_urlopen(self, monkeypatch):
"""
monkeypatch the actual urlopen calls done by the internal plugin
@@ -105,6 +146,19 @@ class TestPaste:
monkeypatch.setattr(urllib.request, "urlopen", mocked)
return calls
+ def test_pastebin_invalid_url(self, pastebin, mocked_urlopen_invalid):
+ result = pastebin.create_new_paste(b"full-paste-contents")
+ assert (
+ result
+ == "bad response: invalid format ('View <a href=\"/invalid/3c0c6750bd\">raw</a>.')"
+ )
+ assert len(mocked_urlopen_invalid) == 1
+
+ def test_pastebin_http_error(self, pastebin, mocked_urlopen_fail):
+ result = pastebin.create_new_paste(b"full-paste-contents")
+ assert result == "bad response: HTTP Error 400: Bad request"
+ assert len(mocked_urlopen_fail) == 1
+
def test_create_new_paste(self, pastebin, mocked_urlopen):
result = pastebin.create_new_paste(b"full-paste-contents")
assert result == "https://bpaste.net/show/3c0c6750bd"
@@ -127,4 +181,4 @@ class TestPaste:
monkeypatch.setattr(urllib.request, "urlopen", response)
result = pastebin.create_new_paste(b"full-paste-contents")
- assert result == "bad response: something bad occurred"
+ assert result == "bad response: invalid format ('something bad occurred')"