Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_urllib2_localnet.TestUrlopen Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def urlopen
def start_server
def start_https_server
def test_redirection
def test_chunked
def test_404
def test_200
def test_200_with_parameters
def test_https
def test_https_with_cafile
def test_sending_headers
def test_basic
def test_info
def test_geturl
def test_bad_address
def test_iteration
def test_line_iteration

Public Attributes

 server

Detailed Description

Tests urllib.request.urlopen using the network.

These tests are not exhaustive.  Assuming that testing using files does a
good job overall of some of the basic interface features.  There are no
tests exercising the optional 'data' and 'proxies' arguments.  No tests
for transparent redirection have been written.

Definition at line 344 of file test_urllib2_localnet.py.


Member Function Documentation

Definition at line 353 of file test_urllib2_localnet.py.

00353 
00354     def setUp(self):
00355         super(TestUrlopen, self).setUp()
00356         self.server = None

Here is the caller graph for this function:

def test.test_urllib2_localnet.TestUrlopen.start_https_server (   self,
  responses = None,
  certfile = CERT_localhost 
)

Definition at line 387 of file test_urllib2_localnet.py.

00387 
00388     def start_https_server(self, responses=None, certfile=CERT_localhost):
00389         if not hasattr(urllib.request, 'HTTPSHandler'):
00390             self.skipTest('ssl support required')
00391         from test.ssl_servers import make_https_server
00392         if responses is None:
00393             responses = [(200, [], b"we care a bit")]
00394         handler = GetRequestHandler(responses)
00395         server = make_https_server(self, certfile=certfile, handler_class=handler)
00396         handler.port = server.port
00397         return handler

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_urllib2_localnet.TestUrlopen.start_server (   self,
  responses = None 
)

Definition at line 375 of file test_urllib2_localnet.py.

00375 
00376     def start_server(self, responses=None):
00377         if responses is None:
00378             responses = [(200, [], b"we don't care")]
00379         handler = GetRequestHandler(responses)
00380 
00381         self.server = LoopbackHttpServerThread(handler)
00382         self.server.start()
00383         self.server.ready.wait()
00384         port = self.server.port
00385         handler.port = port
00386         return handler

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 357 of file test_urllib2_localnet.py.

00357 
00358     def tearDown(self):
00359         if self.server is not None:
00360             self.server.stop()
00361         super(TestUrlopen, self).tearDown()

Here is the caller graph for this function:

Definition at line 440 of file test_urllib2_localnet.py.

00440 
00441     def test_200(self):
00442         expected_response = b"pycon 2008..."
00443         handler = self.start_server([(200, [], expected_response)])
00444         data = self.urlopen("http://localhost:%s/bizarre" % handler.port)
00445         self.assertEqual(data, expected_response)
00446         self.assertEqual(handler.requests, ["/bizarre"])

Here is the call graph for this function:

Definition at line 447 of file test_urllib2_localnet.py.

00447 
00448     def test_200_with_parameters(self):
00449         expected_response = b"pycon 2008..."
00450         handler = self.start_server([(200, [], expected_response)])
00451         data = self.urlopen("http://localhost:%s/bizarre" % handler.port,
00452                              b"get=with_feeling")
00453         self.assertEqual(data, expected_response)
00454         self.assertEqual(handler.requests, ["/bizarre", b"get=with_feeling"])

Here is the call graph for this function:

Definition at line 425 of file test_urllib2_localnet.py.

00425 
00426     def test_404(self):
00427         expected_response = b"Bad bad bad..."
00428         handler = self.start_server([(404, [], expected_response)])
00429 
00430         try:
00431             self.urlopen("http://localhost:%s/weeble" % handler.port)
00432         except urllib.error.URLError as f:
00433             data = f.read()
00434             f.close()
00435         else:
00436             self.fail("404 should raise URLError")
00437 
00438         self.assertEqual(data, expected_response)
00439         self.assertEqual(handler.requests, ["/weeble"])

Here is the call graph for this function:

Definition at line 515 of file test_urllib2_localnet.py.

00515 
00516     def test_bad_address(self):
00517         # Make sure proper exception is raised when connecting to a bogus
00518         # address.
00519         self.assertRaises(IOError,
00520                           # Given that both VeriSign and various ISPs have in
00521                           # the past or are presently hijacking various invalid
00522                           # domain name requests in an attempt to boost traffic
00523                           # to their own sites, finding a domain name to use
00524                           # for this test is difficult.  RFC2606 leads one to
00525                           # believe that '.invalid' should work, but experience
00526                           # seemed to indicate otherwise.  Single character
00527                           # TLDs are likely to remain invalid, so this seems to
00528                           # be the best choice. The trailing '.' prevents a
00529                           # related problem: The normal DNS resolver appends
00530                           # the domain names from the search path if there is
00531                           # no '.' the end and, and if one of those domains
00532                           # implements a '*' rule a result is returned.
00533                           # However, none of this will prevent the test from
00534                           # failing if the ISP hijacks all invalid domain
00535                           # requests.  The real solution would be to be able to
00536                           # parameterize the framework with a mock resolver.
00537                           urllib.request.urlopen,
00538                           "http://sadflkjsasf.i.nvali.d./")

Here is the call graph for this function:

Definition at line 484 of file test_urllib2_localnet.py.

00484 
00485     def test_basic(self):
00486         handler = self.start_server()
00487         open_url = urllib.request.urlopen("http://localhost:%s" % handler.port)
00488         for attr in ("read", "close", "info", "geturl"):
00489             self.assertTrue(hasattr(open_url, attr), "object returned from "
00490                          "urlopen lacks the %s attribute" % attr)
00491         try:
00492             self.assertTrue(open_url.read(), "calling 'read' failed")
00493         finally:
00494             open_url.close()

Here is the call graph for this function:

Definition at line 411 of file test_urllib2_localnet.py.

00411 
00412     def test_chunked(self):
00413         expected_response = b"hello world"
00414         chunked_start = (
00415                         b'a\r\n'
00416                         b'hello worl\r\n'
00417                         b'1\r\n'
00418                         b'd\r\n'
00419                         b'0\r\n'
00420                         )
00421         response = [(200, [("Transfer-Encoding", "chunked")], chunked_start)]
00422         handler = self.start_server(response)
00423         data = self.urlopen("http://localhost:%s/" % handler.port)
00424         self.assertEqual(data, expected_response)

Here is the call graph for this function:

Definition at line 508 of file test_urllib2_localnet.py.

00508 
00509     def test_geturl(self):
00510         # Make sure same URL as opened is returned by geturl.
00511         handler = self.start_server()
00512         open_url = urllib.request.urlopen("http://localhost:%s" % handler.port)
00513         url = open_url.geturl()
00514         self.assertEqual(url, "http://localhost:%s" % handler.port)

Here is the call graph for this function:

Definition at line 455 of file test_urllib2_localnet.py.

00455 
00456     def test_https(self):
00457         handler = self.start_https_server()
00458         data = self.urlopen("https://localhost:%s/bizarre" % handler.port)
00459         self.assertEqual(data, b"we care a bit")

Here is the call graph for this function:

Definition at line 460 of file test_urllib2_localnet.py.

00460 
00461     def test_https_with_cafile(self):
00462         handler = self.start_https_server(certfile=CERT_localhost)
00463         import ssl
00464         # Good cert
00465         data = self.urlopen("https://localhost:%s/bizarre" % handler.port,
00466                             cafile=CERT_localhost)
00467         self.assertEqual(data, b"we care a bit")
00468         # Bad cert
00469         with self.assertRaises(urllib.error.URLError) as cm:
00470             self.urlopen("https://localhost:%s/bizarre" % handler.port,
00471                          cafile=CERT_fakehostname)
00472         # Good cert, but mismatching hostname
00473         handler = self.start_https_server(certfile=CERT_fakehostname)
00474         with self.assertRaises(ssl.CertificateError) as cm:
00475             self.urlopen("https://localhost:%s/bizarre" % handler.port,
00476                          cafile=CERT_fakehostname)

Here is the call graph for this function:

Definition at line 495 of file test_urllib2_localnet.py.

00495 
00496     def test_info(self):
00497         handler = self.start_server()
00498         try:
00499             open_url = urllib.request.urlopen(
00500                 "http://localhost:%s" % handler.port)
00501             info_obj = open_url.info()
00502             self.assertIsInstance(info_obj, email.message.Message,
00503                                   "object returned by 'info' is not an "
00504                                   "instance of email.message.Message")
00505             self.assertEqual(info_obj.get_content_subtype(), "plain")
00506         finally:
00507             self.server.stop()

Here is the call graph for this function:

Definition at line 539 of file test_urllib2_localnet.py.

00539 
00540     def test_iteration(self):
00541         expected_response = b"pycon 2008..."
00542         handler = self.start_server([(200, [], expected_response)])
00543         data = urllib.request.urlopen("http://localhost:%s" % handler.port)
00544         for line in data:
00545             self.assertEqual(line, expected_response)

Here is the call graph for this function:

Definition at line 546 of file test_urllib2_localnet.py.

00546 
00547     def test_line_iteration(self):
00548         lines = [b"We\n", b"got\n", b"here\n", b"verylong " * 8192 + b"\n"]
00549         expected_response = b"".join(lines)
00550         handler = self.start_server([(200, [], expected_response)])
00551         data = urllib.request.urlopen("http://localhost:%s" % handler.port)
00552         for index, line in enumerate(data):
00553             self.assertEqual(line, lines[index],
00554                              "Fetched line number %s doesn't match expected:\n"
00555                              "    Expected length was %s, got %s" %
00556                              (index, len(lines[index]), len(line)))
00557         self.assertEqual(index + 1, len(lines))
00558 
00559 
@support.reap_threads

Here is the call graph for this function:

Definition at line 398 of file test_urllib2_localnet.py.

00398 
00399     def test_redirection(self):
00400         expected_response = b"We got here..."
00401         responses = [
00402             (302, [("Location", "http://localhost:%(port)s/somewhere_else")],
00403              ""),
00404             (200, [], expected_response)
00405         ]
00406 
00407         handler = self.start_server(responses)
00408         data = self.urlopen("http://localhost:%s/" % handler.port)
00409         self.assertEqual(data, expected_response)
00410         self.assertEqual(handler.requests, ["/", "/somewhere_else"])

Here is the call graph for this function:

Definition at line 477 of file test_urllib2_localnet.py.

00477 
00478     def test_sending_headers(self):
00479         handler = self.start_server()
00480         req = urllib.request.Request("http://localhost:%s/" % handler.port,
00481                                      headers={"Range": "bytes=20-39"})
00482         urllib.request.urlopen(req)
00483         self.assertEqual(handler.headers_received["Range"], "bytes=20-39")

Here is the call graph for this function:

def test.test_urllib2_localnet.TestUrlopen.urlopen (   self,
  url,
  data = None,
  kwargs 
)

Definition at line 362 of file test_urllib2_localnet.py.

00362 
00363     def urlopen(self, url, data=None, **kwargs):
00364         l = []
00365         f = urllib.request.urlopen(url, data, **kwargs)
00366         try:
00367             # Exercise various methods
00368             l.extend(f.readlines(200))
00369             l.append(f.readline())
00370             l.append(f.read(1024))
00371             l.append(f.read())
00372         finally:
00373             f.close()
00374         return b"".join(l)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 355 of file test_urllib2_localnet.py.


The documentation for this class was generated from the following file: