Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Attributes
test.test_urllib.urlopen_HttpTests Class Reference

List of all members.

Public Member Functions

def fakehttp
def unfakehttp
def check_read
def test_url_fragment
def test_willclose
def test_read_0_9
def test_read_1_0
def test_read_1_1
def test_read_bogus
def test_invalid_redirect
def test_empty_socket
def test_userpass_inurl

Public Attributes

 io_refs
 sock

Private Attributes

 _connection_class

Detailed Description

Test urlopen() opening a fake http connection.

Definition at line 142 of file test_urllib.py.


Member Function Documentation

Definition at line 171 of file test_urllib.py.

00171 
00172     def check_read(self, ver):
00173         self.fakehttp(b"HTTP/" + ver + b" 200 OK\r\n\r\nHello!")
00174         try:
00175             fp = urlopen("http://python.org/")
00176             self.assertEqual(fp.readline(), b"Hello!")
00177             self.assertEqual(fp.readline(), b"")
00178             self.assertEqual(fp.geturl(), 'http://python.org/')
00179             self.assertEqual(fp.getcode(), 200)
00180         finally:
00181             self.unfakehttp()

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_urllib.urlopen_HttpTests.fakehttp (   self,
  fakedata 
)

Definition at line 145 of file test_urllib.py.

00145 
00146     def fakehttp(self, fakedata):
00147         class FakeSocket(io.BytesIO):
00148             io_refs = 1
00149             def sendall(self, str): pass
00150             def makefile(self, *args, **kwds):
00151                 self.io_refs += 1
00152                 return self
00153             def read(self, amt=None):
00154                 if self.closed: return b""
00155                 return io.BytesIO.read(self, amt)
00156             def readline(self, length=None):
00157                 if self.closed: return b""
00158                 return io.BytesIO.readline(self, length)
00159             def close(self):
00160                 self.io_refs -= 1
00161                 if self.io_refs == 0:
00162                     io.BytesIO.close(self)
00163         class FakeHTTPConnection(http.client.HTTPConnection):
00164             def connect(self):
00165                 self.sock = FakeSocket(fakedata)
00166         self._connection_class = http.client.HTTPConnection
00167         http.client.HTTPConnection = FakeHTTPConnection

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 239 of file test_urllib.py.

00239 
00240     def test_empty_socket(self):
00241         # urlopen() raises IOError if the underlying socket does not send any
00242         # data. (#1680230)
00243         self.fakehttp(b'')
00244         try:
00245             self.assertRaises(IOError, urlopen, "http://something")
00246         finally:
00247             self.unfakehttp()

Here is the call graph for this function:

Definition at line 224 of file test_urllib.py.

00224 
00225     def test_invalid_redirect(self):
00226         # urlopen() should raise IOError for many error codes.
00227         self.fakehttp(b'''HTTP/1.1 302 Found
00228 Date: Wed, 02 Jan 2008 03:03:54 GMT
00229 Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
00230 Location: file://guidocomputer.athome.com:/python/license
00231 Connection: close
00232 Content-Type: text/html; charset=iso-8859-1
00233 ''')
00234         try:
00235             self.assertRaises(urllib.error.HTTPError, urlopen,
00236                               "http://python.org/")
00237         finally:
00238             self.unfakehttp()

Here is the call graph for this function:

Definition at line 200 of file test_urllib.py.

00200 
00201     def test_read_0_9(self):
00202         # "0.9" response accepted (but not "simple responses" without
00203         # a status line)
00204         self.check_read(b"0.9")

Here is the call graph for this function:

Definition at line 205 of file test_urllib.py.

00205 
00206     def test_read_1_0(self):
00207         self.check_read(b"1.0")

Here is the call graph for this function:

Definition at line 208 of file test_urllib.py.

00208 
00209     def test_read_1_1(self):
00210         self.check_read(b"1.1")

Here is the call graph for this function:

Definition at line 211 of file test_urllib.py.

00211 
00212     def test_read_bogus(self):
00213         # urlopen() should raise IOError for many error codes.
00214         self.fakehttp(b'''HTTP/1.1 401 Authentication Required
00215 Date: Wed, 02 Jan 2008 03:03:54 GMT
00216 Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
00217 Connection: close
00218 Content-Type: text/html; charset=iso-8859-1
00219 ''')
00220         try:
00221             self.assertRaises(IOError, urlopen, "http://python.org/")
00222         finally:
00223             self.unfakehttp()

Here is the call graph for this function:

Definition at line 182 of file test_urllib.py.

00182 
00183     def test_url_fragment(self):
00184         # Issue #11703: geturl() omits fragments in the original URL.
00185         url = 'http://docs.python.org/library/urllib.html#OK'
00186         self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!")
00187         try:
00188             fp = urllib.request.urlopen(url)
00189             self.assertEqual(fp.geturl(), url)
00190         finally:
00191             self.unfakehttp()

Here is the call graph for this function:

Definition at line 248 of file test_urllib.py.

00248 
00249     def test_userpass_inurl(self):
00250         self.fakehttp(b"HTTP/1.0 200 OK\r\n\r\nHello!")
00251         try:
00252             fp = urlopen("http://user:pass@python.org/")
00253             self.assertEqual(fp.readline(), b"Hello!")
00254             self.assertEqual(fp.readline(), b"")
00255             self.assertEqual(fp.geturl(), 'http://user:pass@python.org/')
00256             self.assertEqual(fp.getcode(), 200)
00257         finally:
00258             self.unfakehttp()

Here is the call graph for this function:

Definition at line 192 of file test_urllib.py.

00192 
00193     def test_willclose(self):
00194         self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!")
00195         try:
00196             resp = urlopen("http://www.python.org")
00197             self.assertTrue(resp.fp.will_close)
00198         finally:
00199             self.unfakehttp()

Here is the call graph for this function:

Definition at line 168 of file test_urllib.py.

Here is the caller graph for this function:


Member Data Documentation

Definition at line 165 of file test_urllib.py.

Definition at line 160 of file test_urllib.py.

Definition at line 164 of file test_urllib.py.


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