from test.support import os_helper
from test.support import socket_helper
from test.support import warnings_helper
+from test.support.testcase import ExtraAssertions
import os
try:
import ssl
urllib.request.ftpwrapper = self._ftpwrapper_class
-class urlopen_FileTests(unittest.TestCase):
+class urlopen_FileTests(unittest.TestCase, ExtraAssertions):
"""Test urlopen() opening a temporary file.
Try to test as much functionality as possible so as to cut down on reliance
# Make sure object returned by urlopen() has the specified methods
for attr in ("read", "readline", "readlines", "fileno",
"close", "info", "geturl", "getcode", "__iter__"):
- self.assertTrue(hasattr(self.returned_obj, attr),
- "object returned by urlopen() lacks %s attribute" %
- attr)
+ self.assertHasAttr(self.returned_obj, attr)
def test_read(self):
self.assertEqual(self.text, self.returned_obj.read())
urllib.request.URLopener()
-class urlopen_DataTests(unittest.TestCase):
+class urlopen_DataTests(unittest.TestCase, ExtraAssertions):
"""Test urlopen() opening a data URL."""
def setUp(self):
# Make sure object returned by urlopen() has the specified methods
for attr in ("read", "readline", "readlines",
"close", "info", "geturl", "getcode", "__iter__"):
- self.assertTrue(hasattr(self.text_url_resp, attr),
- "object returned by urlopen() lacks %s attribute" %
- attr)
+ self.assertHasAttr(self.text_url_resp, attr)
def test_info(self):
self.assertIsInstance(self.text_url_resp.info(), email.message.Message)
from test.support import os_helper
from test.support import requires_subprocess
from test.support import warnings_helper
+from test.support.testcase import ExtraAssertions
from test import test_urllib
from unittest import mock
return urlpath
-class HandlerTests(unittest.TestCase):
+class HandlerTests(unittest.TestCase, ExtraAssertions):
def test_ftp(self):
class MockFTPWrapper:
r = MockResponse(200, "OK", {}, "", url)
newr = h.http_response(req, r)
self.assertIs(r, newr)
- self.assertFalse(hasattr(o, "proto")) # o.error not called
+ self.assertNotHasAttr(o, "proto") # o.error not called
r = MockResponse(202, "Accepted", {}, "", url)
newr = h.http_response(req, r)
self.assertIs(r, newr)
- self.assertFalse(hasattr(o, "proto")) # o.error not called
+ self.assertNotHasAttr(o, "proto") # o.error not called
r = MockResponse(206, "Partial content", {}, "", url)
newr = h.http_response(req, r)
self.assertIs(r, newr)
- self.assertFalse(hasattr(o, "proto")) # o.error not called
+ self.assertNotHasAttr(o, "proto") # o.error not called
# anything else calls o.error (and MockOpener returns None, here)
r = MockResponse(502, "Bad gateway", {}, "", url)
self.assertIsNone(h.http_response(req, r))
response = opener.open('http://example.com/')
expected = b'GET ' + result + b' '
request = handler.last_buf
- self.assertTrue(request.startswith(expected), repr(request))
+ self.assertStartsWith(request, expected)
def test_redirect_head_request(self):
from_url = "http://example.com/a.html"
self.assertTrue(conn.fakesock.closed, "Connection not closed")
-class MiscTests(unittest.TestCase):
+class MiscTests(unittest.TestCase, ExtraAssertions):
def opener_has_handler(self, opener, handler_class):
self.assertTrue(any(h.__class__ == handler_class
url = code = fp = None
hdrs = 'Content-Length: 42'
err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
- self.assertTrue(hasattr(err, 'reason'))
+ self.assertHasAttr(err, 'reason')
self.assertEqual(err.reason, 'something bad happened')
- self.assertTrue(hasattr(err, 'headers'))
+ self.assertHasAttr(err, 'headers')
self.assertEqual(err.headers, 'Content-Length: 42')
expected_errmsg = 'HTTP Error %s: %s' % (err.code, err.msg)
self.assertEqual(str(err), expected_errmsg)
from test import support
from test.support import hashlib_helper
from test.support import threading_helper
+from test.support.testcase import ExtraAssertions
try:
import ssl
return FakeHTTPRequestHandler
-class TestUrlopen(unittest.TestCase):
+class TestUrlopen(unittest.TestCase, ExtraAssertions):
"""Tests urllib.request.urlopen using the network.
These tests are not exhaustive. Assuming that testing using files does a
handler = self.start_server()
with urllib.request.urlopen("http://localhost:%s" % handler.port) as open_url:
for attr in ("read", "close", "info", "geturl"):
- self.assertTrue(hasattr(open_url, attr), "object returned from "
- "urlopen lacks the %s attribute" % attr)
+ self.assertHasAttr(open_url, attr)
self.assertTrue(open_url.read(), "calling 'read' failed")
def test_info(self):
from test import support
from test.support import os_helper
from test.support import socket_helper
+from test.support.testcase import ExtraAssertions
import contextlib
import socket
f.read()
-class urlopenNetworkTests(unittest.TestCase):
+class urlopenNetworkTests(unittest.TestCase, ExtraAssertions):
"""Tests urllib.request.urlopen using the network.
These tests are not exhaustive. Assuming that testing using files does a
with self.urlopen(self.url) as open_url:
for attr in ("read", "readline", "readlines", "fileno", "close",
"info", "geturl"):
- self.assertTrue(hasattr(open_url, attr), "object returned from "
- "urlopen lacks the %s attribute" % attr)
+ self.assertHasAttr(open_url, attr)
self.assertTrue(open_url.read(), "calling 'read' failed")
def test_readlines(self):
import unicodedata
import unittest
import urllib.parse
+from test.support.testcase import ExtraAssertions
RFC1808_BASE = "http://a/b/c/d;p?q#f"
RFC2396_BASE = "http://a/b/c/d;p?q"
(b"%81=%A9", {b'\x81': [b'\xa9']}),
]
-class UrlParseTestCase(unittest.TestCase):
+class UrlParseTestCase(unittest.TestCase, ExtraAssertions):
def checkRoundtrips(self, url, parsed, split, url2=None):
if url2 is None:
with self.subTest(url=url, function=func):
result = func(url, allow_fragments=False)
self.assertEqual(result.fragment, "")
- self.assertTrue(
- getattr(result, attr).endswith("#" + expected_frag))
+ self.assertEndsWith(getattr(result, attr),
+ "#" + expected_frag)
self.assertEqual(func(url, "", False).fragment, "")
result = func(url, allow_fragments=True)
self.assertEqual(result.fragment, expected_frag)
- self.assertFalse(
- getattr(result, attr).endswith(expected_frag))
+ self.assertNotEndsWith(getattr(result, attr), expected_frag)
self.assertEqual(func(url, "", True).fragment,
expected_frag)
self.assertEqual(func(url).fragment, expected_frag)