@@ -82,6 +82,47 @@ class TestPaste:
82
82
def pastebin (self , request ):
83
83
return request .config .pluginmanager .getplugin ("pastebin" )
84
84
85
+ @pytest .fixture
86
+ def mocked_urlopen_fail (self , monkeypatch ):
87
+ """
88
+ monkeypatch the actual urlopen call to emulate a HTTP Error 400
89
+ """
90
+ calls = []
91
+
92
+ import urllib .error
93
+ import urllib .request
94
+
95
+ def mocked (url , data ):
96
+ calls .append ((url , data ))
97
+ raise urllib .error .HTTPError (url , 400 , "Bad request" , None , None )
98
+
99
+ monkeypatch .setattr (urllib .request , "urlopen" , mocked )
100
+ return calls
101
+
102
+ @pytest .fixture
103
+ def mocked_urlopen_invalid (self , monkeypatch ):
104
+ """
105
+ monkeypatch the actual urlopen calls done by the internal plugin
106
+ function that connects to bpaste service, but return a url in an
107
+ unexpected format
108
+ """
109
+ calls = []
110
+
111
+ def mocked (url , data ):
112
+ calls .append ((url , data ))
113
+
114
+ class DummyFile :
115
+ def read (self ):
116
+ # part of html of a normal response
117
+ return b'View <a href="/invalid/3c0c6750bd">raw</a>.'
118
+
119
+ return DummyFile ()
120
+
121
+ import urllib .request
122
+
123
+ monkeypatch .setattr (urllib .request , "urlopen" , mocked )
124
+ return calls
125
+
85
126
@pytest .fixture
86
127
def mocked_urlopen (self , monkeypatch ):
87
128
"""
@@ -105,6 +146,19 @@ def read(self):
105
146
monkeypatch .setattr (urllib .request , "urlopen" , mocked )
106
147
return calls
107
148
149
+ def test_pastebin_invalid_url (self , pastebin , mocked_urlopen_invalid ):
150
+ result = pastebin .create_new_paste (b"full-paste-contents" )
151
+ assert (
152
+ result
153
+ == "bad response: invalid format ('View <a href=\" /invalid/3c0c6750bd\" >raw</a>.')"
154
+ )
155
+ assert len (mocked_urlopen_invalid ) == 1
156
+
157
+ def test_pastebin_http_error (self , pastebin , mocked_urlopen_fail ):
158
+ result = pastebin .create_new_paste (b"full-paste-contents" )
159
+ assert result == "bad response: HTTP Error 400: Bad request"
160
+ assert len (mocked_urlopen_fail ) == 1
161
+
108
162
def test_create_new_paste (self , pastebin , mocked_urlopen ):
109
163
result = pastebin .create_new_paste (b"full-paste-contents" )
110
164
assert result == "https://bpaste.net/show/3c0c6750bd"
0 commit comments